##// END OF EJS Templates
Search engine now only searches objects the user is allowed to view....
Jean-Philippe Lang -
r747:42fe6c6e0456
parent child
Show More
@@ -1,84 +1,97
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 layout 'base'
19 layout 'base'
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 @scope = params[:scope] || (params[:submit] ? [] : %w(projects issues changesets news documents wiki messages) )
29
28
30 # quick jump to an issue
29 # quick jump to an issue
31 if @scope.include?('issues') && @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user))
30 if @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user))
32 redirect_to :controller => "issues", :action => "show", :id => $1
31 redirect_to :controller => "issues", :action => "show", :id => $1
33 return
32 return
34 end
33 end
35
34
36 if params[:id]
35 if params[:id]
37 find_project
36 find_project
38 return unless check_project_privacy
37 return unless check_project_privacy
39 end
38 end
40
39
40 if @project
41 @object_types = %w(projects issues changesets news documents wiki_pages messages)
42 @object_types.delete('wiki_pages') unless @project.wiki
43 @object_types.delete('changesets') unless @project.repository
44 # only show what the user is allowed to view
45 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
46
47 @scope = @object_types.select {|t| params[t]}
48 # default objects to search if none is specified in parameters
49 @scope = @object_types if @scope.empty?
50 else
51 @scope = %w(projects)
52 end
53
41 # tokens must be at least 3 character long
54 # tokens must be at least 3 character long
42 @tokens = @question.split.uniq.select {|w| w.length > 2 }
55 @tokens = @question.split.uniq.select {|w| w.length > 2 }
43
56
44 if !@tokens.empty?
57 if !@tokens.empty?
45 # no more than 5 tokens to search for
58 # no more than 5 tokens to search for
46 @tokens.slice! 5..-1 if @tokens.size > 5
59 @tokens.slice! 5..-1 if @tokens.size > 5
47 # strings used in sql like statement
60 # strings used in sql like statement
48 like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}
61 like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}
49 operator = @all_words ? " AND " : " OR "
62 operator = @all_words ? " AND " : " OR "
50 limit = 10
63 limit = 10
51 @results = []
64 @results = []
52 if @project
65 if @project
53 @results += @project.issues.find(:all, :limit => limit, :include => :author, :conditions => [ (["(LOWER(subject) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'issues'
66 @results += @project.issues.find(:all, :limit => limit, :include => :author, :conditions => [ (["(LOWER(subject) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'issues'
54 Journal.with_scope :find => {:conditions => ["#{Issue.table_name}.project_id = ?", @project.id]} do
67 Journal.with_scope :find => {:conditions => ["#{Issue.table_name}.project_id = ?", @project.id]} do
55 @results += Journal.find(:all, :include => :issue, :limit => limit, :conditions => [ (["(LOWER(notes) like ? OR LOWER(notes) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ).collect(&:issue) if @scope.include? 'issues'
68 @results += Journal.find(:all, :include => :issue, :limit => limit, :conditions => [ (["(LOWER(notes) like ? OR LOWER(notes) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ).collect(&:issue) if @scope.include? 'issues'
56 end
69 end
57 @results.uniq!
70 @results.uniq!
58 @results += @project.news.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort], :include => :author ) if @scope.include? 'news'
71 @results += @project.news.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort], :include => :author ) if @scope.include? 'news'
59 @results += @project.documents.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'documents'
72 @results += @project.documents.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'documents'
60 @results += @project.wiki.pages.find(:all, :limit => limit, :include => :content, :conditions => [ (["(LOWER(title) like ? OR LOWER(text) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @project.wiki && @scope.include?('wiki')
73 @results += @project.wiki.pages.find(:all, :limit => limit, :include => :content, :conditions => [ (["(LOWER(title) like ? OR LOWER(text) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @project.wiki && @scope.include?('wiki_pages')
61 @results += @project.repository.changesets.find(:all, :limit => limit, :conditions => [ (["(LOWER(comments) like ?)"] * like_tokens.size).join(operator), * (like_tokens).sort] ) if @project.repository && @scope.include?('changesets')
74 @results += @project.repository.changesets.find(:all, :limit => limit, :conditions => [ (["(LOWER(comments) like ?)"] * like_tokens.size).join(operator), * (like_tokens).sort] ) if @project.repository && @scope.include?('changesets')
62 Message.with_scope :find => {:conditions => ["#{Board.table_name}.project_id = ?", @project.id]} do
75 Message.with_scope :find => {:conditions => ["#{Board.table_name}.project_id = ?", @project.id]} do
63 @results += Message.find(:all, :include => :board, :limit => limit, :conditions => [ (["(LOWER(subject) like ? OR LOWER(content) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'messages'
76 @results += Message.find(:all, :include => :board, :limit => limit, :conditions => [ (["(LOWER(subject) like ? OR LOWER(content) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'messages'
64 end
77 end
65 else
78 else
66 Project.with_scope(:find => {:conditions => Project.visible_by(logged_in_user)}) do
79 Project.with_scope(:find => {:conditions => Project.visible_by(logged_in_user)}) do
67 @results += Project.find(:all, :limit => limit, :conditions => [ (["(LOWER(name) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'projects'
80 @results += Project.find(:all, :limit => limit, :conditions => [ (["(LOWER(name) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'projects'
68 end
81 end
69 # if only one project is found, user is redirected to its overview
82 # if only one project is found, user is redirected to its overview
70 redirect_to :controller => 'projects', :action => 'show', :id => @results.first and return if @results.size == 1
83 redirect_to :controller => 'projects', :action => 'show', :id => @results.first and return if @results.size == 1
71 end
84 end
72 @question = @tokens.join(" ")
85 @question = @tokens.join(" ")
73 else
86 else
74 @question = ""
87 @question = ""
75 end
88 end
76 end
89 end
77
90
78 private
91 private
79 def find_project
92 def find_project
80 @project = Project.find(params[:id])
93 @project = Project.find(params[:id])
81 rescue ActiveRecord::RecordNotFound
94 rescue ActiveRecord::RecordNotFound
82 render_404
95 render_404
83 end
96 end
84 end
97 end
@@ -1,66 +1,53
1 <h2><%= l(:label_search) %></h2>
1 <h2><%= l(:label_search) %></h2>
2
2
3 <div class="box">
3 <div class="box">
4 <% form_tag({}, :method => :get) do %>
4 <% form_tag({}, :method => :get) do %>
5 <p><%= text_field_tag 'q', @question, :size => 30, :id => 'search-input' %>
5 <p><%= text_field_tag 'q', @question, :size => 30, :id => 'search-input' %>
6 <%= javascript_tag "Field.focus('search-input')" %>
6 <%= javascript_tag "Field.focus('search-input')" %>
7
7
8 <% if @project %>
8 <% @object_types.each do |t| %>
9 <%= check_box_tag 'scope[]', 'issues', (@scope.include? 'issues') %> <label><%= l(:label_issue_plural) %></label>
9 <label><%= check_box_tag t, 1, @scope.include?(t) %> <%= l("label_#{t.singularize}_plural")%></label>
10 <% if @project.repository %>
11 <%= check_box_tag 'scope[]', 'changesets', (@scope.include? 'changesets') %> <label><%= l(:label_revision_plural) %></label>
12 <% end %>
13 <%= check_box_tag 'scope[]', 'news', (@scope.include? 'news') %> <label><%= l(:label_news_plural) %></label>
14 <%= check_box_tag 'scope[]', 'documents', (@scope.include? 'documents') %> <label><%= l(:label_document_plural) %></label>
15 <% if @project.wiki %>
16 <%= check_box_tag 'scope[]', 'wiki', (@scope.include? 'wiki') %> <label><%= l(:label_wiki) %></label>
17 <% end %>
18 <% if @project.boards.any? %>
19 <%= check_box_tag 'scope[]', 'messages', (@scope.include? 'messages') %> <label><%= l(:label_message_plural) %></label>
20 <% end %>
21 <% else %>
22 <%= check_box_tag 'scope[]', 'projects', (@scope.include? 'projects') %> <label><%= l(:label_project_plural) %></label>
23 <% end %>
10 <% end %>
24 <br />
11 <br />
25 <%= check_box_tag 'all_words', 1, @all_words %> <%= l(:label_all_words) %></p>
12 <%= check_box_tag 'all_words', 1, @all_words %> <%= l(:label_all_words) %></p>
26 <%= submit_tag l(:button_submit), :name => 'submit' %>
13 <%= submit_tag l(:button_submit), :name => 'submit' %>
27 <% end %>
14 <% end %>
28 </div>
15 </div>
29
16
30 <% if @results %>
17 <% if @results %>
31 <h3><%= lwr(:label_result, @results.length) %></h3>
18 <h3><%= lwr(:label_result, @results.length) %></h3>
32 <ul>
19 <ul>
33 <% @results.each do |e| %>
20 <% @results.each do |e| %>
34 <li><p>
21 <li><p>
35 <% if e.is_a? Project %>
22 <% if e.is_a? Project %>
36 <%= link_to highlight_tokens(h(e.name), @tokens), :controller => 'projects', :action => 'show', :id => e %><br />
23 <%= link_to highlight_tokens(h(e.name), @tokens), :controller => 'projects', :action => 'show', :id => e %><br />
37 <%= highlight_tokens(e.description, @tokens) %>
24 <%= highlight_tokens(e.description, @tokens) %>
38 <% elsif e.is_a? Issue %>
25 <% elsif e.is_a? Issue %>
39 <%= link_to_issue e %>: <%= highlight_tokens(h(e.subject), @tokens) %><br />
26 <%= link_to_issue e %>: <%= highlight_tokens(h(e.subject), @tokens) %><br />
40 <%= highlight_tokens(e.description, @tokens) %><br />
27 <%= highlight_tokens(e.description, @tokens) %><br />
41 <i><%= e.author.name %>, <%= format_time(e.created_on) %></i>
28 <i><%= e.author.name %>, <%= format_time(e.created_on) %></i>
42 <% elsif e.is_a? News %>
29 <% elsif e.is_a? News %>
43 <%=l(:label_news)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'news', :action => 'show', :id => e %><br />
30 <%=l(:label_news)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'news', :action => 'show', :id => e %><br />
44 <%= highlight_tokens(e.description, @tokens) %><br />
31 <%= highlight_tokens(e.description, @tokens) %><br />
45 <i><%= e.author.name %>, <%= format_time(e.created_on) %></i>
32 <i><%= e.author.name %>, <%= format_time(e.created_on) %></i>
46 <% elsif e.is_a? Document %>
33 <% elsif e.is_a? Document %>
47 <%=l(:label_document)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'documents', :action => 'show', :id => e %><br />
34 <%=l(:label_document)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'documents', :action => 'show', :id => e %><br />
48 <%= highlight_tokens(e.description, @tokens) %><br />
35 <%= highlight_tokens(e.description, @tokens) %><br />
49 <i><%= format_time(e.created_on) %></i>
36 <i><%= format_time(e.created_on) %></i>
50 <% elsif e.is_a? WikiPage %>
37 <% elsif e.is_a? WikiPage %>
51 <%=l(:label_wiki)%>: <%= link_to highlight_tokens(h(e.pretty_title), @tokens), :controller => 'wiki', :action => 'index', :id => @project, :page => e.title %><br />
38 <%=l(:label_wiki)%>: <%= link_to highlight_tokens(h(e.pretty_title), @tokens), :controller => 'wiki', :action => 'index', :id => @project, :page => e.title %><br />
52 <%= highlight_tokens(e.content.text, @tokens) %><br />
39 <%= highlight_tokens(e.content.text, @tokens) %><br />
53 <i><%= e.content.author ? e.content.author.name : "Anonymous" %>, <%= format_time(e.content.updated_on) %></i>
40 <i><%= e.content.author ? e.content.author.name : "Anonymous" %>, <%= format_time(e.content.updated_on) %></i>
54 <% elsif e.is_a? Changeset %>
41 <% elsif e.is_a? Changeset %>
55 <%=l(:label_revision)%> <%= link_to h(e.revision), :controller => 'repositories', :action => 'revision', :id => @project, :rev => e.revision %><br />
42 <%=l(:label_revision)%> <%= link_to h(e.revision), :controller => 'repositories', :action => 'revision', :id => @project, :rev => e.revision %><br />
56 <%= highlight_tokens(e.comments, @tokens) %><br />
43 <%= highlight_tokens(e.comments, @tokens) %><br />
57 <em><%= e.committer.blank? ? e.committer : "Anonymous" %>, <%= format_time(e.committed_on) %></em>
44 <em><%= e.committer.blank? ? e.committer : "Anonymous" %>, <%= format_time(e.committed_on) %></em>
58 <% elsif e.is_a? Message %>
45 <% elsif e.is_a? Message %>
59 <%=h e.board.name %>: <%= link_to_message e %><br />
46 <%=h e.board.name %>: <%= link_to_message e %><br />
60 <%= highlight_tokens(e.content, @tokens) %><br />
47 <%= highlight_tokens(e.content, @tokens) %><br />
61 <em><%= e.author ? e.author.name : "Anonymous" %>, <%= format_time(e.created_on) %></em>
48 <em><%= e.author ? e.author.name : "Anonymous" %>, <%= format_time(e.created_on) %></em>
62 <% end %>
49 <% end %>
63 </p></li>
50 </p></li>
64 <% end %>
51 <% end %>
65 </ul>
52 </ul>
66 <% end %>
53 <% end %>
General Comments 0
You need to be logged in to leave comments. Login now