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',
101 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
102 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
104 for ( $i = 0; $i < count( $magicWords ); ++
$i )
105 define( $magicWords[$i], $i );
107 $wgVariableIDs = array(
109 MAG_CURRENTMONTHNAME
,
110 MAG_CURRENTMONTHNAMEGEN
,
111 MAG_CURRENTMONTHABBREV
,
117 MAG_NUMBEROFARTICLES
,
142 MAG_SUBJECTPAGENAMEE
,
151 MAG_CURRENTTIMESTAMP
,
155 MAG_PAGESINNAMESPACE
,
158 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
159 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
162 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
164 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
166 * Possible future improvements:
167 * * Simultaneous searching for a number of magic words
168 * * $wgMagicWords in shared memory
170 * Please avoid reading the data out of one of these objects and then writing
171 * special case code. If possible, add another match()-like function here.
179 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
180 var $mRegexStart, $mBaseRegex, $mVariableRegex;
184 function MagicWord($id = 0, $syn = '', $cs = false) {
186 $this->mSynonyms
= (array)$syn;
187 $this->mCaseSensitive
= $cs;
189 $this->mRegexStart
= '';
190 $this->mVariableRegex
= '';
191 $this->mVariableStartToEndRegex
= '';
192 $this->mModified
= false;
196 * Factory: creates an object representing an ID
199 function &get( $id ) {
200 global $wgMagicWords;
202 if ( !is_array( $wgMagicWords ) ) {
203 throw new MWException( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
205 if (!array_key_exists( $id, $wgMagicWords ) ) {
206 $mw = new MagicWord();
208 $wgMagicWords[$id] = $mw;
210 return $wgMagicWords[$id];
213 # Initialises this object with an ID
214 function load( $id ) {
217 $wgContLang->getMagic( $this );
221 * Preliminary initialisation
224 function initRegex() {
225 #$variableClass = Title::legalChars();
226 # This was used for matching "$1" variables, but different uses of the feature will have
227 # different restrictions, which should be checked *after* the MagicWord has been matched,
231 foreach ( $this->mSynonyms
as $synonym )
232 // In case a magic word contains /, like that's going to happen;)
233 $escSyn[] = preg_quote( $synonym, '/' );
234 $this->mBaseRegex
= implode( '|', $escSyn );
236 $case = $this->mCaseSensitive ?
'' : 'i';
237 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
238 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
239 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
240 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
241 "/^(?:{$this->mBaseRegex})$/{$case}" );
245 * Gets a regex representing matching the word
247 function getRegex() {
248 if ($this->mRegex
== '' ) {
251 return $this->mRegex
;
255 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
256 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
257 * the complete expression
259 function getRegexCase() {
260 if ( $this->mRegex
=== '' )
263 return $this->mCaseSensitive ?
'' : 'i';
267 * Gets a regex matching the word, if it is at the string start
269 function getRegexStart() {
270 if ($this->mRegex
== '' ) {
273 return $this->mRegexStart
;
277 * regex without the slashes and what not
279 function getBaseRegex() {
280 if ($this->mRegex
== '') {
283 return $this->mBaseRegex
;
287 * Returns true if the text contains the word
290 function match( $text ) {
291 return preg_match( $this->getRegex(), $text );
295 * Returns true if the text starts with the word
298 function matchStart( $text ) {
299 return preg_match( $this->getRegexStart(), $text );
303 * Returns NULL if there's no match, the value of $1 otherwise
304 * The return code is the matched string, if there's no variable
305 * part in the regex and the matched variable part ($1) if there
308 function matchVariableStartToEnd( $text ) {
310 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
311 if ( $matchcount == 0 ) {
313 } elseif ( count($matches) == 1 ) {
316 # multiple matched parts (variable match); some will be empty because of
317 # synonyms. The variable will be the second non-empty one so remove any
318 # blank elements and re-sort the indices.
319 $matches = array_values(array_filter($matches));
326 * Returns true if the text matches the word, and alters the
327 * input string, removing all instances of the word
329 function matchAndRemove( &$text ) {
330 global $wgMagicFound;
331 $wgMagicFound = false;
332 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
333 return $wgMagicFound;
336 function matchStartAndRemove( &$text ) {
337 global $wgMagicFound;
338 $wgMagicFound = false;
339 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
340 return $wgMagicFound;
345 * Replaces the word with something else
347 function replace( $replacement, $subject, $limit=-1 ) {
348 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
349 $this->mModified
= !($res === $subject);
354 * Variable handling: {{SUBST:xxx}} style words
355 * Calls back a function to determine what to replace xxx with
356 * Input word must contain $1
358 function substituteCallback( $text, $callback ) {
359 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
360 $this->mModified
= !($res === $text);
365 * Matches the word, where $1 is a wildcard
367 function getVariableRegex() {
368 if ( $this->mVariableRegex
== '' ) {
371 return $this->mVariableRegex
;
375 * Matches the entire string, where $1 is a wildcard
377 function getVariableStartToEndRegex() {
378 if ( $this->mVariableStartToEndRegex
== '' ) {
381 return $this->mVariableStartToEndRegex
;
385 * Accesses the synonym list directly
387 function getSynonym( $i ) {
388 return $this->mSynonyms
[$i];
391 function getSynonyms() {
392 return $this->mSynonyms
;
396 * Returns true if the last call to replace() or substituteCallback()
397 * returned a modified text, otherwise false.
399 function getWasModified(){
400 return $this->mModified
;
404 * $magicarr is an associative array of (magic word ID => replacement)
405 * This method uses the php feature to do several replacements at the same time,
406 * thereby gaining some efficiency. The result is placed in the out variable
407 * $result. The return value is true if something was replaced.
410 function replaceMultiple( $magicarr, $subject, &$result ){
413 foreach( $magicarr as $id => $replacement ){
414 $mw = MagicWord
::get( $id );
415 $search[] = $mw->getRegex();
416 $replace[] = $replacement;
419 $result = preg_replace( $search, $replace, $subject );
420 return !($result === $subject);
424 * Adds all the synonyms of this MagicWord to an array, to allow quick
425 * lookup in a list of magic words
427 function addToArray( &$array, $value ) {
428 foreach ( $this->mSynonyms
as $syn ) {
429 $array[$syn] = $value;
433 function isCaseSensitive() {
434 return $this->mCaseSensitive
;
439 * Used in matchAndRemove()
442 function pregRemoveAndRecord( $match ) {
443 global $wgMagicFound;
444 $wgMagicFound = true;