" ~ _sf ~ ")├", "i");
if (auto m = _txt.matchFirst(rgx_url)) {
_txt = replaceAll!(m =>
_mark_open
~ m.captures[0]
~ _mark_close
)(_txt, rgx_matched_text);
_txt = replaceAll!(m =>
replaceAll!(u =>
u["keep"]
)(m.hit, rgx_marked_pair)
)(_txt, rgx_url);
_txt = replaceAll!(m =>
_span_yellow
~ m["keep"]
~ _span_close
)(_txt, rgx_marked_pair);
} else {
_txt = replaceAll!(m =>
_span_yellow
~ m.captures[0]
~ _span_close
)(_txt, rgx_matched_text);
}
}
return _txt;
}
string select_field_like(string db_field, string search_field) {
string where_ = "";
if (!(search_field.empty)) {
string _sf = search_field.strip.split("%%20").join(" ");
if (_sf.match(r" OR ")) {
_sf = _sf.split(" OR ").join("%%' OR " ~ db_field ~ " LIKE '%%");
}
if (_sf.match(r" AND ")) {
_sf = _sf.split(" AND ").join("%%' AND " ~ db_field ~ " LIKE '%%");
}
_sf = "( " ~ db_field ~ " LIKE\n '%%" ~ _sf ~ "%%' )";
where_ ~= format(q"┃
%%s
┃",
_sf
);
}
return where_;
}
string[] _fields;
_fields ~= select_field_like("doc_objects.clean", tf.text);
_fields ~= select_field_like("metadata_and_text.title", tf.title);
_fields ~= select_field_like("metadata_and_text.creator_author", tf.author);
_fields ~= select_field_like("metadata_and_text.uid", tf.uid);
_fields ~= select_field_like("metadata_and_text.src_filename_base", tf.fn);
_fields ~= select_field_like("metadata_and_text.src_filename_base", tf.src_filename_base);
_fields ~= select_field_like("metadata_and_text.language_document_char", tf.language);
_fields ~= select_field_like("metadata_and_text.date_published", tf.date);
_fields ~= select_field_like("metadata_and_text.classify_keywords", tf.keywords);
_fields ~= select_field_like("metadata_and_text.classify_topic_register", tf.topic_register);
string[] fields;
foreach (f; _fields) {
if (!(f.empty)) { fields ~= f; }
}
string fields_str = "";
fields_str ~= fields.join(" AND ");
sql_select.the_body ~= format(q"┃
SELECT
metadata_and_text.uid,
metadata_and_text.title,
metadata_and_text.creator_author_last_first,
metadata_and_text.creator_author,
metadata_and_text.src_filename_base,
metadata_and_text.language_document_char,
metadata_and_text.date_published,
metadata_and_text.classify_keywords,
metadata_and_text.classify_topic_register,
doc_objects.body,
doc_objects.seg_name,
doc_objects.ocn,
metadata_and_text.uid
FROM
doc_objects,
metadata_and_text
WHERE (
%%s
)
AND
doc_objects.uid_metadata_and_text = metadata_and_text.uid
ORDER BY
metadata_and_text.creator_author_last_first,
metadata_and_text.date_published DESC,
metadata_and_text.title,
metadata_and_text.language_document_char,
metadata_and_text.src_filename_base,
doc_objects.ocn
LIMIT %%s OFFSET %%s
;┃",
fields_str,
cv.sql_match_limit,
cv.sql_match_offset,
);
(cv.checked_sql)
? cgi.write(previous_next ~ "
" ~ sql_select.the_body.strip.split("\n ").join(" ").split("\n").join("
") ~ "\n")
: "";
cgi.write(previous_next);
auto select_query_results = db.execute(sql_select.the_body).cached;
string _old_uid = "";
if (!select_query_results.empty) {
foreach (row; select_query_results) {
if (row["uid"].as!string != _old_uid) {
_old_uid = row["uid"].as!string;
auto m = (row["date_published"].as!string).match(regex(r"^([0-9]{4})")); // breaks if row missing or no match?
cgi.write(
"
\""
~ row["title"].as!string ~ "\""
~ " ("
~ m.hit
~ ") "
~ "["
~ row["language_document_char"].as!string
~ "] "
~ row["creator_author_last_first"].as!string
~ " "
~ show_matched_objects(row["src_filename_base"].as!string)
~ "
\n"
);
}
if (cv.results_type == "txt") {
if (row["ocn"].as!string != "0") {
cgi.write(
"
"
~ row["ocn"].as!string
~ ""
~ "
"
~ highlight_text_matched(row["body"].as!string, tf.text)
);
} else {
cgi.write(
"
"
~ row["ocn"].as!string
~ ""
~ "
"
~ highlight_text_matched(row["body"].as!string, tf.text)
);
}
} else {
if (row["ocn"].as!string != "0") {
cgi.write(
""
~ row["ocn"].as!string
~ ", "
);
} else {
cgi.write(
""
~ row["ocn"].as!string
~ ", "
);
}
}
}
cgi.write( previous_next);
} else { // offset_not_beyond_limit = false;
cgi.write("select_query_results empty\n");
}
}
sql_search_query;
}
{
db.close;
}
{
string tail = format(q"┃
┃");
cgi.write(tail);
}
}
mixin GenericMain!cgi_function_intro;
≓",
_sqlite_db_fn,
_cgi_search_script,
).strip;
auto pth_sqlite_cgi = spinePathsSQLiteCGI!()(_cgi_search_script_raw_fn_d, _cgi_search_script, opt_action.output_dir_set); // TODO
{ // cgi-bin search form src d
try {
if (!exists(pth_sqlite_cgi.src)) {
pth_sqlite_cgi.src.mkdirRecurse;
}
if (!exists(pth_sqlite_cgi.cgi_bin)) {
pth_sqlite_cgi.cgi_bin.mkdirRecurse;
}
auto f = File(pth_sqlite_cgi.search_form_path_out, "w");
f.write(the_cgi_search_form);
// foreach (o; metadata_) {
// f.writeln(o);
// }
} catch (ErrnoException ex) {
// Handle error
}
// if (!(opt_action.quiet)) {
// writeln(" ", pth_sqlite_cgi.search_form);
// }
}
string the_dub_sdl = format(q"≓
name "spine_cgi_sqlite_search"
description "spine cgi sqlite search"
authors "Ralph Amissah"
copyright "Copyright © 2020, Ralph Amissah"
license "GPL-3.0+"
dependency "d2sqlite3" version="%s"
targetType "executable"
targetPath "./cgi-bin"
mainSourceFile "%s"
configuration "default" {
targetType "executable"
targetName "%s"
postGenerateCommands "/usr/bin/notify-send -t 0 'D executable ready' 'spine cgi sqlite search d'"
}
≓",
"~>0.18.3", // d2sqlite3 dependency version
"src/" ~ _cgi_search_script_raw_fn_d,
_cgi_search_script
).strip;
{ // dub.sdl
try {
auto f = File(pth_sqlite_cgi.dub_sdl_path_out, "w");
f.write(the_dub_sdl);
// foreach (o; metadata_) {
// f.writeln(o);
// }
} catch (ErrnoException ex) {
// Handle error
}
}
{ // get cgi.d
import std.net.curl, std.stdio;
char[] cgi_d;
try {
cgi_d = get!HTTP("https://raw.githubusercontent.com/adamdruppe/arsd/master/cgi.d");
} catch (ErrnoException ex) {
// Handle error
// CurlCode perform(ThrowOnError throwOnError = Yes.throwOnError);
CurlCode perform(ThrowOnError throwOnError = No.throwOnError);
}
if (cgi_d && cgi_d.length > 0) {
try {
auto f = File(pth_sqlite_cgi.cgi_d_path_out, "w");
f.write(cgi_d);
} catch (ErrnoException ex) {
// Handle error
}
}
}
}
}