@@ -1,116 +1,116 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class SearchController < ApplicationController |
|
18 | class SearchController < ApplicationController | |
19 | before_filter :find_optional_project |
|
19 | before_filter :find_optional_project | |
20 |
|
20 | |||
21 | helper :messages |
|
21 | helper :messages | |
22 | include MessagesHelper |
|
22 | include MessagesHelper | |
23 |
|
23 | |||
24 | def index |
|
24 | def index | |
25 | @question = params[:q] || "" |
|
25 | @question = params[:q] || "" | |
26 | @question.strip! |
|
26 | @question.strip! | |
27 | @all_words = params[:all_words] || (params[:submit] ? false : true) |
|
27 | @all_words = params[:all_words] || (params[:submit] ? false : true) | |
28 | @titles_only = !params[:titles_only].nil? |
|
28 | @titles_only = !params[:titles_only].nil? | |
29 |
|
29 | |||
30 | projects_to_search = |
|
30 | projects_to_search = | |
31 | case params[:scope] |
|
31 | case params[:scope] | |
32 | when 'all' |
|
32 | when 'all' | |
33 | nil |
|
33 | nil | |
34 | when 'my_projects' |
|
34 | when 'my_projects' | |
35 | User.current.memberships.collect(&:project) |
|
35 | User.current.memberships.collect(&:project) | |
36 | when 'subprojects' |
|
36 | when 'subprojects' | |
37 | @project ? (@project.self_and_descendants.active) : nil |
|
37 | @project ? (@project.self_and_descendants.active) : nil | |
38 | else |
|
38 | else | |
39 | @project |
|
39 | @project | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | offset = nil |
|
42 | offset = nil | |
43 | begin; offset = params[:offset].to_time if params[:offset]; rescue; end |
|
43 | begin; offset = params[:offset].to_time if params[:offset]; rescue; end | |
44 |
|
44 | |||
45 | # quick jump to an issue |
|
45 | # quick jump to an issue | |
46 | if @question.match(/^#?(\d+)$/) && Issue.visible.find_by_id($1) |
|
46 | if @question.match(/^#?(\d+)$/) && Issue.visible.find_by_id($1) | |
47 | redirect_to :controller => "issues", :action => "show", :id => $1 |
|
47 | redirect_to :controller => "issues", :action => "show", :id => $1 | |
48 | return |
|
48 | return | |
49 | end |
|
49 | end | |
50 |
|
50 | |||
51 | @object_types = %w(issues news documents changesets wiki_pages messages projects) |
|
51 | @object_types = %w(issues news documents changesets wiki_pages messages projects) | |
52 | if projects_to_search.is_a? Project |
|
52 | if projects_to_search.is_a? Project | |
53 | # don't search projects |
|
53 | # don't search projects | |
54 | @object_types.delete('projects') |
|
54 | @object_types.delete('projects') | |
55 | # only show what the user is allowed to view |
|
55 | # only show what the user is allowed to view | |
56 | @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)} |
|
56 | @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)} | |
57 | end |
|
57 | end | |
58 |
|
58 | |||
59 | @scope = @object_types.select {|t| params[t]} |
|
59 | @scope = @object_types.select {|t| params[t]} | |
60 | @scope = @object_types if @scope.empty? |
|
60 | @scope = @object_types if @scope.empty? | |
61 |
|
61 | |||
62 | # extract tokens from the question |
|
62 | # extract tokens from the question | |
63 | # eg. hello "bye bye" => ["hello", "bye bye"] |
|
63 | # eg. hello "bye bye" => ["hello", "bye bye"] | |
64 | @tokens = @question.scan(%r{((\s|^)"[\s\w]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')} |
|
64 | @tokens = @question.scan(%r{((\s|^)"[\s\w]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')} | |
65 |
# tokens must be at least |
|
65 | # tokens must be at least 2 characters long | |
66 |
@tokens = @tokens.uniq.select {|w| w.length > |
|
66 | @tokens = @tokens.uniq.select {|w| w.length > 1 } | |
67 |
|
67 | |||
68 | if !@tokens.empty? |
|
68 | if !@tokens.empty? | |
69 | # no more than 5 tokens to search for |
|
69 | # no more than 5 tokens to search for | |
70 | @tokens.slice! 5..-1 if @tokens.size > 5 |
|
70 | @tokens.slice! 5..-1 if @tokens.size > 5 | |
71 | # strings used in sql like statement |
|
71 | # strings used in sql like statement | |
72 | like_tokens = @tokens.collect {|w| "%#{w.downcase}%"} |
|
72 | like_tokens = @tokens.collect {|w| "%#{w.downcase}%"} | |
73 |
|
73 | |||
74 | @results = [] |
|
74 | @results = [] | |
75 | @results_by_type = Hash.new {|h,k| h[k] = 0} |
|
75 | @results_by_type = Hash.new {|h,k| h[k] = 0} | |
76 |
|
76 | |||
77 | limit = 10 |
|
77 | limit = 10 | |
78 | @scope.each do |s| |
|
78 | @scope.each do |s| | |
79 | r, c = s.singularize.camelcase.constantize.search(like_tokens, projects_to_search, |
|
79 | r, c = s.singularize.camelcase.constantize.search(like_tokens, projects_to_search, | |
80 | :all_words => @all_words, |
|
80 | :all_words => @all_words, | |
81 | :titles_only => @titles_only, |
|
81 | :titles_only => @titles_only, | |
82 | :limit => (limit+1), |
|
82 | :limit => (limit+1), | |
83 | :offset => offset, |
|
83 | :offset => offset, | |
84 | :before => params[:previous].nil?) |
|
84 | :before => params[:previous].nil?) | |
85 | @results += r |
|
85 | @results += r | |
86 | @results_by_type[s] += c |
|
86 | @results_by_type[s] += c | |
87 | end |
|
87 | end | |
88 | @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime} |
|
88 | @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime} | |
89 | if params[:previous].nil? |
|
89 | if params[:previous].nil? | |
90 | @pagination_previous_date = @results[0].event_datetime if offset && @results[0] |
|
90 | @pagination_previous_date = @results[0].event_datetime if offset && @results[0] | |
91 | if @results.size > limit |
|
91 | if @results.size > limit | |
92 | @pagination_next_date = @results[limit-1].event_datetime |
|
92 | @pagination_next_date = @results[limit-1].event_datetime | |
93 | @results = @results[0, limit] |
|
93 | @results = @results[0, limit] | |
94 | end |
|
94 | end | |
95 | else |
|
95 | else | |
96 | @pagination_next_date = @results[-1].event_datetime if offset && @results[-1] |
|
96 | @pagination_next_date = @results[-1].event_datetime if offset && @results[-1] | |
97 | if @results.size > limit |
|
97 | if @results.size > limit | |
98 | @pagination_previous_date = @results[-(limit)].event_datetime |
|
98 | @pagination_previous_date = @results[-(limit)].event_datetime | |
99 | @results = @results[-(limit), limit] |
|
99 | @results = @results[-(limit), limit] | |
100 | end |
|
100 | end | |
101 | end |
|
101 | end | |
102 | else |
|
102 | else | |
103 | @question = "" |
|
103 | @question = "" | |
104 | end |
|
104 | end | |
105 | render :layout => false if request.xhr? |
|
105 | render :layout => false if request.xhr? | |
106 | end |
|
106 | end | |
107 |
|
107 | |||
108 | private |
|
108 | private | |
109 | def find_optional_project |
|
109 | def find_optional_project | |
110 | return true unless params[:id] |
|
110 | return true unless params[:id] | |
111 | @project = Project.find(params[:id]) |
|
111 | @project = Project.find(params[:id]) | |
112 | check_project_privacy |
|
112 | check_project_privacy | |
113 | rescue ActiveRecord::RecordNotFound |
|
113 | rescue ActiveRecord::RecordNotFound | |
114 | render_404 |
|
114 | render_404 | |
115 | end |
|
115 | end | |
116 | end |
|
116 | end |
General Comments 0
You need to be logged in to leave comments.
Login now