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 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
93 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
95 for ( $i = 0; $i < count( $magicWords ); ++
$i )
96 define( $magicWords[$i], $i );
98 $wgVariableIDs = array(
100 MAG_CURRENTMONTHNAME
,
101 MAG_CURRENTMONTHNAMEGEN
,
102 MAG_CURRENTMONTHABBREV
,
108 MAG_NUMBEROFARTICLES
,
133 MAG_SUBJECTPAGENAMEE
,
142 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
143 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
146 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
148 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
150 * Possible future improvements:
151 * * Simultaneous searching for a number of magic words
152 * * $wgMagicWords in shared memory
154 * Please avoid reading the data out of one of these objects and then writing
155 * special case code. If possible, add another match()-like function here.
163 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
164 var $mRegexStart, $mBaseRegex, $mVariableRegex;
168 function MagicWord($id = 0, $syn = '', $cs = false) {
170 $this->mSynonyms
= (array)$syn;
171 $this->mCaseSensitive
= $cs;
173 $this->mRegexStart
= '';
174 $this->mVariableRegex
= '';
175 $this->mVariableStartToEndRegex
= '';
176 $this->mModified
= false;
180 * Factory: creates an object representing an ID
183 function &get( $id ) {
184 global $wgMagicWords;
186 if ( !is_array( $wgMagicWords ) ) {
187 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
189 if (!array_key_exists( $id, $wgMagicWords ) ) {
190 $mw = new MagicWord();
192 $wgMagicWords[$id] = $mw;
194 return $wgMagicWords[$id];
197 # Initialises this object with an ID
198 function load( $id ) {
201 $wgContLang->getMagic( $this );
205 * Preliminary initialisation
208 function initRegex() {
209 #$variableClass = Title::legalChars();
210 # This was used for matching "$1" variables, but different uses of the feature will have
211 # different restrictions, which should be checked *after* the MagicWord has been matched,
215 foreach ( $this->mSynonyms
as $synonym )
216 // In case a magic word contains /, like that's going to happen;)
217 $escSyn[] = preg_quote( $synonym, '/' );
218 $this->mBaseRegex
= implode( '|', $escSyn );
220 $case = $this->mCaseSensitive ?
'' : 'i';
221 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
222 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
223 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
224 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
225 "/^(?:{$this->mBaseRegex})$/{$case}" );
229 * Gets a regex representing matching the word
231 function getRegex() {
232 if ($this->mRegex
== '' ) {
235 return $this->mRegex
;
239 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
240 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
241 * the complete expression
243 function getRegexCase() {
244 if ( $this->mRegex
=== '' )
247 return $this->mCaseSensitive ?
'' : 'i';
251 * Gets a regex matching the word, if it is at the string start
253 function getRegexStart() {
254 if ($this->mRegex
== '' ) {
257 return $this->mRegexStart
;
261 * regex without the slashes and what not
263 function getBaseRegex() {
264 if ($this->mRegex
== '') {
267 return $this->mBaseRegex
;
271 * Returns true if the text contains the word
274 function match( $text ) {
275 return preg_match( $this->getRegex(), $text );
279 * Returns true if the text starts with the word
282 function matchStart( $text ) {
283 return preg_match( $this->getRegexStart(), $text );
287 * Returns NULL if there's no match, the value of $1 otherwise
288 * The return code is the matched string, if there's no variable
289 * part in the regex and the matched variable part ($1) if there
292 function matchVariableStartToEnd( $text ) {
294 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
295 if ( $matchcount == 0 ) {
297 } elseif ( count($matches) == 1 ) {
300 # multiple matched parts (variable match); some will be empty because of synonyms
301 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
302 $matches = array_values(array_filter($matches));
309 * Returns true if the text matches the word, and alters the
310 * input string, removing all instances of the word
312 function matchAndRemove( &$text ) {
313 global $wgMagicFound;
314 $wgMagicFound = false;
315 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
316 return $wgMagicFound;
319 function matchStartAndRemove( &$text ) {
320 global $wgMagicFound;
321 $wgMagicFound = false;
322 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
323 return $wgMagicFound;
328 * Replaces the word with something else
330 function replace( $replacement, $subject ) {
331 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
332 $this->mModified
= !($res === $subject);
337 * Variable handling: {{SUBST:xxx}} style words
338 * Calls back a function to determine what to replace xxx with
339 * Input word must contain $1
341 function substituteCallback( $text, $callback ) {
342 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
343 $this->mModified
= !($res === $text);
348 * Matches the word, where $1 is a wildcard
350 function getVariableRegex() {
351 if ( $this->mVariableRegex
== '' ) {
354 return $this->mVariableRegex
;
358 * Matches the entire string, where $1 is a wildcard
360 function getVariableStartToEndRegex() {
361 if ( $this->mVariableStartToEndRegex
== '' ) {
364 return $this->mVariableStartToEndRegex
;
368 * Accesses the synonym list directly
370 function getSynonym( $i ) {
371 return $this->mSynonyms
[$i];
375 * Returns true if the last call to replace() or substituteCallback()
376 * returned a modified text, otherwise false.
378 function getWasModified(){
379 return $this->mModified
;
383 * $magicarr is an associative array of (magic word ID => replacement)
384 * This method uses the php feature to do several replacements at the same time,
385 * thereby gaining some efficiency. The result is placed in the out variable
386 * $result. The return value is true if something was replaced.
389 function replaceMultiple( $magicarr, $subject, &$result ){
392 foreach( $magicarr as $id => $replacement ){
393 $mw = MagicWord
::get( $id );
394 $search[] = $mw->getRegex();
395 $replace[] = $replacement;
398 $result = preg_replace( $search, $replace, $subject );
399 return !($result === $subject);
403 * Adds all the synonyms of this MagicWord to an array, to allow quick
404 * lookup in a list of magic words
406 function addToArray( &$array, $value ) {
407 foreach ( $this->mSynonyms
as $syn ) {
408 $array[$syn] = $value;
414 * Used in matchAndRemove()
417 function pregRemoveAndRecord( $match ) {
418 global $wgMagicFound;
419 $wgMagicFound = true;