CirrusSearch
Elasticsearch-powered search for MediaWiki
Loading...
Searching...
No Matches
CirrusSearch\Util Class Reference

Random utility functions that don't have a better home. More...

Static Public Member Functions

static getNamespaceText (Title $title)
 Get the textual representation of a namespace with underscores stripped, varying by gender if need be (using Title::getNsText()).
 
static doPoolCounterWork ( $type, $user, $workCallback, $busyErrorMsg=null)
 Wraps the complex pool counter interface to force the single call pattern that Cirrus always uses.
 
static parsePotentialPercent ( $str)
 
static parseSettingsInMessage ( $message)
 Parse a message content into an array.
 
static overrideYesNo (&$dest, $request, $name)
 Set $dest to the true/false from $request->getVal( $name ) if yes/no.
 
static overrideNumeric (&$dest, $request, $name, $limit=null, $upperLimit=true)
 Set $dest to the numeric value from $request->getVal( $name ) if it is <= $limit or => $limit if upperLimit is false.
 
static getDefaultBoostTemplates (?SearchConfig $config=null)
 Get boost templates configured in messages.
 
static stripQuestionMarks ( $term, $strippingLevel)
 Strip question marks from queries, according to the defined stripping level, defined by $wgCirrusSearchStripQuestionMarks.
 
static getExecutionId ()
 Identifies a specific execution of php.
 
static resetExecutionId ()
 Unit tests only.
 
static getRequestSetToken ()
 Get a token that (hopefully) uniquely identifies this search.
 
static generateIdentToken ( $extraData='')
 
static getExecutionContext ()
 
static identifyNamespace ( $namespace, $method='naive', ?Language $language=null)
 Identify a namespace by attempting some unicode folding techniques.
 
static isEmpty ( $v)
 Helper for PHP's annoying emptiness check.
 
static setIfDefined (array $sourceArray, $sourceKey, array &$destArray, $destKey=null, $mapFn=null, $checkEmpty=true)
 Helper function to conditionally set a key in a dest array only if it is defined in a source array.
 
static getStatsFactory ()
 
static looksLikeAutomation (SearchConfig $config, string $ip, array $headers)
 
static processSearchRawReturn ( $result, WebRequest $request, CirrusDebugOptions $debugOptions)
 If we're supposed to create raw result, create and return it, or output it and finish.
 

Public Attributes

return $term
 

Detailed Description

Random utility functions that don't have a better home.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. http://www.gnu.org/copyleft/gpl.html

Member Function Documentation

◆ doPoolCounterWork()

static CirrusSearch\Util::doPoolCounterWork ( $type,
$user,
$workCallback,
$busyErrorMsg = null )
static

Wraps the complex pool counter interface to force the single call pattern that Cirrus always uses.

Parameters
string$typesame as type parameter on PoolCounter::factory
UserIdentity | null$user
callable$workCallbackcallback when pool counter is acquired. Called with no parameters.
string | null$busyErrorMsgThe i18n key to return when the queue is full, or null to use the default.
Returns
mixed

◆ generateIdentToken()

static CirrusSearch\Util::generateIdentToken ( $extraData = '')
static
Parameters
string$extraDataExtra information to mix into the hash
Returns
string A token that identifies the source of the request

◆ getDefaultBoostTemplates()

static CirrusSearch\Util::getDefaultBoostTemplates ( ?SearchConfig $config = null)
static

Get boost templates configured in messages.

Parameters
SearchConfig | null$configSearch config requesting the templates
Returns
float[]

◆ getExecutionContext()

static CirrusSearch\Util::getExecutionContext ( )
static
Returns
string The context the request is in. Either cli, api, web or misc.

◆ getExecutionId()

static CirrusSearch\Util::getExecutionId ( )
static

Identifies a specific execution of php.

That might be one web request, or multiple jobs run in the same executor. An execution id is valid over a brief timespan, perhaps a minute or two for some jobs.

Returns
string unique identifier

◆ getNamespaceText()

static CirrusSearch\Util::getNamespaceText ( Title $title)
static

Get the textual representation of a namespace with underscores stripped, varying by gender if need be (using Title::getNsText()).

Parameters
Title$titleThe page title to use
Returns
string|false

◆ getRequestSetToken()

static CirrusSearch\Util::getRequestSetToken ( )
static

Get a token that (hopefully) uniquely identifies this search.

It will be added to the search result page js config vars, and put into the url with history.replaceState(). This means click through's from supported browsers will record this token as part of the referrer.

Returns
string

◆ getStatsFactory()

static CirrusSearch\Util::getStatsFactory ( )
static
Returns
StatsFactory prefixed with the "CirrusSearch" component

