@@ -1,113 +1,113 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2014 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 | def index |
|
21 | def index | |
22 | @question = params[:q] || "" |
|
22 | @question = params[:q] || "" | |
23 | @question.strip! |
|
23 | @question.strip! | |
24 | @all_words = params[:all_words] ? params[:all_words].present? : true |
|
24 | @all_words = params[:all_words] ? params[:all_words].present? : true | |
25 | @titles_only = params[:titles_only] ? params[:titles_only].present? : false |
|
25 | @titles_only = params[:titles_only] ? params[:titles_only].present? : false | |
26 |
|
26 | |||
27 | projects_to_search = |
|
27 | projects_to_search = | |
28 | case params[:scope] |
|
28 | case params[:scope] | |
29 | when 'all' |
|
29 | when 'all' | |
30 | nil |
|
30 | nil | |
31 | when 'my_projects' |
|
31 | when 'my_projects' | |
32 |
User.current. |
|
32 | User.current.projects | |
33 | when 'subprojects' |
|
33 | when 'subprojects' | |
34 | @project ? (@project.self_and_descendants.active.to_a) : nil |
|
34 | @project ? (@project.self_and_descendants.active.to_a) : nil | |
35 | else |
|
35 | else | |
36 | @project |
|
36 | @project | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | # quick jump to an issue |
|
39 | # quick jump to an issue | |
40 | if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i)) |
|
40 | if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i)) | |
41 | redirect_to issue_path(issue) |
|
41 | redirect_to issue_path(issue) | |
42 | return |
|
42 | return | |
43 | end |
|
43 | end | |
44 |
|
44 | |||
45 | @object_types = Redmine::Search.available_search_types.dup |
|
45 | @object_types = Redmine::Search.available_search_types.dup | |
46 | if projects_to_search.is_a? Project |
|
46 | if projects_to_search.is_a? Project | |
47 | # don't search projects |
|
47 | # don't search projects | |
48 | @object_types.delete('projects') |
|
48 | @object_types.delete('projects') | |
49 | # only show what the user is allowed to view |
|
49 | # only show what the user is allowed to view | |
50 | @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)} |
|
50 | @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)} | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | @scope = @object_types.select {|t| params[t]} |
|
53 | @scope = @object_types.select {|t| params[t]} | |
54 | @scope = @object_types if @scope.empty? |
|
54 | @scope = @object_types if @scope.empty? | |
55 |
|
55 | |||
56 | # extract tokens from the question |
|
56 | # extract tokens from the question | |
57 | # eg. hello "bye bye" => ["hello", "bye bye"] |
|
57 | # eg. hello "bye bye" => ["hello", "bye bye"] | |
58 | @tokens = @question.scan(%r{((\s|^)"[\s\w]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')} |
|
58 | @tokens = @question.scan(%r{((\s|^)"[\s\w]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')} | |
59 | # tokens must be at least 2 characters long |
|
59 | # tokens must be at least 2 characters long | |
60 | @tokens = @tokens.uniq.select {|w| w.length > 1 } |
|
60 | @tokens = @tokens.uniq.select {|w| w.length > 1 } | |
61 |
|
61 | |||
62 | if !@tokens.empty? |
|
62 | if !@tokens.empty? | |
63 | # no more than 5 tokens to search for |
|
63 | # no more than 5 tokens to search for | |
64 | @tokens.slice! 5..-1 if @tokens.size > 5 |
|
64 | @tokens.slice! 5..-1 if @tokens.size > 5 | |
65 |
|
65 | |||
66 | limit = 10 |
|
66 | limit = 10 | |
67 |
|
67 | |||
68 | @result_count = 0 |
|
68 | @result_count = 0 | |
69 | @result_count_by_type = Hash.new {|h,k| h[k] = 0} |
|
69 | @result_count_by_type = Hash.new {|h,k| h[k] = 0} | |
70 | ranks_and_ids = [] |
|
70 | ranks_and_ids = [] | |
71 |
|
71 | |||
72 | # get all the results ranks and ids |
|
72 | # get all the results ranks and ids | |
73 | @scope.each do |scope| |
|
73 | @scope.each do |scope| | |
74 | klass = scope.singularize.camelcase.constantize |
|
74 | klass = scope.singularize.camelcase.constantize | |
75 | ranks_and_ids_in_scope = klass.search_result_ranks_and_ids(@tokens, User.current, projects_to_search, |
|
75 | ranks_and_ids_in_scope = klass.search_result_ranks_and_ids(@tokens, User.current, projects_to_search, | |
76 | :all_words => @all_words, |
|
76 | :all_words => @all_words, | |
77 | :titles_only => @titles_only |
|
77 | :titles_only => @titles_only | |
78 | ) |
|
78 | ) | |
79 | @result_count_by_type[scope] += ranks_and_ids_in_scope.size |
|
79 | @result_count_by_type[scope] += ranks_and_ids_in_scope.size | |
80 | @result_count += ranks_and_ids_in_scope.size |
|
80 | @result_count += ranks_and_ids_in_scope.size | |
81 | ranks_and_ids += ranks_and_ids_in_scope.map {|r| [scope, r]} |
|
81 | ranks_and_ids += ranks_and_ids_in_scope.map {|r| [scope, r]} | |
82 | end |
|
82 | end | |
83 | @result_pages = Paginator.new @result_count, limit, params['page'] |
|
83 | @result_pages = Paginator.new @result_count, limit, params['page'] | |
84 |
|
84 | |||
85 | # sort results, higher rank and id first |
|
85 | # sort results, higher rank and id first | |
86 | ranks_and_ids.sort! {|a,b| b.last <=> a.last } |
|
86 | ranks_and_ids.sort! {|a,b| b.last <=> a.last } | |
87 | ranks_and_ids = ranks_and_ids[@result_pages.offset, limit] || [] |
|
87 | ranks_and_ids = ranks_and_ids[@result_pages.offset, limit] || [] | |
88 |
|
88 | |||
89 | # load the results to display |
|
89 | # load the results to display | |
90 | results_by_scope = Hash.new {|h,k| h[k] = []} |
|
90 | results_by_scope = Hash.new {|h,k| h[k] = []} | |
91 | ranks_and_ids.group_by(&:first).each do |scope, rs| |
|
91 | ranks_and_ids.group_by(&:first).each do |scope, rs| | |
92 | klass = scope.singularize.camelcase.constantize |
|
92 | klass = scope.singularize.camelcase.constantize | |
93 | results_by_scope[scope] += klass.search_results_from_ids(rs.map(&:last).map(&:last)) |
|
93 | results_by_scope[scope] += klass.search_results_from_ids(rs.map(&:last).map(&:last)) | |
94 | end |
|
94 | end | |
95 |
|
95 | |||
96 | @results = ranks_and_ids.map do |scope, r| |
|
96 | @results = ranks_and_ids.map do |scope, r| | |
97 | results_by_scope[scope].detect {|record| record.id == r.last} |
|
97 | results_by_scope[scope].detect {|record| record.id == r.last} | |
98 | end.compact |
|
98 | end.compact | |
99 | else |
|
99 | else | |
100 | @question = "" |
|
100 | @question = "" | |
101 | end |
|
101 | end | |
102 | render :layout => false if request.xhr? |
|
102 | render :layout => false if request.xhr? | |
103 | end |
|
103 | end | |
104 |
|
104 | |||
105 | private |
|
105 | private | |
106 | def find_optional_project |
|
106 | def find_optional_project | |
107 | return true unless params[:id] |
|
107 | return true unless params[:id] | |
108 | @project = Project.find(params[:id]) |
|
108 | @project = Project.find(params[:id]) | |
109 | check_project_privacy |
|
109 | check_project_privacy | |
110 | rescue ActiveRecord::RecordNotFound |
|
110 | rescue ActiveRecord::RecordNotFound | |
111 | render_404 |
|
111 | render_404 | |
112 | end |
|
112 | end | |
113 | end |
|
113 | end |
General Comments 0
You need to be logged in to leave comments.
Login now