3 # This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
5 # if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
7 # Possible future improvements:
8 # * Simultaneous searching for a number of magic words
9 # * $wgMagicWords in shared memory
11 # Please avoid reading the data out of one of these objects and then writing
12 # special case code. If possible, add another match()-like function here.
14 /*private*/ $wgMagicFound = false;
17 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
18 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
19 /*private*/ var $mModified;
21 function MagicWord($id = 0, $syn = "", $cs = false)
24 $this->mSynonyms
= (array)$syn;
25 $this->mCaseSensitive
= $cs;
27 $this->mRegexStart
= "";
28 $this->mVariableRegex
= "";
29 $this->mModified
= false;
32 # Factory: creates an object representing an ID
33 /*static*/ function &get( $id )
37 if (!array_key_exists( $id, $wgMagicWords ) ) {
38 $mw = new MagicWord();
40 $wgMagicWords[$id] = $mw;
42 return $wgMagicWords[$id];
45 # Initialises this object with an ID
50 $wgLang->getMagic( $this );
53 # Preliminary initialisation
54 /* private */ function initRegex()
56 $escSyn = array_map( "preg_quote", $this->mSynonyms
);
57 $this->mBaseRegex
= implode( "|", $escSyn );
58 $case = $this->mCaseSensitive ?
"" : "i";
59 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
60 $this->mRegexStart
= "/^{$this->mBaseRegex}/{$case}";
61 $this->mVariableRegex
= str_replace( "\\$1", "([A-Za-z0-9_\-]*)", $this->mRegex
);
64 # Gets a regex representing matching the word
67 if ($this->mRegex
== "" ) {
73 # Gets a regex matching the word, if it is at the
75 function getRegexStart()
77 if ($this->mRegex
== "" ) {
80 return $this->mRegexStart
;
83 # regex without the slashes and what not
84 function getBaseRegex()
86 if ($this->mRegex
== "") {
89 return $this->mBaseRegex
;
92 # Returns true if the text contains the word
93 function match( $text ) {
94 return preg_match( $this->getRegex(), $text );
97 # Returns true if the text starts with the word
98 function matchStart( $text )
100 return preg_match( $this->getRegexStart(), $text );
103 # Returns true if the text matches the word, and alters the
104 # input string, removing all instances of the word
105 function matchAndRemove( &$text )
107 global $wgMagicFound;
108 $wgMagicFound = false;
109 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
110 return $wgMagicFound;
113 # Replaces the word with something else
114 function replace( $replacement, $subject )
116 $res = preg_replace( $this->getRegex(), $replacement, $subject );
117 $this->mModified
= !($res === $subject);
121 # Variable handling: {{SUBST:xxx}} style words
122 # Calls back a function to determine what to replace xxx with
123 # Input word must contain $1
124 function substituteCallback( $text, $callback ) {
125 $regex = $this->getVariableRegex();
126 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
127 $this->mModified
= !($res === $text);
131 # Matches the word, where $1 is a wildcard
132 function getVariableRegex()
134 if ( $this->mVariableRegex
== "" ) {
137 return $this->mVariableRegex
;
140 # Accesses the synonym list directly
141 function getSynonym( $i ) {
142 return $this->mSynonyms
[$i];
145 # Returns true if the last call to replace() or substituteCallback()
146 # returned a modified text, otherwise false.
147 function getWasModified(){
148 return $this->mModified
;
152 # Used in matchAndRemove()
153 /*private*/ function pregRemoveAndRecord( $match )
155 global $wgMagicFound;
156 $wgMagicFound = true;