class CRM_Utils_QueryFormatter

Class CRM_Utils_QueryFormatter

This class is a bad idea. It exists for the unholy reason that a single installation may have up to three query engines (MySQL LIKE, MySQL FTS, Solr) processing the same query-text. It labors* to take the user's search expression and provide similar search semantics in different contexts. It is unknown whether this labor will be fruitful or in vain.

Constants

LANG_SQL_LIKE

Generate queries using SQL LIKE expressions.

LANG_SQL_FTS

Generate queries using MySQL FTS expressions.

LANG_SQL_FTSBOOL

Generate queries using MySQL's boolean FTS expressions.

LANG_SOLR

Generate queries using Solr expressions.

MODE_NONE

Attempt to leave the text as-is.

MODE_PHRASE

Attempt to treat the input text as a phrase

MODE_WILDPHRASE

Attempt to treat the input text as a phrase with wildcards on each end.

MODE_WILDWORDS

Attempt to treat individual word as if it had wildcards at the start and end.

MODE_WILDWORDS_SUFFIX

Attempt to treat individual word as if it had a wildcard at the end.

Properties

static protected CRM_Utils_QueryFormatter|null $singleton Singleton object.
protected string $mode

Methods

singleton(bool $fresh = FALSE)

No description

__construct(string $mode)

No description

setMode(mixed $mode)

No description

mixed
getMode()

No description

string
format(string $text, string $language)

No description

string
formatSql(string $table, array|string $columns, string $queryText)

Create a SQL WHERE expression for matching against a list of text columns.

mixed
_formatFts(string $text, $mode)

Format Fts.

mixed
_formatFtsBool(string $text, $mode)

Format FTS.

mixed
_formatLike($text, $mode)

Format like.

string
mapWords(string $text, string $template, bool $quotes = FALSE)

No description

array
parseWords(string $text, bool $quotes)

No description

mixed
dedupeWildcards($text, $wildcard)

No description

static array
getModes()

Get modes.

static array
getLanguages()

Get languages.

static 
dumpExampleTable($text)

No description

Details

at line 102
static CRM_Utils_QueryFormatter singleton(bool $fresh = FALSE)

Parameters

bool $fresh

Return Value

CRM_Utils_QueryFormatter

at line 120
__construct(string $mode)

Parameters

string $mode Eg MODE_NONE.

at line 127
setMode(mixed $mode)

Parameters

mixed $mode

at line 134
mixed getMode()

Return Value

mixed

at line 145
string format(string $text, string $language)

Parameters

string $text
string $language Eg LANG_SQL_LIKE, LANG_SQL_FTS, LANG_SOLR.

Return Value

string

Exceptions

CRM_Core_Exception

at line 186
string formatSql(string $table, array|string $columns, string $queryText)

Create a SQL WHERE expression for matching against a list of text columns.

Parameters

string $table Eg "civicrm_note" or "civicrm_note mynote".
array|string $columns List of columns to search against. Eg "first_name" or "activity_details".
string $queryText

Return Value

string SQL, eg "MATCH (col1) AGAINST (queryText)" or "col1 LIKE '%queryText%'"

at line 242
protected mixed _formatFts(string $text, $mode)

Format Fts.

Parameters

string $text
$mode

Return Value

mixed

at line 293
protected mixed _formatFtsBool(string $text, $mode)

Format FTS.

Parameters

string $text
$mode

Return Value

mixed

at line 366
protected mixed _formatLike($text, $mode)

Format like.

Parameters

$text
$mode

Return Value

mixed

at line 406
protected string mapWords(string $text, string $template, bool $quotes = FALSE)

Parameters

string $text User-supplied query string.
string $template A prototypical description of each word, eg "word%" or "word" or "word*".
bool $quotes True if each searched keyword need to be surrounded with quotes.

Return Value

string

at line 419
protected array parseWords(string $text, bool $quotes)

Parameters

string $text
bool $quotes

Return Value

array

at line 448
protected mixed dedupeWildcards($text, $wildcard)

Parameters

$text
$wildcard

Return Value

mixed

at line 465
static array getModes()

Get modes.

Return Value

array

at line 480
static array getLanguages()

Get languages.

Return Value

array

at line 494
static dumpExampleTable($text)

Parameters

$text Ex: drush eval 'civicrm_initialize(); CRM_Utils_QueryFormatter::dumpExampleTable("firstword secondword");'