-*- mode: org -*-
#+TITLE: spine (doc_reform) hub
#+DESCRIPTION: documents - structuring, various output representations & search
#+FILETAGS: :spine:hub:
#+AUTHOR: Ralph Amissah
#+EMAIL: [[mailto:ralph.amissah@gmail.com][ralph.amissah@gmail.com]]
#+COPYRIGHT: Copyright (C) 2015 - 2020 Ralph Amissah
#+LANGUAGE: en
#+STARTUP: content hideblocks hidestars noindent entitiespretty
#+OPTIONS: H:3 num:nil toc:t \n:nil @:t ::t |:t ^:nil _:nil -:t f:t *:t <:t
#+PROPERTY: header-args :exports code
#+PROPERTY: header-args+ :noweb yes
#+PROPERTY: header-args+ :eval no
#+PROPERTY: header-args+ :results no
#+PROPERTY: header-args+ :cache no
#+PROPERTY: header-args+ :padline no
[[./spine.org][spine.org]] [[../org/][org/]]
[[./spine_build_scaffold.org][make/build]]
* cgi search
cd util/d/cgi/search
dub --force --compiler=ldc2 && sudo cp -v cgi-bin/spine-search /usr/lib/cgi-bin/.
** 0. set program tangle
*** output cgi program
- compile spine
make ldc
- create db
~dr/bin/spine-ldc -v \
--sqlite-db-create --sqlite-db-filename="spine.search.db" \
--output=/var/www/html \
~grotto/repo/git.repo/code/project-spine/doc-reform-markup/markup_samples/markup/pod/*
- update db
~dr/bin/spine-ldc -v --sqlite-update --sqlite-db-filename="spine.search.db" \
--output=/var/www/html \
~grotto/repo/git.repo/code/project-spine/doc-reform-markup/markup_samples/markup/pod/*
- create search form d
~dr/bin/spine-ldc -v --cgi-search-form-codegen \
--output=/var/www/html \
~grotto/repo/git.repo/code/project-spine/doc-reform-markup/markup_samples/markup/pod
~dr/bin/spine-ldc -v --cgi-search-form-codegen \
--sqlite-db-filename="spine.search.db" \
--cgi-sqlite-search-filename="spine-search" \
--output=/var/www/html \
~grotto/repo/git.repo/code/project-spine/doc-reform-markup/markup_samples/markup/pod
- create db & search form
~dr/bin/spine-ldc -v \
--sqlite-db-create --sqlite-db-filename="spine.search.db" \
--cgi-search-form-codegen --cgi-sqlite-search-filename="spine-search" \
--output=/var/www/html \
~grotto/repo/git.repo/code/project-spine/doc-reform-markup/markup_samples/markup/pod/*
- compile cgi search form
cd /var/www/html/cgi # /var/www/html (default document root)
dub --force --compiler=ldc2 && sudo cp -v cgi-bin/spine-search /usr/lib/cgi-bin/.
- html
~dr/bin/spine-ldc -v --html --html-link-search \
--output=/var/www/html \
~grotto/repo/git.repo/code/project-spine/doc-reform-markup/markup_samples/markup/pod/*
#+BEGIN_SRC d :tangle "../src/doc_reform/io_out/cgi_sqlite_search_form.d"
module doc_reform.io_out.cgi_sqlite_search_form;
template CGIsearchSQLite() {
void CGIsearchSQLite(E,O,M)(E env, O opt_action, M make_and_meta_struct) {
import
std.file,
std.format;
import doc_reform.io_out;
string _sqlite_db_fn = (opt_action.sqlite_filename.empty)
? make_and_meta_struct.conf.w_srv_db_sqlite
: opt_action.sqlite_filename;
string _cgi_search_script = (opt_action.cgi_sqlite_search_filename.empty)
? make_and_meta_struct.conf.w_srv_cgi_search_script
: opt_action.cgi_sqlite_search_filename;
string _cgi_search_script_raw_fn_d = (opt_action.cgi_sqlite_search_filename_d.empty)
? make_and_meta_struct.conf.w_srv_cgi_search_script_raw_fn_d
: opt_action.cgi_sqlite_search_filename_d;
string get_doc_collection_sub_root(string output_path) {
string web_doc_root_path = environment.get("DOCUMENT_ROOT", "/var/www/html");
auto m = output_path.matchFirst(regex("^(" ~ web_doc_root_path ~ ")"));
return m.post;
}
string the_cgi_search_form = format(q"≓
<
");
string[] pair = pair_str.split("=");
canned_query[pair[0]] = pair[1];
}
// foreach (field, content; canned_query) {
// cgi.write(field ~ ": " ~ content ~ "
");
// }
}
#+END_SRC
***** rgx
#+NAME: cgi_sqlite_initialize_rgx
#+BEGIN_SRC d
static struct Rgx {
// static canned_query = ctRegex!(`\A(?P
");
#+END_SRC
****** GET
#+NAME: cgi_sqlite_initialize_env_1
#+BEGIN_SRC d
} else if (environment.get("REQUEST_METHOD", "POST") == "GET") {
got.canned_query = environment.get("QUERY_STRING", "");
// cgi.write("f.canned_query: " ~ got.canned_query ~ "
");
got.search_text_area = "";
if ("sf" in canned_query && !(canned_query["sf"]).empty) {
got.text = canned_query["sf"].split("%%20").join(" ");
got.search_text_area ~= "text: " ~ got.text ~ "\n";
}
if ("au" in canned_query && !(canned_query["au"]).empty) {
got.author = canned_query["au"].split("%%20").join(" ");
got.search_text_area ~= "author: " ~ got.author ~ "\n";
}
if ("ti" in canned_query && !(canned_query["ti"]).empty) {
got.title = canned_query["ti"].split("%%20").join(" ");
got.search_text_area ~= "title: " ~ got.title ~ "\n";
}
if ("uid" in canned_query && !(canned_query["uid"]).empty) {
got.uid = canned_query["uid"].split("%%20").join(" ");
got.search_text_area ~= "uid: " ~ got.uid ~ "\n";
}
if ("fn" in canned_query && !(canned_query["fn"]).empty) {
got.fn = canned_query["fn"].split("%%20").join(" ");
got.search_text_area ~= "fn: " ~ got.fn ~ "\n";
}
if ("kw" in canned_query && !(canned_query["kw"]).empty) {
got.keywords = canned_query["kw"].split("%%20").join(" ");
got.search_text_area ~= "keywords: " ~ got.keywords ~ "\n";
}
if ("tr" in canned_query && !(canned_query["tr"]).empty) {
got.topic_register = canned_query["tr"].split("%%20").join(" ");
got.search_text_area ~= "topic_register: " ~ got.topic_register ~ "\n";
}
if ("su" in canned_query && !(canned_query["su"]).empty) {
got.subject = canned_query["su"].split("%%20").join(" ");
got.search_text_area ~= "subject: " ~ got.subject ~ "\n";
}
if ("de" in canned_query && !(canned_query["de"]).empty) {
got.description = canned_query["de"].split("%%20").join(" ");
got.search_text_area ~= "description: " ~ got.description ~ "\n";
}
if ("pb" in canned_query && !(canned_query["pb"]).empty) {
got.publisher = canned_query["pb"].split("%%20").join(" ");
got.search_text_area ~= "publisher: " ~ got.publisher ~ "\n";
}
if ("ed" in canned_query && !(canned_query["ed"]).empty) {
got.editor = canned_query["ed"].split("%%20").join(" ");
got.search_text_area ~= "editor: " ~ got.editor ~ "\n";
}
if ("ct" in canned_query && !(canned_query["ct"]).empty) {
got.contributor = canned_query["ct"].split("%%20").join(" ");
got.search_text_area ~= "contributor: " ~ got.contributor ~ "\n";
}
if ("dt" in canned_query && !(canned_query["dt"]).empty) {
got.date = canned_query["dt"].split("%%20").join(" ");
got.search_text_area ~= "date: " ~ got.date ~ "\n";
}
if ("rt" in canned_query && !(canned_query["rt"]).empty) {
got.results_type = canned_query["rt"].split("%%20").join(" ");
// got.search_text_area ~= "results_type: " ~ got.results_type ~ "\n";
}
if ("fmt" in canned_query && !(canned_query["fmt"]).empty) {
got.format = canned_query["fmt"].split("%%20").join(" ");
got.search_text_area ~= "format: " ~ got.format ~ "\n";
}
if ("src" in canned_query && !(canned_query["src"]).empty) {
got.source = canned_query["src"].split("%%20").join(" ");
got.search_text_area ~= "source: " ~ got.source ~ "\n";
}
if ("lng" in canned_query && !(canned_query["lng"]).empty) {
got.language = canned_query["lng"].split("%%20").join(" ");
got.search_text_area ~= "language: " ~ got.language ~ "\n";
}
if ("rl" in canned_query && !(canned_query["rl"]).empty) {
got.relation = canned_query["rl"].split("%%20").join(" ");
got.search_text_area ~= "relation: " ~ got.relation ~ "\n";
}
if ("cv" in canned_query && !(canned_query["cv"]).empty) {
got.coverage = canned_query["cv"].split("%%20").join(" ");
got.search_text_area ~= "coverage: " ~ got.coverage ~ "\n";
}
if ("rgt" in canned_query && !(canned_query["rgt"]).empty) {
got.rights = canned_query["rgt"].split("%%20").join(" ");
got.search_text_area ~= "rights: " ~ got.rights ~ "\n";
}
if ("cmt" in canned_query && !(canned_query["cmt"]).empty) {
got.comment = canned_query["cmt"].split("%%20").join(" ");
got.search_text_area ~= "comment: " ~ got.comment ~ "\n";
}
// if ("abstract" in canned_query && !(canned_query["abstract"]).empty) {
// got.abstract = canned_query["abstract"];
// }
if ("bfn" in canned_query && !(canned_query["bfn"]).empty) { // search_field
got.src_filename_base = canned_query["bfn"].split("%%20").join(" ");
got.search_text_area ~= "src_filename_base: " ~ got.src_filename_base ~ "\n";
}
if ("sml" in canned_query && !(canned_query["sml"]).empty) {
got.sql_match_limit = canned_query["sml"].split("%%20").join(" ");
// got.search_text_area ~= "sql_match_limit: " ~ got.sql_match_limit ~ "\n";
}
// cgi.write("f.search_text_area: " ~ got.search_text_area ~ "
");
}
return got;
#+END_SRC
***** tail
#+NAME: cgi_sqlite_initialize_tail
#+BEGIN_SRC d
}
auto tf = text_fields; //
#+END_SRC
**** SQL select
#+NAME: cgi_sqlite_initialize_sql_select
#+BEGIN_SRC d
struct SQL_select {
string the_body = "";
string the_range = "";
}
auto sql_select = SQL_select();
#+END_SRC
**** misc
***** canned url
#+NAME: cgi_sqlite_initialize_canned_url
#+BEGIN_SRC d
string canned_url () {
string _url = "";
if (environment.get("REQUEST_METHOD", "POST") == "POST") {
_url = conf.http_request_type ~ "://" ~ conf.http_host ~ conf.cgi_script ~ "?" ~ tf.canned_query;
} else if (environment.get("REQUEST_METHOD", "POST") == "GET") {
_url = conf.http_request_type ~ "://" ~ conf.http_host ~ conf.cgi_script ~ "?" ~ environment.get("QUERY_STRING", "");
}
return _url;
}
#+END_SRC
***** canned url regex
#+NAME: cgi_sqlite_initialize_regex_for_canned_search
#+BEGIN_SRC d
auto regex_canned_search () {
static struct RgxCS {
static track_offset = ctRegex!(`(?P
" ~ arrow_previous ~ arrow_next;
return _previous_next;
}
#+END_SRC
***** show matched objects text | index toggle
#+NAME: cgi_sqlite_initialize_show_matched_objects
#+BEGIN_SRC d
string show_matched_objects (string fn) {
auto rgx = regex_canned_search;
string _matched_objects_text = "";
string _url = canned_url;
string _url_new = "";
string _matches_show_text = "&rt=txt";
string _matches_show_index = "&rt=idx";
string _fn = "&fn=" ~ fn;
_url_new = _url;
if (_url_new.match(rgx.results_type_index)) {
_url_new = _url_new.replace(rgx.results_type_index, _matches_show_text);
} else if (_url.match(rgx.results_type_text)) {
_url_new = _url_new.replace(rgx.results_type_text, _matches_show_index);
} else {
if (!(_url.match(rgx.results_type))) {
_url_new = _url ~ _matches_show_text;
}
}
if (!(_url_new.match(rgx.fn))) {
_url_new = _url_new ~ _fn;
}
_matched_objects_text =
""
~ ""
~ "※"
~ "";
return _matched_objects_text;
}
#+END_SRC
** cgi
*** cgi html header
**** format
#+NAME: cgi_sqlite_header_0
#+BEGIN_SRC d
{
header = format(q"┃
#+END_SRC
**** html
#+NAME: cgi_sqlite_header_1
#+BEGIN_SRC html
┃");
}
#+END_SRC
*** cgi html form
#+NAME: cgi_sqlite_form_0
#+BEGIN_SRC d
{
string post_value(string field_name, string type="box", string set="on") {
string val = "";
switch (type) {
case "field":
val = ((field_name in cgi.post && !(cgi.post[field_name]).empty)
? cgi.post[field_name]
: (field_name in cgi.get)
? cgi.get[field_name]
: "");
val = tf.search_text_area;
break;
case "box": // generic for checkbox or radio; checkbox set == "on" radio set == "name set"
val = ((field_name in cgi.post && !(cgi.post[field_name]).empty)
? (cgi.post[field_name] == set ? "checked" : "off")
: (field_name in cgi.get)
? (cgi.get[field_name] == set ? "checked" : "off")
: "off");
break;
case "radio": // used generic bo
val = ((field_name in cgi.post && !(cgi.post[field_name]).empty)
? (cgi.post[field_name] == set ? "checked" : "off")
: (field_name in cgi.get)
? (cgi.get[field_name] == set ? "checked" : "off")
: "checked");
break;
case "checkbox": // used generic bo
val = ((field_name in cgi.post && !(cgi.post[field_name]).empty)
? (cgi.post[field_name] == set ? "checked" : "off")
: (field_name in cgi.get)
? (cgi.get[field_name] == set ? "checked" : "off")
: "checked");
break;
default:
}
return val;
}
#+END_SRC
**** canned search
#+NAME: cgi_sqlite_form_1
#+BEGIN_SRC d
string the_can(string fv) {
string show_the_can = post_value("url");
string _the_can = "";
if (show_the_can == "checked") {
tf = text_fields;
string method_get_url = conf.http_request_type ~ "://" ~ conf.http_host ~ conf.cgi_script ~ "?" ~ environment.get("QUERY_STRING", "");
string method_post_url_construct = conf.http_request_type ~ "://" ~ conf.http_host ~ conf.cgi_script ~ "?" ~ tf.canned_query;
// assert(method_get_url == environment.get("HTTP_REFERER", conf.http_request_type ~ "://" ~ conf.http_host ~ conf.cgi_script ~ "?" ~ conf.query_string));
if (conf.request_method == "POST") {
_the_can =
""
~ "POST: "
~ ""
~ method_post_url_construct
~ ""
~ "
%s
";
} else if (conf.request_method == "GET") {
_the_can =
""
~ "GET: "
~ ""
~ method_get_url
~ "";
}
conf.http_url = conf.http_request_type ~ "://" ~ conf.http_host ~ conf.cgi_script ~ tf.canned_query;
}
return _the_can;
}
#+END_SRC
**** provide tip
#+NAME: cgi_sqlite_form_2
#+BEGIN_SRC d
string provide_tip() {
string searched_tip = post_value("se");
string tip = "";
if (searched_tip == "checked") {
string search_field = post_value("sf", "field");
tf = text_fields;
tip = format(q"┃
database: %%s; selected view: index
search string: %%s %%s %%s %%s %%s %%s
%%s %%s %%s %%s %%s %%s
┃",
cv.db_selected,
(tf.text.empty ? "" : "\"text: " ~ tf.text ~ "; "),
(tf.title.empty ? "" : "\"title: " ~ tf.title ~ "; "),
(tf.author.empty ? "" : "\"author: " ~ tf.author ~ "; "),
(tf.date.empty ? "" : "\"date " ~ tf.date ~ "; "),
(tf.uid.empty ? "" : "\"uid: " ~ tf.uid ~ "; "),
(tf.fn.empty ? "" : "\"fn: " ~ tf.fn ~ "; "),
(tf.text.empty ? "" : "text: " ~ tf.text ~ "
"),
(tf.title.empty ? "" : "title: " ~ tf.title ~ "
"),
(tf.author.empty ? "" : "author: " ~ tf.author ~ "
"),
(tf.date.empty ? "" : "date: " ~ tf.date ~ "
"),
(tf.uid.empty ? "" : "\"uid: " ~ tf.uid ~ "; "),
(tf.fn.empty ? "" : "\"fn: " ~ tf.fn ~ "; "),
);
}
return tip;
}
#+END_SRC
**** the form
***** form html
#+NAME: cgi_sqlite_form_3
#+BEGIN_SRC html
form = format(q"┃
┃",
#+END_SRC
***** form values
#+NAME: cgi_sqlite_form_post_0
#+BEGIN_SRC d
(post_value("ec") == "checked") ? post_value("sf", "field") : "",
provide_tip,
search_note,
the_can(post_value("sf", "field")),
cv.db_selected,
post_value("rt", "box", "idx"),
post_value("rt", "box", "txt"),
post_value("sml", "box", "1000"),
post_value("sml", "box", "2500"),
post_value("ec"),
post_value("url"),
post_value("se"),
post_value("sql"),
);
#+END_SRC
**** set value (debug)
#+NAME: cgi_sqlite_form_post_1
#+BEGIN_SRC d
{
string set_value(string field_name, string default_val) {
string val;
if (field_name in cgi.post) {
val = cgi.post[field_name];
} else if (field_name in cgi.get) {
val = cgi.get[field_name];
} else { val = default_val; }
return val;
}
bool set_bool(string field_name) {
bool val;
if (field_name in cgi.post
&& cgi.post[field_name] == "on") {
val = true;
} else if (field_name in cgi.get
&& cgi.get[field_name] == "on") {
val = true;
} else { val = false; }
return val;
}
cv.db_selected = set_value("selected_db", "%s"); // selected_db_name == db (spine.search.db or whatever)
cv.sql_match_limit = set_value("sml", "1000");
cv.sql_match_offset = set_value("smo", "0");
cv.search_text = set_value("sf", "");
cv.results_type = set_value("rt", "idx");
cv.checked_echo = set_bool("ec");
cv.checked_stats = set_bool("sts");
cv.checked_url = set_bool("url");
cv.checked_searched = set_bool("se");
cv.checked_tip = set_bool("tip");
cv.checked_sql = set_bool("sql");
tf = text_fields;
}
}
#+END_SRC
*** cgi write
#+NAME: cgi_sqlite_write
#+BEGIN_SRC d
{
cgi.write(header);
cgi.write(table);
cgi.write(form);
// cgi.write(previous_next);
{ // debug environment
// foreach (k, d; environment.toAA) {
// cgi.write(k ~ ": " ~ d ~ "
");
// }
}
{ // debug cgi info
// cgi.write("db_selected: " ~ cv.db_selected ~ "
\n");
// cgi.write("search_text: " ~ cv.search_text ~ "
\n");
// cgi.write("sql_match_limit: " ~ cv.sql_match_limit ~ ";\n");
// cgi.write("sql_match_offset: " ~ cv.sql_match_offset ~ ";\n");
// cgi.write("results_type: " ~ cv.results_type ~ "
\n");
// cgi.write("cv.checked_echo: " ~ (cv.checked_echo ? "checked" : "off") ~ "; \n");
// cgi.write("cv.checked_stats: " ~ (cv.checked_stats ? "checked" : "off") ~ "; \n");
// cgi.write("cv.checked_url: " ~ (cv.checked_url ? "checked" : "off") ~ "; \n");
// cgi.write("cv.checked_searched: " ~ (cv.checked_searched ? "checked" : "off") ~ ";
\n");
// cgi.write("cv.checked_tip: " ~ (cv.checked_tip ? "checked" : "off") ~ "; \n");
// cgi.write("cv.checked_sql: " ~ (cv.checked_sql ? "checked" : "off") ~ "
\n");
}
}
#+END_SRC
** db
*** db set
#+NAME: cgi_sqlite_set_db
#+BEGIN_SRC d
auto db = Database(conf.db_path ~ cv.db_selected);
#+END_SRC
*** db sql SELECT statement
**** select where
#+NAME: cgi_sqlite_select_statement_0
#+BEGIN_SRC d
{
uint sql_match_offset_counter(T)(T cv) {
sql_match_offset_count += cv.sql_match_limit.to!uint;
return sql_match_offset_count;
}
void sql_search_query() {
string highlight_text_matched(string _txt, string search_field) {
string _mark_open = "┤";
string _mark_close = "├";
string _span_match = "";
string _span_close = "";
string _sf_str = search_field.strip.split("%%20").join(" ").strip;
string[] _sf_arr = _sf_str.split(regex(r"\s+AND\s+|\s+OR\s+"));
auto rgx_url = regex(r"]+?>");
foreach (_sf; _sf_arr) {
auto rgx_matched_text = regex(_sf, "i");
auto rgx_marked_pair = regex(r"┤(?P
"
~ 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) {
string _date_published = "0000";
string _close_para = "";
string _matched_ocn_open = "";
foreach (idx, row; select_query_results) {
if (row["uid"].as!string != _old_uid) {
_close_para = (idx == 1) ? "" : "
"; _old_uid = row["uid"].as!string; _date_published = (row["date_published"].as!string.match(regex(r"^([0-9]{4})"))) ? row["date_published"].as!string : "0000"; // used in regex that breaks if no match auto m = _date_published.match(regex(r"^([0-9]{4})")); string _date = (m.hit == "0000") ? "(year?) " : "(" ~ m.hit ~ ") "; cgi.write( _close_para ~ "
\"" ~ row["title"].as!string ~ "\"" ~ " " ~ _date ~ "[" ~ row["language_document_char"].as!string ~ "] " ~ row["creator_author_last_first"].as!string ~ " " ~ show_matched_objects(row["src_filename_base"].as!string) ~ "
" ~ "\n");
}
cgi.write("