aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v0/cgi_pgsql.rb
blob: 9a6486ff8a9a6dcff44e8b19ef370c9a868b0149 (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
=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 Ralph Amissah All Rights Reserved.

 * License: GPL 3 or later:

   SiSU, a framework for document structuring, publishing and search

   Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
   2007 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/copyleft/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

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

 * Download:
   <http://www.jus.uio.no/sisu/SiSU/download.html>

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

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

=end
module  SiSU_CGI_pgsql                                                            #% database building documents
  require "#{SiSU_lib}/sysenv"
  require "#{SiSU_lib}/cgi_sql_common"
  include SiSU_CGI_sql
  class SiSU_search_pgsql < SiSU_CGI_common
    def initialize(opt,webserv)
      @opt,@webserv=opt,webserv
      @env=SiSU_Env::Info_env.new('',opt)
      @sys=SiSU_Env::System_call.new
      @image_src="#{@env.url.webserv_cgi}/_sisu/image_sys"
      @common=SiSU_CGI_sql::SiSU_CGI_common.new(@webserv,@opt.cmd,@image_src,@env)
      @db=SiSU_Env::Info_db.new
      #p @db.port_psql
    end
    def pgsql
      serve=[]
      if @sys.psql
        available_db_table=`psql --list`                            # system call requires psql
        available_db=available_db_table.scan(/(SiSU_\S+)/) if not available_db_table.nil?
        if available_db \
        and available_db.class == Array
          available_db.flatten.each do |x|
            serve << x.gsub(/SiSU_(\S+)/,'\1')
          end
        else put "WARNING: no postgresql database available, (have you created one?)"
        end
        serve.sort!
        f1,f2,f3=[],[],[]
        serve.each do |x|
          f1 << %{              <option value="SiSU_#{x}">#{x}</option>\n} unless x =~/apache|sisu\/image/ #check
        end
        f2 <<  %{          selected_db=case cgi['db']\n}
        serve.each do |x|
          f2 << %{          when /SiSU_#{x}/;        '<option value="SiSU_#{x}">#{x}</option>'\n} unless x =~/apache|sisu\/image/ #check
        end
      end
      f2 << "          end\n"
      if FileTest.writable?('.')
        output=File.open('sisu_pgsql.cgi','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
        a=%{        generated sisu_pgsql.cgi,
            BASED ON ALREADY EXISTING directories that could potentially be used to populate postgresql db, (-D)
        }
        tell=SiSU_Screen::Ansi.new(@opt.cmd,a)
        tell.print_grey
        c=case @webserv
        when /pwd/; ''
        else "if necessary make the directory /usr/lib/cgi-bin :
                  sudo cp -vi #{Dir.pwd}/sisu_pgsql.cgi /usr/lib/cgi-bin/.
                  sudo chmod -v 755 /usr/lib/cgi-bin/sisu_pgsql.cgi
                  (copy sisu_pgsql.cgi to your cgi directory) and set file permissions to 755"
        end
        a=%{#{c}
                  #{@env.webserv_base_cgi}/cgi-bin/sisu_pgsql.cgi
        }
        tell=SiSU_Screen::Ansi.new(@opt.cmd,a)
        tell.warn
        a="postgresql db used for present directory: #{@db.psql.db}"
        b="\n\t(to create and populate postgresql database see 'man sisu' and in particular the -D flag)\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\tsee 'sisu --help sql'\n\tif you have permission to create databases:\n\t'sisu -d --createdb'\n\tor using postgresql tools directly:\n\t'createdb #{@db.psql.db}'\n\tfor a list of existing databases try 'psql --list']"
        tell=SiSU_Screen::Ansi.new(@opt.cmd,a,b)
        tell.txt_cyan
      else puts 'failed in attempt to write sisu_pgsql.cgi 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
      require 'cgi'
      require 'fcgi'
      require 'dbi'
      @version='sisu_pgsql'
      @image_src="#{@env.url.webserv_cgi}/_sisu/image_sys"
      @hosturl_db="#{@env.url.webserv_base_cgi}"
      @hosturl_files="#{@env.url.webserv_files_from_db}"
      @port="#{@db.psql.port}"
      user=''  # '#{@env.user}'
      WOK_SQL
    end
    def search_statement
      <<-'WOK_SQL'
      class Dbi_search_string
        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+/;     unescaped_search=CGI.unescape(@t)
            elsif @q =~/\S+/;  unescaped_search=CGI.unescape(@q)
            end
            search_construct=[]
            if @c
              unescaped_search.gsub!(/\s*(AND|OR)\s*/,"' \) \\1 #@l~\( '")
              unescaped_search.gsub!(/(.+)/,"#@l~\( '\\1' \)")
            else
              unescaped_search.gsub!(/\s*(AND|OR)\s*/,"' \) \\1 #@l~*\( '")
              unescaped_search.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_endnotes=[],[]
                                                                                 #%
          search[:text].each{|x| @search_text << "#{x} AND " }
          @search_text=@search_text.to_s.gsub!(/AND\s+$/,'')
          @search_text.gsub!(/(documents\.clean~[*]?\(\s*'[^']+'\s*\)\s+(?:(?:AND|OR)\s+documents\.clean~[*]?\(\s*'[^']+'\s*\))+)/,'(\1)')
          search[:endnotes].each{|x| @search_endnotes << "#{x} AND " }
          @search_endnotes=@search_endnotes.to_s.gsub!(/AND\s+$/,'')
          @search_endnotes.gsub!(/(endnotes\.clean~\(\s*'[^']+'\s*\)\s+(?:(?:AND|OR)\s+endnotes\.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.title, metadata.subtitle, metadata.creator, metadata.filename, metadata.suffix, documents.body, documents.seg, documents.ocn, metadata.tid FROM documents, metadata WHERE (#@search_text) AND documents.metadata_tid = metadata.tid ORDER BY metadata.title, metadata.filename, documents.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.title, metadata.subtitle, metadata.creator, metadata.filename, endnotes.body, endnotes.nr, endnotes.ocn, endnotes.metadata_tid FROM metadata, endnotes WHERE (#@search_endnotes) AND metadata.tid = endnotes.metadata_tid ORDER BY metadata.title, metadata.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.select_all(sql_select_body)
        end
        def endnotes
          @conn.select_all(sql_select_endnotes)
        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'
          dbi="dbi:Pg:database=#{@db};port=#{@port}"
          @conn=DBI.connect(dbi,user)
      WOK_SQL
    end
  end
end
__END__