MediaWiki REL1_39
MagicWord Class Reference

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

Public Member Functions

 __construct ( $id=null, $syn=[], $cs=false, Language $contLang=null)
 #-
 
 addToArray (&$array, $value)
 Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words.
 
 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.
 
 getBaseRegex ()
 regex without the slashes and what not
 
 getId ()
 
 getRegex ()
 Gets a regex representing matching the word.
 
 getRegexCase ()
 Gets the regexp case modifier to use, i.e.
 
 getRegexStart ()
 Gets a regex matching the word, if it is at the string start.
 
 getRegexStartToEnd ()
 Gets a regex matching the word from start to end of a string.
 
 getSynonym ( $i)
 Accesses the synonym list directly.
 
 getSynonyms ()
 
 getVariableRegex ()
 Matches the word, where $1 is a wildcard.
 
 getVariableStartToEndRegex ()
 Matches the entire string, where $1 is a wildcard.
 
 getWasModified ()
 Returns true if the last call to replace() or substituteCallback() returned a modified text, otherwise false.
 
 initRegex ()
 Preliminary initialisation.
 
 isCaseSensitive ()
 
 load ( $id)
 Initialises this object with an ID.
 
 match ( $text)
 Returns true if the text contains the word.
 
 matchAndRemove (&$text)
 Returns true if the text matches the word, and alters the input string, removing all instances of the word.
 
 matchStart ( $text)
 Returns true if the text starts with the word.
 
 matchStartAndRemove (&$text)
 
 matchStartToEnd ( $text)
 Returns true if the text matched the word.
 
 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.
 
 pregRemoveAndRecord ()
 Used in matchAndRemove()
 
 replace ( $replacement, $subject, $limit=-1)
 Replaces the word with something else.
 
 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.
 

Public Attributes

bool $mCaseSensitive
 
string $mId
 #-
 
string[] $mSynonyms
 

Detailed Description

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

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

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:
$magicWords['en'] = [
'magicwordkey' => [ 0, 'case_insensitive_magic_word' ],
'magicwordkey2' => [ 1, 'CASE_sensitive_magic_word2' ],
];
$magicWords
@phpcs-require-sorted-array

For magic words which name Parser double underscore names, add a GetDoubleUnderscoreIDs hook. Use string keys.

For magic words which name Parser magic variables, add a GetMagicVariableIDs hook. Use string keys.

Definition at line 60 of file MagicWord.php.

Constructor & Destructor Documentation

◆ __construct()

MagicWord::__construct ( $id = null,
$syn = [],
$cs = false,
Language $contLang = null )

#-

Create a new MagicWord object

Use factory instead: MagicWordFactory::get

Parameters
string | null$idThe internal name of the magic word
string[] | string$synsynonyms for the magic word
bool$csIf magic word is case sensitive
Language | null$contLangContent language

Definition at line 111 of file MagicWord.php.

Member Function Documentation

◆ addToArray()

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
string[]&$array
string$value

Definition at line 443 of file MagicWord.php.

◆ compareStringLength()

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 169 of file MagicWord.php.

◆ getBaseRegex()

MagicWord::getBaseRegex ( )

regex without the slashes and what not

Returns
string

Definition at line 232 of file MagicWord.php.

References initRegex().

◆ getId()

MagicWord::getId ( )
Returns
string

Definition at line 459 of file MagicWord.php.

References $mId.

◆ getRegex()

MagicWord::getRegex ( )

Gets a regex representing matching the word.

Returns
string

Definition at line 180 of file MagicWord.php.

References initRegex().

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

◆ getRegexCase()

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 194 of file MagicWord.php.

References initRegex().

◆ getRegexStart()

MagicWord::getRegexStart ( )

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

Returns
string

Definition at line 207 of file MagicWord.php.

References initRegex().

Referenced by matchStart(), and matchStartAndRemove().

◆ getRegexStartToEnd()

MagicWord::getRegexStartToEnd ( )

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

Returns
string
Since
1.23

Definition at line 220 of file MagicWord.php.

References initRegex().

Referenced by matchStartToEnd().

◆ getSynonym()

MagicWord::getSynonym ( $i)

Accesses the synonym list directly.

Parameters
int$i
Returns
string

Definition at line 415 of file MagicWord.php.

◆ getSynonyms()

MagicWord::getSynonyms ( )
Returns
string[]

Definition at line 422 of file MagicWord.php.

References $mSynonyms.

◆ getVariableRegex()

MagicWord::getVariableRegex ( )

Matches the word, where $1 is a wildcard.

Returns
string

Definition at line 389 of file MagicWord.php.

References initRegex().

Referenced by substituteCallback().

◆ getVariableStartToEndRegex()

MagicWord::getVariableStartToEndRegex ( )

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

Returns
string

Definition at line 401 of file MagicWord.php.

References initRegex().

Referenced by matchVariableStartToEnd().

◆ getWasModified()

MagicWord::getWasModified ( )

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

Returns
bool

Definition at line 432 of file MagicWord.php.

◆ initRegex()

MagicWord::initRegex ( )

Preliminary initialisation.

Access: internal

Definition at line 137 of file MagicWord.php.

References $mSynonyms.

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

◆ isCaseSensitive()

MagicWord::isCaseSensitive ( )
Returns
bool

Definition at line 452 of file MagicWord.php.

References $mCaseSensitive.

◆ load()

MagicWord::load ( $id)

Initialises this object with an ID.

Parameters
string$id
Exceptions
MWException

Definition at line 124 of file MagicWord.php.

◆ match()

MagicWord::match ( $text)

Returns true if the text contains the word.

Parameters
string$text
Returns
bool

Definition at line 246 of file MagicWord.php.

References getRegex().

Referenced by WikitextContent\matchMagicWord().

◆ matchAndRemove()

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 312 of file MagicWord.php.

References getRegex().

◆ matchStart()

MagicWord::matchStart ( $text)

Returns true if the text starts with the word.

Parameters
string$text
Returns
bool

Definition at line 257 of file MagicWord.php.

References getRegexStart().

◆ matchStartAndRemove()

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

Definition at line 327 of file MagicWord.php.

References getRegexStart().

◆ matchStartToEnd()

MagicWord::matchStartToEnd ( $text)

Returns true if the text matched the word.

Parameters
string$text
Returns
bool
Since
1.23

Definition at line 269 of file MagicWord.php.

References getRegexStartToEnd().

◆ matchVariableStartToEnd()

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|null

Definition at line 283 of file MagicWord.php.

References $matches, and getVariableStartToEndRegex().

◆ pregRemoveAndRecord()

MagicWord::pregRemoveAndRecord ( )

Used in matchAndRemove()

Returns
string

Definition at line 343 of file MagicWord.php.

◆ replace()

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

Replaces the word with something else.

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

Definition at line 357 of file MagicWord.php.

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

◆ substituteCallback()

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 378 of file MagicWord.php.

References $res, and getVariableRegex().

Member Data Documentation

◆ $mCaseSensitive

bool MagicWord::$mCaseSensitive

Definition at line 70 of file MagicWord.php.

Referenced by isCaseSensitive().

◆ $mId

string MagicWord::$mId

#-

Definition at line 64 of file MagicWord.php.

Referenced by getId().

◆ $mSynonyms

string [] MagicWord::$mSynonyms

Definition at line 67 of file MagicWord.php.

Referenced by getSynonyms(), and initRegex().


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