diff options
| author | hukl <contact@smyck.org> | 2011-02-10 14:19:00 +0100 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2011-02-10 14:19:00 +0100 |
| commit | 7379daad1c73bd3610ed296436250b417ac3673d (patch) | |
| tree | 04f722efc678de9d3aa5bf8f1c96e3be33b18bc4 /vendor/plugins/thinking-sphinx/lib/thinking_sphinx/configuration.rb | |
| parent | 91633ac4419d839661e35ae8f2efe5c9089cfb67 (diff) | |
removed thinking_sphinx plugin and replaced it with gem.
also tuned dependencies
Diffstat (limited to 'vendor/plugins/thinking-sphinx/lib/thinking_sphinx/configuration.rb')
| -rw-r--r-- | vendor/plugins/thinking-sphinx/lib/thinking_sphinx/configuration.rb | 237 |
1 files changed, 0 insertions, 237 deletions
diff --git a/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/configuration.rb b/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/configuration.rb deleted file mode 100644 index 31543f5..0000000 --- a/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/configuration.rb +++ /dev/null | |||
| @@ -1,237 +0,0 @@ | |||
| 1 | require 'erb' | ||
| 2 | require 'singleton' | ||
| 3 | |||
| 4 | module ThinkingSphinx | ||
| 5 | # This class both keeps track of the configuration settings for Sphinx and | ||
| 6 | # also generates the resulting file for Sphinx to use. | ||
| 7 | # | ||
| 8 | # Here are the default settings, relative to RAILS_ROOT where relevant: | ||
| 9 | # | ||
| 10 | # config file:: config/#{environment}.sphinx.conf | ||
| 11 | # searchd log file:: log/searchd.log | ||
| 12 | # query log file:: log/searchd.query.log | ||
| 13 | # pid file:: log/searchd.#{environment}.pid | ||
| 14 | # searchd files:: db/sphinx/#{environment}/ | ||
| 15 | # address:: 127.0.0.1 | ||
| 16 | # port:: 3312 | ||
| 17 | # allow star:: false | ||
| 18 | # min prefix length:: 1 | ||
| 19 | # min infix length:: 1 | ||
| 20 | # mem limit:: 64M | ||
| 21 | # max matches:: 1000 | ||
| 22 | # morphology:: stem_en | ||
| 23 | # charset type:: utf-8 | ||
| 24 | # charset table:: nil | ||
| 25 | # ignore chars:: nil | ||
| 26 | # html strip:: false | ||
| 27 | # html remove elements:: '' | ||
| 28 | # | ||
| 29 | # If you want to change these settings, create a YAML file at | ||
| 30 | # config/sphinx.yml with settings for each environment, in a similar | ||
| 31 | # fashion to database.yml - using the following keys: config_file, | ||
| 32 | # searchd_log_file, query_log_file, pid_file, searchd_file_path, port, | ||
| 33 | # allow_star, enable_star, min_prefix_len, min_infix_len, mem_limit, | ||
| 34 | # max_matches, # morphology, charset_type, charset_table, ignore_chars, | ||
| 35 | # html_strip, # html_remove_elements. I think you've got the idea. | ||
| 36 | # | ||
| 37 | # Each setting in the YAML file is optional - so only put in the ones you | ||
| 38 | # want to change. | ||
| 39 | # | ||
| 40 | # Keep in mind, if for some particular reason you're using a version of | ||
| 41 | # Sphinx older than 0.9.8 r871 (that's prior to the proper 0.9.8 release), | ||
| 42 | # don't set allow_star to true. | ||
| 43 | # | ||
| 44 | class Configuration | ||
| 45 | include Singleton | ||
| 46 | |||
| 47 | SourceOptions = %w( mysql_connect_flags sql_range_step sql_query_pre | ||
| 48 | sql_query_post sql_ranged_throttle sql_query_post_index ) | ||
| 49 | |||
| 50 | IndexOptions = %w( charset_table charset_type docinfo enable_star | ||
| 51 | exceptions html_index_attrs html_remove_elements html_strip ignore_chars | ||
| 52 | min_infix_len min_prefix_len min_word_len mlock morphology ngram_chars | ||
| 53 | ngram_len phrase_boundary phrase_boundary_step preopen stopwords | ||
| 54 | wordforms ) | ||
| 55 | |||
| 56 | attr_accessor :config_file, :searchd_log_file, :query_log_file, | ||
| 57 | :pid_file, :searchd_file_path, :address, :port, :allow_star, | ||
| 58 | :database_yml_file, :app_root, :bin_path, :model_directories | ||
| 59 | |||
| 60 | attr_accessor :source_options, :index_options | ||
| 61 | |||
| 62 | attr_reader :environment, :configuration | ||
| 63 | |||
| 64 | # Load in the configuration settings - this will look for config/sphinx.yml | ||
| 65 | # and parse it according to the current environment. | ||
| 66 | # | ||
| 67 | def initialize(app_root = Dir.pwd) | ||
| 68 | self.reset | ||
| 69 | end | ||
| 70 | |||
| 71 | def reset | ||
| 72 | self.app_root = RAILS_ROOT if defined?(RAILS_ROOT) | ||
| 73 | self.app_root = Merb.root if defined?(Merb) | ||
| 74 | self.app_root ||= app_root | ||
| 75 | |||
| 76 | @configuration = Riddle::Configuration.new | ||
| 77 | @configuration.searchd.address = "127.0.0.1" | ||
| 78 | @configuration.searchd.port = 3312 | ||
| 79 | @configuration.searchd.pid_file = "#{self.app_root}/log/searchd.#{environment}.pid" | ||
| 80 | @configuration.searchd.log = "#{self.app_root}/log/searchd.log" | ||
| 81 | @configuration.searchd.query_log = "#{self.app_root}/log/searchd.query.log" | ||
| 82 | |||
| 83 | self.database_yml_file = "#{self.app_root}/config/database.yml" | ||
| 84 | self.config_file = "#{self.app_root}/config/#{environment}.sphinx.conf" | ||
| 85 | self.searchd_file_path = "#{self.app_root}/db/sphinx/#{environment}" | ||
| 86 | self.allow_star = false | ||
| 87 | self.bin_path = "" | ||
| 88 | self.model_directories = ["#{app_root}/app/models/"] + | ||
| 89 | Dir.glob("#{app_root}/vendor/plugins/*/app/models/") | ||
| 90 | |||
| 91 | self.source_options = {} | ||
| 92 | self.index_options = { | ||
| 93 | :charset_type => "utf-8", | ||
| 94 | :morphology => "stem_en" | ||
| 95 | } | ||
| 96 | |||
| 97 | parse_config | ||
| 98 | |||
| 99 | self | ||
| 100 | end | ||
| 101 | |||
| 102 | def self.environment | ||
| 103 | @@environment ||= ( | ||
| 104 | defined?(Merb) ? Merb.environment : ENV['RAILS_ENV'] | ||
| 105 | ) || "development" | ||
| 106 | end | ||
| 107 | |||
| 108 | def environment | ||
| 109 | self.class.environment | ||
| 110 | end | ||
| 111 | |||
| 112 | def controller | ||
| 113 | @controller ||= Riddle::Controller.new(@configuration, self.config_file) | ||
| 114 | end | ||
| 115 | |||
| 116 | # Generate the config file for Sphinx by using all the settings defined and | ||
| 117 | # looping through all the models with indexes to build the relevant | ||
| 118 | # indexer and searchd configuration, and sources and indexes details. | ||
| 119 | # | ||
| 120 | def build(file_path=nil) | ||
| 121 | load_models | ||
| 122 | file_path ||= "#{self.config_file}" | ||
| 123 | |||
| 124 | @configuration.indexes.clear | ||
| 125 | |||
| 126 | ThinkingSphinx.indexed_models.each_with_index do |model, model_index| | ||
| 127 | @configuration.indexes.concat model.constantize.to_riddle(model_index) | ||
| 128 | end | ||
| 129 | |||
| 130 | open(file_path, "w") do |file| | ||
| 131 | file.write @configuration.render | ||
| 132 | end | ||
| 133 | end | ||
| 134 | |||
| 135 | # Make sure all models are loaded - without reloading any that | ||
| 136 | # ActiveRecord::Base is already aware of (otherwise we start to hit some | ||
| 137 | # messy dependencies issues). | ||
| 138 | # | ||
| 139 | def load_models | ||
| 140 | self.model_directories.each do |base| | ||
| 141 | Dir["#{base}**/*.rb"].each do |file| | ||
| 142 | model_name = file.gsub(/^#{base}([\w_\/\\]+)\.rb/, '\1') | ||
| 143 | |||
| 144 | next if model_name.nil? | ||
| 145 | next if ::ActiveRecord::Base.send(:subclasses).detect { |model| | ||
| 146 | model.name == model_name | ||
| 147 | } | ||
| 148 | |||
| 149 | begin | ||
| 150 | model_name.camelize.constantize | ||
| 151 | rescue LoadError | ||
| 152 | model_name.gsub!(/.*[\/\\]/, '').nil? ? next : retry | ||
| 153 | rescue NameError | ||
| 154 | next | ||
| 155 | end | ||
| 156 | end | ||
| 157 | end | ||
| 158 | end | ||
| 159 | |||
| 160 | def address | ||
| 161 | @configuration.searchd.address | ||
| 162 | end | ||
| 163 | |||
| 164 | def address=(address) | ||
| 165 | @configuration.searchd.address = address | ||
| 166 | end | ||
| 167 | |||
| 168 | def port | ||
| 169 | @configuration.searchd.port | ||
| 170 | end | ||
| 171 | |||
| 172 | def port=(port) | ||
| 173 | @configuration.searchd.port = port | ||
| 174 | end | ||
| 175 | |||
| 176 | def pid_file | ||
| 177 | @configuration.searchd.pid_file | ||
| 178 | end | ||
| 179 | |||
| 180 | def pid_file=(pid_file) | ||
| 181 | @configuration.searchd.pid_file = pid_file | ||
| 182 | end | ||
| 183 | |||
| 184 | def searchd_log_file | ||
| 185 | @configuration.searchd.log | ||
| 186 | end | ||
| 187 | |||
| 188 | def searchd_log_file=(file) | ||
| 189 | @configuration.searchd.log = file | ||
| 190 | end | ||
| 191 | |||
| 192 | def query_log_file | ||
| 193 | @configuration.searchd.query_log | ||
| 194 | end | ||
| 195 | |||
| 196 | def query_log_file=(file) | ||
| 197 | @configuration.searchd.query_log = file | ||
| 198 | end | ||
| 199 | |||
| 200 | private | ||
| 201 | |||
| 202 | # Parse the config/sphinx.yml file - if it exists - then use the attribute | ||
| 203 | # accessors to set the appropriate values. Nothing too clever. | ||
| 204 | # | ||
| 205 | def parse_config | ||
| 206 | path = "#{app_root}/config/sphinx.yml" | ||
| 207 | return unless File.exists?(path) | ||
| 208 | |||
| 209 | conf = YAML::load(ERB.new(IO.read(path)).result)[environment] | ||
| 210 | |||
| 211 | conf.each do |key,value| | ||
| 212 | self.send("#{key}=", value) if self.methods.include?("#{key}=") | ||
| 213 | |||
| 214 | set_sphinx_setting self.source_options, key, value, SourceOptions | ||
| 215 | set_sphinx_setting self.index_options, key, value, IndexOptions | ||
| 216 | set_sphinx_setting @configuration.searchd, key, value | ||
| 217 | set_sphinx_setting @configuration.indexer, key, value | ||
| 218 | end unless conf.nil? | ||
| 219 | |||
| 220 | self.bin_path += '/' unless self.bin_path.blank? | ||
| 221 | |||
| 222 | if self.allow_star | ||
| 223 | self.index_options[:enable_star] = true | ||
| 224 | self.index_options[:min_prefix_len] = 1 | ||
| 225 | end | ||
| 226 | end | ||
| 227 | |||
| 228 | def set_sphinx_setting(object, key, value, allowed = {}) | ||
| 229 | if object.is_a?(Hash) | ||
| 230 | object[key.to_sym] = value if allowed.include?(key.to_s) | ||
| 231 | else | ||
| 232 | object.send("#{key}=", value) if object.methods.include?("#{key}") | ||
| 233 | send("#{key}=", value) if self.methods.include?("#{key}") | ||
| 234 | end | ||
| 235 | end | ||
| 236 | end | ||
| 237 | end | ||
