Class: Gonzui::SearchServlet

Inherits:
GonzuiAbstractServlet show all
Defined in:
lib/gonzui/webapp/search.rb

Constant Summary

Constants included from URIMaker

URIMaker::ParamTable

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GonzuiAbstractServlet

#format_html, #get_media_type, #get_mime_type, #init_servlet, #initialize, #log, #make_path, #set_content_type_text_html

Methods included from HTMLMaker

#make_content_script_type, #make_css, #make_footer, #make_format_select, #make_h1, #make_html, #make_license_select, #make_meta, #make_meta_and_css, #make_navi, #make_property_select, #make_script, #make_search_form, #make_spacer, #make_status_line, #make_title

Methods included from URIMaker

#decompose_search_query, #escape_path, #get_default_query_value, #get_query_value, #get_short_name, #make_advanced_search_uri, #make_doc_uri, #make_google_uri, #make_lineno_uri, #make_markup_uri, #make_search_uri, #make_search_uri_partial, #make_source_uri, #make_stat_uri, #make_top_uri, #make_uri_general, #make_uri_with_options

Methods included from GetText

#gettext, #gettext_noop, #load_catalog, #set_catalog

Methods included from Util

assert, assert_equal, assert_equal_all, assert_non_nil, assert_not_reached, benchmark, command_exist?, commify, eprintf, format_bytes, program_name, protect_from_signals, require_command, set_verbosity, shell_escape, unix?, vprintf, windows?, wprintf

Constructor Details

This class inherits a constructor from Gonzui::GonzuiAbstractServlet

Class Method Details

.mount_pointObject



14
15
16
# File 'lib/gonzui/webapp/search.rb', line 14

def self.mount_point
  "search"
end

Instance Method Details

#do_GET(request, response) ⇒ Object



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
# File 'lib/gonzui/webapp/search.rb', line 225

def do_GET(request, response)
  init_servlet(request, response)
  log(@search_query.string)

  title = make_title(@search_query.string)
  html = make_html
  head = [:head, title,  make_script, *make_meta_and_css]
  body = [:body]
  body.push(make_h1)
  body.push(make_search_form)

  result = nil
  navi = nil
  begin
    @search_query.validate
    redirect_if_necessary

    searcher = Searcher.new(@dbm, @search_query, 
                            @config.max_results_overall)
    result = searcher.search
    content = make_result_content(result)
    if result.length > @nresults_per_page
      navi = make_result_navi(result)
    end
  rescue QueryError => e
    result = SearchResult.new
    content = make_query_error_message(e)
  end
  status_line = make_status_line(_("Search"), result)

  body.push(make_notice)
  body.push(status_line)
  body.push(content)
  body.push(navi) if navi

  body.push(make_footer)
  html.push(head)
  html.push(body)
  set_content_type_text_html
  response.body = format_html(html)
end

#do_make_navi(from) ⇒ Object



144
145
146
147
148
# File 'lib/gonzui/webapp/search.rb', line 144

def do_make_navi(from)
  make_search_uri(@search_query, 
                  :from => from,
                  :nresults_per_page => @nresults_per_page)
end

#do_make_status_line(result) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gonzui/webapp/search.rb', line 18

def do_make_status_line(result)
  right = ""
  if result.nhits > 0
    qs = @search_query.string
    from = (@search_query.package or @search_query.path)
    qs = @search_query.simplified_string if from
    to = [result.nhits, @from + @nresults_per_page].min
    right = [:span]
    limit_exceeded_mark = if result.limit_exceeded? then "+" else "" end
    right.push(_("Results "), 
               [:strong, commify(@from + 1)], " - ", 
               [:strong, commify(to)], _(" of "), 
               [:strong, commify(result.nhits)],
               limit_exceeded_mark,
               _(" for "),
               [:strong, qs])
    right.push(_(" from "), [:strong, from]) if from
  end
  center =  [:a, {:href => make_google_uri(@search_query)},
    _("Google it")]
  return center, right
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gonzui/webapp/search.rb', line 41

def make_expansion_link(item)
  if item.has_more_in_package?
    package_name = @dbm.get_package_name(item.package_id)
    # FIXME: it should be simplified.
    qs = sprintf("package:%s %s", package_name, 
                 @search_query.simplified_string)
    search_query = SearchQuery.new(@config, qs, @search_query.options)
    uri = make_search_uri(search_query)
    from = package_name
  elsif item.has_more_in_path?
    path = @dbm.get_path(item.path_id)
    qs = sprintf("path:%s %s", path, 
                 @search_query.simplified_string)
    search_query = SearchQuery.new(@config, qs, @search_query.options)
    uri = make_search_uri(search_query)
    from = File.basename(path)
  else
    assert_not_reached
  end
  dd = [:dd,[:a, {:href => uri, :class => "more" }, 
      sprintf(_("More results from %s"), from)]]
  return dd
end

#make_not_found_messageObject



167
168
169
170
171
172
173
# File 'lib/gonzui/webapp/search.rb', line 167

def make_not_found_message
  message = [:div, {:class => "message"},
    sprintf(_("No contents were found matching your search - %s."), 
            @search_query.string)
  ]
  return message
end

#make_noticeObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/gonzui/webapp/search.rb', line 210

