summaryrefslogtreecommitdiffstats
path: root/lib/sisu/v6/cgi_sqlite.rb
blob: 0b702ec4af832ea9a9776243400c178c28a36efa (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
# encoding: utf-8
=begin

 * Name: SiSU

 * Description: a framework for document structuring, publishing and search

 * Author: Ralph Amissah

 * Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
   2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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

 * Hompages:
   <http://www.jus.uio.no/sisu>
   <http://www.sisudoc.org>

 * Download:
   <http://www.sisudoc.org/sisu/en/SiSU/download.html>

 * Git
   <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
   <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/v6/cgi_sql_sqlite.rb;hb=HEAD>

 * Ralph Amissah
   <ralph@amissah.com>
   <ralph.amissah@gmail.com>

 ** Description: generates naive cgi search form for search of sisu database (sqlite)

=end
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_endnotes='',''
          @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)')
          @search_endnotes=search[:endnotes].flatten.join(' AND ')
          @search_endnotes=@search_endnotes.gsub(/(endnotes\.clean\s+LIKE\s+\(\s*'%[^']+%'\s*\)\s+(?:(?:AND|OR)\s+endnotes\.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_endnotes
          limit ||=@@limit
          offset ||=@@offset
          @sql_statement[:endnotes]=%{SELECT metadata_and_text.title, metadata_and_text.creator_author, metadata_and_text.src_filename, metadata_and_text.language_document_char, endnotes.body, endnotes.nr, endnotes.ocn, endnotes.metadata_tid FROM metadata_and_text, endnotes WHERE #{@search_endnotes} AND metadata_and_text.tid = endnotes.metadata_tid ORDER BY metadata_and_text.language_document_char, metadata_and_text.title, metadata_and_text.src_filename, endnotes.nr}
          @sql_statement[:range]=%{LIMIT #{limit} OFFSET #{offset} ;}
          select=@sql_statement[:endnotes] + ' ' + @sql_statement[:range]
          select
        end
        def sql_select_body_format
          %{<font color="#666666" size="2">#{sql_select_body}</font>}
        end
        def sql_select_endnotes_format
          %{<font color="#666666" size="2">#{sql_select_endnotes}</font>}
        end
        def contents
          @conn.execute(sql_select_body)
        end
        def endnotes
          @conn.execute(sql_select_endnotes)
        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__