Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. module Search
  20. mattr_accessor :available_search_types
  21. @@available_search_types = []
  22. class << self
  23. def map(&block)
  24. yield self
  25. end
  26. # Registers a search provider
  27. def register(search_type, options={})
  28. search_type = search_type.to_s
  29. @@available_search_types << search_type unless @@available_search_types.include?(search_type)
  30. end
  31. # Returns the cache store for search results
  32. # Can be configured with config.redmine_search_cache_store= in config/application.rb
  33. def cache_store
  34. @@cache_store ||= begin
  35. # if config.search_cache_store was not previously set, a no method error would be raised
  36. config = Rails.application.config.redmine_search_cache_store rescue :memory_store
  37. if config
  38. ActiveSupport::Cache.lookup_store config
  39. end
  40. end
  41. end
  42. end
  43. class Fetcher
  44. attr_reader :tokens
  45. def initialize(question, user, scope, projects, options={})
  46. @user = user
  47. @question = question.strip
  48. @scope = scope
  49. @projects = projects
  50. @cache = options.delete(:cache)
  51. @options = options
  52. @tokens = Tokenizer.new(@question).tokens
  53. end
  54. # Returns the total result count
  55. def result_count
  56. result_ids.size
  57. end
  58. # Returns the result count by type
  59. def result_count_by_type
  60. ret = Hash.new {|h, k| h[k] = 0}
  61. result_ids.group_by(&:first).each do |scope, ids|
  62. ret[scope] += ids.size
  63. end
  64. ret
  65. end
  66. # Returns the results for the given offset and limit
  67. def results(offset, limit)
  68. result_ids_to_load = result_ids[offset, limit] || []
  69. results_by_scope = Hash.new {|h, k| h[k] = []}
  70. result_ids_to_load.group_by(&:first).each do |scope, scope_and_ids|
  71. klass = scope.singularize.camelcase.constantize
  72. results_by_scope[scope] += klass.search_results_from_ids(scope_and_ids.map(&:last))
  73. end
  74. result_ids_to_load.filter_map do |scope, id|
  75. results_by_scope[scope].detect {|record| record.id == id}
  76. end
  77. end
  78. # Returns the results ids, sorted by rank
  79. def result_ids
  80. @ranks_and_ids ||= load_result_ids_from_cache
  81. end
  82. private
  83. def project_ids
  84. Array.wrap(@projects).map(&:id)
  85. end
  86. def load_result_ids_from_cache
  87. if Redmine::Search.cache_store
  88. cache_key = ActiveSupport::Cache.expand_cache_key(
  89. [@question, @user.id, @scope.sort, @options, project_ids.sort]
  90. )
  91. Redmine::Search.cache_store.fetch(cache_key, :force => !@cache) do
  92. load_result_ids
  93. end
  94. else
  95. load_result_ids
  96. end
  97. end
  98. def load_result_ids
  99. ret = []
  100. # get all the results ranks and ids
  101. @scope.each do |scope|
  102. klass = scope.singularize.camelcase.constantize
  103. ranks_and_ids_in_scope = klass.search_result_ranks_and_ids(@tokens, User.current, @projects, @options)
  104. ret += ranks_and_ids_in_scope.map {|rs| [scope, rs]}
  105. end
  106. # sort results, higher rank and id first
  107. ret.sort! {|a, b| b.last <=> a.last}
  108. # only keep ids now that results are sorted
  109. ret.map! {|scope, r| [scope, r.last]}
  110. ret
  111. end
  112. end
  113. class Tokenizer
  114. def initialize(question)
  115. @question = question.to_s
  116. end
  117. def tokens
  118. # extract tokens from the question
  119. # eg. hello "bye bye" => ["hello", "bye bye"]
  120. tokens = @question.scan(%r{(([[:space:]]|^)"[^"]+"([[:space:]]|$)|[[:^space:]]+)}).collect {|m| m.first.gsub(%r{(^[[:space:]]*"[[:space:]]*|[[:space:]]*"[[:space:]]*$)}, '')}
  121. # tokens must be at least 2 characters long
  122. # but for Chinese characters (Chinese HANZI/Japanese KANJI), tokens can be one character
  123. # no more than 5 tokens to search for
  124. tokens.uniq.select{|w| w.length > 1 || w =~ /\p{Han}/}.first 5
  125. end
  126. end
  127. module Controller
  128. def self.included(base)
  129. base.extend(ClassMethods)
  130. end
  131. module ClassMethods
  132. @@default_search_scopes = Hash.new {|hash, key| hash[key] = {:default => nil, :actions => {}}}
  133. mattr_accessor :default_search_scopes
  134. # Set the default search scope for a controller or specific actions
  135. # Examples:
  136. # * search_scope :issues # => sets the search scope to :issues for the whole controller
  137. # * search_scope :issues, :only => :index
  138. # * search_scope :issues, :only => [:index, :show]
  139. def default_search_scope(id, options = {})
  140. if actions = options[:only]
  141. actions = [] << actions unless actions.is_a?(Array)
  142. actions.each {|a| default_search_scopes[controller_name.to_sym][:actions][a.to_sym] = id.to_s}
  143. else
  144. default_search_scopes[controller_name.to_sym][:default] = id.to_s
  145. end
  146. end
  147. end
  148. def default_search_scopes
  149. self.class.default_search_scopes
  150. end
  151. # Returns the default search scope according to the current action
  152. def default_search_scope
  153. @default_search_scope ||= default_search_scopes[controller_name.to_sym][:actions][action_name.to_sym] ||
  154. default_search_scopes[controller_name.to_sym][:default]
  155. end
  156. end
  157. end
  158. end