11 $wgMagicFound = false;
13 /** Actual keyword to be used is set in Language.php */
20 'MAG_CURRENTMONTHNAME',
21 'MAG_CURRENTMONTHNAMEGEN',
22 'MAG_CURRENTMONTHABBREV',
28 'MAG_NUMBEROFARTICLES',
55 'MAG_NOCONTENTCONVERT',
62 'MAG_IMG_MANUALTHUMB',
82 'MAG_SUBJECTPAGENAME',
83 'MAG_SUBJECTPAGENAMEE',
92 'MAG_CURRENTTIMESTAMP',
95 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
96 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
98 for ( $i = 0; $i < count( $magicWords ); ++
$i )
99 define( $magicWords[$i], $i );
101 $wgVariableIDs = array(
103 MAG_CURRENTMONTHNAME
,
104 MAG_CURRENTMONTHNAMEGEN
,
105 MAG_CURRENTMONTHABBREV
,
111 MAG_NUMBEROFARTICLES
,
136 MAG_SUBJECTPAGENAMEE
,
145 MAG_CURRENTTIMESTAMP
,
148 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
149 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
152 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
154 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
156 * Possible future improvements:
157 * * Simultaneous searching for a number of magic words
158 * * $wgMagicWords in shared memory
160 * Please avoid reading the data out of one of these objects and then writing
161 * special case code. If possible, add another match()-like function here.
169 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
170 var $mRegexStart, $mBaseRegex, $mVariableRegex;
174 function MagicWord($id = 0, $syn = '', $cs = false) {
176 $this->mSynonyms
= (array)$syn;
177 $this->mCaseSensitive
= $cs;
179 $this->mRegexStart
= '';
180 $this->mVariableRegex
= '';
181 $this->mVariableStartToEndRegex
= '';
182 $this->mModified
= false;
186 * Factory: creates an object representing an ID
189 function &get( $id ) {
190 global $wgMagicWords;
192 if ( !is_array( $wgMagicWords ) ) {
193 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
195 if (!array_key_exists( $id, $wgMagicWords ) ) {
196 $mw = new MagicWord();
198 $wgMagicWords[$id] = $mw;
200 return $wgMagicWords[$id];
203 # Initialises this object with an ID
204 function load( $id ) {
207 $wgContLang->getMagic( $this );
211 * Preliminary initialisation
214 function initRegex() {
215 #$variableClass = Title::legalChars();
216 # This was used for matching "$1" variables, but different uses of the feature will have
217 # different restrictions, which should be checked *after* the MagicWord has been matched,
221 foreach ( $this->mSynonyms
as $synonym )
222 // In case a magic word contains /, like that's going to happen;)
223 $escSyn[] = preg_quote( $synonym, '/' );
224 $this->mBaseRegex
= implode( '|', $escSyn );
226 $case = $this->mCaseSensitive ?
'' : 'i';
227 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
228 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
229 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
230 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
231 "/^(?:{$this->mBaseRegex})$/{$case}" );
235 * Gets a regex representing matching the word
237 function getRegex() {
238 if ($this->mRegex
== '' ) {
241 return $this->mRegex
;
245 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
246 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
247 * the complete expression
249 function getRegexCase() {
250 if ( $this->mRegex
=== '' )
253 return $this->mCaseSensitive ?
'' : 'i';
257 * Gets a regex matching the word, if it is at the string start
259 function getRegexStart() {
260 if ($this->mRegex
== '' ) {
263 return $this->mRegexStart
;
267 * regex without the slashes and what not
269 function getBaseRegex() {
270 if ($this->mRegex
== '') {
273 return $this->mBaseRegex
;
277 * Returns true if the text contains the word
280 function match( $text ) {
281 return preg_match( $this->getRegex(), $text );
285 * Returns true if the text starts with the word
288 function matchStart( $text ) {
289 return preg_match( $this->getRegexStart(), $text );
293 * Returns NULL if there's no match, the value of $1 otherwise
294 * The return code is the matched string, if there's no variable
295 * part in the regex and the matched variable part ($1) if there
298 function matchVariableStartToEnd( $text ) {
300 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
301 if ( $matchcount == 0 ) {
303 } elseif ( count($matches) == 1 ) {
306 # multiple matched parts (variable match); some will be empty because of synonyms
307 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
308 $matches = array_values(array_filter($matches));
315 * Returns true if the text matches the word, and alters the
316 * input string, removing all instances of the word
318 function matchAndRemove( &$text ) {
319 global $wgMagicFound;
320 $wgMagicFound = false;
321 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
322 return $wgMagicFound;
325 function matchStartAndRemove( &$text ) {
326 global $wgMagicFound;
327 $wgMagicFound = false;
328 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
329 return $wgMagicFound;
334 * Replaces the word with something else
336 function replace( $replacement, $subject, $limit=-1 ) {
337 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
338 $this->mModified
= !($res === $subject);
343 * Variable handling: {{SUBST:xxx}} style words
344 * Calls back a function to determine what to replace xxx with
345 * Input word must contain $1
347 function substituteCallback( $text, $callback ) {
348 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
349 $this->mModified
= !($res === $text);
354 * Matches the word, where $1 is a wildcard
356 function getVariableRegex() {
357 if ( $this->mVariableRegex
== '' ) {
360 return $this->mVariableRegex
;
364 * Matches the entire string, where $1 is a wildcard
366 function getVariableStartToEndRegex() {
367 if ( $this->mVariableStartToEndRegex
== '' ) {
370 return $this->mVariableStartToEndRegex
;
374 * Accesses the synonym list directly
376 function getSynonym( $i ) {
377 return $this->mSynonyms
[$i];
381 * Returns true if the last call to replace() or substituteCallback()
382 * returned a modified text, otherwise false.
384 function getWasModified(){
385 return $this->mModified
;
389 * $magicarr is an associative array of (magic word ID => replacement)
390 * This method uses the php feature to do several replacements at the same time,
391 * thereby gaining some efficiency. The result is placed in the out variable
392 * $result. The return value is true if something was replaced.
395 function replaceMultiple( $magicarr, $subject, &$result ){
398 foreach( $magicarr as $id => $replacement ){
399 $mw = MagicWord
::get( $id );
400 $search[] = $mw->getRegex();
401 $replace[] = $replacement;
404 $result = preg_replace( $search, $replace, $subject );
405 return !($result === $subject);
409 * Adds all the synonyms of this MagicWord to an array, to allow quick
410 * lookup in a list of magic words
412 function addToArray( &$array, $value ) {
413 foreach ( $this->mSynonyms
as $syn ) {
414 $array[$syn] = $value;
420 * Used in matchAndRemove()
423 function pregRemoveAndRecord( $match ) {
424 global $wgMagicFound;
425 $wgMagicFound = true;