Mercurial > hg > MPIWG-drupal-modules
comparison sites/all/modules/custom/solrconnect/Solr_Base_Query.php @ 0:015d06b10d37 default tip
initial
| author | dwinter |
|---|---|
| date | Wed, 31 Jul 2013 13:49:13 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:015d06b10d37 |
|---|---|
| 1 <?php | |
| 2 /** | |
| 3 * This class allows you to make operations on a query that will be sent to | |
| 4 * Apache Solr. methods such as adding and removing sorts, remove and replace | |
| 5 * parameters, adding and removing filters, getters and setters for various | |
| 6 * parameters and more | |
| 7 * @file | |
| 8 * Class that defines the base query for the Apache Solr Drupal module. | |
| 9 */ | |
| 10 | |
| 11 class SolrFilterSubQuery { | |
| 12 | |
| 13 /** | |
| 14 * Static shared by all instances, used to increment ID numbers. | |
| 15 */ | |
| 16 protected static $idCount = 0; | |
| 17 | |
| 18 /** | |
| 19 * Each query/subquery will have a unique ID. | |
| 20 */ | |
| 21 public $id; | |
| 22 public $operator; | |
| 23 | |
| 24 /** | |
| 25 * A keyed array where the key is a position integer and the value | |
| 26 * is an array with #name and #value properties. Each value is a | |
| 27 * used for filter queries, e.g. array('#name' => 'is_uid', '#value' => 0) | |
| 28 * for anonymous content. | |
| 29 */ | |
| 30 protected $fields = array(); | |
| 31 | |
| 32 /** | |
| 33 * An array of subqueries. | |
| 34 */ | |
| 35 protected $subqueries = array(); | |
| 36 | |
| 37 function __construct($operator = 'OR') { | |
| 38 $this->operator = $operator; | |
| 39 $this->id = ++SolrFilterSubQuery::$idCount; | |
| 40 } | |
| 41 | |
| 42 function __clone() { | |
| 43 $this->id = ++SolrFilterSubQuery::$idCount; | |
| 44 } | |
| 45 | |
| 46 public function getFilters($name = NULL) { | |
| 47 if (empty($name)) { | |
| 48 return $this->fields; | |
| 49 } | |
| 50 reset($this->fields); | |
| 51 $matches = array(); | |
| 52 foreach ($this->fields as $filter) { | |
| 53 if ($filter['#name'] == $name) { | |
| 54 $matches[] = $filter; | |
| 55 } | |
| 56 } | |
| 57 return $matches; | |
| 58 } | |
| 59 | |
| 60 public function hasFilter($name, $value, $exclude = FALSE) { | |
| 61 foreach ($this->fields as $pos => $values) { | |
| 62 if ($values['#name'] == $name && $values['#value'] == $value && $values['#exclude'] == $exclude) { | |
| 63 return TRUE; | |
| 64 } | |
| 65 } | |
| 66 return FALSE; | |
| 67 } | |
| 68 | |
| 69 public function addFilter($name, $value, $exclude = FALSE, $local = '') { | |
| 70 // @todo - escape the value if it has spaces in it and is not a range query or parenthesized. | |
| 71 $filter = array( | |
| 72 '#exclude' => (bool) $exclude, | |
| 73 '#name' => trim($name), | |
| 74 '#value' => trim($value), | |
| 75 '#local' => trim($local), | |
| 76 ); | |
| 77 $this->fields[] = $filter; | |
| 78 return $this; | |
| 79 } | |
| 80 | |
| 81 public function removeFilter($name, $value = NULL, $exclude = FALSE) { | |
| 82 // Remove from the public list of filters. | |
| 83 $this->unsetFilter($this->fields, $name, $value, $exclude); | |
| 84 return $this; | |
| 85 } | |
| 86 | |
| 87 protected function unsetFilter(&$fields, $name, $value, $exclude) { | |
| 88 if (!isset($value)) { | |
| 89 foreach ($fields as $pos => $values) { | |
| 90 if ($values['#name'] == $name) { | |
| 91 unset($fields[$pos]); | |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 else { | |
| 96 foreach ($fields as $pos => $values) { | |
| 97 if ($values['#name'] == $name && $values['#value'] == $value && $values['#exclude'] == $exclude) { | |
| 98 unset($fields[$pos]); | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 public function getFilterSubQueries() { | |
| 105 return $this->subqueries; | |
| 106 } | |
| 107 | |
| 108 public function addFilterSubQuery(SolrFilterSubQuery $query) { | |
| 109 $this->subqueries[$query->id] = $query; | |
| 110 return $this; | |
| 111 } | |
| 112 | |
| 113 public function removeFilterSubQuery(SolrFilterSubQuery $query) { | |
| 114 unset($this->subqueries[$query->id]); | |
| 115 return $this; | |
| 116 } | |
| 117 | |
| 118 public function removeFilterSubQueries() { | |
| 119 $this->subqueries = array(); | |
| 120 return $this; | |
| 121 } | |
| 122 | |
| 123 public function makeFilterQuery(array $filter) { | |
| 124 $prefix = empty($filter['#exclude']) ? '' : '-'; | |
| 125 if ($filter['#local']) { | |
| 126 $prefix = '{!' . $filter['#local'] . '}' . $prefix; | |
| 127 } | |
| 128 // If the field value contains a colon or a space, wrap it in double quotes, | |
| 129 // unless it is a range query or is already wrapped in double quotes or | |
| 130 // parentheses. | |
| 131 if (preg_match('/[ :]/', $filter['#value']) && !preg_match('/^[\[\{]\S+ TO \S+[\]\}]$/', $filter['#value']) && !preg_match('/^["\(].*["\)]$/', $filter['#value'])) { | |
| 132 $filter['#value'] = '"' . $filter['#value'] . '"'; | |
| 133 } | |
| 134 return $prefix . $filter['#name'] . ':' . $filter['#value']; | |
| 135 } | |
| 136 | |
| 137 /** | |
| 138 * Make sure our query matches the pattern name:value or name:"value" | |
| 139 * Make sure that if we are ranges we use name:[ AND ] | |
| 140 * allowed inputs : | |
| 141 * a. bundle:article | |
| 142 * b. date:[1970-12-31T23:59:59Z TO NOW] | |
| 143 * Split the text in 4 different parts | |
| 144 * 1. name, eg.: bundle or date | |
| 145 * 2. The first opening bracket (or nothing), eg.: [ | |
| 146 * 3. The value of the field, eg. article or 1970-12-31T23:59:59Z TO NOW | |
| 147 * 4. The last closing bracket, eg.: ] | |
| 148 * @param string $filter | |
| 149 * The filter to validate | |
| 150 * @return boolean | |
| 151 */ | |
| 152 public static function validFilterValue($filter) { | |
| 153 $opening = 0; | |
| 154 $closing = 0; | |
| 155 $name = NULL; | |
| 156 $value = NULL; | |
| 157 | |
| 158 if (preg_match('/(?P<name>[^:]+):(?P<value>.+)?$/', $filter, $matches)) { | |
| 159 foreach ($matches as $match_id => $match) { | |
| 160 switch($match_id) { | |
| 161 case 'name' : | |
| 162 $name = $match; | |
| 163 break; | |
| 164 case 'value' : | |
| 165 $value = $match; | |
| 166 break; | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 // For the name we allow any character that fits between the A-Z0-9 range and | |
| 171 // any alternative for this in other languages. No special characters allowed | |
| 172 if (!preg_match('/^[a-zA-Z0-9_\x7f-\xff]+$/', $name)) { | |
| 173 return FALSE; | |
| 174 } | |
| 175 | |
| 176 // For the value we allow anything that is UTF8 | |
| 177 if (!drupal_validate_utf8($value)) { | |
| 178 return FALSE; | |
| 179 } | |
| 180 | |
| 181 // Check our bracket count. If it does not match it is also not valid | |
| 182 $valid_brackets = TRUE; | |
| 183 $brackets['opening']['{'] = substr_count($value, '{'); | |
| 184 $brackets['closing']['}'] = substr_count($value, '}'); | |
| 185 $valid_brackets = ($brackets['opening']['{'] != $brackets['closing']['}']) ? FALSE : TRUE; | |
| 186 $brackets['opening']['['] = substr_count($value, '['); | |
| 187 $brackets['closing'][']'] = substr_count($value, ']'); | |
| 188 $valid_brackets = ($brackets['opening']['['] != $brackets['closing'][']']) ? FALSE : TRUE; | |
| 189 $brackets['opening']['('] = substr_count($value, '('); | |
| 190 $brackets['closing'][')'] = substr_count($value, ')'); | |
| 191 $valid_brackets = ($brackets['opening']['('] != $brackets['closing'][')']) ? FALSE : TRUE; | |
| 192 if (!$valid_brackets) { | |
| 193 return FALSE; | |
| 194 } | |
| 195 | |
| 196 // Check the date field inputs | |
| 197 if (preg_match('/\[(.+) TO (.+)\]$/', $value, $datefields)) { | |
| 198 // Only Allow a value in the form of | |
| 199 // http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html | |
| 200 // http://lucene.apache.org/solr/api/org/apache/solr/util/DateMathParser.html | |
| 201 // http://wiki.apache.org/solr/SolrQuerySyntax | |
| 202 // 1976-03-06T23:59:59.999Z (valid) | |
| 203 // * (valid) | |
| 204 // 1995-12-31T23:59:59.999Z (valid) | |
| 205 // 2007-03-06T00:00:00Z (valid) | |
| 206 // NOW-1YEAR/DAY (valid) | |
| 207 // NOW/DAY+1DAY (valid) | |
| 208 // 1976-03-06T23:59:59.999Z (valid) | |
| 209 // 1976-03-06T23:59:59.999Z+1YEAR (valid) | |
| 210 // 1976-03-06T23:59:59.999Z/YEAR (valid) | |
| 211 // 1976-03-06T23:59:59.999Z (valid) | |
| 212 // 1976-03-06T23::59::59.999Z (invalid) | |
| 213 if (!empty($datefields[1]) && !empty($datefields[2])) { | |
| 214 // Do not check to full value, only the splitted ones | |
| 215 unset($datefields[0]); | |
| 216 // Check if both matches are valid datefields | |
| 217 foreach ($datefields as $datefield) { | |
| 218 if (!preg_match('/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:[\d\.]{2,6}Z(\S)*)|(^([A-Z\*]+)(\A-Z0-9\+\-\/)*)/', $datefield, $datefield_match)) { | |
| 219 return FALSE; | |
| 220 } | |
| 221 } | |
| 222 } | |
| 223 } | |
| 224 } | |
| 225 return TRUE; | |
| 226 } | |
| 227 | |
| 228 /** | |
| 229 * Builds a set of filter queries from $this->fields and all subqueries. | |
| 230 * | |
| 231 * Returns an array of strings that can be combined into | |
| 232 * a URL query parameter or passed to Solr as fq paramters. | |
| 233 */ | |
| 234 protected function rebuildFq() { | |
| 235 $fq = array(); | |
| 236 foreach ($this->fields as $pos => $field) { | |
| 237 $fq[] = $this->makeFilterQuery($field); | |
| 238 } | |
| 239 foreach ($this->subqueries as $subquery) { | |
| 240 $subfq = $subquery->rebuildFq(); | |
| 241 if ($subfq) { | |
| 242 $operator = $subquery->operator; | |
| 243 $fq[] = "(" . implode(" $operator ", $subfq) . ")"; | |
| 244 } | |
| 245 } | |
| 246 return $fq; | |
| 247 } | |
| 248 | |
| 249 } | |
| 250 | |
| 251 class SolrBaseQuery extends SolrFilterSubQuery implements DrupalSolrQueryInterface { | |
| 252 | |
| 253 /** | |
| 254 * The parameters that get sent to Solr. | |
| 255 */ | |
| 256 protected $params = array('start' => 0, 'rows' => 10, 'fq' => array()); | |
| 257 | |
| 258 /** | |
| 259 * The search base path. | |
| 260 */ | |
| 261 protected $base_path; | |
| 262 protected $field_map = array(); | |
| 263 | |
| 264 /** | |
| 265 * DrupalApacheSolrService object | |
| 266 */ | |
| 267 protected $solr; | |
| 268 // The array keys must always be real Solr index fields. | |
| 269 protected $available_sorts; | |
| 270 | |
| 271 /** | |
| 272 * The query name is used to construct a searcher string. Mostly the | |
| 273 * environment id | |
| 274 */ | |
| 275 protected $name; | |
| 276 protected $context = array(); | |
| 277 // Makes sure we always have a valid sort. | |
| 278 protected $solrsort = array('#name' => 'score', '#direction' => 'desc'); | |
| 279 // A flag to allow the search to be aborted. | |
| 280 public $abort_search = FALSE; | |
| 281 | |
| 282 // A flag to check if need to retrieve another page of the result set | |
| 283 public $page = 0; | |
| 284 | |
| 285 /** | |
| 286 * @param $name | |
| 287 * The search name, used for finding the correct blocks and other config. | |
| 288 * Typically "apachesolr". | |
| 289 * | |
| 290 * @param $solr | |
| 291 * An instantiated DrupalApacheSolrService Object. | |
| 292 * Can be instantiated from apachesolr_get_solr(). | |
| 293 * | |
| 294 * @param $params | |
| 295 * Array of params to initialize the object (typically 'q' and 'fq'). | |
| 296 * | |
| 297 * @param $sortstring | |
| 298 * Visible string telling solr how to sort - added to GET query params. | |
| 299 * | |
| 300 * @param $base_path | |
| 301 * The search base path (without the keywords) for this query, without trailing slash. | |
| 302 */ | |
| 303 function __construct($name, $solr, array $params = array(), $sortstring = '', $base_path = '', $context = array()) { | |
| 304 parent::__construct(); | |
| 305 $this->name = $name; | |
| 306 $this->solr = $solr; | |
| 307 $this->addContext((array) $context); | |
| 308 $this->addParams((array) $params); | |
| 309 $this->available_sorts = $this->defaultSorts(); | |
| 310 $this->sortstring = trim($sortstring); | |
| 311 $this->parseSortString(); | |
| 312 $this->base_path = $base_path; | |
| 313 } | |
| 314 | |
| 315 protected function defaultSorts() { | |
| 316 return array( | |
| 317 'score' => array('title' => t('Relevancy'), 'default' => 'desc'), | |
| 318 'sort_label' => array('title' => t('Title'), 'default' => 'asc'), | |
| 319 'bundle' => array('title' => t('Type'), 'default' => 'asc'), | |
| 320 'sort_name' => array('title' => t('Author'), 'default' => 'asc'), | |
| 321 'ds_created' => array('title' => t('Date'), 'default' => 'desc'), | |
| 322 ); | |
| 323 } | |
| 324 | |
| 325 /** | |
| 326 * Get query name. | |
| 327 */ | |
| 328 public function getName() { | |
| 329 return $this->name; | |
| 330 } | |
| 331 | |
| 332 /** | |
| 333 * Get query searcher name (for facetapi, views, pages, etc). | |
| 334 */ | |
| 335 public function getSearcher() { | |
| 336 return $this->name . '@' . $this->solr->getId(); | |
| 337 } | |
| 338 | |
| 339 /** | |
| 340 * Get context values. | |
| 341 */ | |
| 342 public function getContext() { | |
| 343 return $this->context; | |
| 344 } | |
| 345 | |
| 346 /** | |
| 347 * Set context value. | |
| 348 */ | |
| 349 public function addContext(array $context) { | |
| 350 foreach ($context as $k => $v) { | |
| 351 $this->context[$k] = $v; | |
| 352 } | |
| 353 // The env_id must match that of the actual $solr object | |
| 354 $this->context['env_id'] = $this->solr->getId(); | |
| 355 return $this->context; | |
| 356 } | |
| 357 | |
| 358 protected $single_value_params = array( | |
| 359 'q' => TRUE, // http://wiki.apache.org/solr/SearchHandler#q | |
| 360 'q.op' => TRUE, // http://wiki.apache.org/solr/SearchHandler#q.op | |
| 361 'q.alt' => TRUE, // http://wiki.apache.org/solr/SearchHandler#q | |
| 362 'df' => TRUE, | |
| 363 'qt' => TRUE, | |
| 364 'defType' => TRUE, | |
| 365 'timeAllowed' => TRUE, | |
| 366 'omitHeader' => TRUE, | |
| 367 'debugQuery' => TRUE, | |
| 368 'start' => TRUE, | |
| 369 'rows' => TRUE, | |
| 370 'stats' => TRUE, | |
| 371 'facet' => TRUE, | |
| 372 'facet.prefix' => TRUE, | |
| 373 'facet.limit' => TRUE, | |
| 374 'facet.offset' => TRUE, | |
| 375 'facet.mincount' => TRUE, | |
| 376 'facet.missing' => TRUE, | |
| 377 'facet.method' => TRUE, | |
| 378 'facet.enum.cache.minDf' => TRUE, | |
| 379 'facet.date.start' => TRUE, | |
| 380 'facet.date.end' => TRUE, | |
| 381 'facet.date.gap' => TRUE, | |
| 382 'facet.date.hardend' => TRUE, | |
| 383 'facet.date.other' => TRUE, | |
| 384 'facet.date.include' => TRUE, | |
| 385 'hl' => TRUE, | |
| 386 'hl.snippets' => TRUE, | |
| 387 'hl.fragsize' => TRUE, | |
| 388 'hl.mergeContiguous' => TRUE, | |
| 389 'hl.requireFieldMatch' => TRUE, | |
| 390 'hl.maxAnalyzedChars' => TRUE, | |
| 391 'hl.alternateField' => TRUE, | |
| 392 'hl.maxAlternateFieldLength' => TRUE, | |
| 393 'hl.formatter' => TRUE, | |
| 394 'hl.simple.pre/hl.simple.post' => TRUE, | |
| 395 'hl.fragmenter' => TRUE, | |
| 396 'hl.fragListBuilder' => TRUE, | |
| 397 'hl.fragmentsBuilder' => TRUE, | |
| 398 'hl.useFastVectorHighlighter' => TRUE, | |
| 399 'hl.usePhraseHighlighter' => TRUE, | |
| 400 'hl.highlightMultiTerm' => TRUE, | |
| 401 'hl.regex.slop' => TRUE, | |
| 402 'hl.regex.pattern' => TRUE, | |
| 403 'hl.regex.maxAnalyzedChars' => TRUE, | |
| 404 'spellcheck' => TRUE, | |
| 405 ); | |
| 406 | |
| 407 public function getParam($name) { | |
| 408 if ($name == 'fq') { | |
| 409 return $this->rebuildFq(); | |
| 410 } | |
| 411 $empty = isset($this->single_value_params[$name]) ? NULL : array(); | |
| 412 return isset($this->params[$name]) ? $this->params[$name] : $empty; | |
| 413 } | |
| 414 | |
| 415 public function getParams() { | |
| 416 $params = $this->params; | |
| 417 $params['fq'] = $this->rebuildFq(); | |
| 418 return $params; | |
| 419 } | |
| 420 | |
| 421 public function getSolrParams() { | |
| 422 $params = $this->getParams(); | |
| 423 // For certain fields Solr prefers a comma separated list. | |
| 424 foreach (array('fl', 'hl.fl', 'sort', 'mlt.fl') as $name) { | |
| 425 if (isset($params[$name])) { | |
| 426 $params[$name] = implode(',', $params[$name]); | |
| 427 } | |
| 428 } | |
| 429 return $params; | |
| 430 } | |
| 431 | |
| 432 protected function addFq($string, $index = NULL) { | |
| 433 $string = trim($string); | |
| 434 $local = ''; | |
| 435 $exclude = FALSE; | |
| 436 $name = NULL; | |
| 437 $value = NULL; | |
| 438 | |
| 439 // Check if we are dealing with an exclude | |
| 440 if (preg_match('/^-(.*)/', $string, $matches)) { | |
| 441 $exclude = TRUE; | |
| 442 $string = $matches[1]; | |
| 443 } | |
| 444 | |
| 445 // If {!something} is found as first character then this is a local value | |
| 446 if (preg_match('/\{!([^}]+)\}(.*)/', $string, $matches)) { | |
| 447 $local = $matches[1]; | |
| 448 $string = $matches[2]; | |
| 449 } | |
| 450 | |
| 451 // Anything that has a name and value | |
| 452 // check if we have a : in the string | |
| 453 if (strstr($string, ':')) { | |
| 454 list($name, $value) = explode(":", $string, 2); | |
| 455 } | |
| 456 else { | |
| 457 $value = $string; | |
| 458 } | |
| 459 $this->addFilter($name, $value, $exclude, $local); | |
| 460 return $this; | |
| 461 } | |
| 462 | |
| 463 public function addParam($name, $value) { | |
| 464 if (isset($this->single_value_params[$name])) { | |
| 465 if (is_array($value)) { | |
| 466 $value = end($value); | |
| 467 } | |
| 468 $this->params[$name] = $this->normalizeParamValue($value); | |
| 469 return $this; | |
| 470 } | |
| 471 // We never actually populate $this->params['fq']. Instead | |
| 472 // we manage everything via the filter methods. | |
| 473 if ($name == 'fq') { | |
| 474 if (is_array($value)) { | |
| 475 array_walk_recursive($value, array($this, 'addFq')); | |
| 476 return $this; | |
| 477 } | |
| 478 else { | |
| 479 return $this->addFq($value); | |
| 480 } | |
| 481 } | |
| 482 | |
| 483 if (!isset($this->params[$name])) { | |
| 484 $this->params[$name] = array(); | |
| 485 } | |
| 486 | |
| 487 if (!is_array($value)) { | |
| 488 // Convert to array for array_map. | |
| 489 $param_values = array($value); | |
| 490 } | |
| 491 else { | |
| 492 // Convert to a numerically keyed array. | |
| 493 $param_values = array_values($value); | |
| 494 } | |
| 495 $this->params[$name] = array_merge($this->params[$name], array_map(array($this, 'normalizeParamValue'), $param_values)); | |
| 496 | |
| 497 return $this; | |
| 498 } | |
| 499 | |
| 500 protected function normalizeParamValue($value) { | |
| 501 // Convert boolean to string. | |
| 502 if (is_bool($value)) { | |
| 503 return $value ? 'true' : 'false'; | |
| 504 } | |
| 505 // Convert to trimmed string. | |
| 506 return trim($value); | |
| 507 } | |
| 508 | |
| 509 public function addParams(Array $params) { | |
| 510 foreach ($params as $name => $value) { | |
| 511 $this->addParam($name, $value); | |
| 512 } | |
| 513 return $this; | |
| 514 } | |
| 515 | |
| 516 public function removeParam($name) { | |
| 517 unset($this->params[$name]); | |
| 518 if ($name == 'fq') { | |
| 519 $this->fields = array(); | |
| 520 $this->subqueries = array(); | |
| 521 } | |
| 522 return $this; | |
| 523 } | |
| 524 | |
| 525 public function replaceParam($name, $value) { | |
| 526 $this->removeParam($name); | |
| 527 return $this->addParam($name, $value); | |
| 528 } | |
| 529 | |
| 530 /** | |
| 531 * Handles aliases for field to make nicer URLs. | |
| 532 * | |
| 533 * @param $field_map | |
| 534 * An array keyed with real Solr index field names with the alias as value. | |
| 535 * | |
| 536 * @return DrupalSolrQueryInterface | |
| 537 * The called object. | |
| 538 */ | |
| 539 public function addFieldAliases($field_map) { | |
| 540 $this->field_map = array_merge($this->field_map, $field_map); | |
| 541 // We have to re-parse the filters. | |
| 542 $this->parseSortString(); | |
| 543 return $this; | |
| 544 } | |
| 545 | |
| 546 public function getFieldAliases() { | |
| 547 return $this->field_map; | |
| 548 } | |
| 549 | |
| 550 public function clearFieldAliases() { | |
| 551 $this->field_map = array(); | |
| 552 // We have to re-parse the filters. | |
| 553 $this->parseSortString(); | |
| 554 return $this; | |
| 555 } | |
| 556 | |
| 557 protected function parseSortString() { | |
| 558 // Substitute any field aliases with real field names. | |
| 559 $sortstring = strtr($this->sortstring, $this->field_map); | |
| 560 // Score is a special case - it's the default sort for Solr. | |
| 561 if ('' == $sortstring || 'score desc' == $sortstring) { | |
| 562 $this->solrsort['#name'] = 'score'; | |
| 563 $this->solrsort['#direction'] = 'desc'; | |
| 564 unset($this->params['sort']); | |
| 565 } | |
| 566 else { | |
| 567 // Validate and set sort parameter | |
| 568 $fields = implode('|', array_keys($this->available_sorts)); | |
| 569 if (preg_match('/^(?:(' . $fields . ') (asc|desc),?)+$/', $sortstring, $matches)) { | |
| 570 // We only use the last match. | |
| 571 $this->solrsort['#name'] = $matches[1]; | |
| 572 $this->solrsort['#direction'] = $matches[2]; | |
| 573 $this->params['sort'] = array($sortstring); | |
| 574 } | |
| 575 } | |
| 576 } | |
| 577 | |
| 578 public function getAvailableSorts() { | |
| 579 return $this->available_sorts; | |
| 580 } | |
| 581 | |
| 582 public function setAvailableSort($name, $sort) { | |
| 583 // We expect non-aliased sorts to be added. | |
| 584 $this->available_sorts[$name] = $sort; | |
| 585 // Re-parse the sortstring. | |
| 586 $this->parseSortString(); | |
| 587 return $this; | |
| 588 } | |
| 589 | |
| 590 public function setAvailableSorts($sorts) { | |
| 591 // We expect a complete array of valid sorts. | |
| 592 $this->available_sorts = $sorts; | |
| 593 $this->parseSortString(); | |
| 594 return $this; | |
| 595 } | |
| 596 | |
| 597 public function removeAvailableSort($name) { | |
| 598 unset($this->available_sorts[$name]); | |
| 599 // Re-parse the sortstring. | |
| 600 $this->parseSortString(); | |
| 601 return $this; | |
| 602 } | |
| 603 | |
| 604 public function getSolrsort() { | |
| 605 return $this->solrsort; | |
| 606 } | |
| 607 | |
| 608 public function setSolrsort($name, $direction) { | |
| 609 $this->sortstring = trim($name) . ' ' . trim($direction); | |
| 610 $this->parseSortString(); | |
| 611 return $this; | |
| 612 } | |
| 613 | |
| 614 public function getPath($new_keywords = NULL) { | |
| 615 if (isset($new_keywords)) { | |
| 616 return $this->base_path . '/' . $new_keywords; | |
| 617 } | |
| 618 elseif ($this->getParam('q')) { | |
| 619 return $this->base_path . '/' . $this->getParam('q'); | |
| 620 } | |
| 621 else { | |
| 622 // Return with empty query (the slash). The path for a facet | |
| 623 // becomes $this->base_path . '//facetinfo'; | |
| 624 // We do this so we can have a consistent way of retrieving the query + | |
| 625 // additional parameters | |
| 626 return $this->base_path . '/'; | |
| 627 } | |
| 628 } | |
| 629 | |
| 630 public function getSolrsortUrlQuery() { | |
| 631 $queryvalues = array(); | |
| 632 $solrsort = $this->solrsort; | |
| 633 if ($solrsort && ($solrsort['#name'] != 'score')) { | |
| 634 if (isset($this->field_map[$solrsort['#name']])) { | |
| 635 $solrsort['#name'] = $this->field_map[$solrsort['#name']]; | |
| 636 } | |
| 637 $queryvalues['solrsort'] = $solrsort['#name'] . ' ' . $solrsort['#direction']; | |
| 638 } | |
| 639 else { | |
| 640 // Return to default relevancy sort. | |
| 641 unset($queryvalues['solrsort']); | |
| 642 } | |
| 643 return $queryvalues; | |
| 644 } | |
| 645 | |
| 646 public function search($keys = NULL) { | |
| 647 if ($this->abort_search) { | |
| 648 return NULL; | |
| 649 } | |
| 650 return $this->solr->search($keys, $this->getSolrParams()); | |
| 651 } | |
| 652 | |
| 653 public function solr($method) { | |
| 654 return $this->solr->$method(); | |
| 655 } | |
| 656 | |
| 657 } |
