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',
85 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
86 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
88 for ( $i = 0; $i < count( $magicWords ); ++
$i )
89 define( $magicWords[$i], $i );
91 $wgVariableIDs = array(
94 MAG_CURRENTMONTHNAMEGEN
,
95 MAG_CURRENTMONTHABBREV
,
101 MAG_NUMBEROFARTICLES
,
126 MAG_SUBJECTPAGENAMEE
,
128 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
129 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
132 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
134 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
136 * Possible future improvements:
137 * * Simultaneous searching for a number of magic words
138 * * $wgMagicWords in shared memory
140 * Please avoid reading the data out of one of these objects and then writing
141 * special case code. If possible, add another match()-like function here.
149 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
150 var $mRegexStart, $mBaseRegex, $mVariableRegex;
154 function MagicWord($id = 0, $syn = '', $cs = false) {
156 $this->mSynonyms
= (array)$syn;
157 $this->mCaseSensitive
= $cs;
159 $this->mRegexStart
= '';
160 $this->mVariableRegex
= '';
161 $this->mVariableStartToEndRegex
= '';
162 $this->mModified
= false;
166 * Factory: creates an object representing an ID
169 function &get( $id ) {
170 global $wgMagicWords;
172 if ( !is_array( $wgMagicWords ) ) {
173 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
175 if (!array_key_exists( $id, $wgMagicWords ) ) {
176 $mw = new MagicWord();
178 $wgMagicWords[$id] = $mw;
180 return $wgMagicWords[$id];
183 # Initialises this object with an ID
184 function load( $id ) {
187 $wgContLang->getMagic( $this );
191 * Preliminary initialisation
194 function initRegex() {
195 #$variableClass = Title::legalChars();
196 # This was used for matching "$1" variables, but different uses of the feature will have
197 # different restrictions, which should be checked *after* the MagicWord has been matched,
201 foreach ( $this->mSynonyms
as $synonym )
202 // In case a magic word contains /, like that's going to happen;)
203 $escSyn[] = preg_quote( $synonym, '/' );
204 $this->mBaseRegex
= implode( '|', $escSyn );
206 $case = $this->mCaseSensitive ?
'' : 'i';
207 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
208 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
209 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
210 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
211 "/^(?:{$this->mBaseRegex})$/{$case}" );
215 * Gets a regex representing matching the word
217 function getRegex() {
218 if ($this->mRegex
== '' ) {
221 return $this->mRegex
;
225 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
226 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
227 * the complete expression
229 function getRegexCase() {
230 if ( $this->mRegex
=== '' )
233 return $this->mCaseSensitive ?
'' : 'i';
237 * Gets a regex matching the word, if it is at the string start
239 function getRegexStart() {
240 if ($this->mRegex
== '' ) {
243 return $this->mRegexStart
;
247 * regex without the slashes and what not
249 function getBaseRegex() {
250 if ($this->mRegex
== '') {
253 return $this->mBaseRegex
;
257 * Returns true if the text contains the word
260 function match( $text ) {
261 return preg_match( $this->getRegex(), $text );
265 * Returns true if the text starts with the word
268 function matchStart( $text ) {
269 return preg_match( $this->getRegexStart(), $text );
273 * Returns NULL if there's no match, the value of $1 otherwise
274 * The return code is the matched string, if there's no variable
275 * part in the regex and the matched variable part ($1) if there
278 function matchVariableStartToEnd( $text ) {
280 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
281 if ( $matchcount == 0 ) {
283 } elseif ( count($matches) == 1 ) {
286 # multiple matched parts (variable match); some will be empty because of synonyms
287 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
288 $matches = array_values(array_filter($matches));
295 * Returns true if the text matches the word, and alters the
296 * input string, removing all instances of the word
298 function matchAndRemove( &$text ) {
299 global $wgMagicFound;
300 $wgMagicFound = false;
301 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
302 return $wgMagicFound;
305 function matchStartAndRemove( &$text ) {
306 global $wgMagicFound;
307 $wgMagicFound = false;
308 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
309 return $wgMagicFound;
314 * Replaces the word with something else
316 function replace( $replacement, $subject ) {
317 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
318 $this->mModified
= !($res === $subject);
323 * Variable handling: {{SUBST:xxx}} style words
324 * Calls back a function to determine what to replace xxx with
325 * Input word must contain $1
327 function substituteCallback( $text, $callback ) {
328 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
329 $this->mModified
= !($res === $text);
334 * Matches the word, where $1 is a wildcard
336 function getVariableRegex() {
337 if ( $this->mVariableRegex
== '' ) {
340 return $this->mVariableRegex
;
344 * Matches the entire string, where $1 is a wildcard
346 function getVariableStartToEndRegex() {
347 if ( $this->mVariableStartToEndRegex
== '' ) {
350 return $this->mVariableStartToEndRegex
;
354 * Accesses the synonym list directly
356 function getSynonym( $i ) {
357 return $this->mSynonyms
[$i];
361 * Returns true if the last call to replace() or substituteCallback()
362 * returned a modified text, otherwise false.
364 function getWasModified(){
365 return $this->mModified
;
369 * $magicarr is an associative array of (magic word ID => replacement)
370 * This method uses the php feature to do several replacements at the same time,
371 * thereby gaining some efficiency. The result is placed in the out variable
372 * $result. The return value is true if something was replaced.
375 function replaceMultiple( $magicarr, $subject, &$result ){
378 foreach( $magicarr as $id => $replacement ){
379 $mw = MagicWord
::get( $id );
380 $search[] = $mw->getRegex();
381 $replace[] = $replacement;
384 $result = preg_replace( $search, $replace, $subject );
385 return !($result === $subject);
389 * Adds all the synonyms of this MagicWord to an array, to allow quick
390 * lookup in a list of magic words
392 function addToArray( &$array, $value ) {
393 foreach ( $this->mSynonyms
as $syn ) {
394 $array[$syn] = $value;
400 * Used in matchAndRemove()
403 function pregRemoveAndRecord( $match ) {
404 global $wgMagicFound;
405 $wgMagicFound = true;