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',
98 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
99 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
101 for ( $i = 0; $i < count( $magicWords ); ++
$i )
102 define( $magicWords[$i], $i );
104 $wgVariableIDs = array(
106 MAG_CURRENTMONTHNAME
,
107 MAG_CURRENTMONTHNAMEGEN
,
108 MAG_CURRENTMONTHABBREV
,
114 MAG_NUMBEROFARTICLES
,
139 MAG_SUBJECTPAGENAMEE
,
148 MAG_CURRENTTIMESTAMP
,
152 MAG_PAGESINNAMESPACE
,
154 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
155 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
158 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
160 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
162 * Possible future improvements:
163 * * Simultaneous searching for a number of magic words
164 * * $wgMagicWords in shared memory
166 * Please avoid reading the data out of one of these objects and then writing
167 * special case code. If possible, add another match()-like function here.
175 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
176 var $mRegexStart, $mBaseRegex, $mVariableRegex;
180 function MagicWord($id = 0, $syn = '', $cs = false) {
182 $this->mSynonyms
= (array)$syn;
183 $this->mCaseSensitive
= $cs;
185 $this->mRegexStart
= '';
186 $this->mVariableRegex
= '';
187 $this->mVariableStartToEndRegex
= '';
188 $this->mModified
= false;
192 * Factory: creates an object representing an ID
195 function &get( $id ) {
196 global $wgMagicWords;
198 if ( !is_array( $wgMagicWords ) ) {
199 throw new MWException( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
201 if (!array_key_exists( $id, $wgMagicWords ) ) {
202 $mw = new MagicWord();
204 $wgMagicWords[$id] = $mw;
206 return $wgMagicWords[$id];
209 # Initialises this object with an ID
210 function load( $id ) {
213 $wgContLang->getMagic( $this );
217 * Preliminary initialisation
220 function initRegex() {
221 #$variableClass = Title::legalChars();
222 # This was used for matching "$1" variables, but different uses of the feature will have
223 # different restrictions, which should be checked *after* the MagicWord has been matched,
227 foreach ( $this->mSynonyms
as $synonym )
228 // In case a magic word contains /, like that's going to happen;)
229 $escSyn[] = preg_quote( $synonym, '/' );
230 $this->mBaseRegex
= implode( '|', $escSyn );
232 $case = $this->mCaseSensitive ?
'' : 'i';
233 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
234 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
235 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
236 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
237 "/^(?:{$this->mBaseRegex})$/{$case}" );
241 * Gets a regex representing matching the word
243 function getRegex() {
244 if ($this->mRegex
== '' ) {
247 return $this->mRegex
;
251 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
252 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
253 * the complete expression
255 function getRegexCase() {
256 if ( $this->mRegex
=== '' )
259 return $this->mCaseSensitive ?
'' : 'i';
263 * Gets a regex matching the word, if it is at the string start
265 function getRegexStart() {
266 if ($this->mRegex
== '' ) {
269 return $this->mRegexStart
;
273 * regex without the slashes and what not
275 function getBaseRegex() {
276 if ($this->mRegex
== '') {
279 return $this->mBaseRegex
;
283 * Returns true if the text contains the word
286 function match( $text ) {
287 return preg_match( $this->getRegex(), $text );
291 * Returns true if the text starts with the word
294 function matchStart( $text ) {
295 return preg_match( $this->getRegexStart(), $text );
299 * Returns NULL if there's no match, the value of $1 otherwise
300 * The return code is the matched string, if there's no variable
301 * part in the regex and the matched variable part ($1) if there
304 function matchVariableStartToEnd( $text ) {
306 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
307 if ( $matchcount == 0 ) {
309 } elseif ( count($matches) == 1 ) {
312 # multiple matched parts (variable match); some will be empty because of synonyms
313 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
314 $matches = array_values(array_filter($matches));
321 * Returns true if the text matches the word, and alters the
322 * input string, removing all instances of the word
324 function matchAndRemove( &$text ) {
325 global $wgMagicFound;
326 $wgMagicFound = false;
327 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
328 return $wgMagicFound;
331 function matchStartAndRemove( &$text ) {
332 global $wgMagicFound;
333 $wgMagicFound = false;
334 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
335 return $wgMagicFound;
340 * Replaces the word with something else
342 function replace( $replacement, $subject, $limit=-1 ) {
343 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
344 $this->mModified
= !($res === $subject);
349 * Variable handling: {{SUBST:xxx}} style words
350 * Calls back a function to determine what to replace xxx with
351 * Input word must contain $1
353 function substituteCallback( $text, $callback ) {
354 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
355 $this->mModified
= !($res === $text);
360 * Matches the word, where $1 is a wildcard
362 function getVariableRegex() {
363 if ( $this->mVariableRegex
== '' ) {
366 return $this->mVariableRegex
;
370 * Matches the entire string, where $1 is a wildcard
372 function getVariableStartToEndRegex() {
373 if ( $this->mVariableStartToEndRegex
== '' ) {
376 return $this->mVariableStartToEndRegex
;
380 * Accesses the synonym list directly
382 function getSynonym( $i ) {
383 return $this->mSynonyms
[$i];
387 * Returns true if the last call to replace() or substituteCallback()
388 * returned a modified text, otherwise false.
390 function getWasModified(){
391 return $this->mModified
;
395 * $magicarr is an associative array of (magic word ID => replacement)
396 * This method uses the php feature to do several replacements at the same time,
397 * thereby gaining some efficiency. The result is placed in the out variable
398 * $result. The return value is true if something was replaced.
401 function replaceMultiple( $magicarr, $subject, &$result ){
404 foreach( $magicarr as $id => $replacement ){
405 $mw = MagicWord
::get( $id );
406 $search[] = $mw->getRegex();
407 $replace[] = $replacement;
410 $result = preg_replace( $search, $replace, $subject );
411 return !($result === $subject);
415 * Adds all the synonyms of this MagicWord to an array, to allow quick
416 * lookup in a list of magic words
418 function addToArray( &$array, $value ) {
419 foreach ( $this->mSynonyms
as $syn ) {
420 $array[$syn] = $value;
426 * Used in matchAndRemove()
429 function pregRemoveAndRecord( $match ) {
430 global $wgMagicFound;
431 $wgMagicFound = true;