You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_controller.rb 3.9 kB

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

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...