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 'MAG_CONTENTLANGUAGE',
96 'MAG_PAGESINNAMESPACE',
99 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
100 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
102 for ( $i = 0; $i < count( $magicWords ); ++
$i )
103 define( $magicWords[$i], $i );
105 $wgVariableIDs = array(
107 MAG_CURRENTMONTHNAME
,
108 MAG_CURRENTMONTHNAMEGEN
,
109 MAG_CURRENTMONTHABBREV
,
115 MAG_NUMBEROFARTICLES
,
140 MAG_SUBJECTPAGENAMEE
,
149 MAG_CURRENTTIMESTAMP
,
153 MAG_PAGESINNAMESPACE
,
155 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
156 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
159 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
161 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
163 * Possible future improvements:
164 * * Simultaneous searching for a number of magic words
165 * * $wgMagicWords in shared memory
167 * Please avoid reading the data out of one of these objects and then writing
168 * special case code. If possible, add another match()-like function here.
176 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
177 var $mRegexStart, $mBaseRegex, $mVariableRegex;
181 function MagicWord($id = 0, $syn = '', $cs = false) {
183 $this->mSynonyms
= (array)$syn;
184 $this->mCaseSensitive
= $cs;
186 $this->mRegexStart
= '';
187 $this->mVariableRegex
= '';
188 $this->mVariableStartToEndRegex
= '';
189 $this->mModified
= false;
193 * Factory: creates an object representing an ID
196 function &get( $id ) {
197 global $wgMagicWords;
199 if ( !is_array( $wgMagicWords ) ) {
200 throw new MWException( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
202 if (!array_key_exists( $id, $wgMagicWords ) ) {
203 $mw = new MagicWord();
205 $wgMagicWords[$id] = $mw;
207 return $wgMagicWords[$id];
210 # Initialises this object with an ID
211 function load( $id ) {
214 $wgContLang->getMagic( $this );
218 * Preliminary initialisation
221 function initRegex() {
222 #$variableClass = Title::legalChars();
223 # This was used for matching "$1" variables, but different uses of the feature will have
224 # different restrictions, which should be checked *after* the MagicWord has been matched,
228 foreach ( $this->mSynonyms
as $synonym )
229 // In case a magic word contains /, like that's going to happen;)
230 $escSyn[] = preg_quote( $synonym, '/' );
231 $this->mBaseRegex
= implode( '|', $escSyn );
233 $case = $this->mCaseSensitive ?
'' : 'i';
234 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
235 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
236 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
237 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
238 "/^(?:{$this->mBaseRegex})$/{$case}" );
242 * Gets a regex representing matching the word
244 function getRegex() {
245 if ($this->mRegex
== '' ) {
248 return $this->mRegex
;
252 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
253 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
254 * the complete expression
256 function getRegexCase() {
257 if ( $this->mRegex
=== '' )
260 return $this->mCaseSensitive ?
'' : 'i';
264 * Gets a regex matching the word, if it is at the string start
266 function getRegexStart() {
267 if ($this->mRegex
== '' ) {
270 return $this->mRegexStart
;
274 * regex without the slashes and what not
276 function getBaseRegex() {
277 if ($this->mRegex
== '') {
280 return $this->mBaseRegex
;
284 * Returns true if the text contains the word
287 function match( $text ) {
288 return preg_match( $this->getRegex(), $text );
292 * Returns true if the text starts with the word
295 function matchStart( $text ) {
296 return preg_match( $this->getRegexStart(), $text );
300 * Returns NULL if there's no match, the value of $1 otherwise
301 * The return code is the matched string, if there's no variable
302 * part in the regex and the matched variable part ($1) if there
305 function matchVariableStartToEnd( $text ) {
307 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
308 if ( $matchcount == 0 ) {
310 } elseif ( count($matches) == 1 ) {
313 # multiple matched parts (variable match); some will be empty because of synonyms
314 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
315 $matches = array_values(array_filter($matches));
322 * Returns true if the text matches the word, and alters the
323 * input string, removing all instances of the word
325 function matchAndRemove( &$text ) {
326 global $wgMagicFound;
327 $wgMagicFound = false;
328 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
329 return $wgMagicFound;
332 function matchStartAndRemove( &$text ) {
333 global $wgMagicFound;
334 $wgMagicFound = false;
335 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
336 return $wgMagicFound;
341 * Replaces the word with something else
343 function replace( $replacement, $subject, $limit=-1 ) {
344 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
345 $this->mModified
= !($res === $subject);
350 * Variable handling: {{SUBST:xxx}} style words
351 * Calls back a function to determine what to replace xxx with
352 * Input word must contain $1
354 function substituteCallback( $text, $callback ) {
355 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
356 $this->mModified
= !($res === $text);
361 * Matches the word, where $1 is a wildcard
363 function getVariableRegex() {
364 if ( $this->mVariableRegex
== '' ) {
367 return $this->mVariableRegex
;
371 * Matches the entire string, where $1 is a wildcard
373 function getVariableStartToEndRegex() {
374 if ( $this->mVariableStartToEndRegex
== '' ) {
377 return $this->mVariableStartToEndRegex
;
381 * Accesses the synonym list directly
383 function getSynonym( $i ) {
384 return $this->mSynonyms
[$i];
388 * Returns true if the last call to replace() or substituteCallback()
389 * returned a modified text, otherwise false.
391 function getWasModified(){
392 return $this->mModified
;
396 * $magicarr is an associative array of (magic word ID => replacement)
397 * This method uses the php feature to do several replacements at the same time,
398 * thereby gaining some efficiency. The result is placed in the out variable
399 * $result. The return value is true if something was replaced.
402 function replaceMultiple( $magicarr, $subject, &$result ){
405 foreach( $magicarr as $id => $replacement ){
406 $mw = MagicWord
::get( $id );
407 $search[] = $mw->getRegex();
408 $replace[] = $replacement;
411 $result = preg_replace( $search, $replace, $subject );
412 return !($result === $subject);
416 * Adds all the synonyms of this MagicWord to an array, to allow quick
417 * lookup in a list of magic words
419 function addToArray( &$array, $value ) {
420 foreach ( $this->mSynonyms
as $syn ) {
421 $array[$syn] = $value;
427 * Used in matchAndRemove()
430 function pregRemoveAndRecord( $match ) {
431 global $wgMagicFound;
432 $wgMagicFound = true;