MediaWiki  1.33.0
MagicWord Class Reference

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

Collaboration diagram for MagicWord:

Public Member Functions

 __construct ( $id=null, $syn=[], $cs=false, Language $contLang=null)
 #- 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...
 
 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 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
 
string $mId
 #- More...
 
string[] $mSynonyms
 

Private Attributes

Language $contLang
 
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 ( $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' ],
];

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

Definition at line 57 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 108 of file MagicWord.php.

References $contLang, and array().

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

References $value, and as.

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

◆ get()

static MagicWord::get (   $id)
static

Factory: creates an object representing an ID.

Parameters
string$idThe internal name of the magic word
Returns
MagicWord
Deprecated:
since 1.32, use MagicWordFactory::get

Definition at line 127 of file MagicWord.php.

References wfDeprecated().

Referenced by WikiEditorHooks\getMagicWords().

◆ getBaseRegex()

MagicWord::getBaseRegex ( )

regex without the slashes and what not

Returns
string

Definition at line 290 of file MagicWord.php.

References $mBaseRegex, and initRegex().

◆ getCacheTTL()

static MagicWord::getCacheTTL (   $id)
static

Allow external reads of TTL array.

Parameters
string$id
Returns
int
Deprecated:
since 1.32, use MagicWordFactory::getCacheTTL

Definition at line 160 of file MagicWord.php.

References wfDeprecated().

◆ getDoubleUnderscoreArray()

static MagicWord::getDoubleUnderscoreArray ( )
static

Get a MagicWordArray of double-underscore entities.

Returns
MagicWordArray
Deprecated:
since 1.32, use MagicWordFactory::getDoubleUnderscoreArray

Definition at line 171 of file MagicWord.php.

References wfDeprecated().

◆ getId()

MagicWord::getId ( )
Returns
string

Definition at line 517 of file MagicWord.php.

References $mId.

◆ getRegex()

MagicWord::getRegex ( )

Gets a regex representing matching the word.

Returns
string

Definition at line 238 of file MagicWord.php.

References $mRegex, and 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 252 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 265 of file MagicWord.php.

References $mRegexStart, and 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 278 of file MagicWord.php.

References $mRegexStartToEnd, and initRegex().

Referenced by matchStartToEnd().

◆ getSubstIDs()

static MagicWord::getSubstIDs ( )
static

Get an array of parser substitution modifier IDs.

Returns
string[]
Deprecated:
since 1.32, use MagicWordFactory::getSubstIDs

Definition at line 148 of file MagicWord.php.

References wfDeprecated().

◆ getSynonym()

MagicWord::getSynonym (   $i)

Accesses the synonym list directly.

Parameters
int$i
Returns
string

Definition at line 473 of file MagicWord.php.

◆ getSynonyms()

MagicWord::getSynonyms ( )
Returns
string[]

Definition at line 480 of file MagicWord.php.

References $mSynonyms.

◆ getVariableIDs()

static MagicWord::getVariableIDs ( )
static

Get an array of parser variable IDs.

Returns
string[]
Deprecated:
since 1.32, use MagicWordFactory::getVariableIDs

Definition at line 138 of file MagicWord.php.

References wfDeprecated().

◆ getVariableRegex()

MagicWord::getVariableRegex ( )

Matches the word, where $1 is a wildcard.

Returns
string

Definition at line 447 of file MagicWord.php.

References $mVariableRegex, and initRegex().

Referenced by substituteCallback().

◆ getVariableStartToEndRegex()

MagicWord::getVariableStartToEndRegex ( )

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

Returns
string

Definition at line 459 of file MagicWord.php.

References $mVariableStartToEndRegex, and 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 490 of file MagicWord.php.

References $mModified.

◆ initRegex()

MagicWord::initRegex ( )

Preliminary initialisation.

Access:\n private

Definition at line 195 of file MagicWord.php.

References $mSynonyms, and as.

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

◆ isCaseSensitive()

MagicWord::isCaseSensitive ( )
Returns
bool

Definition at line 510 of file MagicWord.php.

References $mCaseSensitive.

◆ load()

MagicWord::load (   $id)

Initialises this object with an ID.

Parameters
string$id
Exceptions
MWException

Definition at line 182 of file MagicWord.php.

◆ match()

MagicWord::match (   $text)

Returns true if the text contains the word.

Parameters
string$text
Returns
bool

Definition at line 304 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 370 of file MagicWord.php.

References $mFound, and getRegex().

◆ matchStart()

MagicWord::matchStart (   $text)

Returns true if the text starts with the word.

Parameters
string$text
Returns
bool

Definition at line 315 of file MagicWord.php.

References getRegexStart().

◆ matchStartAndRemove()

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

Definition at line 385 of file MagicWord.php.

References $mFound, and getRegexStart().

◆ matchStartToEnd()

MagicWord::matchStartToEnd (   $text)

Returns true if the text matched the word.

Parameters
string$text
Returns
bool
Since
1.23

Definition at line 327 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

Definition at line 341 of file MagicWord.php.

References $matches, captcha-old\count, and getVariableStartToEndRegex().

◆ pregRemoveAndRecord()

MagicWord::pregRemoveAndRecord ( )

Used in matchAndRemove()

Returns
string

Definition at line 401 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 415 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 436 of file MagicWord.php.

References $res, and getVariableRegex().

Member Data Documentation

◆ $contLang

Language MagicWord::$contLang
private

Definition at line 94 of file MagicWord.php.

Referenced by __construct().

◆ $mBaseRegex

string MagicWord::$mBaseRegex = ''
private

Definition at line 79 of file MagicWord.php.

Referenced by getBaseRegex().

◆ $mCaseSensitive

bool MagicWord::$mCaseSensitive

Definition at line 67 of file MagicWord.php.

Referenced by isCaseSensitive().

◆ $mFound

bool MagicWord::$mFound = false
private

Definition at line 91 of file MagicWord.php.

Referenced by matchAndRemove(), and matchStartAndRemove().

◆ $mId

string MagicWord::$mId

#-

Definition at line 61 of file MagicWord.php.

Referenced by getId().

◆ $mModified

bool MagicWord::$mModified = false
private

Definition at line 88 of file MagicWord.php.

Referenced by getWasModified().

◆ $mRegex

string MagicWord::$mRegex = ''
private

Definition at line 70 of file MagicWord.php.

Referenced by getRegex().

◆ $mRegexStart

string MagicWord::$mRegexStart = ''
private

Definition at line 73 of file MagicWord.php.

Referenced by getRegexStart().

◆ $mRegexStartToEnd

string MagicWord::$mRegexStartToEnd = ''
private

Definition at line 76 of file MagicWord.php.

Referenced by getRegexStartToEnd().

◆ $mSynonyms

string [] MagicWord::$mSynonyms

Definition at line 64 of file MagicWord.php.

Referenced by getSynonyms(), and initRegex().

◆ $mVariableRegex

string MagicWord::$mVariableRegex = ''
private

Definition at line 82 of file MagicWord.php.

Referenced by getVariableRegex().

◆ $mVariableStartToEndRegex

string MagicWord::$mVariableStartToEndRegex = ''
private

Definition at line 85 of file MagicWord.php.

Referenced by getVariableStartToEndRegex().


The documentation for this class was generated from the following file:
$magicWords
magicword txt Magic Words are some phrases used in the wikitext They are used for two that looks like templates but that don t accept any parameter *Parser functions(like {{fullurl:...}}, {{#special:...}}) $magicWords['en']
Definition: magicword.txt:33