bcms_google_mini_search 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/.gitignore +12 -0
  2. data/.rvmrc +1 -0
  3. data/COPYRIGHT.txt +23 -0
  4. data/GPL.txt +674 -0
  5. data/Gemfile +7 -0
  6. data/LICENSE.txt +165 -0
  7. data/README.markdown +3 -7
  8. data/Rakefile +10 -0
  9. data/app/models/search_result.rb +34 -104
  10. data/app/portlets/google_mini_search_engine_portlet.rb +4 -20
  11. data/app/views/layouts/templates/{default.html.erb.html.erb → default.html.erb} +0 -0
  12. data/app/views/portlets/google_mini_search_engine/_form.html.erb +0 -2
  13. data/app/views/portlets/google_mini_search_engine/render.html.erb +17 -39
  14. data/bcms_google_mini_search.gemspec +35 -0
  15. data/doc/README_FOR_APP +2 -0
  16. data/lib/bcms_google_mini_search.rb +1 -2
  17. data/lib/bcms_google_mini_search/engine.rb +7 -0
  18. data/lib/bcms_google_mini_search/routes.rb +3 -3
  19. data/lib/bcms_google_mini_search/version.rb +3 -0
  20. data/lib/generators/bcms_google_mini_search/install/USAGE +10 -0
  21. data/lib/generators/bcms_google_mini_search/install/install_generator.rb +9 -0
  22. data/lib/tasks/.gitkeep +0 -0
  23. data/public/stylesheets/.gitkeep +0 -0
  24. data/test/performance/browsing_test.rb +9 -0
  25. data/test/test_helper.rb +18 -0
  26. data/test/unit/helpers/search_engine_helper_test.rb +4 -0
  27. data/test/unit/portlets/google_mini_search_engine_portlet_test.rb +15 -0
  28. data/test/unit/portlets/search_box_portlet_test.rb +9 -0
  29. data/test/unit/search_result_test.rb +474 -0
  30. data/vendor/plugins/.gitkeep +0 -0
  31. metadata +70 -48
  32. data/app/controllers/application_controller.rb +0 -10
  33. data/app/helpers/application_helper.rb +0 -3
  34. data/lib/bcms_google_mini_search/gsa.rb +0 -144
  35. data/rails/init.rb +0 -3
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "mysql2", '0.2.18'
6
+ gem "mocha", '0.9.12', :group => :test, :require=>false
7
+
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -8,14 +8,10 @@ It consists of the following two portlets.
8
8
  2. Google Mini Search Results Portlet - Sends query to the Mini, formats the XML response and displays the results.
9
9
 
10
10
  Features:
11
-
12
11
  1. Allows for formatted results of GSA search results.
13
- 2. Will display Key Matches
14
- 3. Will display Synonyms/Related Queries
15
- 4. Allows conditional overriding of collections (i.e. by default it will search one default collection, but a 'site=OTHER_COLLECTION' param can be passed to override that)
16
- 5. Sort by Date/Relevance - Users can now toggle the results display between date and relevance.
17
- 6. Cached Links - Each result now correctly generate a link to the cached version of the document as stored by the GSA.
18
- 7. [GSA Only] Narrow Your Search - Shows users a set of other suggested queries to allow them to 'Narrow Your Search', based on the current query and what results are stored in GSA. Only available for GSA instances, as Google Mini does not support Dynamic Result Clustering.
12
+ 2. Will display Key Matches (as of 1.1)
13
+ 3. Will display Synonyms/Related Queries (as of 1.1)
14
+ 4. Allows conditional overriding of collections (i.e. by default it will search one collection, but a 'site=OTHER_COLLECTION' param can be passed to override that)
19
15
 
20
16
  Note: This module assume the BrowserCMS web site owner has access to their own GSA/Google Mini server, either hosted by
21
17
  themselves or a third party service.
@@ -0,0 +1,10 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ BcmsGoogleMiniSearch::Application.load_tasks
8
+
9
+ require 'bundler'
10
+ Bundler::GemHelper.install_tasks
@@ -1,18 +1,14 @@
1
+ require 'rexml/document'
2
+ require 'net/http'
3
+
1
4
  class SearchResult
2
5
 
3
- # Creates a new GSA::Appliance from a GoogleMiniSearchPortlet.
4
- #
5
- def self.new_gsa(portlet)
6
- options = {:portlet=>portlet}
7
- normalize_query_options(options)
8
- GSA::Appliance.new(options)
9
- end
6
+ attr_accessor :number, :title, :url, :description, :size
10
7
 
11
8
  #
12
9
  # Queries google mini by a specific URL to find all the results. Converts XML results to
13
10
  # a paging results of Search Results.
14
11
  #
15
- # See SearchResult#query_url for acceptable _options_ params
16
12
  def self.find(query, options={})
17
13
  return QueryResult.new unless query
18
14
  xml_doc = fetch_xml_doc(query, options)
@@ -23,14 +19,6 @@ class SearchResult
23
19
  results
24
20
  end
25
21
 
