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',
96 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
97 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
99 for ( $i = 0; $i < count( $magicWords ); ++
$i )
100 define( $magicWords[$i], $i );
102 $wgVariableIDs = array(
104 MAG_CURRENTMONTHNAME
,
105 MAG_CURRENTMONTHNAMEGEN
,
106 MAG_CURRENTMONTHABBREV
,
112 MAG_NUMBEROFARTICLES
,
137 MAG_SUBJECTPAGENAMEE
,
146 MAG_CURRENTTIMESTAMP
,
150 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
151 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
154 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
156 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
158 * Possible future improvements:
159 * * Simultaneous searching for a number of magic words
160 * * $wgMagicWords in shared memory
162 * Please avoid reading the data out of one of these objects and then writing
163 * special case code. If possible, add another match()-like function here.
171 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
172 var $mRegexStart, $mBaseRegex, $mVariableRegex;
176 function MagicWord($id = 0, $syn = '', $cs = false) {
178 $this->mSynonyms
= (array)$syn;
179 $this->mCaseSensitive
= $cs;
181 $this->mRegexStart
= '';
182 $this->mVariableRegex
= '';
183 $this->mVariableStartToEndRegex
= '';
184 $this->mModified
= false;
188 * Factory: creates an object representing an ID
191 function &get( $id ) {
192 global $wgMagicWords;
194 if ( !is_array( $wgMagicWords ) ) {
195 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
197 if (!array_key_exists( $id, $wgMagicWords ) ) {
198 $mw = new MagicWord();
200 $wgMagicWords[$id] = $mw;
202 return $wgMagicWords[$id];
205 # Initialises this object with an ID
206 function load( $id ) {
209 $wgContLang->getMagic( $this );
213 * Preliminary initialisation
216 function initRegex() {
217 #$variableClass = Title::legalChars();
218 # This was used for matching "$1" variables, but different uses of the feature will have
219 # different restrictions, which should be checked *after* the MagicWord has been matched,
223 foreach ( $this->mSynonyms
as $synonym )
224 // In case a magic word contains /, like that's going to happen;)
225 $escSyn[] = preg_quote( $synonym, '/' );
226 $this->mBaseRegex
= implode( '|', $escSyn );
228 $case = $this->mCaseSensitive ?
'' : 'i';
229 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
230 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
231 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
232 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
233 "/^(?:{$this->mBaseRegex})$/{$case}" );
237 * Gets a regex representing matching the word
239 function getRegex() {
240 if ($this->mRegex
== '' ) {
243 return $this->mRegex
;
247 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
248 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
249 * the complete expression
251 function getRegexCase() {
252 if ( $this->mRegex
=== '' )
255 return $this->mCaseSensitive ?
'' : 'i';
259 * Gets a regex matching the word, if it is at the string start
261 function getRegexStart() {
262 if ($this->mRegex
== '' ) {
265 return $this->mRegexStart
;
269 * regex without the slashes and what not
271 function getBaseRegex() {
272 if ($this->mRegex
== '') {
275 return $this->mBaseRegex
;
279 * Returns true if the text contains the word
282 function match( $text ) {
283 return preg_match( $this->getRegex(), $text );
287 * Returns true if the text starts with the word
290 function matchStart( $text ) {
291 return preg_match( $this->getRegexStart(), $text );
295 * Returns NULL if there's no match, the value of $1 otherwise
296 * The return code is the matched string, if there's no variable
297 * part in the regex and the matched variable part ($1) if there
300 function matchVariableStartToEnd( $text ) {
302 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
303 if ( $matchcount == 0 ) {
305 } elseif ( count($matches) == 1 ) {
308 # multiple matched parts (variable match); some will be empty because of synonyms
309 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
310 $matches = array_values(array_filter($matches));
317 * Returns true if the text matches the word, and alters the
318 * input string, removing all instances of the word
320 function matchAndRemove( &$text ) {
321 global $wgMagicFound;
322 $wgMagicFound = false;
323 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
324 return $wgMagicFound;
327 function matchStartAndRemove( &$text ) {
328 global $wgMagicFound;
329 $wgMagicFound = false;
330 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
331 return $wgMagicFound;
336 * Replaces the word with something else
338 function replace( $replacement, $subject, $limit=-1 ) {
339 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
340 $this->mModified
= !($res === $subject);
345 * Variable handling: {{SUBST:xxx}} style words
346 * Calls back a function to determine what to replace xxx with
347 * Input word must contain $1
349 function substituteCallback( $text, $callback ) {
350 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
351 $this->mModified
= !($res === $text);
356 * Matches the word, where $1 is a wildcard
358 function getVariableRegex() {
359 if ( $this->mVariableRegex
== '' ) {
362 return $this->mVariableRegex
;
366 * Matches the entire string, where $1 is a wildcard
368 function getVariableStartToEndRegex() {
369 if ( $this->mVariableStartToEndRegex
== '' ) {
372 return $this->mVariableStartToEndRegex
;
376 * Accesses the synonym list directly
378 function getSynonym( $i ) {
379 return $this->mSynonyms
[$i];
383 * Returns true if the last call to replace() or substituteCallback()
384 * returned a modified text, otherwise false.
386 function getWasModified(){
387 return $this->mModified
;
391 * $magicarr is an associative array of (magic word ID => replacement)
392 * This method uses the php feature to do several replacements at the same time,
393 * thereby gaining some efficiency. The result is placed in the out variable
394 * $result. The return value is true if something was replaced.
397 function replaceMultiple( $magicarr, $subject, &$result ){
400 foreach( $magicarr as $id => $replacement ){
401 $mw = MagicWord
::get( $id );
402 $search[] = $mw->getRegex();
403 $replace[] = $replacement;
406 $result = preg_replace( $search, $replace, $subject );
407 return !($result === $subject);
411 * Adds all the synonyms of this MagicWord to an array, to allow quick
412 * lookup in a list of magic words
414 function addToArray( &$array, $value ) {
415 foreach ( $this->mSynonyms
as $syn ) {
416 $array[$syn] = $value;
422 * Used in matchAndRemove()
425 function pregRemoveAndRecord( $match ) {
426 global $wgMagicFound;
427 $wgMagicFound = true;