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}]");
147 protected function _formatFts($text, $mode) {
150 // normalize user-inputted wildcards
151 $text = str_replace('%', '*', $text);
156 elseif (strpos($text, '*') !== FALSE) {
157 // if user supplies their own wildcards, then don't do any sophisticated changes
162 case self
::MODE_NONE
:
166 case self
::MODE_PHRASE
:
167 $result = '"' . $text . '"';
170 case self
::MODE_WILDPHRASE
:
171 $result = '"*' . $text . '*"';
174 case self
::MODE_WILDWORDS
:
175 $result = $this->mapWords($text, '*word*');
178 case self
::MODE_WILDWORDS_SUFFIX
:
179 $result = $this->mapWords($text, 'word*');
187 return $this->dedupeWildcards($result, '%');
190 protected function _formatFtsBool($text, $mode) {
193 // normalize user-inputted wildcards
194 $text = str_replace('%', '*', $text);
199 elseif (strpos($text, '+') !== FALSE ||
strpos($text, '-') !== FALSE) {
200 // if user supplies their own include/exclude operators, use text as is (with trailing wildcard)
201 $result = $this->mapWords($text, 'word*');
203 elseif (strpos($text, '*') !== FALSE) {
204 // if user supplies their own wildcards, then don't do any sophisticated changes
205 $result = $this->mapWords($text, '+word');
207 elseif (preg_match('/^(["\']).*\1$/m', $text)) {
208 // if surrounded by quotes, use term as is
213 case self
::MODE_NONE
:
214 $result = $this->mapWords($text, '+word');
217 case self
::MODE_PHRASE
:
218 $result = '+"' . $text . '"';
221 case self
::MODE_WILDPHRASE
:
222 $result = '+"*' . $text . '*"';
225 case self
::MODE_WILDWORDS
:
226 $result = $this->mapWords($text, '+*word*');
229 case self
::MODE_WILDWORDS_SUFFIX
:
230 $result = $this->mapWords($text, '+word*');
238 return $this->dedupeWildcards($result, '%');
241 protected function _formatLike($text, $mode) {
247 elseif (strpos($text, '%') !== FALSE) {
248 // if user supplies their own wildcards, then don't do any sophisticated changes
253 case self
::MODE_NONE
:
254 case self
::MODE_PHRASE
:
255 case self
::MODE_WILDPHRASE
:
256 $result = "%" . $text . "%";
259 case self
::MODE_WILDWORDS
:
260 case self
::MODE_WILDWORDS_SUFFIX
:
261 $result = "%" . preg_replace('/[ \r\n]+/', '%', $text) . '%';
269 return $this->dedupeWildcards($result, '%');
273 * @param string $text
274 * User-supplied query string.
275 * @param string $template
276 * A prototypical description of each word, eg "word%" or "word*" or "*word*".
279 protected function mapWords($text, $template) {
281 foreach ($this->parseWords($text) as $word) {
282 $result[] = str_replace('word', $word, $template);
284 return implode(' ', $result);
291 protected function parseWords($text) {
292 return explode(' ', preg_replace('/[ \r\n\t]+/', ' ', trim($text)));
300 protected function dedupeWildcards($text, $wildcard) {
301 if ($text === NULL) {
305 // don't use preg_replace because $wildcard might be special char
306 while (strpos($text, "{$wildcard}{$wildcard}") !== FALSE) {
307 $text = str_replace("{$wildcard}{$wildcard}", "{$wildcard}", $text);
312 public static function getModes() {
316 self
::MODE_WILDPHRASE
,
317 self
::MODE_WILDWORDS
,
318 self
::MODE_WILDWORDS_SUFFIX
,
322 public static function getLanguages() {
326 self
::LANG_SQL_FTSBOOL
,
334 * Ex: drush eval 'civicrm_initialize(); CRM_Utils_QueryFormatter::dumpExampleTable("firstword secondword");'
336 public static function dumpExampleTable($text) {
337 $width = strlen($text) +
8;
340 $buf .= sprintf("%-{$width}s", 'mode');
341 foreach (self
::getLanguages() as $lang) {
342 $buf .= sprintf("%-{$width}s", $lang);
346 foreach (self
::getModes() as $mode) {
347 $formatter = new CRM_Utils_QueryFormatter($mode);
348 $buf .= sprintf("%-{$width}s", $mode);
349 foreach (self
::getLanguages() as $lang) {
350 $buf .= sprintf("%-{$width}s", $formatter->format($text, $lang));