MediaWiki  1.27.1
MagicWord Class Reference

This class encapsulates "magic words" such as "#redirect", NOTOC, etc. More...

Collaboration diagram for MagicWord:

Public Member Functions

 __construct ($id=0, $syn=[], $cs=false)
 #@- More...
 
 addToArray (&$array, $value)
 Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words. More...
 
 compareStringLength ($s1, $s2)
 A comparison function that returns -1, 0 or 1 depending on whether the first string is longer, the same length or shorter than the second string. More...
 
 getBaseRegex ()
 regex without the slashes and what not More...
 
 getId ()
 
 getRegex ()
 Gets a regex representing matching the word. More...
 
 getRegexCase ()
 Gets the regexp case modifier to use, i.e. More...
 
 getRegexStart ()
 Gets a regex matching the word, if it is at the string start. More...
 
 getRegexStartToEnd ()
 Gets a regex matching the word from start to end of a string. More...
 
 getSynonym ($i)
 Accesses the synonym list directly. More...
 
 getSynonyms ()
 
 getVariableRegex ()
 Matches the word, where $1 is a wildcard. More...
 
 getVariableStartToEndRegex ()
 Matches the entire string, where $1 is a wildcard. More...
 
 getWasModified ()
 Returns true if the last call to replace() or substituteCallback() returned a modified text, otherwise false. More...
 
 initRegex ()
 Preliminary initialisation. More...
 
 isCaseSensitive ()
 
 load ($id)
 Initialises this object with an ID. More...
 
 match ($text)
 Returns true if the text contains the word. More...
 
 matchAndRemove (&$text)
 Returns true if the text matches the word, and alters the input string, removing all instances of the word. More...
 
 matchStart ($text)
 Returns true if the text starts with the word. More...
 
 matchStartAndRemove (&$text)
 
 matchStartToEnd ($text)
 Returns true if the text matched the word. More...
 
 matchVariableStartToEnd ($text)
 Returns NULL if there's no match, the value of $1 otherwise The return code is the matched string, if there's no variable part in the regex and the matched variable part ($1) if there is one. More...
 
 pregRemoveAndRecord ()
 Used in matchAndRemove() More...
 
 replace ($replacement, $subject, $limit=-1)
 Replaces the word with something else. More...
 
 replaceMultiple ($magicarr, $subject, &$result)
 $magicarr is an associative array of (magic word ID => replacement) This method uses the php feature to do several replacements at the same time, thereby gaining some efficiency. More...
 
 substituteCallback ($text, $callback)
 Variable handling: {{SUBST:xxx}} style words Calls back a function to determine what to replace xxx with Input word must contain $1. More...
 

Static Public Member Functions

static clearCache ()
 Clear the self::$mObjects variable For use in parser tests. More...
 
static & get ($id)
 Factory: creates an object representing an ID. More...
 
static getCacheTTL ($id)
 Allow external reads of TTL array. More...
 
static getDoubleUnderscoreArray ()
 Get a MagicWordArray of double-underscore entities. More...
 
static getSubstIDs ()
 Get an array of parser substitution modifier IDs. More...
 
static getVariableIDs ()
 Get an array of parser variable IDs. More...
 

Public Attributes

bool $mCaseSensitive
 
int $mId
 #@- More...
 
array $mSynonyms
 

Static Public Attributes

static $mCacheTTLs
 
static $mDoubleUnderscoreArray = null
 
static static $mDoubleUnderscoreIDs
 
static $mObjects = []
 
static $mSubstIDs
 
static $mVariableIDs
 
static $mVariableIDsInitialised = false
 

Private Attributes

string $mBaseRegex = ''
 
bool $mFound = false
 
bool $mModified = false
 
string $mRegex = ''
 
string $mRegexStart = ''
 
string $mRegexStartToEnd = ''
 
string $mVariableRegex = ''
 
string $mVariableStartToEndRegex = ''
 

Detailed Description

This class encapsulates "magic words" such as "#redirect", NOTOC, etc.

Usage:
if (MagicWord::get( 'redirect' )->match( $text ) ) {
// some code
}

Possible future improvements:

Please avoid reading the data out of one of these objects and then writing special case code. If possible, add another match()-like function here.

To add magic words in an extension, use $magicWords in a file listed in $wgExtensionMessagesFiles[].

Example:
'magicwordkey' => array( 0, 'case_insensitive_magic_word' ),
'magicwordkey2' => array( 1, 'CASE_sensitive_magic_word2' ),
);

For magic words which are also Parser variables, add a MagicWordwgVariableIDs hook. Use string keys.

Definition at line 59 of file MagicWord.php.

Constructor & Destructor Documentation

MagicWord::__construct (   $id = 0,
  $syn = [],
  $cs = false 
)

#@-

Definition at line 244 of file MagicWord.php.

References array().

Member Function Documentation

MagicWord::addToArray ( $array,
  $value 
)

Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words.

Parameters
array$array
string$value

