PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/all/modules/apachesolr/Solr_Base_Query.php

https://bitbucket.org/norvin/mingen-dts-server
PHP | 450 lines | 262 code | 46 blank | 142 comment | 47 complexity | 81d6677feb7e3eda7d265d6dacb0113c MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. // $Id: Solr_Base_Query.php,v 1.1.4.40.4.4 2010/01/22 16:20:37 claudiucristea Exp $
  3. class Solr_Base_Query implements Drupal_Solr_Query_Interface {
  4. /**
  5. * Extract all uses of one named field from a filter string e.g. 'type:book'
  6. */
  7. public function filter_extract(&$filterstring, $name) {
  8. $extracted = array();
  9. // Range queries. The "TO" is case-sensitive.
  10. $patterns[] = '/(^| |-)'. $name .':([\[\{](\S+) TO (\S+)[\]\}])/';
  11. // Match quoted values.
  12. $patterns[] = '/(^| |-)'. $name .':"([^"]*)"/';
  13. // Match unquoted values.
  14. $patterns[] = '/(^| |-)'. $name .':([^ ]*)/';
  15. foreach ($patterns as $p) {
  16. if (preg_match_all($p, $filterstring, $matches, PREG_SET_ORDER)) {
  17. foreach($matches as $match) {
  18. $filter = array();
  19. $filter['#query'] = $match[0];
  20. $filter['#exclude'] = ($match[1] == '-');
  21. $filter['#value'] = trim($match[2]);
  22. if (isset($match[3])) {
  23. // Extra data for range queries
  24. $filter['#start'] = $match[3];
  25. $filter['#end'] = $match[4];
  26. }
  27. $extracted[] = $filter;
  28. // Update the local copy of $filters by removing the match.
  29. $filterstring = str_replace($match[0], '', $filterstring);
  30. }
  31. }
  32. }
  33. return $extracted;
  34. }
  35. /**
  36. * Takes an array $field and combines the #name and #value in a way
  37. * suitable for use in a Solr query.
  38. */
  39. public function make_filter(array $filter) {
  40. // If the field value has spaces, or : in it, wrap it in double quotes.
  41. // unless it is a range query.
  42. if (preg_match('/[ :]/', $filter['#value']) && !isset($filter['#start']) && !preg_match('/[\[\{]\S+ TO \S+[\]\}]/', $filter['#value'])) {
  43. $filter['#value'] = '"'. $filter['#value']. '"';
  44. }
  45. $prefix = empty($filter['#exclude']) ? '' : '-';
  46. return $prefix . $filter['#name'] . ':' . $filter['#value'];
  47. }
  48. /**
  49. * Static shared by all instances, used to increment ID numbers.
  50. */
  51. protected static $idCount = 0;
  52. /**
  53. * Each query/subquery will have a unique ID
  54. */
  55. public $id;
  56. /**
  57. * A keyed array where the key is a position integer and the value
  58. * is an array with #name and #value properties. Each value is a
  59. * used for filter queries, e.g. array('#name' => 'uid', '#value' => 0)
  60. * for anonymous content.
  61. */
  62. protected $fields;
  63. /**
  64. * The complete filter string for a query. Usually from $_GET['filters']
  65. * Contains name:value pairs for filter queries. For example,
  66. * "type:book" for book nodes.
  67. */
  68. protected $filterstring;
  69. /**
  70. * A mapping of field names from the URL to real index field names.
  71. */
  72. protected $field_map = array();
  73. /**
  74. * An array of subqueries.
  75. */
  76. protected $subqueries = array();
  77. /**
  78. * The search keywords.
  79. */
  80. protected $keys;
  81. /**
  82. * The search base path.
  83. */
  84. protected $base_path;
  85. /**
  86. * Apache_Solr_Service object
  87. */
  88. protected $solr;
  89. protected $available_sorts;
  90. // Makes sure we always have a valid sort.
  91. protected $solrsort = array('#name' => 'score', '#direction' => 'asc');
  92. /**
  93. * @param $solr
  94. * An instantiated Apache_Solr_Service Object.
  95. * Can be instantiated from apachesolr_get_solr().
  96. *
  97. * @param $keys
  98. * The string that a user would type into the search box. Suitable input
  99. * may come from search_get_keys().
  100. *
  101. * @param $filterstring
  102. * Key and value pairs that are applied as filter queries.
  103. *
  104. * @param $sortstring
  105. * Visible string telling solr how to sort - added to GET query params.
  106. *
  107. * @param $base_path
  108. * The search base path (without the keywords) for this query.
  109. */
  110. function __construct($solr, $keys, $filterstring, $sortstring, $base_path) {
  111. $this->solr = $solr;
  112. $this->keys = trim($keys);
  113. $this->filterstring = trim($filterstring);
  114. $this->parse_filters();
  115. $this->available_sorts = $this->default_sorts();
  116. $this->sortstring = trim($sortstring);
  117. $this->parse_sortstring();
  118. $this->base_path = $base_path;
  119. $this->id = ++self::$idCount;
  120. }
  121. function __clone() {
  122. $this->id = ++self::$idCount;
  123. }
  124. public function add_filter($field, $value, $exclude = FALSE, $callbacks = array()) {
  125. $this->fields[] = array('#exclude' => $exclude, '#name' => $field, '#value' => trim($value), '#callbacks' => $callbacks);
  126. }
  127. /**
  128. * Get all filters, or the subset of filters for one field.
  129. *
  130. * @param $name
  131. * Optional name of a Solr field.
  132. */
  133. public function get_filters($name = NULL) {
  134. if (empty($name)) {
  135. return $this->fields;
  136. }
  137. reset($this->fields);
  138. $matches = array();
  139. foreach ($this->fields as $filter) {
  140. if ($filter['#name'] == $name) {
  141. $matches[] = $filter;
  142. }
  143. }
  144. return $matches;
  145. }
  146. public function remove_filter($name, $value = NULL) {
  147. // We can only remove named fields.
  148. if (empty($name)) {
  149. return;
  150. }
  151. if (!isset($value)) {
  152. foreach ($this->fields as $pos => $values) {
  153. if ($values['#name'] == $name) {
  154. unset($this->fields[$pos]);
  155. }
  156. }
  157. }
  158. else {
  159. foreach ($this->fields as $pos => $values) {
  160. if ($values['#name'] == $name && $values['#value'] == $value) {
  161. unset($this->fields[$pos]);
  162. }
  163. }
  164. }
  165. }
  166. public function has_filter($name, $value) {
  167. foreach ($this->fields as $pos => $values) {
  168. if (isset($values['#name']) && isset($values['#value']) && $values['#name'] == $name && $values['#value'] == $value) {
  169. return TRUE;
  170. }
  171. }
  172. return FALSE;
  173. }
  174. /**
  175. * Handle aliases for field to make nicer URLs
  176. *
  177. * @param $field_map
  178. * An array keyed with real Solr index field names, with value being the alias.
  179. */
  180. function add_field_aliases($field_map) {
  181. $this->field_map = array_merge($this->field_map, $field_map);
  182. // We have to re-parse the filters.
  183. $this->parse_filters();
  184. }
  185. function get_field_aliases() {
  186. return $this->field_map;
  187. }
  188. function clear_field_aliases() {
  189. $this->field_map = array();
  190. // We have to re-parse the filters.
  191. $this->parse_filters();
  192. }
  193. /**
  194. * A subquery is another instance of a Solr_Base_Query that should be joined
  195. * to the query. The operator determines whether it will be joined with AND or
  196. * OR.
  197. *
  198. * @param $query
  199. * An instance of Drupal_Solr_Query_Interface.
  200. *
  201. * @param $operator
  202. * 'AND' or 'OR'
  203. */
  204. public function add_subquery(Drupal_Solr_Query_Interface $query, $fq_operator = 'OR', $q_operator = 'AND') {
  205. $this->subqueries[$query->id] = array('#query' => $query, '#fq_operator' => $fq_operator, '#q_operator' => $q_operator);
  206. }
  207. public function remove_subquery(Drupal_Solr_Query_Interface $query) {
  208. unset($this->subqueries[$query->id]);
  209. }
  210. public function remove_subqueries() {
  211. $this->subqueries = array();
  212. }
  213. protected function parse_sortstring() {
  214. // Substitute any field aliases with real field names.
  215. $sortstring = strtr($this->sortstring, array_flip($this->field_map));
  216. // Score is a special case - it's the default sort for Solr.
  217. if ('' == $sortstring) {
  218. $this->set_solrsort('score', 'asc');
  219. }
  220. else {
  221. // Validate and set sort parameter
  222. $fields = implode('|', array_keys($this->available_sorts));
  223. if (preg_match('/^(?:('. $fields .') (asc|desc),?)+$/', $sortstring, $matches)) {
  224. // We only use the last match.
  225. $this->set_solrsort($matches[1], $matches[2]);
  226. }
  227. }
  228. }
  229. public function set_solrsort($name, $direction) {
  230. if (isset($this->available_sorts[$name])) {
  231. $this->solrsort = array('#name' => $name, '#direction' => $direction);
  232. }
  233. }
  234. public function get_solrsort() {
  235. return $this->solrsort;
  236. }
  237. public function get_available_sorts() {
  238. return $this->available_sorts;
  239. }
  240. public function set_available_sort($name, $sort) {
  241. // We expect non-aliased sorts to be added.
  242. $this->available_sorts[$name] = $sort;
  243. // Re-parse the sortstring.
  244. $this->parse_sortstring();
  245. }
  246. public function remove_available_sort($name) {
  247. unset($this->available_sorts[$name]);
  248. // Re-parse the sortstring.
  249. $this->parse_sortstring();
  250. }
  251. /**
  252. * Returns a default list of sorts.
  253. */
  254. protected function default_sorts() {
  255. // The array keys must always be real Solr index fields.
  256. return array(
  257. 'score' => array('title' => t('Relevancy'), 'default' => 'asc'),
  258. 'sort_title' => array('title' => t('Title'), 'default' => 'asc'),
  259. 'type' => array('title' => t('Type'), 'default' => 'asc'),
  260. 'sort_name' => array('title' => t('Author'), 'default' => 'asc'),
  261. 'created' => array('title' => t('Date'), 'default' => 'desc'),
  262. );
  263. }
  264. /**
  265. * Return filters and sort in a form suitable for a query param to url().
  266. */
  267. public function get_url_queryvalues() {
  268. $queryvalues = array();
  269. if ($fq = $this->rebuild_fq(TRUE)) {
  270. $queryvalues['filters'] = implode(' ', $fq);
  271. }
  272. $solrsort = $this->solrsort;
  273. if ($solrsort && ($solrsort['#name'] != 'score' || $solrsort['#direction'] != 'asc')) {
  274. if (isset($this->field_map[$solrsort['#name']])) {
  275. $solrsort['#name'] = $this->field_map[$solrsort['#name']];
  276. }
  277. $queryvalues['solrsort'] = $solrsort['#name'] .' '. $solrsort['#direction'];
  278. }
  279. return $queryvalues;
  280. }
  281. public function get_fq() {
  282. return $this->rebuild_fq();
  283. }
  284. /**
  285. * A function to get just the keyword components of the query,
  286. * omitting any field:value portions.
  287. */
  288. public function get_query_basic() {
  289. return $this->rebuild_query();
  290. }
  291. /**
  292. * Return the search path.
  293. *
  294. * @param string $new_keywords
  295. * Optional. When set, this string overrides the query's current keywords.
  296. */
  297. public function get_path($new_keywords = NULL) {
  298. if (isset($new_keywords)) {
  299. return $this->base_path . '/' . $new_keywords;
  300. }
  301. return $this->base_path . '/' . $this->get_query_basic();
  302. }
  303. /**
  304. * Build additional breadcrumb elements relative to $base.
  305. */
  306. public function get_breadcrumb($base = NULL) {
  307. $progressive_crumb = array();
  308. if (!isset($base)) {
  309. $base = $this->get_path();
  310. }
  311. $search_keys = $this->get_query_basic();
  312. if ($search_keys) {
  313. $breadcrumb[] = l($search_keys, $base);
  314. }
  315. foreach ($this->fields as $field) {
  316. $name = $field['#name'];
  317. // Look for a field alias.
  318. if (isset($this->field_map[$name])) {
  319. $field['#name'] = $this->field_map[$name];
  320. }
  321. $progressive_crumb[] = $this->make_filter($field);
  322. $options_query = 'filters=' . rawurlencode(implode(' ', $progressive_crumb));
  323. $breadcrumb_name = 'apachesolr_breadcrumb_' . $name;
  324. // Modules utilize this alter to consolidate several fields into one
  325. // theme function. This is how CCK breadcrumbs are handled.
  326. drupal_alter('apachesolr_theme_breadcrumb', $breadcrumb_name);
  327. if ($themed = theme($breadcrumb_name, $field)) {
  328. $breadcrumb[] = l($themed, $base, array(), $options_query);
  329. }
  330. else {
  331. $breadcrumb[] = l($field['#value'], $base, array(), $options_query);
  332. }
  333. }
  334. // The last breadcrumb is the current page, so it shouldn't be a link.
  335. $last = count($breadcrumb) - 1;
  336. $breadcrumb[$last] = strip_tags($breadcrumb[$last]);
  337. return $breadcrumb;
  338. }
  339. /**
  340. * Parse the filter string in $this->filters into $this->fields.
  341. *
  342. * Builds an array of field name/value pairs.
  343. */
  344. protected function parse_filters() {
  345. $this->fields = array();
  346. $filterstring = $this->filterstring;
  347. // Gets information about the fields already in solr index.
  348. $index_fields = $this->solr->getFields();
  349. foreach ((array) $index_fields as $name => $data) {
  350. // Look for a field alias.
  351. $alias = isset($this->field_map[$name]) ? $this->field_map[$name] : $name;
  352. // Get the values for $name
  353. $extracted = $this->filter_extract($filterstring, $alias);
  354. if (count($extracted)) {
  355. foreach ($extracted as $filter) {
  356. $pos = strpos($this->filterstring, $filter['#query']);
  357. // $solr_keys and $solr_crumbs are keyed on $pos so that query order
  358. // is maintained. This is important for breadcrumbs.
  359. $filter['#name'] = $name;
  360. $this->fields[$pos] = $filter;
  361. }
  362. }
  363. }
  364. // Even though the array has the right keys they are likely in the wrong
  365. // order. ksort() sorts the array by key while maintaining the key.
  366. ksort($this->fields);
  367. }
  368. /**
  369. * Builds a set of filter queries from $this->fields and all subqueries.
  370. *
  371. * Returns an array of strings that can be combined into
  372. * a URL query parameter or passed to Solr as fq paramters.
  373. */
  374. protected function rebuild_fq($aliases = FALSE) {
  375. $fq = array();
  376. $fields = array();
  377. foreach ($this->fields as $pos => $field) {
  378. // Look for a field alias.
  379. if ($aliases && isset($this->field_map[$field['#name']])) {
  380. $field['#name'] = $this->field_map[$field['#name']];
  381. }
  382. $fq[] = $this->make_filter($field);
  383. }
  384. foreach ($this->subqueries as $id => $data) {
  385. $subfq = $data['#query']->rebuild_fq($aliases);
  386. if ($subfq) {
  387. $operator = $data['#fq_operator'];
  388. $fq[] = "(" . implode(" {$operator} ", $subfq) .")";
  389. }
  390. }
  391. return $fq;
  392. }
  393. protected function rebuild_query() {
  394. $query = $this->keys;
  395. foreach ($this->subqueries as $id => $data) {
  396. $operator = $data['#q_operator'];
  397. $subquery = $data['#query']->get_query_basic();
  398. if ($subquery) {
  399. $query .= " {$operator} ({$subquery})";
  400. }
  401. }
  402. return $query;
  403. }
  404. }