View source
<?php
class Solr_Base_Query implements Drupal_Solr_Query_Interface {
public function filter_extract(&$filterstring, $name) {
$extracted = array();
$name = preg_quote($name, '/');
$patterns[] = '/(^| |-)' . $name . ':([\\[\\{](\\S+) TO (\\S+)[\\]\\}])/';
$patterns[] = '/(^| |-)' . $name . ':"([^"]*)"/';
$patterns[] = '/(^| |-)' . $name . ':([^ ]*)/';
foreach ($patterns as $p) {
if (preg_match_all($p, $filterstring, $matches, PREG_SET_ORDER)) {
usort($matches, array(
$this,
'filter_extract_cmp',
));
foreach ($matches as $match) {
$filter = array();
$filter['#query'] = $match[0];
$filter['#exclude'] = $match[1] == '-';
$filter['#value'] = trim($match[2]);
if (strlen($filter['#value'])) {
if (isset($match[3])) {
$filter['#start'] = $match[3];
$filter['#end'] = $match[4];
}
$extracted[] = $filter;
}
$filterstring = str_replace($match[0], '', $filterstring);
}
}
}
return $extracted;
}
public function filter_extract_cmp($a, $b) {
if (strlen($a[0]) == strlen($b[0])) {
return 0;
}
return strlen($a[0]) > strlen($b[0]) ? -1 : 1;
}
public function make_filter(array $filter) {
if (preg_match('/[ :]/', $filter['#value']) && !isset($filter['#start']) && !preg_match('/[\\[\\{]\\S+ TO \\S+[\\]\\}]/', $filter['#value'])) {
$filter['#value'] = '"' . $filter['#value'] . '"';
}
$prefix = empty($filter['#exclude']) ? '' : '-';
return $prefix . $filter['#name'] . ':' . $filter['#value'];
}
protected static $idCount = 0;
public $id;
protected $fields = array();
protected $fields_added = array();
protected $fields_removed = array();
protected $filterstring;
protected $field_map = array();
protected $subqueries = array();
protected $keys;
protected $base_path;
protected $solr;
protected $available_sorts;
protected $solrsort = array(
'#name' => 'score',
'#direction' => 'desc',
);
function __construct($solr, $keys, $filterstring, $sortstring, $base_path) {
$this->solr = $solr;
$this->keys = trim($keys);
$this->filterstring = trim($filterstring);
$this
->parse_filters();
$this->available_sorts = $this
->default_sorts();
$this->sortstring = trim($sortstring);
$this
->parse_sortstring();
$this->base_path = $base_path;
$this->id = ++self::$idCount;
}
function __clone() {
$this->id = ++self::$idCount;
}
public function get_filters($name = NULL) {
if (empty($name)) {
return $this->fields;
}
reset($this->fields);
$matches = array();
foreach ($this->fields as $filter) {
if ($filter['#name'] == $name) {
$matches[] = $filter;
}
}
return $matches;
}
public function has_filter($name, $value) {
foreach ($this->fields as $pos => $values) {
if (isset($values['#name']) && isset($values['#value']) && $values['#name'] == $name && $values['#value'] == $value) {
return TRUE;
}
}
return FALSE;
}
public function add_filter($name, $value, $exclude = FALSE, $callbacks = array()) {
$filter = array(
'#exclude' => $exclude,
'#name' => $name,
'#value' => trim($value),
'#callbacks' => $callbacks,
);
$this->fields_added[] = $filter;
$this->fields[] = $filter;
$this
->unset_filter($this->fields_removed, $name, $value);
}
public function remove_filter($name, $value = NULL) {
if (empty($name)) {
return;
}
$this->fields_removed[$name][] = $value;
$this
->unset_filter($this->fields, $name, $value);
$this
->unset_filter($this->fields_added, $name, $value);
}
protected function unset_filter(&$fields, $name, $value) {
if (!isset($value)) {
foreach ($fields as $pos => $values) {
if ($values['#name'] == $name) {
unset($fields[$pos]);
}
}
}
else {
foreach ($fields as $pos => $values) {
if ($values['#name'] == $name && $values['#value'] == $value) {
unset($fields[$pos]);
}
}
}
}
function add_field_aliases($field_map) {
$this->field_map = array_merge($this->field_map, $field_map);
$this
->parse_filters();
}
function get_field_aliases() {
return $this->field_map;
}
function clear_field_aliases() {
$this->field_map = array();
$this
->parse_filters();
}
function get_keys() {
return $this->keys;
}
function set_keys($keys) {
$this->keys = $keys;
}
public function remove_keys() {
$this->keys = '';
}
public function add_subquery(Drupal_Solr_Query_Interface $query, $fq_operator = 'OR', $q_operator = 'AND') {
$this->subqueries[$query->id] = array(
'#query' => $query,
'#fq_operator' => $fq_operator,
'#q_operator' => $q_operator,
);
}
public function remove_subquery(Drupal_Solr_Query_Interface $query) {
unset($this->subqueries[$query->id]);
}
public function remove_subqueries() {
$this->subqueries = array();
}
protected function parse_sortstring() {
$sortstring = strtr($this->sortstring, array_flip($this->field_map));
if ('' == $sortstring) {
$this
->set_solrsort('score', 'desc');
}
else {
$fields = implode('|', array_keys($this->available_sorts));
if (preg_match('/^(?:(' . $fields . ') (asc|desc),?)+$/', $sortstring, $matches)) {
$this
->set_solrsort($matches[1], $matches[2]);
}
}
}
protected function default_sorts() {
return array(
'score' => array(
'title' => t('Relevancy'),
'default' => 'desc',
),
'sort_title' => array(
'title' => t('Title'),
'default' => 'asc',
),
'type' => array(
'title' => t('Type'),
'default' => 'asc',
),
'sort_name' => array(
'title' => t('Author'),
'default' => 'asc',
),
'created' => array(
'title' => t('Date'),
'default' => 'desc',
),
);
}
public function get_available_sorts() {
return $this->available_sorts;
}
public function set_available_sort($name, $sort) {
$this->available_sorts[$name] = $sort;
$this
->parse_sortstring();
}
public function remove_available_sort($name) {
unset($this->available_sorts[$name]);
$this
->parse_sortstring();
}
public function get_solrsort() {
return $this->solrsort;
}
public function set_solrsort($name, $direction) {
if (isset($this->available_sorts[$name])) {
$this->solrsort = array(
'#name' => $name,
'#direction' => $direction,
);
}
}
public function get_path($new_keywords = NULL) {
if (isset($new_keywords)) {
return $this->base_path . '/' . $new_keywords;
}
return $this->base_path . '/' . $this
->get_query_basic();
}
public function get_url_queryvalues() {
$queryvalues = array();
$filters = array();
foreach ($this->fields as $pos => $field) {
if (isset($this->field_map[$field['#name']])) {
$field['#name'] = $this->field_map[$field['#name']];
}
$filters[] = $this
->make_filter($field);
}
if ($filters) {
$queryvalues['filters'] = implode(' ', $filters);
}
$solrsort = $this->solrsort;
if ($solrsort && ($solrsort['#name'] != 'score' || $solrsort['#direction'] != 'desc')) {
if (isset($this->field_map[$solrsort['#name']])) {
$solrsort['#name'] = $this->field_map[$solrsort['#name']];
}
$queryvalues['solrsort'] = $solrsort['#name'] . ' ' . $solrsort['#direction'];
}
return $queryvalues;
}
public function get_query_basic() {
return $this
->rebuild_query();
}
public function get_fq() {
return $this
->rebuild_fq();
}
public function get_breadcrumb($base = NULL) {
$breadcrumb = array();
$progressive_crumb = array();
if (!isset($base)) {
$base = $this
->get_path();
}
$search_keys = $this
->get_query_basic();
if ($search_keys) {
$breadcrumb[] = l($search_keys, $base);
}
foreach ($this->fields as $field) {
$name = $field['#name'];
if (isset($this->field_map[$name])) {
$field['#name'] = $this->field_map[$name];
}
$progressive_crumb[] = $this
->make_filter($field);
$options = array(
'query' => 'filters=' . rawurlencode(implode(' ', $progressive_crumb)),
);
$breadcrumb_name = 'apachesolr_breadcrumb_' . $name;
drupal_alter('apachesolr_theme_breadcrumb', $breadcrumb_name);
if ($themed = theme($breadcrumb_name, $field)) {
$breadcrumb[] = l($themed, $base, $options);
}
else {
$breadcrumb[] = l($field['#value'], $base, $options);
}
}
if (!empty($breadcrumb)) {
$last = count($breadcrumb) - 1;
$breadcrumb[$last] = strip_tags($breadcrumb[$last]);
}
return $breadcrumb;
}
protected function parse_filters() {
$this->fields = array();
$parsed_fields = array();
$filterstring = $this->filterstring;
$index_fields = $this->solr
->getFields();
foreach ((array) $index_fields as $name => $data) {
$alias = isset($this->field_map[$name]) ? $this->field_map[$name] : $name;
$extracted = $this
->filter_extract($filterstring, $alias);
if (count($extracted)) {
$filter_pos_string = $this->filterstring . ' ';
foreach ($extracted as $filter) {
$pos = strpos($filter_pos_string, $filter['#query'] . ' ');
$filter['#name'] = $name;
$parsed_fields[$pos] = $filter;
}
}
}
ksort($parsed_fields);
foreach ($this->fields_removed as $name => $values) {
foreach ($values as $val) {
$this
->unset_filter($parsed_fields, $name, $val);
}
}
$this->fields = array_merge(array_values($parsed_fields), $this->fields_added);
}
protected function rebuild_fq($aliases = FALSE) {
$fq = array();
$fields = array();
foreach ($this->fields as $pos => $field) {
if ($aliases && isset($this->field_map[$field['#name']])) {
$field['#name'] = $this->field_map[$field['#name']];
}
$fq[$field['#name']][] = $this
->make_filter($field);
}
foreach ($this->subqueries as $id => $data) {
$subfq = $data['#query']
->rebuild_fq($aliases);
if ($subfq) {
$operator = $data['#fq_operator'];
$subqueries = array();
foreach ($subfq as $key => $values) {
foreach ($values as $value) {
$subqueries[] = $value;
}
}
$fq['subqueries'][$id] = " (" . implode(" {$operator} ", $subqueries) . ")";
}
}
return $fq;
}
protected function rebuild_query() {
$query = $this->keys;
foreach ($this->subqueries as $id => $data) {
$operator = $data['#q_operator'];
$subquery = $data['#query']
->get_query_basic();
if ($subquery) {
$query .= " {$operator} ({$subquery})";
}
}
return $query;
}
}