aboutsummaryrefslogtreecommitdiffhomepage
path: root/org/cgi.org
blob: 25ecd88ae8a536e2e12bb03bcaa233fb34f3f0c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
-*- mode: org -*-
#+TITLE:       sisu cgi
#+DESCRIPTION: documents - structuring, various output representations & search
#+FILETAGS:    :sisu:cgi:
#+AUTHOR:      Ralph Amissah
#+EMAIL:       [[mailto:ralph.amissah@gmail.com][ralph.amissah@gmail.com]]
#+COPYRIGHT:   Copyright (C) 2015 - 2021 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
#+PROPERTY:    header-args+ :mkdirp yes

* cgi
** cgi.rb

#+BEGIN_SRC ruby  :tangle "../lib/sisu/cgi.rb"
<<sisu_document_header>>
module  SiSU_CGI                                        #% database building documents
  require_relative 'se'                                 # se.rb
  require_relative 'cgi_pgsql'                          # cgi_pgsql.rb
  require_relative 'cgi_sqlite'                         # cgi_sqlite.rb
  class SearchSQL
    def initialize(opt)
      @opt=opt
      @webserv=@opt.files[0].to_s.strip
    end
    def read
      if @opt.act[:sample_search_form][:db]==:pg        # cgi_pgsql.rb
        SiSU_CGI_PgSQL::SearchPgSQL.new(@opt,@webserv).pgsql
      elsif @opt.act[:sample_search_form][:db]==:sqlite # cgi_sqlite.rb
        SiSU_CGI_SQLite::SearchSQLite.new(@opt,@webserv).sqlite
      else
        puts <<-WOK
  please select database type for which sample search form should be built (pgsql or sqlite)
    sisu --sample-search-form --db=sqlite
    sisu --sample-search-form --db=pg
  other options include
     --webserv-cgi='[cgi-server-name]'
     --webserv-output='[sisu-output-server-with-base-path]'
        WOK
      end
    end
  end
end
__END__
#+END_SRC

** cgi_sqlite.rb

