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 synonyms
317 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
318 $matches = array_values(array_filter($matches));
325 * Returns true if the text matches the word, and alters the
326 * input string, removing all instances of the word
328 function matchAndRemove( &$text ) {
329 global $wgMagicFound;
330 $wgMagicFound = false;
331 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
332 return $wgMagicFound;
335 function matchStartAndRemove( &$text ) {
336 global $wgMagicFound;
337 $wgMagicFound = false;
338 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
339 return $wgMagicFound;
344 * Replaces the word with something else
346 function replace( $replacement, $subject, $limit=-1 ) {
347 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
348 $this->mModified
= !($res === $subject);
353 * Variable handling: {{SUBST:xxx}} style words
354 * Calls back a function to determine what to replace xxx with
355 * Input word must contain $1
357 function substituteCallback( $text, $callback ) {
358 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
359 $this->mModified
= !($res === $text);
364 * Matches the word, where $1 is a wildcard
366 function getVariableRegex() {
367 if ( $this->mVariableRegex
== '' ) {
370 return $this->mVariableRegex
;
374 * Matches the entire string, where $1 is a wildcard
376 function getVariableStartToEndRegex() {
377 if ( $this->mVariableStartToEndRegex
== '' ) {
380 return $this->mVariableStartToEndRegex
;
384 * Accesses the synonym list directly
386 function getSynonym( $i ) {
387 return $this->mSynonyms
[$i];
390 function getSynonyms() {
391 return $this->mSynonyms
;
395 * Returns true if the last call to replace() or substituteCallback()
396 * returned a modified text, otherwise false.
398 function getWasModified(){
399 return $this->mModified
;
403 * $magicarr is an associative array of (magic word ID => replacement)
404 * This method uses the php feature to do several replacements at the same time,
405 * thereby gaining some efficiency. The result is placed in the out variable
406 * $result. The return value is true if something was replaced.
409 function replaceMultiple( $magicarr, $subject, &$result ){
412 foreach( $magicarr as $id => $replacement ){
413 $mw = MagicWord
::get( $id );
414 $search[] = $mw->getRegex();
415 $replace[] = $replacement;
418 $result = preg_replace( $search, $replace, $subject );
419 return !($result === $subject);
423 * Adds all the synonyms of this MagicWord to an array, to allow quick
424 * lookup in a list of magic words
426 function addToArray( &$array, $value ) {
427 foreach ( $this->mSynonyms
as $syn ) {
428 $array[$syn] = $value;
434 * Used in matchAndRemove()
437 function pregRemoveAndRecord( $match ) {
438 global $wgMagicFound;
439 $wgMagicFound = true;