def make_notice
  if @search_query.ignored_words.empty?
    return ""
  else
    div = [:div, {:class => "notice"}]
    message = 
      sprintf(_("\"%s\" (and any subsequent words) was ignored because queries are limited to %d words."),
              @search_query.ignored_words.first,
              @config.max_words
              )
    div.push(message)
    return div
  end
end

#make_package_name_line(package_name) ⇒ Object



65
66
67
68
69
# File 'lib/gonzui/webapp/search.rb', line 65

def make_package_name_line(package_name)
  uri = make_markup_uri(package_name)
  dt = [:dt, {:class => "package"}, [:a, {:href => uri}, package_name]]
  return dt
end

#make_query_error_message(e) ⇒ Object



175
176
177
178
179
180
# File 'lib/gonzui/webapp/search.rb', line 175

def make_query_error_message(e)
  message =[:div]
  message.push(make_not_found_message)
  message.push([:div, {:class => "message"}, _(e.message)])
  return message
end

#make_result_content(result) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/gonzui/webapp/search.rb', line 182

def make_result_content(result)
  @from = 0 if @from >= result.length

  if result.empty?
    make_not_found_message
  elsif result.single_path?
    make_result_list_like_grep(result)
  else
    make_result_list_by_snippet(result)
  end
end

#make_result_list_by_snippet(result) ⇒ Object



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
# File 'lib/gonzui/webapp/search.rb', line 85

def make_result_list_by_snippet(result)
  dl = [:dl]
  prev_package_id = nil
  nresults = 0

  result.each_from(@from) {|item|
    assert(item.list.length > 0)
    package_name = @dbm.get_package_name(item.package_id)

    if item.package_id != prev_package_id
      dt = make_package_name_line(package_name)
      dl.push(dt)
      prev_package_id = item.package_id
    end

    path = @dbm.get_path(item.path_id)
    markup_uri = make_markup_uri(path, @search_query)
    dt = make_source_path_line(item, package_name, path, markup_uri)
    dl.push(dt)

    #
    # FIXME: if the target content is a text file, it's
    # better to make a snippet like google's one instead
    # of a line-oriented style.
    #
    content = @dbm.get_content(item.path_id)
    maker = SnippetMaker.new(content, item, markup_uri)
    pre = maker.make_line_oriented_kwic
    dl.push([:dd, pre])

    if item.has_more?
      link = make_expansion_link(item)
      dl.push(link)
    end
    nresults += 1
    break if nresults >= @nresults_per_page
  }
  return dl
end

#make_result_list_like_grep(result) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gonzui/webapp/search.rb', line 125

def make_result_list_like_grep(result)
  assert_equal(1, result.length)
  dl = [:dl]
  item = result.first

  package_name = @dbm.get_package_name(item.package_id)
  path = @dbm.get_path(item.path_id)
  markup_uri = make_markup_uri(path, @search_query)

  dl.push(make_package_name_line(package_name))
  dl.push(make_source_path_line(item, package_name, path, markup_uri))

  content = @dbm.get_content(item.path_id)
  maker = SnippetMaker.new(content, item, markup_uri)
  snippet = maker.make_context_grep
  dl.push([:dd, snippet])
  return dl
end

#make_result_navi(result) ⇒ Object



206
207
208
# File 'lib/gonzui/webapp/search.rb', line 206

def make_result_navi(result)
  make_navi(result.length, @from, @nresults_per_page, @config.max_pages)
end

#make_source_path_line(item, package_name, path, markup_uri) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gonzui/webapp/search.rb', line 71

def make_source_path_line(item, package_name, path, markup_uri)
  lineno_uri = make_lineno_uri(markup_uri, item.list.first.lineno)
  source_line = [:dt, {:class => "source"}, [:a, 
      {:href => lineno_uri}, 
      File.relative_path(path, package_name)]]
  file_info = @dbm.(item.path_id)
  size = [:span, {:class => "size"}, 
    sprintf(_(" - %s - %s lines"), 
            format_bytes(file_info.size),
            commify(file_info.nlines))
          ]
  source_line.push(size)
end

#redirect_if_necessaryObject



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/gonzui/webapp/search.rb', line 194

def redirect_if_necessary
  if @search_query.empty?
    redirect_to_uri(make_top_uri)
  elsif @search_query.path_only?
    markup_uri = make_markup_uri(@search_query.path, @search_query)
    redirect_to_uri(markup_uri)
  elsif @search_query.package_only?
    markup_uri = make_markup_uri(@search_query.package, @search_query)
    redirect_to_uri(markup_uri)
  end
end

#redirect_to_single_result(result) ⇒ Object

FIXME: it’s not used



156
157
158
159
160
161
162
163
164
# File 'lib/gonzui/webapp/search.rb', line 156

def redirect_to_single_result(result)
  item = result.first
  occ = item.list.first
  path = @dbm.get_path(item.path_id)
  lineno = (occ.lineno or 1)
  markup_uri = make_markup_uri(path, @search_query)
  lineno_uri = make_lineno_uri(markup_uri, lineno)
  redirect_to_uri(lineno_uri)
end

#redirect_to_uri(uri) ⇒ Object



150
151
152
153
# File 'lib/gonzui/webapp/search.rb', line 150

def redirect_to_uri(uri)
  @response.set_redirect(HTTPStatus::MovedPermanently, uri)
  assert_not_reached
end