MediaWiki master
MediaWiki\Parser\MagicWord Class Reference

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

Public Member Functions

 __construct ( $id=null, $syn=[], $cs=false, Language $contentLanguage=null)
 
 getBaseRegex ()
 Get the middle of {.
 
 getId ()
 
 getRegex ()
 Create a regex to match the magic word in wikitext.
 
 getRegexCase ()
 Get the regexp case modifier ("iu" or empty string).
 
 getRegexStart ()
 Create a regex to match the word at the start of a line in wikitext.
 
 getRegexStartToEnd ()
 Create a regex to match the word as the only thing on a line of wikitext.
 
 getSynonym ( $i)
 Get one of the synonyms.
 
 getSynonyms ()
 Get full list of synonyms.
 
 isCaseSensitive ()
 
 load ( $id)
 Load synonym data from {.
 
 match ( $text)
 Check if given wikitext contains the magic word.
 
 matchAndRemove (&$text)
 Remove any matches of this magic word from a given text.
 
 matchStartAndRemove (&$text)
 
 matchStartToEnd ( $text)
 Check if given wikitext contains the word as the only thing on a line.
 
 replace ( $replacement, $subject, $limit=-1)
 Replace any matches of this word with something else.
 

Public Attributes

bool $mCaseSensitive
 
string null $mId
 Potentially null for a short time before {.
 
array string[] $mSynonyms
 

Detailed Description

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

See docs/magicword.md.

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.

Since
1.1

Definition at line 65 of file MagicWord.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Parser\MagicWord::__construct (   $id = null,
  $syn = [],
  $cs = false,
Language  $contentLanguage = null 
)
Access: internal
Use {
See also
MagicWordFactory::get} instead
Parameters
string | null$idPreload internal name of the magic word
string[] | string$synPreload synonyms for the magic word
bool$csIf magic word is case sensitive
Language | null$contentLanguage

Definition at line 87 of file MagicWord.php.

References MediaWiki\MediaWikiServices\getInstance().

Member Function Documentation

◆ getBaseRegex()

MediaWiki\Parser\MagicWord::getBaseRegex ( )

Get the middle of {.

See also
getRegex}, without the surrounding slashes or modifiers
Access: internal
Exposed for {
See also
Parser::cleanSig} only
Since
1.1
Returns
string

Definition at line 159 of file MagicWord.php.

◆ getId()

MediaWiki\Parser\MagicWord::getId ( )
Returns
string
Deprecated:
since 1.42 Internal method should not be used

Definition at line 284 of file MagicWord.php.

References wfDeprecated().

◆ getRegex()

MediaWiki\Parser\MagicWord::getRegex ( )

Create a regex to match the magic word in wikitext.

Since
1.1
Returns
string

Definition at line 115 of file MagicWord.php.

◆ getRegexCase()

MediaWiki\Parser\MagicWord::getRegexCase ( )

Get the regexp case modifier ("iu" or empty string).

This is for building custom regexes that include {

See also
getBaseRegex}. The other getter methods return complete expressions that include this already.
Access: internal
Exposed for {
See also
Parser::cleanSig} only
Returns
string

Definition at line 128 of file MagicWord.php.

◆ getRegexStart()

MediaWiki\Parser\MagicWord::getRegexStart ( )

Create a regex to match the word at the start of a line in wikitext.

Since
1.1
Returns
string

Definition at line 138 of file MagicWord.php.

◆ getRegexStartToEnd()

MediaWiki\Parser\MagicWord::getRegexStartToEnd ( )

Create a regex to match the word as the only thing on a line of wikitext.

Since
1.23
Returns
string

Definition at line 148 of file MagicWord.php.

◆ getSynonym()

MediaWiki\Parser\MagicWord::getSynonym (   $i)

Get one of the synonyms.

This exists primarly for calling getSynonym( 0 ), which is how you can obtain the preferred name of a magic word according to the current wiki's content language. For example, when demonstrating or semi-automatically creating content that uses a given magic word.

This works because {

See also
LocalisationCache} merges magic word data by appending fallback languages (i.e. "en") after to the language's own data, and each language's Messages*.php file lists the preferred/canonical form as the first value.

Calling this with a number other than 0 is unsupported and may fail.

Since
1.1
Parameters
int$i
Returns
string

Definition at line 258 of file MagicWord.php.

◆ getSynonyms()

MediaWiki\Parser\MagicWord::getSynonyms ( )

Get full list of synonyms.

Since
1.7
Returns
string[]

Definition at line 268 of file MagicWord.php.

◆ isCaseSensitive()

MediaWiki\Parser\MagicWord::isCaseSensitive ( )
Since
1.7
Returns
bool

Definition at line 276 of file MagicWord.php.

◆ load()

MediaWiki\Parser\MagicWord::load (   $id)

Load synonym data from {.

See also
LocalisationCache}.
Access: internal
For use by {
See also
MagicWordFactory::get} only
Since
1.1
Parameters
string$id

Definition at line 101 of file MagicWord.php.

◆ match()

MediaWiki\Parser\MagicWord::match (   $text)

Check if given wikitext contains the magic word.

Since
1.1
Parameters
string$text
Returns
bool

Definition at line 180 of file MagicWord.php.

Referenced by WikitextContent\matchMagicWord().

◆ matchAndRemove()

MediaWiki\Parser\MagicWord::matchAndRemove ( $text)

Remove any matches of this magic word from a given text.

Returns true if the text contains one or more matches, and alters the input string to remove all instances of the magic word.

Since
1.1
Parameters
string&$text
Returns
bool

Definition at line 205 of file MagicWord.php.

◆ matchStartAndRemove()

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

Definition at line 214 of file MagicWord.php.

◆ matchStartToEnd()

MediaWiki\Parser\MagicWord::matchStartToEnd (   $text)

Check if given wikitext contains the word as the only thing on a line.

Parameters
string$text
Returns
bool
Since
1.23

Definition at line 191 of file MagicWord.php.

◆ replace()

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

Replace any matches of this word with something else.

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

Definition at line 228 of file MagicWord.php.

Member Data Documentation

◆ $mCaseSensitive

bool MediaWiki\Parser\MagicWord::$mCaseSensitive

Definition at line 74 of file MagicWord.php.

◆ $mId

string null MediaWiki\Parser\MagicWord::$mId

Potentially null for a short time before {.

See also
load} is called

Definition at line 68 of file MagicWord.php.

◆ $mSynonyms

array string [] MediaWiki\Parser\MagicWord::$mSynonyms

Definition at line 71 of file MagicWord.php.


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