#+BEGIN_SRC ruby  :tangle "../lib/sisu/cgi_sqlite.rb"
<<sisu_document_header>>
module  SiSU_CGI_SQLite                                 #% database building documents
  require_relative 'se'                                 # se.rb
  require_relative 'cgi_sql_common'                     # cgi_sql_common.rb
    include SiSU_CGI_SQL
  class SearchSQLite < CGI_Common
    def initialize(opt,webserv)
      @opt,@webserv=opt,webserv
      @cX=SiSU_Screen::Ansi.new(opt.act[:color_state][:set]).cX
      @env=SiSU_Env::InfoEnv.new('',opt)
      @image_src="#{@env.url.webserv_cgi(opt)}/_sisu/image_sys"
      @name_of={}
      @name_of[:output_dir_structure]=if opt.dir_structure_by.to_s =~/(?:language|filetype|filename)/
        opt.dir_structure_by.to_s
      else 'language'
      end
      @name_of[:lingual]=if opt.lingual.to_s =~/(?:mono|multi)/
        opt.lingual.to_s
      else 'multi'
      end
      @name_of[:host_url_cgi]=%q{http://#{ENV['HTTP_HOST']}#{ENV['PATH_INFO']}}
      @name_of[:host_url_docs]=%q{http://#{ENV['HTTP_HOST']}}
      @name_of[:cgi_script]=%q{#{ENV['SCRIPT_NAME']}}
      @image_src=%q{http://#{ENV['HTTP_HOST']}/_sisu/image_sys}
      @common=SiSU_CGI_SQL::CGI_Common.new(@webserv,opt,@image_src,@env)
      @cgi_file_name=@env.url.cgi_sample_search_form_name(opt)
      @name_of_sqlite_db_file='sisu_sqlite.db'
    end
    def sqlite
      serve=[]
      Dir.foreach(@env.path.webserv) do |x|
        if x !~/^\./ \
        and FileTest.directory?("#{@env.path.webserv}/#{x}")
          if FileTest.file?("#{@env.path.webserv}/#{x}/#{@name_of_sqlite_db_file}")
            serve << x unless x =~/^_\S+/
          end
        end
      end
      serve=serve.sort
      f1,f2,f3='','',''
      serve.each do |x|
        f1 << %{              <option value="#{Db[:name_prefix]}#{x}">#{x}</option>\n}
      end
      f2 <<  %{          selected_db=case cgi['db']\n}
      serve.each do |x|
        f2 << %{           when /#{Db[:name_prefix]}#{x}/ then '<option value="#{Db[:name_prefix]}#{x}">#{x}</option>'\n}
      end
      f2 << "          end\n"
      f3 << %{          db_name='#{@name_of_sqlite_db_file}'\n}
      f3 << %{          db_sqlite=case cgi['db']\n}
      serve.each do |x|
        f3 << %{          when /#{Db[:name_prefix]}#{x}/ then "#{@env.path.webserv}/#{x}/\#{db_name}"\n}
      end
      f3 << %{           else  "#{@env.path.webserv}/#{serve[0]}/\#{db_name}"\n          end\n}
      if FileTest.writable?('.')
        output=File.open(@cgi_file_name,'w')
        output << header0 << header1 << header_desc << header2 << f1 << buttons1 << buttons2 << search_request << search_statement << search_statement_common << search_query1 << @common.pages << search_query2 << @common.tail << @common.main1 << f2 << f3 << dbi_connect << @common.main2 << @common.dir_structure << @common.main3
        puts <<-WOK
            generated sample search form: #{@cX.green}#{@cgi_file_name}#{@cX.off}
            default database name:     #{@cX.green}#{Db[:name_prefix]}#{@env.path.base_markup_dir_stub}#{@cX.off} (#{@env.path.base_markup_dir_stub})
            cgi & db host on:          #{@cX.blue}#{@env.url.webserv_base_cgi(@opt)}#{@cX.off}
              to modify use:           #{@cX.brown}sisu --db-sqlite --webserv-search='#{@env.url.webserv_base_cgi(@opt)}'#{@cX.off}
            sisu output on:            #{@cX.blue}#{@env.url.webserv_files_from_db(@opt)}#{@cX.off}
              to modify use:           #{@cX.brown}sisu --db-sqlite --webserv-output='#{@env.url.webserv_files_from_db(@opt)}'#{@cX.off}
            cgi search form link name: #{@cX.green}#{@env.url.cgi_sample_search_form_name(@opt)}#{@cX.off}
              to modify use:           #{@cX.brown}sisu --db-sqlite --cgi-search-form-name='#{@env.url.cgi_sample_search_form_name(@opt)}'#{@cX.off}
            #{@cX.fuchsia}(settings priority: command line; sisurc.yml; else defaults)#{@cX.off}

            #{@cX.fuchsia}NOTE it is first necessary to create the database and tables and populate it#{@cX.off}

            sisu --sqlite --dropall                          # removes existing postgresql db & tables
            sisu --sqlite --createall -v                     # creates postgresql db & tables
            sisu --sqlite --update -v *.sst  *.ssm           # populate the db
            sisu --sample-search-form --sqlite               # creates the postgresql search form
                                                             # this should be done after creating the db
                                                             # to be searched
            sisu --webrick &                                 # starts ruby webrick web server

            # if necessary make the directory '/usr/lib/cgi-bin'
            # here we copy the postgresql search form to cgi-bin
            # (copy #{@cgi_file_name} to your cgi directory)
            # set file permissions to 755
        WOK
        a=case @webserv
        when /pwd/ then ''
        else <<-WOK

              sudo cp -vi #{Dir.pwd}/#{@cgi_file_name} /usr/lib/cgi-bin/.; \\
              sudo chmod -v 755  /usr/lib/cgi-bin/#{@cgi_file_name}
          WOK
        end
        b='(to create and populate sisu sqlite database see "man sisu" and in particular the -d flag)'
        SiSU_Screen::Ansi.new(@opt.act[:color_state][:set],a,b).warn
        a=<<-WOK

              #{@env.webserv_base_cgi(@opt)}/cgi-bin/#{@cgi_file_name}

        WOK
        SiSU_Screen::Ansi.new(@opt.act[:color_state][:set],a).print_blue
      else puts "failed in attempt to write #{@cgi_file_name} to present directory, is directory writable?"
      end
    end
    def header0
      <<-WOK_SQL
#!/usr/bin/env ruby
=begin
#{about}
 * Description: generates naive cgi search form for search of sisu database (sqlite)
#{gpl}
=end
      begin
        require 'cgi'
        require 'fcgi'
        require 'sqlite3'
      rescue LoadError
        puts 'cgi, fcgi or sqlite3 NOT FOUND (LoadError)'
      end
      @stub_default='sisu_sqlite'
      @image_src="#{@image_src}"
      @hosturl_cgi="#{@name_of[:host_url_cgi]}"
      @hosturl_files="#{@name_of[:host_url_docs]}"
      @output_dir_structure_by='#{@name_of[:output_dir_structure]}'
      @lingual='#{@name_of[:lingual]}'
      @db_name_prefix='#{Db[:name_prefix]}'
      @base="#{@name_of[:host_url_cgi]}#{@name_of[:cgi_script]}"
      WOK_SQL
    end
    def search_statement
      <<-'WOK_SQL'
      class DBI_SearchString
        def initialize(l,t,q,cse=false)
          @l,@t,@q=l,t,q
        end
        def string
          search={ search: [], flag: false }
          if @t =~/\S+/ or @q =~/\S+/
            if @t =~/\S+/    then unescaped_search=CGI.unescape(@t)
            elsif @q =~/\S+/ then unescaped_search=CGI.unescape(@q)
            end
            search_construct=[]
            unescaped_search=unescaped_search.gsub(/\s*(AND|OR)\s*/,"%' \) \\1 #{@l} LIKE \( '%").
              gsub(/(.+)/,"#{@l} LIKE \( '%\\1%' \)")
            search_construct << unescaped_search
            search_construct=search_construct.join(' ')
            search[:search]                    << search_construct
            search[:flag]=true
            search
          end
          search
        end
      end
      WOK_SQL
    end
    def search_query1
      <<-'WOK_SQL'
          @search_text=''
          @search_text=search[:text].flatten.join(' AND ')
          @search_text=@search_text.gsub(/(doc_objects\.clean\s+LIKE\s+\(\s*'%[^']+%'\s*\)\s+(?:(?:AND|OR)\s+doc_objects\.clean\s+LIKE\s+\(\s*'%[^']+%'\s*\))+)/,'(\1)')
        end
      WOK_SQL
    end
    def search_query2
      <<-'WOK_SQL'
        def sql_select_body
          limit ||=@@limit
          offset ||=@@offset
          @sql_statement[:body]=%{SELECT metadata_and_text.title, metadata_and_text.creator_author, metadata_and_text.src_filename, metadata_and_text.language_document_char, metadata_and_text.notes_suffix, doc_objects.body, doc_objects.seg, doc_objects.ocn, metadata_and_text.tid FROM doc_objects, metadata_and_text WHERE #{@search_text} AND doc_objects.metadata_tid = metadata_and_text.tid ORDER BY metadata_and_text.language_document_char, metadata_and_text.title, metadata_and_text.src_filename, doc_objects.ocn}
          @sql_statement[:range]=%{LIMIT #{limit} OFFSET #{offset} ;}
          select=@sql_statement[:body] + ' ' + @sql_statement[:range]
          select
        end
        def sql_select_body_format
          %{<font color="#666666" size="2">#{sql_select_body}</font>}
        end
        def contents
          @conn.execute(sql_select_body)
        end
      end
      WOK_SQL
    end
    def dbi_connect
      <<-'WOK_SQL'
          @conn=SQLite3::Database.new(db_sqlite)
          @conn.results_as_hash=true
      WOK_SQL
    end
  end
end
__END__
#+END_SRC

** cgi_pgsql.rb

#+BEGIN_SRC ruby  :tangle "../lib/sisu/cgi_pgsql.rb"
<<sisu_document_header>>
module  SiSU_CGI_PgSQL                                  #% database building documents
  require_relative 'se'                                 # se.rb
  require_relative 'cgi_sql_common'                     # cgi_sql_common.rb
    include SiSU_CGI_SQL
  class SearchPgSQL < CGI_Common
    def initialize(opt,webserv)
      @opt,@webserv=opt,webserv
      @cX=SiSU_Screen::Ansi.new(opt.act[:color_state][:set]).cX
      @env=SiSU_Env::InfoEnv.new('',opt)
      @sys=SiSU_Env::SystemCall.new
      @db=SiSU_Env::InfoDb.new
      get_init=SiSU_Env::GetInit.new
      @rc=get_init.sisu_yaml.rc
      @name_of={}
      @name_of[:output_dir_structure]=if opt.dir_structure_by.to_s =~/(?:language|filetype|filename)/
        opt.dir_structure_by.to_s
      else 'language'
      end
      @name_of[:lingual]=if opt.lingual.to_s =~/(?:mono|multi)/
        opt.lingual.to_s
      else 'multi'
      end
      @name_of[:db]=if defined? @rc['search'] \
      and defined? @rc['search']['sisu'] \
      and defined? @rc['search']['sisu']['action'] \
      and @rc['search']['sisu']['action'] =~/https?:\/\/\S+?\.cgi/ \
      and defined? @rc['search']['sisu']['db'] \
      and @rc['search']['sisu']['db'] =~/\S+/
        @rc['search']['sisu']['db']
      else
        @env.path.base_markup_dir_stub #'sisu' #breaks if not present
      end
      @name_of[:host_url_cgi]=%q{http://#{ENV['HTTP_HOST']}#{ENV['PATH_INFO']}}
      @name_of[:host_url_docs]=%q{http://#{ENV['HTTP_HOST']}}
      @name_of[:cgi_script]=%q{#{ENV['SCRIPT_NAME']}}
      @name_of[:user]=@db.psql.user(opt)
      @image_src=%q{http://#{ENV['HTTP_HOST']}/_sisu/image_sys}
      @common=SiSU_CGI_SQL::CGI_Common.new(@webserv,opt,@image_src,@env)
      @cgi_file_name=@env.url.cgi_sample_search_form_name(opt)
    end
    def pgsql
      serve=[]
      if @sys.psql
        available_db_table=`psql --list`                            # system call requires psql
        available_db=available_db_table.scan(/(#{Db[:name_prefix]}\S+)/) if not available_db_table.nil?
        if available_db \
        and available_db.is_a?(Array)
          available_db.flatten.each do |x|
            serve << x.gsub(/#{Db[:name_prefix]}(\S+)/,'\1')
          end
        else STDERR.puts "WARNING: no postgresql database available, (have you created one?)"
        end
        serve=serve.sort
        f1,f2='',''
        serve.each do |x|
          f1 << %{              <option value="#{Db[:name_prefix]}#{x}">#{x}</option>\n} unless x =~/apache|sisu\/image/ #check
        end
      end
      f2 << %q{          selected_db=%{<option value="#{@db_name_prefix}#{@stub}">#{@stub}</option>}} + "\n"
      if FileTest.writable?('.')
        output=File.open(@cgi_file_name,'w')
        output << header0 << header1 << header_desc << header2 << f1 << buttons1 << buttons1_pgsql << buttons2 << search_request << search_statement << search_statement_common << search_query1 << @common.pages << search_query2 << @common.tail << @common.main1 << f2 << dbi_connect << @common.main2 << @common.dir_structure << @common.main3
        puts <<-WOK
            generated sample search form: #{@cX.green}#{@cgi_file_name}#{@cX.off}
            default database name:     #{@cX.green}#{Db[:name_prefix]}#{@name_of[:db]}#{@cX.off} (#{@name_of[:db]})
            db user:                   #{@cX.green}#{@name_of[:user]}#{@cX.off}
              to modify use:           #{@cX.brown}sisu --db-pg --db-user='#{@name_of[:user]}'#{@cX.off}

            #{@cX.fuchsia}BASED ON ALREADY EXISTING databases#{@cX.off} (default database name: #{@db.psql.db})
            NOTE it is first necessary to createdb,
            use sisu to create the tables & populate the postgresql db

            the database to be used for this directory (#{@db.psql.db})
            will have to be created manually if it does not exist:
            using postgresql tools directly (the following may work):
              (i) if you are not yet a postgresql user,
                #{@cX.brown}sudo su postgres
                  createuser -d -a #{@env.user}
                exit#{@cX.off}
              (ii) create the database:
                #{@cX.brown}createdb #{@db.psql.db}#{@cX.off}
            [for a list of existing databases try 'psql --list']"

            you can use sisu to create the database tables and populate the database with documents

            sisu --pg --dropall                              # removes existing postgresql db & tables
            sisu --pg --createall -v                         # creates postgresql db & tables
            sisu --pg --update -v *.sst  *.ssm               # populate the db
            sisu --sample-search-form --db-pg                # creates the postgresql search form
                                                             # this should be done after creating the db
                                                             # to be searched
            sisu --webrick &                                 # starts ruby webrick web server

            # if necessary make the directory '/usr/lib/cgi-bin'
            # here we copy the postgresql search form to cgi-bin
            # (copy #{@cgi_file_name} to your cgi directory)
            # set file permissions to 755
        WOK
        a=case @webserv
        when /pwd/ then ''
        else <<-WOK

              sudo cp -vi #{Dir.pwd}/#{@cgi_file_name} /usr/lib/cgi-bin/.; \\
              sudo chmod -v 755  /usr/lib/cgi-bin/#{@cgi_file_name}
          WOK
        end
        SiSU_Screen::Ansi.new(@opt.act[:color_state][:set],a).warn
        a=<<-WOK
              #{@env.webserv_base_cgi(@opt)}/cgi-bin/#{@cgi_file_name}
        WOK
        SiSU_Screen::Ansi.new(@opt.act[:color_state][:set],a).print_blue
        a="\n\t(to create and populate postgresql database see 'man sisu' and in particular the --pg option)\n\t[the database to be used for this directory (#{@db.psql.db}) will have to be created manually if it does not exist,\n\tusing postgresql tools directly: 'createdb #{@db.psql.db}' for a list of existing databases try 'psql --list']"
        SiSU_Screen::Ansi.new(@opt.act[:color_state][:set],a).txt_grey
      else puts 'failed in attempt to write #{@cgi_file_name} to present directory,  is directory writable?'
      end
    end
    def header0
      <<-WOK_SQL
#!/usr/bin/env ruby
=begin
#{about}
 * Description: generates naive cgi search form for search of sisu database (pgsql)
#{gpl}
=end
      begin
        require 'cgi'
        require 'fcgi'
        require 'pg'
      rescue LoadError
        puts 'cgi, fcgi or pg NOT FOUND (LoadError)'
      end
      @stub_default='#{@name_of[:db]}'
      @image_src="#{@image_src}"
      @hosturl_cgi="#{@name_of[:host_url_cgi]}"
      @hosturl_files="#{@name_of[:host_url_docs]}"
      @output_dir_structure_by='#{@name_of[:output_dir_structure]}'
      @lingual='#{@name_of[:lingual]}'
      @port='#{@db.psql.port}'
      @db_name_prefix='#{Db[:name_prefix]}'
      @user='#{@name_of[:user]}'  # check user name for access to pg database: e.g. www-data or '#{@env.user}'
      @base="#{@name_of[:host_url_cgi]}#{@name_of[:cgi_script]}"
      WOK_SQL
    end
    def search_statement
      <<-'WOK_SQL'
      class DBI_SearchString
        def initialize(l,t,q,cse=false)
          @l,@t,@q,@c=l,t,q,cse
        end
        def string
          search={ search: [], flag: false }
          if @t =~/\S+/ or @q =~/\S+/
            if @t =~/\S+/    then unescaped_search=CGI.unescape(@t)
            elsif @q =~/\S+/ then unescaped_search=CGI.unescape(@q)
            end
            search_construct=[]
            unescaped_search=if @c
              unescaped_search.gsub(/\s*(AND|OR)\s*/,"' \) \\1 #{@l}~\( '").
                gsub(/(.+)/,"#{@l}~\( '\\1' \)")
            else
              unescaped_search.gsub(/\s*(AND|OR)\s*/,"' \) \\1 #{@l}~*\( '").
                gsub(/(.+)/,"#{@l}~*\( '\\1' \)")
            end
            search_construct << unescaped_search
            search_construct=search_construct.join(' ')
            search[:search]                    << search_construct
            search[:flag]=true
            search
          end
          search
        end
      end
      WOK_SQL
    end
    def search_query1
      <<-'WOK_SQL'
          @search_text=''
          @search_text=search[:text].flatten.join(' AND ')
          @search_text=@search_text.gsub(/(doc_objects\.clean~[*]?\(\s*'[^']+'\s*\)\s+(?:(?:AND|OR)\s+doc_objects\.clean~[*]?\(\s*'[^']+'\s*\))+)/,'(\1)')
        end
      WOK_SQL
    end
    def search_query2
      <<-'WOK_SQL'
        def sql_select_body
          limit ||=@@limit
          offset ||=@@offset
          @sql_statement[:body]=%{SELECT metadata_and_text.title, metadata_and_text.creator_author, metadata_and_text.src_filename, metadata_and_text.language_document_char, metadata_and_text.notes_suffix, doc_objects.body, doc_objects.seg, doc_objects.ocn, metadata_and_text.tid FROM doc_objects, metadata_and_text WHERE (#{@search_text}) AND doc_objects.metadata_tid = metadata_and_text.tid ORDER BY metadata_and_text.language_document_char, metadata_and_text.title, metadata_and_text.src_filename, doc_objects.ocn}
          @sql_statement[:range]=%{LIMIT #{limit} OFFSET #{offset} ;}
          select=@sql_statement[:body] + ' ' + @sql_statement[:range]
          select
        end
        def sql_select_body_format
          %{<font color="#666666" size="2">#{sql_select_body}</font>}
        end
        def contents
          @conn.exec(sql_select_body)
        end
      end
      WOK_SQL
    end
    def buttons1_pgsql
      <<-'WOK_SQL'
        <input type="checkbox" name="casesense" #{@checked_case}> case sensitive
      WOK_SQL
    end
    def dbi_connect
      <<-'WOK_SQL'
          @conn=PG::Connection.open(dbname: @db, port: @port, user: @user)
      WOK_SQL
    end
  end
end
__END__
#+END_SRC

** cgi_sql_common.rb

#+BEGIN_SRC ruby  :tangle "../lib/sisu/cgi_sql_common.rb"
<<sisu_document_header>>
module SiSU_CGI_SQL
  class CGI_Common
    def initialize(webserv,opt,image_src,dir)
      @webserv,@opt,@image_src,@env=webserv,opt,image_src,dir
    end
    def about
      <<-'WOK_SQL'
 * Name: SiSU information Structuring Universe
 * Author: Ralph Amissah
   * http://www.jus.uio.no/sisu
   * http://www.jus.uio.no/sisu/SiSU/download
      WOK_SQL
    end
    def gpl
      <<-'WOK_SQL'
 * Name: SiSU generated sample cgi search form

 * Description: generated sample cgi search form for SiSU
   (SiSU is a framework for document structuring, publishing and search)

 * Author: Ralph Amissah

 * Copyright: (C) 1997 - 2015, Ralph Amissah, All Rights Reserved.

 * License: GPL 3 or later:

   SiSU, a framework for document structuring, publishing and search

   Copyright (C) Ralph Amissah

   This program is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the Free
   Software Foundation, either version 3 of the License, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   more details.

   You should have received a copy of the GNU General Public License along with
   this program. If not, see [http://www.gnu.org/licenses/].

   If you have Internet connection, the latest version of the GPL should be
   available at these locations:
   <http://www.fsf.org/licenses/gpl.html>
   [http://www.gnu.org/licenses/gpl.html]
   <http://www.jus.uio.no/sisu/gpl.fsf>

 * SiSU uses:
   * Standard SiSU markup syntax,
   * Standard SiSU meta-markup syntax, and the
   * Standard SiSU object citation numbering and system

 * Homepages:
   [http://www.jus.uio.no/sisu]
   [http://www.sisudoc.org]

 * Ralph Amissah
   [ralph@amissah.com]
   [ralph.amissah@gmail.com]
      WOK_SQL
    end
    def header1
      <<-'WOK_SQL'
#Common TOP
      @@offset=0
      @@canned_search_url=@base
      @color_heading='#DDFFAA'
      @color_match='#ffff48'
      class Form
        def initialize(base,search_field,selected_db,result_type,checked_sql_limit,checked_tip,checked_stats,checked_searched,checked_url,checked_case,checked_echo,checked_sql,checked_all,checked_none,checked_selected,checked_default,search_note,the_can='')
          search_note='' if checked_searched !~/\S/
          the_can='' if checked_url !~/\S/
          search_field='' if checked_echo !~/\S/
          @base,@search_field,@selected_db,@result_type,@checked_sql_limit,@checked_tip,@checked_stats,@checked_searched,@checked_url,@checked_case,@checked_echo,@checked_sql,@checked_all,@checked_none,@checked_selected,@checked_default,@search_note,@the_can=base,search_field,selected_db,result_type,checked_sql_limit,checked_tip,checked_stats,checked_searched,checked_url,checked_case,checked_echo,checked_sql,checked_all,checked_none,checked_selected,checked_default,search_note,the_can
          @tip=if checked_tip =~/\S/
            '<font size="2" color="#666666">text:__; fulltxt:__; keywords:__; title:__; author:__; topic_register:__; subject:__; description:__; publisher:__; editor:__; contributor:__; date:__; type:__; format:__; identifier:__; source:__; language:__; relation:__; coverage:__; rights:__; comment:__; abstract:__; filename:__;</font><br>'
          else ''
          end
        end
        def submission_form
            search_form=<<-WOK
      WOK_SQL
    end
    def header_desc
      <<-WOK_SQL
    <!DOCTYPE html>
    <html>
    <head>
      <title>
      <meta charset="utf-8">
      <meta name="sourcefile" content="SiSU._sst" />
      SiSU search form (sample): SiSU information Structuring Universe
      </title>
      <link rel="generator" href="http://www.jus.uio.no/sisu" />
      <link rel="shortcut icon" href="#{@image_src}/rb7.ico" />
      <link href="../_sisu/css/html.css" rel="stylesheet">
    </head>
    <body lang="en" xml:lang="en">
    <table summary="band" border="0" cellpadding="3" cellspacing="0">
    <tr><td width="20%">
     <table summary="home button / home information" border="0" cellpadding="3" cellspacing="0">
     <tr><td align="left">
      <br><a href="http://sisudoc.org/" target="_top">
        <b>SiSU</b>
      </a>
      <br><a href="http://git.sisudoc.org/" target="_top">
        git
      </a>
     </td></tr>
     </table>
    </td>
    <td>
      <label for="find"><b>#{@env.sample_search_form_title(@opt.dir_structure_by)}</b></label>
    </td></tr>
    </table>
      WOK_SQL
    end
    def header2
      <<-'WOK_SQL'
    <form action="#{@base}" id="Test Form" method="post">
      <table cellpadding="2">
      <tr><td valign=\"top\">
          <textarea id="find" name="find" type="text" rows="6" cols="40" maxlength="256">#{@search_field}</textarea>
      </td>
      <td valign=\"top\">
        #{@tip}
        #{@search_note}
        #{@the_can}
      </td></tr></table>
      <td valign=\"top\"><tr><td>
        <!input type="text" id="find" name="find" value="#{@search_field}" />
        <!input type="text" id="find" name="find" value="" />
        <font size="2" color="#222222">
        <b>to search:</b> select which database to search (drop-down menu below); enter your search query (in the form above); and <b>click on the search button</b> (below)
        <br>
        <select name="db" size="1">
          #{@selected_db}
      WOK_SQL
    end
    def buttons1
      <<-'WOK_SQL'
        </select>
        <input type="submit" value="SiSU search" />
        <input type="radio" name="view" value="index" #{@result_type[:index]}> index
        <input type="radio" name="view" value="text" #{@result_type[:text]}> text / grep
      WOK_SQL
    end
    def buttons2
      <<-'WOK_SQL'
        <br>
          match limit:
          <input type="radio" name="sql_match_limit" value="1000" #{@checked_sql_limit[:l1000]}> 1,000
          <input type="radio" name="sql_match_limit" value="2500" #{@checked_sql_limit[:l2500]}> 2,500
        <br>
          <input type="checkbox" name="echo" #{@checked_echo}> echo query
          <input type="checkbox" name="stats" #{@checked_stats}> result stats
          <input type="checkbox" name="url" #{@checked_url}> search url
          <input type="checkbox" name="searched" #{@checked_searched}> searched
          <input type="checkbox" name="tip" #{@checked_tip}> available fields
          <input type="checkbox" name="sql" #{@checked_sql}> sql statement
        <br>
          checks:
          <input type="radio" name="checks" value="check_default" #{@checked_default}> default
          <input type="radio" name="checks" value="check_selected" #{@checked_selected}> selected
          <input type="radio" name="checks" value="check_all" #{@checked_all}> all
          <input type="radio" name="checks" value="check_none" #{@checked_none}> none
          </font>
      </td></tr>
      </table>
    </form>
      WOK
        end
      end
      WOK_SQL
    end
    def search_request
      <<-'WOK_SQL'
      class SearchRequest                                                       #% search_for
        attr_accessor :text1,:fulltext,:keywords,:title,:author,:topic_register,:subject,:description,:publisher,:editor,:contributor,:date,:type,:format,:identifier,:source,:language,:relation,:coverage,:rights,:comment,:abstract,:owner,:date_created,:date_issued,:date_modified,:date_available,:date_valid,:filename
        def initialize(search_field='',q='')
          @search_field,@q=search_field,q
          @text1=@fulltext=@keywords=@title=@author=@topic_register=@subject=@description=@publisher=@editor=@contributor=@date=@type=@format=@identifier=@source=@language=@relation=@coverage=@rights=@comment=@abstract=@owner=@date_created=@date_issued=@date_modified=@date_available=@date_valid=@filename=''
          if @search_field=~/\S/
            @text1=text_to_match('text:')
            @fulltext=text_to_match('fulltxt:')
            @topic_register=text_to_match('topic_register:')
            @title=text_to_match('title:')                  # DublinCore 1  - title
            @author=text_to_match('(?:author|creator)s?:')  # DublinCore 2  - creator/author
            @subject=text_to_match('subj(?:ect)?:')         # DublinCore 3  - subject
            @description=text_to_match('description:')      # DublinCore 4  - description
            @publisher=text_to_match('pub(?:lisher)?:')     # DublinCore 5  - publisher
            @editor=text_to_match('editor:')
            @contributor=text_to_match('contributor:')      # DublinCore 6  - contributor
            @date=text_to_match('date:')                    # DublinCore 7  - date dd-mm-yy
            @type=text_to_match('type:')                    # DublinCore 8  - type
            @format=text_to_match('format:')                # DublinCore 9  - format
            @identifier=text_to_match('identifier:')        # DublinCore 10 - identifier
            @source=text_to_match('source:')                # DublinCore 11 - source
            @language=text_to_match('language:')            # DublinCore 12 - language
            @relation=text_to_match('relation:')            # DublinCore 13 - relation
            @coverage=text_to_match('coverage:')            # DublinCore 14 - coverage
            @rights=text_to_match('rights:')                # DublinCore 15 - rights
            @keywords=text_to_match('key(?:words?)?:')
            @comment=text_to_match('comment:')
            @abstract=text_to_match('abs(?:tract)?:')
            @owner=text_to_match('owner:')
            @date_created=text_to_match('date_created:')
            @date_issued=text_to_match('date_issued:')
            @date_modified=text_to_match('date_modified:')
            @date_available=text_to_match('date_available:')
            @date_valid=text_to_match('date_valid:')
            @filename=text_to_match('filename:')
            @text1=text_to_match unless @keywords or @author or @title or @text1 or @fulltext or @comment or @abstract or @rights or @subject or @publisher or @date or @filename or @topic_register
          else
            @text1=q['s1'] if q['s1']=~/\S/
            @fulltext=q['ft'] if q['ft']=~/\S/
            @keywords=q['key'] if q['key']=~/\S/
            @title=q['ti'] if q['ti']=~/\S/
            @author=q['au'] if q['au']=~/\S/
            @topic_register=q['tr'] if q['tr']=~/\S/
            @subject=q['sj'] if q['sj']=~/\S/
            @description=q['dsc'] if q['dsc']=~/\S/
            @publisher=q['pb'] if q['pb']=~/\S/
            @editor=q['cntr'] if q['cntr']=~/\S/
            @contributor=q['cntr'] if q['cntr']=~/\S/
            @date=q['dt'] if q['dt']=~/\S/
            @type=q['ty'] if q['ty']=~/\S/
            @identifier=q['id'] if q['id']=~/\S/
            @source=q['src'] if q['src']=~/\S/
            @language=q['lang'] if q['lang']=~/\S/
            @relation=q['rel'] if q['rel']=~/\S/
            @coverage=q['cov'] if q['cov']=~/\S/
            @rights=q['cr'] if q['cr']=~/\S/
            @comment=q['co'] if q['co']=~/\S/
            @abstract=q['ab'] if q['ab']=~/\S/
            @date_created=q['dtc'] if q['dtc']=~/\S/
            @date_issued=q['dti'] if q['dti']=~/\S/
            @date_modified=q['dtm'] if q['dtm']=~/\S/
            @date_available=q['dta'] if q['dta']=~/\S/
            @date_valid=q['dtv'] if q['dtv']=~/\S/
            @filename=if q['doc'] and q['search'] !~/search db/ then q['doc']
            elsif q['fns']=~/\S/                                then q['fns']
            end
            @@limit=q['ltd'] if q['ltd']=~/\d+/  # 1000
            @@offset=q['off'] if q['off']=~/\d+/ # 0
          end
        end
        def text_to_match(identifier='')
          m={
            string: /#{identifier}\s*(.+?)/,
            string: /#{identifier}\s*(.+?)(?:;|\n|\r|$)/,
            word: /#{identifier}[\s(]*(\S+)/
          }
          search_string=if @search_field =~m[:word]
            search_string=if @search_field =~m[:braces] then m[:braces].match(@search_field)[1]
            elsif @search_field =~m[:string]            then m[:string].match(@search_field)[1]
            else
              str=m[:word].match(@search_field)[1]
              str=str.gsub(/[()]/,'')
              str
            end
            search_string=search_string.strip.gsub(/\s+/,'+')
          #else
          #  "__"
          end
        end
      end
      WOK_SQL
    end
    def search_statement_common
      <<-'WOK_SQL'
      class DBI_SearchStatement
        attr_reader :text_search_flag,:sql_select_body_format,:sql_offset,:sql_limit
        def initialize(conn,search_for,q,c)
          @conn=conn
          @text_search_flag=false
          @sql_statement={ body: '', endnotes: '', range: '' }
          #@offset||=@@offset
          #@offset+=@@limit
          search={ text: [], endnotes: [] }
          cse=(c =~/\S/) ? true : false
          st=DBI_SearchString.new('doc_objects.clean',search_for.text1,q['s1'],cse).string
          se=DBI_SearchString.new('endnotes.clean',search_for.text1,q['s1'],cse).string
          @text_search_flag=st[:flag]
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.fulltext',search_for.fulltext,q['ft'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.title',search_for.title,q['ti'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.creator_author',search_for.author,q['au'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.classify_topic_register',search_for.topic_register,q['tr'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.classify_subject',search_for.subject,q['sj'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.classify_keywords',search_for.keywords,q['key'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.notes_description',search_for.description,q['dsc'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.publisher',search_for.publisher,q['pb'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.creator_editor',search_for.editor,q['cntr'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.creator_contributor',search_for.contributor,q['cntr'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.date_published',search_for.date,q['dt'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.notes_type',search_for.type,q['ty'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.original_source',search_for.source,q['src'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.language_document_char',search_for.language,q['lang'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.notes_relation',search_for.relation,q['rel'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.notes_coverage',search_for.coverage,q['cov'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.rights_all',search_for.rights,q['cr'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.notes_comment',search_for.comment,q['co'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.notes_abstract',search_for.abstract,q['ab'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          st=DBI_SearchString.new('metadata_and_text.src_filename',search_for.filename,q['fns'],cse).string
          if st[:flag]
            search[:text]                    << st[:search]
          end
          @@limit=q['ltd'] if q['ltd']=~/\d+/  # 1000
          @@offset=q['off'] if q['off']=~/\d+/ # 0
      WOK_SQL
    end
    def pages
      <<-'WOK_SQL'
        def sql_offset
          @@offset
        end
        def sql_match_limit
          @@limit
        end
        def sql_canned_search
          @offset_next=sql_offset.to_i + sql_match_limit.to_i
          @offset_previous=sql_offset.to_i - sql_match_limit.to_i
          def current
            @@canned_search_url.to_s + '&ltd=' + sql_match_limit.to_s + '&off=' + sql_offset.to_s
          end
          def next
            @@canned_search_url.to_s + '&ltd=' + sql_match_limit.to_s + '&off=' + @offset_next.to_s
          end
          def previous
            @offset_previous >= 0 \
            ? (@@canned_search_url.to_s + '&ltd=' + sql_match_limit.to_s + '&off=' + @offset_previous.to_s)
            : ''
          end
          def start
            @@canned_search_url.to_s + '&ltd=' + sql_match_limit.to_s + '&off=' + 0.to_s
          end
          self
        end
        def pre_next(beyond_limit,img)
          can=sql_canned_search
          page=(sql_offset.to_i + sql_match_limit.to_i)/sql_match_limit.to_i
          if beyond_limit
            if page.to_s =~ /^1$/
              %{<br><center>
              pg. #{page.to_s}
              <a href="#{can.next}">
                <img border="0" width="22" height="22" src="#{img}/arrow_next_red.png" alt="&nbsp;&gt;&gt;" />
              </a>
              </center>}
            elsif page.to_s =~ /^2$/
              %{<br><center>
              <a href="#{can.previous}">
                <img border="0" width="22" height="22" src="#{img}/arrow_prev_red.png" alt="&lt;&lt;&nbsp;" />
              </a>
              pg. #{page.to_s}
              <a href="#{can.next}">
                <img border="0" width="22" height="22" src="#{img}/arrow_next_red.png" alt="&nbsp;&gt;&gt;" />
              </a>
              </center>}
            else
              %{<br><center>
              <a href="#{can.start}">
                <img border="0" width="22" height="22" src="#{img}/arrow_prev_red.png" alt="|&lt;&nbsp;" />
              </a>
              <a href="#{can.previous}">
                <img border="0" width="22" height="22" src="#{img}/arrow_prev_red.png" alt="&lt;&lt;&nbsp;" />
              </a>
              pg. #{page.to_s}
              <a href="#{can.next}">
                <img border="0" width="22" height="22" src="#{img}/arrow_next_red.png" alt="&nbsp;&gt;&gt;" />
              </a>
              </center>}
            end
          else
            if page.to_s =~ /^1$/ then ''
            elsif page.to_s =~ /^2$/
              %{<br><center>
              <a href="#{can.previous}">
                <img border="0" width="22" height="22" src="#{img}/arrow_prev_red.png" alt="&lt;&lt;&nbsp;" />
              </a>
              pg. #{page.to_s}
              </center>}
            else
              %{<br><center>
              <a href="#{can.start}">
                <img border="0" width="22" height="22" src="#{img}/arrow_prev_red.png" alt="|&lt;&nbsp;" />
              </a>
              <a href="#{can.previous}">
                <img border="0" width="22" height="22" src="#{img}/arrow_prev_red.png" alt="&lt;&lt;&nbsp;" />
              </a>
              pg. #{page.to_s}
              </center>}
            end
          end
        end
      WOK_SQL
    end
    def tail
      v=SiSU_Env::InfoVersion.instance.get_version
      <<-WOK_SQL
      def tail
        <<-'WOK'
    <br><hr /><br>
<table summary="SiSU summary" cellpadding="2" border="0">
  <!-- widget sisu -->
<tr><td valign="top" width="10%">
 <table summary="home button / home information" border="0" cellpadding="3" cellspacing="0">
 <tr><td align="left">
  <br><a href="http://sisudoc.org/" target="_top">
    <b>SiSU</b>
  </a>
  <br><a href="http://git.sisudoc.org/" target="_top">
    git
  </a>
 </td></tr>
 </table>
</td>
<td valign="top" width="45%">
<!-- SiSU Rights -->
  <p class="tiny_left"><font color="#666666" size="2">
    Generated by
      #{v.project} #{v.version} #{v.date} (#{v.date_stamp})
    <br>
    <a href="http://www.sisudoc.org" >
    <b>#{v.project}</b></a> <sup>&copy;</sup> Ralph Amissah
    1993, current 2015.
    All Rights Reserved.
    <br>
      #{v.project} is software for document structuring, publishing and search,
    <br>
    <a href="http://www.jus.uio.no/sisu" >
      www.jus.uio.no/sisu
    </a>
    and
    <a href="http://www.sisudoc.org" >
      www.sisudoc.org
    </a>
    sources
    <a href="http://git.sisudoc.org" >
      git.sisudoc.org
    </a>
  <br>
    <i>w3 since October 3 1993</i>
    <a href="mailto:ralph@amissah.com" >
      ralph@amissah.com
    </a>
  <br>
    mailing list subscription
    <a href="http://lists.sisudoc.org/listinfo/sisu" >
      http://lists.sisudoc.org/listinfo/sisu
    </a>
  <br>
    <a href="mailto:sisu@lists.sisudoc.org" >
      sisu@lists.sisudoc.org
    </a>
  </font></p>
</td><td valign="top" width="45%">
  <p class="tiny_left"><font color="#666666" size="2">
    #{v.project} using:
    <br>Standard SiSU markup syntax,
    <br>Standard SiSU meta-markup syntax, and the
    <br>Standard SiSU <u>object citation numbering</u> and system, (object/text identifying/locating system)
  <br>
    <sup>&copy;</sup> Ralph Amissah 1997, current 2015.
    All Rights Reserved.
  </font></p>
</td></tr>
  <!-- widget way better -->
<tr><td valign="top" width="10%">
  <p class="tiny_left"><font color="#666666" size="2">
    <a href="http://www.gnu.org/licenses/gpl.html">
      .:
    </a>
  </font></p>
</td><td valign="top" width="45%">
  <p class="tiny_left"><font color="#666666" size="2">
    SiSU is released under
    <a href="http://www.gnu.org/licenses/gpl.html">GPL&nbsp;v3</a>
    or later,
    <a href="http://www.gnu.org/licenses/gpl.html">
      http://www.gnu.org/licenses/gpl.html
    </a>
  </font></p>
</td><td valign="top" width="45%">
  <p class="tiny_left"><font color="#666666" size="2">
    #{v.project}, developed using
    <a href="http://www.ruby-lang.org/en/">
      Ruby
    </a>
    on
    <a href="http://www.debian.org/">
      Debian/Gnu/Linux
    </a>
    software infrastructure,
    with the usual GPL (or OSS) suspects.
  </font></p>
</td></tr>
</table>
    <a name="bottom" id="bottom"></a><a name="down" id="down"></a><a name="end" id="end"></a><a name="finish" id="finish"></a><a name="stop" id="stop"></a><a name="credits" id="credits"></a>
    </body></html>
    WOK
      end
      WOK_SQL
    end
    def main1
      <<-'WOK_SQL'
      @tail=tail
      @counter_txt_doc,@counter_txt_ocn,@counter_endn_doc,@counter_endn_ocn=0,0,0,0
      @counters_txt,@counters_endn,@sql_select_body='','',''
      FCGI.each_cgi do |cgi|
        begin # all code goes in begin section
          @search={ text: [], endnotes: [] }
          q=CGI.new
          @db=if cgi['db'] =~ /#{@db_name_prefix}(\S+)/
            @stub=$1
            cgi['db']
          else
            @stub=@stub_default
            @db_name_prefix + @stub
          end
          checked_url,checked_stats,checked_searched,checked_tip,checked_case,checked_echo,checked_sql,checked_all,checked_none,checked_selected,checked_default,selected_db='','','','','','','','',''
          result_type=(cgi['view']=~/text/) \
          ? result_type={ index: '', text: 'checked'}
          : result_type={ index: 'checked', text: ''}
          @@limit=if cgi['sql_match_limit'].to_s=~/2500/
            checked_sql_limit={ l1000: '', l2500: 'checked'}
            '2500'
          else
            checked_sql_limit={ l1000: 'checked', l2500: ''}
            '1000'
          end
          checked_echo='checked' if cgi['echo'] =~/\S/
          checked_stats='checked' if cgi['stats'] =~/\S/
          checked_url='checked' if cgi['url'] =~/\S/ or cgi['u'].to_i==1
          checked_searched='checked' if cgi['searched'] =~/\S/
          checked_tip='checked' if cgi['tip'] =~/\S/
          checked_case='checked' if cgi['casesense'] =~/\S/
          checked_sql='checked' if cgi['sql'] =~/\S/
          if cgi['checks'] =~/check_all/ or cgi['check_all'] =~/\S/ or cgi['a'].to_i==1
            checked_all='checked'
            checked_echo=checked_stats=checked_url=checked_searched=checked_tip=checked_sql='checked'
            checked_none=''
          elsif cgi['checks'] =~/check_none/
            checked_none='checked'
            checked_all=checked_url=checked_stats=checked_searched=checked_tip=checked_echo=checked_sql=''
          elsif cgi['checks'] =~/check_selected/
            checked_selected='checked'
          elsif cgi['checks'] =~/check_default/
            checked_default='checked'
            checked_echo=checked_stats=checked_url='checked'
            checked_searched=checked_tip=checked_case=checked_sql=''
          else
            checked_selected='checked'
            checked_echo=checked_stats=checked_url='checked'
            checked_searched=checked_tip=checked_case=checked_sql=''
          end
      WOK_SQL
    end
    def main2
      <<-'WOK_SQL'
          search_field=cgi['find'] if cgi['find'] # =~/\S+/
          @search_for=SearchRequest.new(search_field,q) #.analyze               #% search_for
                                                                                 #% searches
          #Canned_search.new(@base,@search_for.text1,cgi)
          if @search_for.text1=~/\S+/ or @search_for.fulltext=~/\S+/ or @search_for.author=~/\S+/ or @search_for.topic_register=~/\S+/  #and search_field =~/\S/
            s1='s1=' + CGI.escape(@search_for.text1) if @search_for.text1=~/\S/
            ft='&ft=' + CGI.escape(@search_for.fulltext) if @search_for.fulltext=~/\S/
            key='key=' + CGI.escape(@search_for.keywords) if @search_for.keywords=~/\S/
            ti='&ti=' + CGI.escape(@search_for.title) if @search_for.title=~/\S/
            au='&au=' + CGI.escape(@search_for.author) if @search_for.author=~/\S/
            tr='&tr=' + CGI.escape(@search_for.topic_register) if @search_for.topic_register=~/\S/
            sj='&sj=' + CGI.escape(@search_for.subject) if @search_for.subject=~/\S/
            dsc='&dsc=' + CGI.escape(@search_for.description) if @search_for.description=~/\S/
            pb='&pb=' + CGI.escape(@search_for.publisher) if @search_for.publisher=~/\S/
            edt='&edt=' + CGI.escape(@search_for.editor) if @search_for.editor=~/\S/
            cntr='&cntr=' + CGI.escape(@search_for.contributor) if @search_for.contributor=~/\S/
            dt='&dt=' + CGI.escape(@search_for.date) if @search_for.date=~/\S/
            ty='&ty=' + CGI.escape(@search_for.type) if @search_for.type=~/\S/
            id='&id=' + CGI.escape(@search_for.identifier) if @search_for.identifier=~/\S/
            src='&src=' + CGI.escape(@search_for.source) if @search_for.source=~/\S/
            lang='&lang=' + CGI.escape(@search_for.language) if @search_for.language=~/\S/
            rel='&rel=' + CGI.escape(@search_for.relation) if @search_for.relation=~/\S/
            cov='&cov=' + CGI.escape(@search_for.coverage) if @search_for.coverage=~/\S/
            cr='&cr=' + CGI.escape(@search_for.rights) if @search_for.rights=~/\S/
            co='&co=' + CGI.escape(@search_for.comment) if @search_for.comment=~/\S/
            ab='&ab=' + CGI.escape(@search_for.abstract) if @search_for.abstract=~/\S/
            dtc='&dtc=' + CGI.escape(@search_for.date_created) if @search_for.date_created=~/\S/
            dti='&dti=' + CGI.escape(@search_for.date_issued) if @search_for.date_issued=~/\S/
            dtm='&dtm=' + CGI.escape(@search_for.date_modified) if @search_for.date_modified=~/\S/
            dta='&dta=' + CGI.escape(@search_for.date_available) if @search_for.date_available=~/\S/
            dtv='&dtv=' + CGI.escape(@search_for.date_valid) if @search_for.date_valid=~/\S/
            fns='&fns=' + CGI.escape(@search_for.filename) if @search_for.filename=~/\S/
            @@canned_search_url=(checked_all =~/checked/) \
            ? "#{@base}?#{s1}#{ft}#{key}#{ti}#{au}#{tr}#{sj}#{dsc}#{pb}#{edt}#{cntr}#{dt}#{ty}#{id}#{src}#{lang}#{rel}#{cov}#{cr}#{co}#{ab}#{dtc}#{dti}#{dtm}#{dta}#{dtv}#{fns}&db=#{cgi['db']}&view=#{cgi['view']}&a=1"
            : "#{@base}?#{s1}#{ft}#{key}#{ti}#{au}#{tr}#{sj}#{dsc}#{pb}#{edt}#{cntr}#{dt}#{ty}#{id}#{src}#{lang}#{rel}#{cov}#{cr}#{co}#{ab}#{dtc}#{dti}#{dtm}#{dta}#{dtv}#{fns}&db=#{cgi['db']}&view=#{cgi['view']}"
            mod=ft=~/\S+/ ? (ft.gsub(/ft/,'s1')) : s1
            @canned_base_url="#{@base}?#{mod}&db=#{cgi['db']}"
            if checked_case=~/\S/
              @search[:text][1]=%{doc_objects.clean~'#{@search_for.text1}'} #s1
            else
              @search[:text][1]=%{doc_objects.clean~*'#{@search_for.text1}'} #s1
            end
            canned_note='search url:'
          else
            @@canned_search_url="#{@base}?db=#{@db}&view=index"
            canned_note='search url example:'
          end
          if search_field =~/\S+/
            analyze_format=search_field.gsub(/\s*\n/,'; ')
          elsif checked_all =~/checked/ or checked_url =~/checked/
            canned_search=@@canned_search_url.scan(/(?:s1|ft|au|ti|fns|tr)=[^&]+/)
            af=canned_search.join('; ')
            af=af.gsub(/s1=/,'text: ').
              gsub(/ft=/,'fulltxt: ').
              gsub(/au=/,'author: ').
              gsub(/ti=/,'title: ').
              gsub(/fns=/,'filename: ').
              gsub(/tr=/,'topic_register: ').
              gsub(/%2B/,' ')
            analyze_format=af
            st=af.split(/\s*;\s*/)
            search_field=st.join("\n")
          end
          green=%{<font size="2" color="#004000">}
          canned_search_url_txt=CGI.escapeHTML(@@canned_search_url)
          the_can=%{<font size="2" color="#666666">#{canned_note} <a href="#{@@canned_search_url}">#{canned_search_url_txt}</a></font><br>}
          p_text=p_fulltext=p_keywords=p_title=p_author=p_topic_register=p_subject=p_description=p_publisher=p_editor=p_contributor=p_date=p_type=p_format=p_identifier=p_source=p_language=p_relation=p_coverage=p_rights=p_comment=p_abstract=p_filename=''
          p_filename=%{filename: #{green}#{@search_for.filename}</font><br>} if @search_for.filename =~/\S+/
          p_text=%{text: #{green}#{@search_for.text1}</font><br>} if @search_for.text1 =~/\S+/
          p_fulltext=%{fulltxt: #{green}#{@search_for.fulltext}</font><br>} if @search_for.fulltext =~/\S+/
          p_title=%{title: #{green}#{@search_for.title}</font><br>} if @search_for.title =~/\S+/
          p_author=%{author: #{green}#{@search_for.author}</font><br>} if @search_for.author =~/\S+/
          p_editor=%{editor: #{green}#{@search_for.editor}</font><br>} if @search_for.editor=~/\S+/
          p_contributor=%{contributor: #{green}#{@search_for.contributor}</font><br>} if @search_for.contributor =~/\S+/
          p_date=%{date: #{green}#{@search_for.date}</font><br>} if @search_for.date =~/\S+/
          p_rights=%{rights: #{green}#{@search_for.rights}</font><br>} if @search_for.rights =~/\S+/
          p_topic_register=%{topic_register: #{green}#{@search_for.topic_register}</font><br>} if @search_for.topic_register =~/\S+/
          p_subject=%{subject: #{green}#{@search_for.subject}</font><br>} if @search_for.subject =~/\S+/
          p_keywords=%{keywords: #{green}#{@search_for.keywords}</font><br>} if @search_for.keywords =~/\S+/
          p_identifier=%{identifier: #{green}#{@search_for.identifier}</font><br>} if @search_for.identifier =~/\S+/
          p_type=%{type: #{green}#{@search_for.type}</font><br>} if @search_for.type =~/\S+/
          p_format=%{format: #{green}#{@search_for.format}</font><br>} if @search_for.format =~/\S+/
          p_relation=%{relation: #{green}#{@search_for.relation}</font><br>} if @search_for.relation =~/\S+/
          p_coverage=%{coverage: #{green}#{@search_for.coverage}</font><br>} if @search_for.coverage =~/\S+/
          p_description=%{description: #{green}#{@search_for.description}</font><br>} if @search_for.description =~/\S+/
          p_abstract=%{abstract: #{green}#{@search_for.abstract}</font><br>} if @search_for.abstract =~/\S+/
          p_comment=%{comment: #{green}#{@search_for.comment}</font><br>} if @search_for.comment =~/\S+/
          p_publisher=%{publisher: #{green}#{@search_for.publisher}</font><br>} if @search_for.publisher =~/\S+/
          p_source=%{source: #{green}#{@search_for.source}</font><br>} if @search_for.source =~/\S+/
          p_language=%{language: #{green}#{@search_for.language}</font><br>} if @search_for.language =~/\S+/
          search_note=<<-WOK
      <font size="2" color="#666666">
      <b>database:</b> #{green}#{@db}</font>; <b>selected view:</b> #{green}#{cgi['view']}</font>
      <b>search string:</b> "#{green}#{analyze_format}</font>"<br>
      #{p_text} #{p_fulltext} #{p_keywords} #{p_title} #{p_author} #{p_topic_register} #{p_subject} #{p_description} #{p_publisher} #{p_editor} #{p_contributor} #{p_date} #{p_type} #{p_format} #{p_identifier} #{p_source} #{p_language} #{p_relation} #{p_coverage} #{p_rights} #{p_comment} #{p_abstract} #{p_filename}
      </font>
      WOK
        #eg = %{canned search e.g.:<br> <a href="#{url}">#{url}</a><br>find: #{analyze}<br>database: #{database}}
        #% dbi_canning
        @header=Form.new(@base,search_field,selected_db,result_type,checked_sql_limit,checked_tip,checked_stats,checked_searched,checked_url,checked_case,checked_echo,checked_sql,checked_all,checked_none,checked_selected,checked_default,search_note,the_can).submission_form #% form
        unless q['s1'] =~/\S/ or q['au'] =~/\S/ or @search[:text][1] =~/\S/
          print "Content-type: text/html\n\n"
          puts (@header+@tail)
        else #% searches
          s1=(@search_for.text1 =~/\S/) \
          ? @search_for.text1
          : 'Unavailable'
          if checked_case=~/\S/
            @search[:text]<<%{doc_objects.clean~'#{CGI.unescape(s1)}'}
          else
            @search[:text]<<%{doc_objects.clean~*'#{CGI.unescape(s1)}'}
          end
          #% dbi_request
          dbi_statement=DBI_SearchStatement.new(@conn,@search_for,q,checked_case)
          @text_search_flag=false
          @text_search_flag=dbi_statement.text_search_flag
          s_contents=dbi_statement.contents
          @body_main=''
          @search_regx=nil
          oldtid=0
          if @text_search_flag
            if checked_sql =~/\S/
              sql_select_body=dbi_statement.sql_select_body_format
            else sql_select_body=''
            end
            @body_main << sql_select_body
          else
          end
          @hostpath="#{@hosturl_files}/#{@stub}"
      WOK_SQL
    end
    def dir_structure #@opt.dir_structure_by
      <<-'WOK_SQL'
          def path_manifest(fn,ln=nil)
            case @output_dir_structure_by
            when 'filename'
              @lingual =='mono' \
              ? "#{@hostpath}/#{fn}/sisu_manifest.html"
              : "#{@hostpath}/#{fn}/sisu_manifest.#{ln}.html"
            when 'filetype'
              @lingual =='mono' \
              ? "#{@hostpath}/manifest/#{fn}.html"
              : "#{@hostpath}/manifest/#{fn}.#{ln}.html"
            else
              "#{@hostpath}/#{ln}/manifest/#{fn}.html"
            end
          end
          def path_html_seg(fn,ln=nil)
            case @output_dir_structure_by
            when 'filename'
              "#{@hostpath}/#{fn}"
            when 'filetype'
              "#{@hostpath}/html/#{fn}"
            else
              "#{@hostpath}/#{ln}/html/#{fn}"
            end
          end
          def path_toc(fn,ln=nil)
            if @output_dir_structure_by =='filename' \
            or @output_dir_structure_by =='filetype'
              @lingual =='mono' \
              ? "#{path_html_seg(fn,ln)}/toc.html"
              : "#{path_html_seg(fn,ln)}/toc.#{ln}.html"
            else
              "#{path_html_seg(fn,ln)}/toc.html"
            end
          end
          def path_filename(fn,seg,ln=nil)
            if @output_dir_structure_by =='filename' \
            or @output_dir_structure_by =='filetype'
              @lingual =='mono' \
              ? "#{path_html_seg(fn,ln)}/#{seg}.html"
              : "#{path_html_seg(fn,ln)}/#{seg}.#{ln}.html"
            else
              "#{path_html_seg(fn,ln)}/#{seg}.html"
            end
          end
          def path_html_doc(fn,ln=nil)
            case @output_dir_structure_by
            when 'filename'
              @lingual =='mono' \
              ? "#{path_html_seg(fn,ln)}/scroll.html"
              : "#{path_html_seg(fn,ln)}/scroll.#{ln}.html"
            when 'filetype'
              @lingual =='mono' \
              ? "#{@hostpath}/html/#{fn}.html"
              : "#{@hostpath}/html/#{fn}.#{ln}.html"
            else
              "#{@hostpath}/#{ln}/html/#{fn}.html"
            end
          end
      WOK_SQL
    end
    def main3
      <<-'WOK_SQL'
                    #% text_objects_body
          s_contents.each do |c|                                               #% text body
            location=c['src_filename'][/(.+?)\.(?:ssm\.sst|sst)$/,1]
            file_suffix=c['src_filename'][/.+?\.(ssm\.sst|sst)$/,1]
            lang=if location =~ /\S+?~(\S\S\S?)$/
              l=location[/\S+?~(\S\S\S?)$/,1]
              location=location.gsub(/(\S+?)~\S\S\S?/,'\1')
              l=".#{l}"
            else ''
            end
          #% metadata_found_body
            if c['tid'].to_i != oldtid.to_i
              ti=c['title']
              can_txt_srch=(cgi['view']=~/index/) \
              ? %{<a href="#{@canned_base_url}&fns=#{c['src_filename']}&lang=#{c['language_document_char']}&view=text"><img border="0" width="24" height="16" src="#{@image_src}/b_search.png" alt="search"></a>&nbsp;}
              : %{<a href="#{@canned_base_url}&fns=#{c['src_filename']}&lang=#{c['language_document_char']}&view=index"><img border="0" width="24" height="16" src="#{@image_src}/b_search.png" alt="search"></a>&nbsp;}
              title=%{<span style="background-color: #{@color_heading}"><a href="#{path_toc(location,c['language_document_char'])}"><img border="0" width="15" height="18" src="#{@image_src}/b_toc.png" alt="toc html">&nbsp;#{ti}</a></span> [#{c['language_document_char']}] by #{c['creator_author']} <a href="#{path_manifest(location,c['language_document_char'])}"><img border="0" width="15" height="15" src="#{@image_src}/b_info.png" alt="manifest"></a> #{can_txt_srch}<br>}  if file_suffix=~/s/ #hmm watch file_suffix
              title=@text_search_flag \
              ? '<br><hr>'+title
              : '<br>'+title
              @counter_txt_doc+=1
              oldtid=c['tid'].to_i
            else                    title=''
            end
            if @text_search_flag
              if cgi['view']=~/text/ \
              or (cgi['view']!~/index/ and cgi['search'] !~/search db/)      #% txt body
                text=if c['suffix'] !~/1/ #seg
                  if @search_for.text1 =~/\S+/ \
                  or q['s1'] =~/\S+/                         #% only this branch is working !!
                    unescaped_search=if @search_for.text1 =~/\S+/
                      CGI.unescape(@search_for.text1)
                    elsif q['s1'] =~/\S+/
                      CGI.unescape(q['s1'])
                    else nil
                    end
                    @search_regx=if unescaped_search                                     #check
                      search_regex=unescaped_search.scan(/\S+/).each.map do |g|
                         (g.to_s =~/(AND|OR)/) \
                         ? ('|')
                         : (%{#{g.to_s}})
                      end.join(' ')
                      search_regex=search_regex.gsub(/\s*\|\s*/,'|')
                      Regexp.new(search_regex, Regexp::IGNORECASE)
                    else nil
                    end
                  else nil
                  end
                  matched_para=(@search_regx.to_s.class==String && @search_regx.to_s=~/\S\S+/) \
                  ? (c['body'].gsub(/(<a\s+href="https?:\/\/[^><\s]+#{@search_regx}[^>]+?>|#{@search_regx})/mi,%{<span style="background-color: #{@color_match}">\\1</span>}))
                  : c['body']
                  %{<hr><p><font size="2">ocn <b><a href="#{path_filename(location,c['seg'],c['language_document_char'])}##{c['ocn']}">#{c['ocn']}</a></b>:</font></p>#{matched_para}}
                elsif c['suffix'] =~/1/ #doc
                  %{#{title}<hr><p><font size="2">ocn #{c['ocn']}:#{c['body']}}
                end
                @counter_txt_ocn+=1
                output=title+text
              else #elsif cgi['view']=~/index/                                #% idx body
                if c['suffix'] !~/1/ #seg
                  index=%{<a href="#{path_filename(location,c['seg'],c['language_document_char'])}##{c['ocn']}">#{c['ocn']}</a>, } if @text_search_flag
                elsif c['suffix'] =~/1/ #doc #FIX
                  index=%{<a href="#{path_html_doc(location,c['language_document_char'])}##{c['ocn']}">#{c['ocn']}</a>, }
                end
                if c['seg'] =~/\S+/
                  if @text_search_flag
                    @counter_txt_ocn+=1
                    output=title+index
                  end
                else
                  @counter_txt_ocn+=1
                  output=c['suffix'] !~/1/ \
                  ? title+index
                  : %{#{title}#{c['ocn'].sort}, }
                end
              end
            else output=title
            end
            @counters_txt=if @counter_txt_doc > 0
              if checked_stats =~/\S/
                @@lt_t=(@counter_txt_ocn==dbi_statement.sql_match_limit.to_i) ? true : false
                start=(@@offset.to_i+1).to_s
                range=(@@offset.to_i+@counter_txt_ocn.to_i).to_s
                %{<hr /><font size="2" color="#666666">Found #{@counter_txt_ocn} times in the main body of #{@counter_txt_doc} documents [ matches #{start} to #{range} ]</font><br>}
              else ''
              end
            else ''
            end
            @body_main << output #+ details
          end
          oldtid = 0
          offset=dbi_statement.sql_offset.to_s
          limit=dbi_statement.sql_match_limit.to_s
          @@lt_t ||=false; @@lt_e ||=false
          canned=(@@lt_t or @@lt_e) \
          ? dbi_statement.pre_next(true,@image_src).to_s
          : dbi_statement.pre_next(false,@image_src).to_s
          limit=dbi_statement.sql_match_limit.to_s
          cgi.out{@header.force_encoding("UTF-8") + @counters_txt.force_encoding("UTF-8") + @counters_endn.force_encoding("UTF-8") + canned.force_encoding("UTF-8") + @body_main.force_encoding("UTF-8") + canned.force_encoding("UTF-8") + @tail.force_encoding("UTF-8")} #% print cgi_output_header+counters+body
        end
        rescue Exception => e
          s='<pre>' + CGI::escapeHTML(e.backtrace.reverse.join("\n"))
          s << CGI::escapeHTML(e.message) + '</pre>'
          cgi.out{s}
          next
        ensure # eg. disconnect from server
          @conn.disconnect if @conn
        end
      end
      WOK_SQL
    end
  end
end
__END__
#+END_SRC

* document header

#+NAME: sisu_document_header
#+BEGIN_SRC text
#encoding: utf-8
=begin
- Name: SiSU

  - Description: documents, structuring, processing, publishing, search
    cgi

  - Author: Ralph Amissah
    <ralph.amissah@gmail.com>

  - Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
    2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019,
    2020, 2021, Ralph Amissah,
    All Rights Reserved.

  - License: GPL 3 or later:

    SiSU, a framework for document structuring, publishing and search

    Copyright (C) Ralph Amissah

    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the Free
    Software Foundation, either version 3 of the License, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
    more details.

    You should have received a copy of the GNU General Public License along with
    this program. If not, see <http://www.gnu.org/licenses/>.

    If you have Internet connection, the latest version of the GPL should be
    available at these locations:
    <http://www.fsf.org/licensing/licenses/gpl.html>
    <http://www.gnu.org/licenses/gpl.html>

    <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>

  - SiSU uses:
    - Standard SiSU markup syntax,
    - Standard SiSU meta-markup syntax, and the
    - Standard SiSU object citation numbering and system

  - Homepages:
    <http://www.sisudoc.org>

  - Git
    <https://git.sisudoc.org/projects/>
    <https://git.sisudoc.org/projects/?p=software/sisu.git;a=summary>
    <https://git.sisudoc.org/projects/?p=markup/sisu-markup-samples.git;a=summary>
=end
#+END_SRC