26
- def self.create_query(query, options={})
27
- opts = options.clone
28
- normalize_query_options(opts)
29
- opts[:query] = query
30
- opts[:engine] = GSA::Engine.new({:host => opts[:host]})
31
- GSA::Query.new(opts)
32
- end
33
-
34
22
  def self.parse_results_count(xml_doc)
35
23
  root = xml_doc.root
36
24
  count = root.elements["RES/M"]
@@ -41,7 +29,16 @@ class SearchResult
41
29
  root = xml_doc.root
42
30
  results = []
43
31
  xml_doc.elements.each('GSP/RES/R') do |ele|
44
- results << GSA::Result.new(ele)
32
+ result = SearchResult.new
33
+ result.number = ele.attributes["N"]
34
+ result.title = ele.elements["T"].text
35
+ result.url = ele.elements["U"].text
36
+ result.description = ele.elements["S"].text
37
+
38
+ doc_size_ele = ele.elements["HAS/C"]
39
+ result.size = doc_size_ele ? doc_size_ele.attributes["SZ"] : ""
40
+
41
+ results << result
45
42
  end
46
43
  results
47
44
  end
@@ -65,76 +62,39 @@ class SearchResult
65
62
  num_pages
66
63
  end
67
64
 
68
- # Construct a query url for the GSA.
69
- #
70
- # @param [String] query
71
- # @param [Hash] options
72
- # @option :host
73
- # @option :start
74
- # @option :front_end
75
- # @option :collection
76
- # @option :sort
77
- # @option :as_xml [Boolean] Determines if the results are returned as xml or html. Default to false.
78
- def self.query_url(query, options)
79
- options[:as_xml] = true if options[:as_xml].nil?
80
65
 
66
+ def self.build_mini_url(options, query)
67
+ portlet = find_search_engine_portlet(options)
81
68
  encoded_query = CGI::escape(query)
82
69
 
83
- # Turns off automatic results filter (filter=0), which when set to 1, allows mini to reduces the # of similar/duplicate results,
84
- # but makes it hard to determine the total # of results.
85
- url = "#{options[:host]}/search?q=#{encoded_query}&output=xml_no_dtd&client=#{options[:front_end]}&site=#{options[:collection]}&filter=0"
70
+ site = portlet.collection_name
71
+ if options[:site]
72
+ site = options[:site]
73
+ end
74
+ # encoded_query = query
75
+ url = "#{portlet.service_url}/search?q=#{encoded_query}&output=xml_no_dtd&client=#{portlet.front_end_name}&site=#{site}&filter=0"
86
76
  if options[:start]
87
77
  url = url + "&start=#{options[:start]}"
88
78
  end
89
-
90
- if options[:sort]
91
- url += "&sort=#{CGI::escape(options[:sort])}"
92
- end
93
-
94
- unless options[:as_xml]
95
- url += "&proxystylesheet=#{options[:front_end]}"
96
- end
97
-
98
- # Ensure both results (oe) and query/input values (ie) are interpreted as UTF-8.
99
- # See http://code.google.com/apis/searchappliance/documentation/46/xml_reference.html#request_i18n
100
- url += "&oe=UTF-8&ie=UTF-8"
101
- return url
102
- end
103
-
104
- def self.create_url_for_query(options, query)
105
- normalize_query_options(options)
106
- return query_url(query, options)
107
- end
108
-
109
- def self.normalize_query_options(options)
110
- portlet = find_search_engine_portlet(options)
111
- options[:front_end] = portlet.front_end_name
112
- options[:collection] = portlet.collection_name
113
- options[:host] = portlet.service_url
114
-
115
- options[:collection] = options.delete(:site) if options[:site]
79
+ return url
116
80
  end
117
81
 
118
82
  def self.find_search_engine_portlet(options)
119
83
  portlet = GoogleMiniSearchEnginePortlet.new
120
84
  if options[:portlet]
121
- portlet = options.delete(:portlet)
85
+ portlet = options[:portlet]
122
86
  end
123
87
  portlet
124
88
  end
125
89
 
126
- # Given a URL, GET it and return the contents
127
- # @param [String] url A URL formatted string
128
- def self.fetch_document(url)
129
- Rails.logger.debug {"GSA: Fetching '#{url}'"}
130
- Net::HTTP.get(URI.parse(url))
131
- end
132
-
133
90
  # Fetches the xml response from the google mini server.
134
91
  def self.fetch_xml_doc(query, options={})
135
- url = create_url_for_query(options, query)
136
- response = fetch_document(url)
137
- REXML::Document.new(response)
92
+ # Turns off automatic results filter (filter=0), which when set to 1, allows mini to reduces the # of similar/duplicate results,
93
+ # but makes it hard to determine the total # of results.
94
+ url = build_mini_url(options, query)
95
+ response = Net::HTTP.get(URI.parse(url))
96
+ xml_doc = REXML::Document.new(response)
97
+ return xml_doc
138
98
  end
139
99
 
140
100
  def self.parse_key_matches(xml_doc)
