Search engines now supports pagination.

Results are sorted in reverse chronological order.

git-svn-id: http://redmine.rubyforge.org/svn/trunk@766 e93f8b46-1217-0410-a6f0-8f06a7374b81
pull/351/head
Jean-Philippe Lang 17 years ago
parent d723bea502
commit a96421019f
  1. 37
      app/controllers/search_controller.rb
  2. 2
      app/helpers/search_helper.rb
  3. 5
      app/models/changeset.rb
  4. 4
      app/models/document.rb
  5. 3
      app/models/issue.rb
  6. 5
      app/models/journal.rb
  7. 5
      app/models/message.rb
  8. 1
      app/models/news.rb
  9. 6
      app/models/project.rb
  10. 15
      app/models/wiki_page.rb
  11. 50
      app/views/search/index.rhtml
  12. 2
      config/environment.rb
  13. 3
      lang/en.yml
  14. 3
      lang/fr.yml
  15. 0
      lib/plugins/acts_as_event/init.rb
  16. 0
      lib/plugins/acts_as_event/lib/acts_as_event.rb
  17. 2
      lib/plugins/acts_as_searchable/init.rb
  18. 89
      lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb
  19. 0
      lib/plugins/acts_as_watchable/init.rb
  20. 0
      lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
  21. 2
      lib/redmine.rb
  22. 2
      test/functional/search_controller_test.rb

@ -26,6 +26,9 @@ class SearchController < ApplicationController
@question.strip!
@all_words = params[:all_words] || (params[:submit] ? false : true)
offset = nil
begin; offset = params[:offset].to_time if params[:offset]; rescue; end
# quick jump to an issue
if @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user))
redirect_to :controller => "issues", :action => "show", :id => $1
@ -38,14 +41,11 @@ class SearchController < ApplicationController
end
if @project
@object_types = %w(projects issues changesets news documents wiki_pages messages)
@object_types.delete('wiki_pages') unless @project.wiki
@object_types.delete('changesets') unless @project.repository
# only show what the user is allowed to view
@object_types = %w(issues news documents changesets wiki_pages messages)
@object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
@scope = @object_types.select {|t| params[t]}
# default objects to search if none is specified in parameters
@scope = @object_types if @scope.empty?
else
@object_types = @scope = %w(projects)
@ -60,20 +60,26 @@ class SearchController < ApplicationController
# strings used in sql like statement
like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}
operator = @all_words ? " AND " : " OR "
limit = 10
@results = []
limit = 10
if @project
@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'
Journal.with_scope :find => {:conditions => ["#{Issue.table_name}.project_id = ?", @project.id]} do
@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'
@scope.each do |s|
@results += s.singularize.camelcase.constantize.search(like_tokens, @all_words, @project,
:limit => (limit+1), :offset => offset, :before => params[:previous].nil?)
end
@results.uniq!
@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'
@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'
@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')
@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')
Message.with_scope :find => {:conditions => ["#{Board.table_name}.project_id = ?", @project.id]} do
@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'
@results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime}
if params[:previous].nil?
@pagination_previous_date = @results[0].event_datetime if offset && @results[0]
if @results.size > limit
@pagination_next_date = @results[limit-1].event_datetime
@results = @results[0, limit]
end
else
@pagination_next_date = @results[-1].event_datetime if offset && @results[-1]
if @results.size > limit
@pagination_previous_date = @results[-(limit)].event_datetime
@results = @results[-(limit), limit]
end
end
else
Project.with_scope(:find => {:conditions => Project.visible_by(logged_in_user)}) do
@ -86,6 +92,7 @@ class SearchController < ApplicationController
else
@question = ""
end
render :layout => false if request.xhr?
end
private

@ -17,7 +17,7 @@
module SearchHelper
def highlight_tokens(text, tokens)
return text unless tokens && !tokens.empty?
return text unless text && tokens && !tokens.empty?
regexp = Regexp.new "(#{tokens.join('|')})", Regexp::IGNORECASE
result = ''
text.split(regexp).each_with_index do |words, i|

@ -25,6 +25,11 @@ class Changeset < ActiveRecord::Base
:datetime => :committed_on,
:author => :committer,
:url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project_id, :rev => o.revision}}
acts_as_searchable :columns => 'comments',
:include => :repository,
:project_key => "#{Repository.table_name}.project_id",
:date_column => 'committed_on'
validates_presence_of :repository_id, :revision, :committed_on, :commit_date
validates_numericality_of :revision, :only_integer => true

@ -20,7 +20,9 @@ class Document < ActiveRecord::Base
belongs_to :category, :class_name => "Enumeration", :foreign_key => "category_id"
has_many :attachments, :as => :container, :dependent => :destroy
acts_as_event :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}}
acts_as_searchable :columns => ['title', 'description']
acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"},
:url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}}
validates_presence_of :project, :title, :category
validates_length_of :title, :maximum => 60