Definition at line 682 of file MagicWord.php.

References $value, $wgContLang, as, and global.

static MagicWord::clearCache ( )
static

Clear the self::$mObjects variable For use in parser tests.

Definition at line 319 of file MagicWord.php.

Referenced by ExtraParserTest\setUp(), NewParserTest\setupGlobals(), ParserTest\setupGlobals(), and WikitextContentHandlerTest\testMakeRedirectContent().

MagicWord::compareStringLength (   $s1,
  $s2 
)

A comparison function that returns -1, 0 or 1 depending on whether the first string is longer, the same length or shorter than the second string.

Parameters
string$s1
string$s2
Returns
int

Definition at line 375 of file MagicWord.php.

MagicWord::getBaseRegex ( )

regex without the slashes and what not

Returns
string

Definition at line 444 of file MagicWord.php.

References $mBaseRegex, and initRegex().

static MagicWord::getCacheTTL (   $id)
static

Allow external reads of TTL array.

Parameters
int$id
Returns
int

Definition at line 294 of file MagicWord.php.

Referenced by Parser\braceSubstitution().

static MagicWord::getDoubleUnderscoreArray ( )
static

Get a MagicWordArray of double-underscore entities.

Returns
MagicWordArray

Definition at line 307 of file MagicWord.php.

References Hooks\run().

Referenced by Parser\doDoubleUnderscore(), and InfoAction\pageInfo().

MagicWord::getId ( )
Returns
int

Definition at line 699 of file MagicWord.php.

References $mId.

MagicWord::getRegex ( )

Gets a regex representing matching the word.

Returns
string

Definition at line 392 of file MagicWord.php.

References $mRegex, and initRegex().

Referenced by match(), matchAndRemove(), and replace().

MagicWord::getRegexCase ( )

Gets the regexp case modifier to use, i.e.

i or nothing, to be used if one is using MagicWord::getBaseRegex(), otherwise it'll be included in the complete expression

Returns
string

Definition at line 406 of file MagicWord.php.

References initRegex().

MagicWord::getRegexStart ( )

Gets a regex matching the word, if it is at the string start.

Returns
string

Definition at line 419 of file MagicWord.php.

References $mRegexStart, and initRegex().

Referenced by matchStart(), and matchStartAndRemove().

MagicWord::getRegexStartToEnd ( )

Gets a regex matching the word from start to end of a string.

Returns
string
Since
1.23

Definition at line 432 of file MagicWord.php.

References $mRegexStartToEnd, and initRegex().

Referenced by matchStartToEnd().

static MagicWord::getSubstIDs ( )
static

Get an array of parser substitution modifier IDs.

Returns
array

Definition at line 284 of file MagicWord.php.

Referenced by Parser\initialiseVariables().

MagicWord::getSynonym (   $i)

Accesses the synonym list directly.

Parameters
int$i
Returns
string

Definition at line 627 of file MagicWord.php.

MagicWord::getSynonyms ( )
Returns
array

Definition at line 634 of file MagicWord.php.

References $mSynonyms.

static MagicWord::getVariableIDs ( )
static

Get an array of parser variable IDs.

Returns
array

Definition at line 271 of file MagicWord.php.

References Hooks\run().

Referenced by ApiQuerySiteinfo\appendVariables(), and Parser\initialiseVariables().

MagicWord::getVariableRegex ( )

Matches the word, where $1 is a wildcard.

Returns
string

Definition at line 601 of file MagicWord.php.

References $mVariableRegex, and initRegex().

Referenced by substituteCallback().

MagicWord::getVariableStartToEndRegex ( )

Matches the entire string, where $1 is a wildcard.

Returns
string

Definition at line 613 of file MagicWord.php.

References $mVariableStartToEndRegex, and initRegex().

Referenced by matchVariableStartToEnd().

MagicWord::getWasModified ( )

Returns true if the last call to replace() or substituteCallback() returned a modified text, otherwise false.

Returns
bool

Definition at line 644 of file MagicWord.php.

References $mModified.

MagicWord::initRegex ( )

Preliminary initialisation.

Access:
private

Definition at line 343 of file MagicWord.php.

References $mSynonyms, and as.

Referenced by getBaseRegex(), getRegex(), getRegexCase(), getRegexStart(), getRegexStartToEnd(), getVariableRegex(), and getVariableStartToEndRegex().

MagicWord::isCaseSensitive ( )
Returns
bool

Definition at line 692 of file MagicWord.php.

References $mCaseSensitive.

MagicWord::load (   $id)

Initialises this object with an ID.

Parameters
int$id
Exceptions
MWException

Definition at line 329 of file MagicWord.php.

References $wgContLang, and global.

MagicWord::match (   $text)

Returns true if the text contains the word.

Parameters
string$text
Returns
bool

Definition at line 458 of file MagicWord.php.

References getRegex().

Referenced by WikitextContent\matchMagicWord().

MagicWord::matchAndRemove ( $text)