@@ -165,10 +125,6 @@ class SearchResult
165
125
  attr_accessor :results_count, :num_pages, :current_page, :start, :query, :pages, :key_matches, :synonyms
166
126
  attr_writer :path
167
127
 
168
- # For what these codes mean, see http://code.google.com/apis/searchappliance/documentation/46/xml_reference.html#request_sort
169
- SORT_BY_DATE_PARAM = "date:D:S:d1"
170
- SORT_BY_RELEVANCE_PARAM = "date:D:L:d1"
171
-
172
128
  def initialize(array=[])
173
129
  # Need to set defaults so an empty result set works.
174
130
  self.start = 0
@@ -193,15 +149,11 @@ class SearchResult
193
149
  previous_start >= 0 && num_pages > 1
194
150
  end
195
151
 
196
- # Returns a range of pages that should appear in the pager control. This is design to mimic GSA's pager control,
197
- # which will show up to 20 pages at a time, based on the 'range' of pages around the current page.
198
- #
199
- # i.e. on page 12: < 2 3 4 5 6 7 8 9 10 11 _12_ 13 14 15 16 17 18 19 20 21 22 >
200
152
  def pages
201
- return [] if num_pages <= 1
202
- first_page = current_page - 10 > 1 ? current_page - 10 : 1
203
- last_page = current_page + 9 > num_pages ? num_pages : current_page + 9
204
- (first_page..last_page)
153
+ if num_pages > 1
154
+ return (1..num_pages)
155
+ end
156
+ []
205
157
  end
206
158
 
207
159
  def next_start
@@ -221,28 +173,6 @@ class SearchResult
221
173
  1
222
174
  end
223
175
 
224
- # Determines the current Query is sorting by date.
225
- #
226
- # @param [Hash] params The query parameter from the search page. (same as Rails params)
227
- def sorting_by_date?(params)
228
- params[:sort] == SearchResult::QueryResult::SORT_BY_DATE_PARAM
229
- end
230
-
231
- def path_for(new_query)
232
- "#{path}?query=#{new_query}"
233
- end
234
- # Return the path to sort the current search results by date.
235
- #
236
- # Based on http://code.google.com/apis/searchappliance/documentation/46/xml_reference.html#request_sort
237
- def sort_by_date_path
238
- "#{path}?query=#{query}&sort=#{SORT_BY_DATE_PARAM}"
239
- end
240
-
241
- # Returns the path to sort the current results by relevance (inverse of sort_by_date_path).
242
- def sort_by_relevance_path
243
- "#{path}?query=#{query}&sort=#{SORT_BY_RELEVANCE_PARAM}"
244
- end
245
-
246
176
  def next_page_path
247
177
  "#{path}?query=#{query}&start=#{next_start}"
248
178
  end
@@ -1,28 +1,12 @@
1
1
  class GoogleMiniSearchEnginePortlet < Portlet
2
2
 
3
3
  enable_template_editor true
4
-
4
+
5
5
  def render
6
+ @query = params[:query]
6
7
  @site = params[:site]
7
8
  @start = params[:start] ? params[:start].to_i : 0
8
- options = {:start => @start, :portlet => self, :site=>@site, :sort=>params[:sort]}
9
- query_string = params[:query]
10
-
11
- @results = SearchResult.find(query_string, options.clone) # Need to clone, so that :portlet isn't removed for the 2nd call.
12
-
13
- # This is temporary, while the API is being reworked. Ideally, the search results would contain a reference
14
- # to the query, so that two X calls isn't needed.
15
- @query = SearchResult.create_query(query_string, options.clone)
16
-
17
- if narrow_your_search?
18
- @appliance = SearchResult.new_gsa(self)
19
- @suggested_queries = @appliance.find_narrow_search_results(query_string)
20
- end
9
+ @results = SearchResult.find(@query, {:start => @start, :portlet => @portlet, :site=>@site})
21
10
  end
22
-
23
- # Handles the fact that all portlet attributes, including checkboxes like enable_your_search are stored as strings.
24
- def narrow_your_search?
25
- self.enable_narrow_your_search == "1"
26
- end
27
-
11
+
28
12
  end
@@ -3,6 +3,4 @@
3
3
  <%= f.cms_text_field :service_url, :default_value=> "http://", :instructions => "Full Domain name where the Google Mini server is hosted, i.e. 'http://mini.somedomain.com'." %>
4
4
  <%= f.cms_text_field :collection_name, :instructions => "Copy the name of the Collection from Google Mini here. (Can pass 'site' param to override as well).", :default_value=>"default_collection" %>
5
5
  <%= f.cms_text_field :front_end_name, :instructions => "Copy the name of the Collection front end from Google Mini here.", :default_value=>"default_frontend" %>
6
- <%= f.cms_check_box :enable_narrow_your_search, :instructions => "The 'Narrow Your Search' feature is a only available for Google Search Appliance, not with Google Mini.",
7
- :label=>"Enable Narrowing?" %>
8
6
  <%= f.cms_template_editor :template %>