@ -36,8 +36,9 @@ class Issue < ActiveRecord::Base
has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
acts_as_watchable
acts_as_searchable :columns => ['subject', 'description'], :with => {:journal => :issue}
acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
:url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}
:url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}
validates_presence_of :subject, :description, :priority, :tracker, :author, :status
validates_length_of :subject, :maximum => 255

@ -23,4 +23,9 @@ class Journal < ActiveRecord::Base
belongs_to :user
has_many :details, :class_name => "JournalDetail", :dependent => :delete_all
acts_as_searchable :columns => 'notes',
:include => :issue,
:project_key => "#{Issue.table_name}.project_id",
:date_column => "#{Issue.table_name}.created_on"
end

@ -22,6 +22,11 @@ class Message < ActiveRecord::Base
has_many :attachments, :as => :container, :dependent => :destroy
belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
acts_as_searchable :columns => ['subject', 'content'], :include => :board, :project_key => "project_id"
acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
:description => :content,
:url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id, :id => o.id}}
validates_presence_of :subject, :content
validates_length_of :subject, :maximum => 255

@ -24,6 +24,7 @@ class News < ActiveRecord::Base
validates_length_of :title, :maximum => 60
validates_length_of :summary, :maximum => 255
acts_as_searchable :columns => ['title', 'description']
acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
# returns latest news for projects visible by user

@ -38,7 +38,11 @@ class Project < ActiveRecord::Base
has_one :wiki, :dependent => :destroy
has_and_belongs_to_many :custom_fields, :class_name => 'IssueCustomField', :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", :association_foreign_key => 'custom_field_id'
acts_as_tree :order => "name", :counter_cache => true
acts_as_searchable :columns => ['name', 'description'], :project_key => 'id'
acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
:url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}
attr_protected :status, :enabled_module_names
validates_presence_of :name, :description, :identifier

@ -21,7 +21,16 @@ class WikiPage < ActiveRecord::Base
belongs_to :wiki
has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
has_many :attachments, :as => :container, :dependent => :destroy
acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
:description => :text,
:datetime => :created_on,
:url => Proc.new {|o| {:controller => 'wiki', :id => o.wiki.project_id, :page => o.title}}
acts_as_searchable :columns => ['title', 'text'],
:include => [:wiki, :content],
:project_key => "#{Wiki.table_name}.project_id"
attr_accessor :redirect_existing_links
validates_presence_of :title
@ -85,6 +94,10 @@ class WikiPage < ActiveRecord::Base
def project
wiki.project
end
def text
content.text if content
end
end
class WikiDiff

@ -15,39 +15,27 @@
</div>
<% if @results %>
<h3><%= lwr(:label_result, @results.length) %></h3>
<h3><%= l(:label_result_plural) %></h3>
<ul>
<% @results.each do |e| %>
<li><p>
<% if e.is_a? Project %>
<%= link_to highlight_tokens(h(e.name), @tokens), :controller => 'projects', :action => 'show', :id => e %><br />
<%= highlight_tokens(e.description, @tokens) %>
<% elsif e.is_a? Issue %>
<%= link_to_issue e %>: <%= highlight_tokens(h(e.subject), @tokens) %><br />
<%= highlight_tokens(e.description, @tokens) %><br />
<i><%= e.author.name %>, <%= format_time(e.created_on) %></i>
<% elsif e.is_a? News %>
<%=l(:label_news)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'news', :action => 'show', :id => e %><br />
<%= highlight_tokens(e.description, @tokens) %><br />
<i><%= e.author.name %>, <%= format_time(e.created_on) %></i>
<% elsif e.is_a? Document %>
<%=l(:label_document)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'documents', :action => 'show', :id => e %><br />
<%= highlight_tokens(e.description, @tokens) %><br />
<i><%= format_time(e.created_on) %></i>
<% elsif e.is_a? WikiPage %>
<%=l(:label_wiki)%>: <%= link_to highlight_tokens(h(e.pretty_title), @tokens), :controller => 'wiki', :action => 'index', :id => @project, :page => e.title %><br />
<%= highlight_tokens(e.content.text, @tokens) %><br />
<i><%= e.content.author ? e.content.author.name : "Anonymous" %>, <%= format_time(e.content.updated_on) %></i>
<% elsif e.is_a? Changeset %>
<%=l(:label_revision)%> <%= link_to h(e.revision), :controller => 'repositories', :action => 'revision', :id => @project, :rev => e.revision %><br />
<%= highlight_tokens(e.comments, @tokens) %><br />
<em><%= e.committer.blank? ? e.committer : "Anonymous" %>, <%= format_time(e.committed_on) %></em>
<% elsif e.is_a? Message %>
<%=h e.board.name %>: <%= link_to_message e %><br />
<%= highlight_tokens(e.content, @tokens) %><br />
<em><%= e.author ? e.author.name : "Anonymous" %>, <%= format_time(e.created_on) %></em>
<% end %>
</p></li>
<li><p><%= link_to highlight_tokens(truncate(e.event_title, 255), @tokens), e.event_url %><br />
<%= highlight_tokens(e.event_description, @tokens) %><br />
<span class="author"><%= format_time(e.event_datetime) %></span></p></li>
<% end %>
</ul>
<% end %>
<p><center>
<% if @pagination_previous_date %>
<%= link_to_remote ('&#171; ' + l(:label_previous)),
{:update => :content,
:url => params.merge(:previous => 1, :offset => @pagination_previous_date.strftime("%Y%m%d%H%M%S"))
}, :href => url_for(params.merge(:previous => 1, :offset => @pagination_previous_date.strftime("%Y%m%d%H%M%S"))) %>&nbsp;
<% end %>
<% if @pagination_next_date %>
<%= link_to_remote (l(:label_next) + ' &#187;'),
{:update => :content,
:url => params.merge(:previous => nil, :offset => @pagination_next_date.strftime("%Y%m%d%H%M%S"))
}, :href => url_for(params.merge(:previous => nil, :offset => @pagination_next_date.strftime("%Y%m%d%H%M%S"))) %>
<% end %>
</center></p>