Returns true if the text matches the word, and alters the input string, removing all instances of the word.

Parameters
string$text
Returns
bool

Definition at line 524 of file MagicWord.php.

References $mFound, and getRegex().

MagicWord::matchStart (   $text)

Returns true if the text starts with the word.

Parameters
string$text
Returns
bool

Definition at line 469 of file MagicWord.php.

References getRegexStart().

MagicWord::matchStartAndRemove ( $text)
Parameters
string$text
Returns
bool

Definition at line 539 of file MagicWord.php.

References $mFound, and getRegexStart().

MagicWord::matchStartToEnd (   $text)

Returns true if the text matched the word.

Parameters
string$text
Returns
bool
Since
1.23

Definition at line 481 of file MagicWord.php.

References getRegexStartToEnd().

MagicWord::matchVariableStartToEnd (   $text)

Returns NULL if there's no match, the value of $1 otherwise The return code is the matched string, if there's no variable part in the regex and the matched variable part ($1) if there is one.

Parameters
string$text
Returns
string

Definition at line 495 of file MagicWord.php.

References $matches, and getVariableStartToEndRegex().

MagicWord::pregRemoveAndRecord ( )

Used in matchAndRemove()

Returns
string

Definition at line 555 of file MagicWord.php.

MagicWord::replace (   $replacement,
  $subject,
  $limit = -1 
)

Replaces the word with something else.

Parameters
string$replacement
string$subject
int$limit
Returns
string

Definition at line 569 of file MagicWord.php.

References $limit, $res, StringUtils\escapeRegexReplacement(), and getRegex().

MagicWord::replaceMultiple (   $magicarr,
  $subject,
$result 
)

$magicarr is an associative array of (magic word ID => replacement) This method uses the php feature to do several replacements at the same time, thereby gaining some efficiency.

The result is placed in the out variable $result. The return value is true if something was replaced.

Deprecated:
since 1.25, unused
Parameters
array$magicarr
string$subject
string$result
Returns
bool

Definition at line 661 of file MagicWord.php.

References as, get(), and wfDeprecated().

MagicWord::substituteCallback (   $text,
  $callback 
)

Variable handling: {{SUBST:xxx}} style words Calls back a function to determine what to replace xxx with Input word must contain $1.

Parameters
string$text
callable$callback
Returns
string

Definition at line 590 of file MagicWord.php.

References $res, and getVariableRegex().

Member Data Documentation

string MagicWord::$mBaseRegex = ''
private

Definition at line 81 of file MagicWord.php.

Referenced by getBaseRegex().

MagicWord::$mCacheTTLs
static
Initial value:
= [
'currentmonth' => 86400

Definition at line 177 of file MagicWord.php.

bool MagicWord::$mCaseSensitive

Definition at line 69 of file MagicWord.php.

Referenced by isCaseSensitive().

MagicWord::$mDoubleUnderscoreArray = null
static

Definition at line 240 of file MagicWord.php.

static MagicWord::$mDoubleUnderscoreIDs
static
Initial value:
= [
'notoc',
'nogallery',
'forcetoc',
'toc',
'noeditsection',
'newsectionlink',
'nonewsectionlink',
'hiddencat',
'index',
'noindex',
'staticredirect',
'notitleconvert',
'nocontentconvert',
]

Definition at line 218 of file MagicWord.php.

bool MagicWord::$mFound = false
private

Definition at line 93 of file MagicWord.php.

Referenced by matchAndRemove(), and matchStartAndRemove().

int MagicWord::$mId

#@-

Definition at line 63 of file MagicWord.php.

Referenced by getId().

bool MagicWord::$mModified = false
private

Definition at line 90 of file MagicWord.php.

Referenced by getWasModified().

MagicWord::$mObjects = []
static

Definition at line 239 of file MagicWord.php.

string MagicWord::$mRegex = ''
private

Definition at line 72 of file MagicWord.php.

Referenced by getRegex().

string MagicWord::$mRegexStart = ''
private

Definition at line 75 of file MagicWord.php.

Referenced by getRegexStart().

string MagicWord::$mRegexStartToEnd = ''
private

Definition at line 78 of file MagicWord.php.

Referenced by getRegexStartToEnd().

MagicWord::$mSubstIDs
static
Initial value:
= [
'subst',
'safesubst',
]

Definition at line 234 of file MagicWord.php.

array MagicWord::$mSynonyms

Definition at line 66 of file MagicWord.php.

Referenced by getSynonyms(), and initRegex().

MagicWord::$mVariableIDs
static

Definition at line 96 of file MagicWord.php.

MagicWord::$mVariableIDsInitialised = false
static

Definition at line 95 of file MagicWord.php.

string MagicWord::$mVariableRegex = ''
private

Definition at line 84 of file MagicWord.php.

Referenced by getVariableRegex().

string MagicWord::$mVariableStartToEndRegex = ''
private

Definition at line 87 of file MagicWord.php.

Referenced by getVariableStartToEndRegex().


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