MediaWiki  1.23.2
MagicWord Class Reference

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

Public Member Functions

 __construct ( $id=0, $syn=array(), $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

 $mBaseRegex = ''
 
 $mCaseSensitive
 
 $mFound = false
 
 $mId
 #+ More...
 
 $mModified = false
 
 $mRegex = ''
 
 $mRegexStart = ''
 
 $mRegexStartToEnd = ''
 
 $mSynonyms
 
 $mVariableRegex = ''
 
 $mVariableStartToEndRegex = ''
 

Static Public Attributes

static $mCacheTTLs
 
static $mDoubleUnderscoreArray = null
 
static $mDoubleUnderscoreIDs
 
static $mObjects = array()
 
static $mSubstIDs
 
static $mVariableIDs
 
static $mVariableIDsInitialised = false
 

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

Constructor & Destructor Documentation

◆ __construct()

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

#-

Definition at line 225 of file MagicWord.php.

References 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
$array
$value

Definition at line 650 of file MagicWord.php.

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

◆ clearCache()

static MagicWord::clearCache ( )
static

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

Definition at line 300 of file MagicWord.php.

References array().

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

◆ 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
$s1string
$s2string
Returns
int

Definition at line 359 of file MagicWord.php.

◆ get()

◆ getBaseRegex()

MagicWord::getBaseRegex ( )

regex without the slashes and what not

Returns
string

Definition at line 428 of file MagicWord.php.

References $mBaseRegex, and initRegex().

◆ getCacheTTL()

static MagicWord::getCacheTTL (   $id)
static

Allow external reads of TTL array.

Parameters
$idint
Returns
array

Definition at line 275 of file MagicWord.php.

◆ getDoubleUnderscoreArray()

static MagicWord::getDoubleUnderscoreArray ( )
static

Get a MagicWordArray of double-underscore entities.

Returns
MagicWordArray

Definition at line 288 of file MagicWord.php.

References $mDoubleUnderscoreArray, array(), and wfRunHooks().

Referenced by InfoAction\pageInfo().

◆ getId()

MagicWord::getId ( )
Returns
int

Definition at line 667 of file MagicWord.php.

References $mId.

◆ getRegex()

MagicWord::getRegex ( )

Gets a regex representing matching the word.

Returns
string

Definition at line 376 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 390 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 403 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 416 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
array

Definition at line 265 of file MagicWord.php.

References $mSubstIDs.

◆ getSynonym()

MagicWord::getSynonym (   $i)

Accesses the synonym list directly.

Parameters
$iint
Returns
string

Definition at line 596 of file MagicWord.php.

◆ getSynonyms()

MagicWord::getSynonyms ( )
Returns
array

Definition at line 603 of file MagicWord.php.

References $mSynonyms.

◆ getVariableIDs()

static MagicWord::getVariableIDs ( )
static

Get an array of parser variable IDs.

Returns
array

Definition at line 252 of file MagicWord.php.

References $mVariableIDs, array(), and wfRunHooks().

◆ getVariableRegex()

MagicWord::getVariableRegex ( )

Matches the word, where $1 is a wildcard.

Returns
string

Definition at line 570 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 582 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 613 of file MagicWord.php.

References $mModified.

◆ initRegex()

MagicWord::initRegex ( )

Preliminary initialisation.

Access:\n private

Definition at line 327 of file MagicWord.php.

References $mSynonyms, array(), and as.

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

◆ isCaseSensitive()

MagicWord::isCaseSensitive ( )
Returns
bool

Definition at line 660 of file MagicWord.php.

References $mCaseSensitive.

◆ load()

MagicWord::load (   $id)

Initialises this object with an ID.

Parameters
$id
Exceptions
MWException

Definition at line 310 of file MagicWord.php.

References $wgContLang, array(), global, wfProfileIn(), and wfProfileOut().

◆ match()

MagicWord::match (   $text)

Returns true if the text contains the word.

Parameters
$textstring
Returns
bool

Definition at line 442 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
$textstring
Returns
bool

Definition at line 508 of file MagicWord.php.

References $mFound, array(), and getRegex().

◆ matchStart()

MagicWord::matchStart (   $text)

Returns true if the text starts with the word.

Parameters
$textstring
Returns
bool

Definition at line 453 of file MagicWord.php.

References getRegexStart().

◆ matchStartAndRemove()

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

Definition at line 518 of file MagicWord.php.

References $mFound, array(), and getRegexStart().

◆ matchStartToEnd()

MagicWord::matchStartToEnd (   $text)

Returns true if the text matched the word.

Parameters
$textstring
Returns
bool
Since
1.23

Definition at line 465 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
$textstring
Returns
string

Definition at line 479 of file MagicWord.php.

References $matches, array(), and getVariableStartToEndRegex().

◆ pregRemoveAndRecord()

MagicWord::pregRemoveAndRecord ( )

Used in matchAndRemove()

Returns
string

Definition at line 529 of file MagicWord.php.

◆ replace()

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

Replaces the word with something else.

Parameters
$replacement
$subject
$limitint
Returns
string

Definition at line 543 of file MagicWord.php.

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

◆ replaceMultiple()

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.

Todo:
Should this be static? It doesn't seem to be used at all
Parameters
$magicarr
$subject
$result
Returns
bool

Definition at line 630 of file MagicWord.php.

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

◆ 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
$textstring
$callback
Returns
string

Definition at line 559 of file MagicWord.php.

References $res, and getVariableRegex().

Member Data Documentation

◆ $mBaseRegex

MagicWord::$mBaseRegex = ''

Definition at line 69 of file MagicWord.php.

Referenced by getBaseRegex().

◆ $mCacheTTLs

MagicWord::$mCacheTTLs
static

Definition at line 157 of file MagicWord.php.

◆ $mCaseSensitive

MagicWord::$mCaseSensitive

Definition at line 65 of file MagicWord.php.

Referenced by isCaseSensitive().

◆ $mDoubleUnderscoreArray

MagicWord::$mDoubleUnderscoreArray = null
static

Definition at line 221 of file MagicWord.php.

Referenced by getDoubleUnderscoreArray().

◆ $mDoubleUnderscoreIDs

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

Definition at line 199 of file MagicWord.php.

◆ $mFound

MagicWord::$mFound = false

Definition at line 73 of file MagicWord.php.

Referenced by matchAndRemove(), and matchStartAndRemove().

◆ $mId

MagicWord::$mId

#+

Access:\n private

Definition at line 65 of file MagicWord.php.

Referenced by getId().

◆ $mModified

MagicWord::$mModified = false

Definition at line 72 of file MagicWord.php.

Referenced by getWasModified().

◆ $mObjects

MagicWord::$mObjects = array()
static

Definition at line 220 of file MagicWord.php.

◆ $mRegex

MagicWord::$mRegex = ''

Definition at line 66 of file MagicWord.php.

Referenced by getRegex().

◆ $mRegexStart

MagicWord::$mRegexStart = ''

Definition at line 67 of file MagicWord.php.

Referenced by getRegexStart().

◆ $mRegexStartToEnd

MagicWord::$mRegexStartToEnd = ''

Definition at line 68 of file MagicWord.php.

Referenced by getRegexStartToEnd().

◆ $mSubstIDs

MagicWord::$mSubstIDs
static
Initial value:
'subst',
'safesubst',
)

Definition at line 215 of file MagicWord.php.

Referenced by getSubstIDs().

◆ $mSynonyms

MagicWord::$mSynonyms

Definition at line 65 of file MagicWord.php.

Referenced by getSynonyms(), and initRegex().

◆ $mVariableIDs

MagicWord::$mVariableIDs
static

Definition at line 76 of file MagicWord.php.

Referenced by getVariableIDs().

◆ $mVariableIDsInitialised

MagicWord::$mVariableIDsInitialised = false
static

Definition at line 75 of file MagicWord.php.

◆ $mVariableRegex

MagicWord::$mVariableRegex = ''

Definition at line 70 of file MagicWord.php.

Referenced by getVariableRegex().

◆ $mVariableStartToEndRegex

MagicWord::$mVariableStartToEndRegex = ''

Definition at line 71 of file MagicWord.php.

Referenced by getVariableStartToEndRegex().


The documentation for this class was generated from the following file:
MagicWord\get
static & get( $id)
Factory: creates an object representing an ID.
Definition: MagicWord.php:238
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$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
MagicWord\match
match( $text)
Returns true if the text contains the word.
Definition: MagicWord.php:442