3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Class CRM_Utils_QueryFormatter
31 * This class is a bad idea. It exists for the unholy reason that a single installation
32 * may have up to three query engines (MySQL LIKE, MySQL FTS, Solr) processing the same
33 * query-text. It labors* to take the user's search expression and provide similar search
34 * semantics in different contexts. It is unknown whether this labor will be fruitful
37 class CRM_Utils_QueryFormatter
{
38 const LANG_SQL_LIKE
= 'like';
39 const LANG_SQL_FTS
= 'fts';
40 const LANG_SQL_FTSBOOL
= 'ftsbool';
41 const LANG_SOLR
= 'solr';
44 * Attempt to leave the text as-is.
46 const MODE_NONE
= 'simple';
49 * Attempt to treat the input text as a phrase
51 const MODE_PHRASE
= 'phrase';
54 * Attempt to treat the input text as a phrase with
55 * wildcards on each end.
57 const MODE_WILDPHRASE
= 'wildphrase';
60 * Attempt to treat individual word as if it
61 * had wildcards at the start and end.
63 const MODE_WILDWORDS
= 'wildwords';
66 * Attempt to treat individual word as if it
67 * had a wildcard at the end.
69 const MODE_WILDWORDS_SUFFIX
= 'wildwords-suffix';
71 static protected $singleton;
75 * @return CRM_Utils_QueryFormatter
77 public static function singleton($fresh = FALSE) {
78 if ($fresh || self
::$singleton === NULL) {
79 $mode = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SEARCH_PREFERENCES_NAME
, 'fts_query_mode', NULL, self
::MODE_NONE
);
80 self
::$singleton = new CRM_Utils_QueryFormatter($mode);
82 return self
::$singleton;
86 * @var string eg MODE_NONE
94 public function __construct($mode) {
101 public function setMode($mode) {
108 public function getMode() {
113 * @param string $text
114 * @param string $language
115 * Eg LANG_SQL_LIKE, LANG_SQL_FTS, LANG_SOLR.
116 * @throws CRM_Core_Exception
119 public function format($text, $language) {
123 case self
::LANG_SOLR
:
124 case self
::LANG_SQL_FTS
:
125 $text = $this->_formatFts($text, $this->mode
);
128 case self
::LANG_SQL_FTSBOOL
:
129 $text = $this->_formatFtsBool($text, $this->mode
);
132 case self
::LANG_SQL_LIKE
:
133 $text = $this->_formatLike($text, $this->mode
);
140 if ($text === NULL) {
141 throw new CRM_Core_Exception("Unrecognized combination: language=[{$language}] mode=[{$this->mode}]");
150 * @param string $text
155 protected function _formatFts($text, $mode) {
158 // normalize user-inputted wildcards
159 $text = str_replace('%', '*', $text);
164 elseif (strpos($text, '*') !== FALSE) {
165 // if user supplies their own wildcards, then don't do any sophisticated changes
170 case self
::MODE_NONE
:
174 case self
::MODE_PHRASE
:
175 $result = '"' . $text . '"';
178 case self
::MODE_WILDPHRASE
:
179 $result = '"*' . $text . '*"';
182 case self
::MODE_WILDWORDS
:
183 $result = $this->mapWords($text, '*word*');
186 case self
::MODE_WILDWORDS_SUFFIX
:
187 $result = $this->mapWords($text, 'word*');
195 return $this->dedupeWildcards($result, '%');
201 * @param string $text
206 protected function _formatFtsBool($text, $mode) {
209 // normalize user-inputted wildcards
210 $text = str_replace('%', '*', $text);
215 elseif (strpos($text, '+') !== FALSE ||
strpos($text, '-') !== FALSE) {
216 // if user supplies their own include/exclude operators, use text as is (with trailing wildcard)
217 $result = $this->mapWords($text, 'word*');
219 elseif (strpos($text, '*') !== FALSE) {
220 // if user supplies their own wildcards, then don't do any sophisticated changes
221 $result = $this->mapWords($text, '+word');
223 elseif (preg_match('/^(["\']).*\1$/m', $text)) {
224 // if surrounded by quotes, use term as is
229 case self
::MODE_NONE
:
230 $result = $this->mapWords($text, '+word');
233 case self
::MODE_PHRASE
:
234 $result = '+"' . $text . '"';
237 case self
::MODE_WILDPHRASE
:
238 $result = '+"*' . $text . '*"';
241 case self
::MODE_WILDWORDS
:
242 $result = $this->mapWords($text, '+*word*');
245 case self
::MODE_WILDWORDS_SUFFIX
:
246 $result = $this->mapWords($text, '+word*');
254 return $this->dedupeWildcards($result, '%');
265 protected function _formatLike($text, $mode) {
271 elseif (strpos($text, '%') !== FALSE) {
272 // if user supplies their own wildcards, then don't do any sophisticated changes
277 case self
::MODE_NONE
:
278 case self
::MODE_PHRASE
:
279 case self
::MODE_WILDPHRASE
:
280 $result = "%" . $text . "%";
283 case self
::MODE_WILDWORDS
:
284 case self
::MODE_WILDWORDS_SUFFIX
:
285 $result = "%" . preg_replace('/[ \r\n]+/', '%', $text) . '%';
293 return $this->dedupeWildcards($result, '%');
297 * @param string $text
298 * User-supplied query string.
299 * @param string $template
300 * A prototypical description of each word, eg "word%" or "word*" or "*word*".
303 protected function mapWords($text, $template) {
305 foreach ($this->parseWords($text) as $word) {
306 $result[] = str_replace('word', $word, $template);
308 return implode(' ', $result);
315 protected function parseWords($text) {
316 return explode(' ', preg_replace('/[ \r\n\t]+/', ' ', trim($text)));
324 protected function dedupeWildcards($text, $wildcard) {
325 if ($text === NULL) {
329 // don't use preg_replace because $wildcard might be special char
330 while (strpos($text, "{$wildcard}{$wildcard}") !== FALSE) {
331 $text = str_replace("{$wildcard}{$wildcard}", "{$wildcard}", $text);
341 public static function getModes() {
345 self
::MODE_WILDPHRASE
,
346 self
::MODE_WILDWORDS
,
347 self
::MODE_WILDWORDS_SUFFIX
,
356 public static function getLanguages() {
360 self
::LANG_SQL_FTSBOOL
,
368 * Ex: drush eval 'civicrm_initialize(); CRM_Utils_QueryFormatter::dumpExampleTable("firstword secondword");'
370 public static function dumpExampleTable($text) {
371 $width = strlen($text) +
8;
374 $buf .= sprintf("%-{$width}s", 'mode');
375 foreach (self
::getLanguages() as $lang) {
376 $buf .= sprintf("%-{$width}s", $lang);
380 foreach (self
::getModes() as $mode) {
381 $formatter = new CRM_Utils_QueryFormatter($mode);
382 $buf .= sprintf("%-{$width}s", $mode);
383 foreach (self
::getLanguages() as $lang) {
384 $buf .= sprintf("%-{$width}s", $formatter->format($text, $lang));