@ -15,6 +15,8 @@ Rails::Initializer.run do |config|
# Add additional load paths for sweepers
config.load_paths += %W( #{RAILS_ROOT}/app/sweepers )
config.plugin_paths = ['lib/plugins', 'vendor/plugins']
# Force all environments to use the same logger level
# (by default production uses :info, the others :debug)

@ -350,8 +350,7 @@ label_roadmap_due_in: Due in
label_roadmap_overdue: %s late
label_roadmap_no_issues: No issues for this version
label_search: Search
label_result: %d result
label_result_plural: %d results
label_result_plural: Results
label_all_words: All words
label_wiki: Wiki
label_wiki_edit: Wiki edit

@ -350,8 +350,7 @@ label_roadmap_due_in: Echéance dans
label_roadmap_overdue: En retard de %s
label_roadmap_no_issues: Aucune demande pour cette version
label_search: Recherche
label_result: %d résultat
label_result_plural: %d résultats
label_result_plural: Résultats
label_all_words: Tous les mots
label_wiki: Wiki
label_wiki_edit: Révision wiki

@ -0,0 +1,2 @@
require File.dirname(__FILE__) + '/lib/acts_as_searchable'
ActiveRecord::Base.send(:include, Redmine::Acts::Searchable)

@ -0,0 +1,89 @@
# redMine - project management software
# Copyright (C) 2006-2007 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module Redmine
module Acts
module Searchable
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def acts_as_searchable(options = {})
return if self.included_modules.include?(Redmine::Acts::Searchable::InstanceMethods)
cattr_accessor :searchable_options
self.searchable_options = options
if searchable_options[:columns].nil?
raise 'No searchable column defined.'
elsif !searchable_options[:columns].is_a?(Array)
searchable_options[:columns] = [] << searchable_options[:columns]
end
if searchable_options[:project_key]
elsif column_names.include?('project_id')
searchable_options[:project_key] = "#{table_name}.project_id"
else
raise 'No project key defined.'
end
if searchable_options[:date_column]
elsif column_names.include?('created_on')
searchable_options[:date_column] = "#{table_name}.created_on"
else
raise 'No date column defined defined.'
end
send :include, Redmine::Acts::Searchable::InstanceMethods
end
end
module InstanceMethods
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def search(tokens, all_tokens, project, options={})
tokens = [] << tokens unless tokens.is_a?(Array)
find_options = {:include => searchable_options[:include]}
find_options[:limit] = options[:limit] if options[:limit]
find_options[:order] = "#{searchable_options[:date_column]} " + (options[:before] ? 'DESC' : 'ASC')
sql = ([ '(' + searchable_options[:columns].collect {|column| "(LOWER(#{column}) LIKE ?)"}.join(' OR ') + ')' ] * tokens.size).join(all_tokens ? ' AND ' : ' OR ')
if options[:offset]
sql = "(#{sql}) AND (#{searchable_options[:date_column]} " + (options[:before] ? '<' : '>') + "'#{connection.quoted_date(options[:offset])}')"
end
find_options[:conditions] = [sql, * (tokens * searchable_options[:columns].size).sort]
results = with_scope(:find => {:conditions => ["#{searchable_options[:project_key]} = ?", project.id]}) do
find(:all, find_options)
end
if searchable_options[:with]
searchable_options[:with].each do |model, assoc|
results += model.to_s.camelcase.constantize.search(tokens, all_tokens, project, options).collect {|r| r.send assoc}
end
results.uniq!
end
results
end
end
end
end
end
end

@ -1,8 +1,6 @@
require 'redmine/access_control'
require 'redmine/menu_manager'
require 'redmine/mime_type'
require 'redmine/acts_as_watchable/init'
require 'redmine/acts_as_event/init'
require 'redmine/plugin'
begin

@ -31,7 +31,7 @@ class SearchControllerTest < Test::Unit::TestCase
assert_template 'index'
assert_not_nil assigns(:project)
get :index, :id => 1, :q => "can", :scope => ["issues", "news", "documents"]
get :index, :id => 1, :q => "can"
assert_response :success
assert_template 'index'
end

Loading…
Cancel
Save