MediaWiki  REL1_24
MagicWord Class Reference

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

Collaboration diagram for MagicWord:

List of all members.

Public Member Functions

 __construct ($id=0, $syn=array(), $cs=false)
 #@-
 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.
 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.
 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.

Static Public Member Functions

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

Public Attributes

bool $mCaseSensitive
 *
int $mId
 #@-
array $mSynonyms
 *

Static Public Attributes

static $mCacheTTLs
static $mDoubleUnderscoreArray = null
static $mDoubleUnderscoreIDs
static $mObjects = array()
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: * Simultaneous searching for a number of magic words * MagicWord::$mObjects in shared memory

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 = array();

 $magicWords['en'] = array(
    '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

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

#@-

Definition at line 237 of file MagicWord.php.


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

static MagicWord::clearCache ( ) [static]

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

Definition at line 312 of file MagicWord.php.

References array(), and wfRunHooks().

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

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

static& MagicWord::get ( id) [static]

regex without the slashes and what not

Returns:
string

Definition at line 440 of file MagicWord.php.

References initRegex().

static MagicWord::getCacheTTL ( id) [static]

Allow external reads of TTL array.

Parameters:
int$id
Returns:
int

Definition at line 287 of file MagicWord.php.

Get a MagicWordArray of double-underscore entities.

Returns:
MagicWordArray

Definition at line 300 of file MagicWord.php.

Referenced by InfoAction\pageInfo().

Returns:
int

Definition at line 694 of file MagicWord.php.

Gets a regex representing matching the word.

Returns:
string

Definition at line 388 of file MagicWord.php.

Referenced by matchStart().

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

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

Returns:
string

Definition at line 415 of file MagicWord.php.

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

Returns:
string
Since:
1.23

Definition at line 428 of file MagicWord.php.

static MagicWord::getSubstIDs ( ) [static]

Get an array of parser substitution modifier IDs.

Returns:
array

Definition at line 277 of file MagicWord.php.

Accesses the synonym list directly.

Parameters:
int$i
Returns:
string

Definition at line 623 of file MagicWord.php.

Returns:
array

Definition at line 630 of file MagicWord.php.

static MagicWord::getVariableIDs ( ) [static]

Get an array of parser variable IDs.

Returns:
array

Definition at line 264 of file MagicWord.php.

Referenced by ApiQuerySiteinfo\appendVariables().

Matches the word, where $1 is a wildcard.

Returns:
string

Definition at line 597 of file MagicWord.php.

References $res.

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

Returns:
string

Definition at line 609 of file MagicWord.php.

References initRegex().

Referenced by matchVariableStartToEnd().

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

Returns:
bool

Definition at line 640 of file MagicWord.php.

Preliminary initialisation.

Access:
private

Definition at line 339 of file MagicWord.php.

Referenced by getBaseRegex(), and getVariableStartToEndRegex().

Returns:
bool

Definition at line 687 of file MagicWord.php.

MagicWord::load ( id)

Initialises this object with an ID.

Parameters:
int$id
Exceptions:
MWException

Definition at line 322 of file MagicWord.php.

MagicWord::match ( text)

Returns true if the text contains the word.

Parameters:
string$text
Returns:
bool

Definition at line 454 of file MagicWord.php.

Referenced by WikitextContent\matchMagicWord().

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

MagicWord::matchStart ( text)

Returns true if the text starts with the word.

Parameters:
string$text
Returns:
bool

Definition at line 465 of file MagicWord.php.

References getRegex().

Parameters:
string$text
Returns:
bool

Definition at line 535 of file MagicWord.php.

Returns true if the text matched the word.

Parameters:
string$text
Returns:
bool
Since:
1.23

Definition at line 477 of file MagicWord.php.

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

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

Used in matchAndRemove()

Returns:
string

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

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:
array$magicarr
string$subject
string$result
Returns:
bool

Definition at line 657 of file MagicWord.php.

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


Member Data Documentation

string MagicWord::$mBaseRegex = '' [private]

*

Definition at line 76 of file MagicWord.php.

MagicWord::$mCacheTTLs [static]

Definition at line 169 of file MagicWord.php.

bool MagicWord::$mCaseSensitive

*

Definition at line 68 of file MagicWord.php.

MagicWord::$mDoubleUnderscoreArray = null [static]

Definition at line 233 of file MagicWord.php.

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

Definition at line 211 of file MagicWord.php.

bool MagicWord::$mFound = false [private]

*

Definition at line 84 of file MagicWord.php.

int MagicWord::$mId

#@-

*

Definition at line 64 of file MagicWord.php.

bool MagicWord::$mModified = false [private]

*

Definition at line 82 of file MagicWord.php.

MagicWord::$mObjects = array() [static]

Definition at line 232 of file MagicWord.php.

string MagicWord::$mRegex = '' [private]

*

Definition at line 70 of file MagicWord.php.

string MagicWord::$mRegexStart = '' [private]

*

Definition at line 72 of file MagicWord.php.

string MagicWord::$mRegexStartToEnd = '' [private]

*

Definition at line 74 of file MagicWord.php.

MagicWord::$mSubstIDs [static]
Initial value:
 array(
        'subst',
        'safesubst',
    )

Definition at line 227 of file MagicWord.php.

array MagicWord::$mSynonyms

*

Definition at line 66 of file MagicWord.php.

MagicWord::$mVariableIDs [static]

Definition at line 87 of file MagicWord.php.

MagicWord::$mVariableIDsInitialised = false [static]

Definition at line 86 of file MagicWord.php.

string MagicWord::$mVariableRegex = '' [private]

*

Definition at line 78 of file MagicWord.php.

string MagicWord::$mVariableStartToEndRegex = '' [private]

*

Definition at line 80 of file MagicWord.php.


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