◆ identifyNamespace()

static CirrusSearch\Util::identifyNamespace ( $namespace,
$method = 'naive',
?Language $language = null )
static

Identify a namespace by attempting some unicode folding techniques.

2 methods supported:

  • naive: case folding + naive accents removal (only some combined accents are removed)
  • utr30: (slow to load) case folding + strong accent squashing based on the withdrawn UTR30 specs all methods will apply something similar to near space flattener.
    Parameters
    string$namespacename of the namespace to identify
    string$methodeither naive or utr30
    Language | null$language
    Returns
    bool|int

◆ isEmpty()

static CirrusSearch\Util::isEmpty ( $v)
static

Helper for PHP's annoying emptiness check.

empty(0) should not be true! empty(false) should not be true! Empty arrays, strings, and nulls/undefined count as empty.

False otherwise.

Parameters
mixed$v
Returns
bool

◆ looksLikeAutomation()

static CirrusSearch\Util::looksLikeAutomation ( SearchConfig $config,
string $ip,
array $headers )
static
Parameters
SearchConfig$configConfiguration of the check
string$ipThe address to check against, ipv4 or ipv6.
string[]$headersMap from http header name to value. All names must be uppercased.
Returns
bool True when the parameters appear to be a non-interactive use case.

◆ overrideNumeric()

static CirrusSearch\Util::overrideNumeric ( & $dest,
$request,
$name,
$limit = null,
$upperLimit = true )
static

Set $dest to the numeric value from $request->getVal( $name ) if it is <= $limit or => $limit if upperLimit is false.

Parameters
mixed&$dest
WebRequest$request
string$name
int | null$limit
bool$upperLimit

◆ overrideYesNo()

static CirrusSearch\Util::overrideYesNo ( & $dest,
$request,
$name )
static

Set $dest to the true/false from $request->getVal( $name ) if yes/no.

Parameters
mixed&$dest
WebRequest$request
string$name

◆ parsePotentialPercent()

static CirrusSearch\Util::parsePotentialPercent ( $str)
static
Parameters
string$str
Returns
float

◆ parseSettingsInMessage()

static CirrusSearch\Util::parseSettingsInMessage ( $message)
static

Parse a message content into an array.

This function is generally used to parse settings stored as i18n messages (see cirrussearch-boost-templates).

Parameters
string$message
Returns
string[]

◆ processSearchRawReturn()

static CirrusSearch\Util::processSearchRawReturn ( $result,
WebRequest $request,
CirrusDebugOptions $debugOptions )
static

If we're supposed to create raw result, create and return it, or output it and finish.

Parameters
mixed$resultSearch result data
WebRequest$requestRequest context
CirrusDebugOptions$debugOptions
Returns
string The new raw result.

◆ setIfDefined()

static CirrusSearch\Util::setIfDefined ( array $sourceArray,
$sourceKey,
array & $destArray,
$destKey = null,
$mapFn = null,
$checkEmpty = true )
static

Helper function to conditionally set a key in a dest array only if it is defined in a source array.

This is just to help DRY up what would otherwise could be a long series of if ( isset($sourceArray[$key] )) { $destArray[$key] = $sourceArray[$key] } statements. This also supports using a different key in the dest array, as well as mapping the value when assigning to $sourceArray.

Usage: $arr1 = ['KEY1' => '123']; $arr2 = [];

setIfDefined($arr1, 'KEY1', $arr2, 'key1', 'intval'); // $arr2['key1'] is now set to 123 (integer value)

setIfDefined($arr1, 'KEY2', $arr2); // $arr2 stays the same, because $arr1 does not have 'KEY2' defined.

Parameters
array$sourceArraythe array from which to look for $sourceKey
string$sourceKeythe key to look for in $sourceArray
array&$destArrayby reference destination array in which to set value if defined
string | null$destKeyoptional, key to use instead of $sourceKey in $destArray.
callable | null$mapFnoptional, If set, this will be called on the value before setting it.
bool$checkEmptyIf false, emptyiness of result after $mapFn is called will not be checked before setting on $destArray. If true, it will, using Util::isEmpty. Default: true
Returns
array

◆ stripQuestionMarks()

static CirrusSearch\Util::stripQuestionMarks ( $term,
$strippingLevel )
static

Strip question marks from queries, according to the defined stripping level, defined by $wgCirrusSearchStripQuestionMarks.

Strip all ?s, those at word breaks, or only string-final. Ignore queries that are all punctuation or use insource. Don't remove escaped \?s, but unescape them.

Parameters
string$term
string$strippingLevelEither "all", "break", or "final"
Returns
string modified term, based on strippingLevel

The documentation for this class was generated from the following file: