11 $wgMagicFound = false;
13 /** Actual keyword to be used is set in Language.php */
20 'MAG_CURRENTMONTHNAME',
21 'MAG_CURRENTMONTHNAMEGEN',
22 'MAG_CURRENTMONTHABBREV',
27 'MAG_NUMBEROFARTICLES',
54 'MAG_NOCONTENTCONVERT',
61 'MAG_IMG_MANUALTHUMB',
72 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
74 for ( $i = 0; $i < count( $magicWords ); ++
$i )
75 define( $magicWords[$i], $i );
77 $wgVariableIDs = array(
80 MAG_CURRENTMONTHNAMEGEN
,
81 MAG_CURRENTMONTHABBREV
,
102 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
105 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
107 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
109 * Possible future improvements:
110 * * Simultaneous searching for a number of magic words
111 * * $wgMagicWords in shared memory
113 * Please avoid reading the data out of one of these objects and then writing
114 * special case code. If possible, add another match()-like function here.
122 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
123 var $mRegexStart, $mBaseRegex, $mVariableRegex;
127 function MagicWord($id = 0, $syn = '', $cs = false) {
129 $this->mSynonyms
= (array)$syn;
130 $this->mCaseSensitive
= $cs;
132 $this->mRegexStart
= '';
133 $this->mVariableRegex
= '';
134 $this->mVariableStartToEndRegex
= '';
135 $this->mModified
= false;
139 * Factory: creates an object representing an ID
142 function &get( $id ) {
143 global $wgMagicWords;
145 if ( !is_array( $wgMagicWords ) ) {
146 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
148 if (!array_key_exists( $id, $wgMagicWords ) ) {
149 $mw = new MagicWord();
151 $wgMagicWords[$id] = $mw;
153 return $wgMagicWords[$id];
156 # Initialises this object with an ID
157 function load( $id ) {
160 $wgContLang->getMagic( $this );
164 * Preliminary initialisation
167 function initRegex() {
168 #$variableClass = Title::legalChars();
169 # This was used for matching "$1" variables, but different uses of the feature will have
170 # different restrictions, which should be checked *after* the MagicWord has been matched,
172 $escSyn = array_map( 'preg_quote', $this->mSynonyms
);
173 $this->mBaseRegex
= implode( '|', $escSyn );
174 $case = $this->mCaseSensitive ?
'' : 'i';
175 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
176 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
177 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
178 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
179 "/^(?:{$this->mBaseRegex})$/{$case}" );
183 * Gets a regex representing matching the word
185 function getRegex() {
186 if ($this->mRegex
== '' ) {
189 return $this->mRegex
;
193 * Gets a regex matching the word, if it is at the string start
195 function getRegexStart() {
196 if ($this->mRegex
== '' ) {
199 return $this->mRegexStart
;
203 * regex without the slashes and what not
205 function getBaseRegex() {
206 if ($this->mRegex
== '') {
209 return $this->mBaseRegex
;
213 * Returns true if the text contains the word
216 function match( $text ) {
217 return preg_match( $this->getRegex(), $text );
221 * Returns true if the text starts with the word
224 function matchStart( $text ) {
225 return preg_match( $this->getRegexStart(), $text );
229 * Returns NULL if there's no match, the value of $1 otherwise
230 * The return code is the matched string, if there's no variable
231 * part in the regex and the matched variable part ($1) if there
234 function matchVariableStartToEnd( $text ) {
235 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
236 if ( $matchcount == 0 ) {
238 } elseif ( count($matches) == 1 ) {
241 # multiple matched parts (variable match); some will be empty because of synonyms
242 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
243 $matches = array_values(array_filter($matches));
250 * Returns true if the text matches the word, and alters the
251 * input string, removing all instances of the word
253 function matchAndRemove( &$text ) {
254 global $wgMagicFound;
255 $wgMagicFound = false;
256 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
257 return $wgMagicFound;
260 function matchStartAndRemove( &$text ) {
261 global $wgMagicFound;
262 $wgMagicFound = false;
263 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
264 return $wgMagicFound;
269 * Replaces the word with something else
271 function replace( $replacement, $subject ) {
272 $res = preg_replace( $this->getRegex(), $replacement, $subject );
273 $this->mModified
= !($res === $subject);
278 * Variable handling: {{SUBST:xxx}} style words
279 * Calls back a function to determine what to replace xxx with
280 * Input word must contain $1
282 function substituteCallback( $text, $callback ) {
283 $regex = $this->getVariableRegex();
284 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
285 $this->mModified
= !($res === $text);
290 * Matches the word, where $1 is a wildcard
292 function getVariableRegex() {
293 if ( $this->mVariableRegex
== '' ) {
296 return $this->mVariableRegex
;
300 * Matches the entire string, where $1 is a wildcard
302 function getVariableStartToEndRegex() {
303 if ( $this->mVariableStartToEndRegex
== '' ) {
306 return $this->mVariableStartToEndRegex
;
310 * Accesses the synonym list directly
312 function getSynonym( $i ) {
313 return $this->mSynonyms
[$i];
317 * Returns true if the last call to replace() or substituteCallback()
318 * returned a modified text, otherwise false.
320 function getWasModified(){
321 return $this->mModified
;
325 * $magicarr is an associative array of (magic word ID => replacement)
326 * This method uses the php feature to do several replacements at the same time,
327 * thereby gaining some efficiency. The result is placed in the out variable
328 * $result. The return value is true if something was replaced.
331 function replaceMultiple( $magicarr, $subject, &$result ){
334 foreach( $magicarr as $id => $replacement ){
335 $mw = MagicWord
::get( $id );
336 $search[] = $mw->getRegex();
337 $replace[] = $replacement;
340 $result = preg_replace( $search, $replace, $subject );
341 return !($result === $subject);
345 * Adds all the synonyms of this MagicWord to an array, to allow quick
346 * lookup in a list of magic words
348 function addToArray( &$array, $value ) {
349 foreach ( $this->mSynonyms
as $syn ) {
350 $array[$syn] = $value;
356 * Used in matchAndRemove()
359 function pregRemoveAndRecord( $match ) {
360 global $wgMagicFound;
361 $wgMagicFound = true;