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',
93 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
94 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
96 for ( $i = 0; $i < count( $magicWords ); ++
$i )
97 define( $magicWords[$i], $i );
99 $wgVariableIDs = array(
101 MAG_CURRENTMONTHNAME
,
102 MAG_CURRENTMONTHNAMEGEN
,
103 MAG_CURRENTMONTHABBREV
,
109 MAG_NUMBEROFARTICLES
,
134 MAG_SUBJECTPAGENAMEE
,
144 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
145 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
148 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
150 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
152 * Possible future improvements:
153 * * Simultaneous searching for a number of magic words
154 * * $wgMagicWords in shared memory
156 * Please avoid reading the data out of one of these objects and then writing
157 * special case code. If possible, add another match()-like function here.
165 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
166 var $mRegexStart, $mBaseRegex, $mVariableRegex;
170 function MagicWord($id = 0, $syn = '', $cs = false) {
172 $this->mSynonyms
= (array)$syn;
173 $this->mCaseSensitive
= $cs;
175 $this->mRegexStart
= '';
176 $this->mVariableRegex
= '';
177 $this->mVariableStartToEndRegex
= '';
178 $this->mModified
= false;
182 * Factory: creates an object representing an ID
185 function &get( $id ) {
186 global $wgMagicWords;
188 if ( !is_array( $wgMagicWords ) ) {
189 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
191 if (!array_key_exists( $id, $wgMagicWords ) ) {
192 $mw = new MagicWord();
194 $wgMagicWords[$id] = $mw;
196 return $wgMagicWords[$id];
199 # Initialises this object with an ID
200 function load( $id ) {
203 $wgContLang->getMagic( $this );
207 * Preliminary initialisation
210 function initRegex() {
211 #$variableClass = Title::legalChars();
212 # This was used for matching "$1" variables, but different uses of the feature will have
213 # different restrictions, which should be checked *after* the MagicWord has been matched,
217 foreach ( $this->mSynonyms
as $synonym )
218 // In case a magic word contains /, like that's going to happen;)
219 $escSyn[] = preg_quote( $synonym, '/' );
220 $this->mBaseRegex
= implode( '|', $escSyn );
222 $case = $this->mCaseSensitive ?
'' : 'i';
223 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
224 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
225 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
226 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
227 "/^(?:{$this->mBaseRegex})$/{$case}" );
231 * Gets a regex representing matching the word
233 function getRegex() {
234 if ($this->mRegex
== '' ) {
237 return $this->mRegex
;
241 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
242 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
243 * the complete expression
245 function getRegexCase() {
246 if ( $this->mRegex
=== '' )
249 return $this->mCaseSensitive ?
'' : 'i';
253 * Gets a regex matching the word, if it is at the string start
255 function getRegexStart() {
256 if ($this->mRegex
== '' ) {
259 return $this->mRegexStart
;
263 * regex without the slashes and what not
265 function getBaseRegex() {
266 if ($this->mRegex
== '') {
269 return $this->mBaseRegex
;
273 * Returns true if the text contains the word
276 function match( $text ) {
277 return preg_match( $this->getRegex(), $text );
281 * Returns true if the text starts with the word
284 function matchStart( $text ) {
285 return preg_match( $this->getRegexStart(), $text );
289 * Returns NULL if there's no match, the value of $1 otherwise
290 * The return code is the matched string, if there's no variable
291 * part in the regex and the matched variable part ($1) if there
294 function matchVariableStartToEnd( $text ) {
296 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
297 if ( $matchcount == 0 ) {
299 } elseif ( count($matches) == 1 ) {
302 # multiple matched parts (variable match); some will be empty because of synonyms
303 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
304 $matches = array_values(array_filter($matches));
311 * Returns true if the text matches the word, and alters the
312 * input string, removing all instances of the word
314 function matchAndRemove( &$text ) {
315 global $wgMagicFound;
316 $wgMagicFound = false;
317 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
318 return $wgMagicFound;
321 function matchStartAndRemove( &$text ) {
322 global $wgMagicFound;
323 $wgMagicFound = false;
324 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
325 return $wgMagicFound;
330 * Replaces the word with something else
332 function replace( $replacement, $subject ) {
333 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
334 $this->mModified
= !($res === $subject);
339 * Variable handling: {{SUBST:xxx}} style words
340 * Calls back a function to determine what to replace xxx with
341 * Input word must contain $1
343 function substituteCallback( $text, $callback ) {
344 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
345 $this->mModified
= !($res === $text);
350 * Matches the word, where $1 is a wildcard
352 function getVariableRegex() {
353 if ( $this->mVariableRegex
== '' ) {
356 return $this->mVariableRegex
;
360 * Matches the entire string, where $1 is a wildcard
362 function getVariableStartToEndRegex() {
363 if ( $this->mVariableStartToEndRegex
== '' ) {
366 return $this->mVariableStartToEndRegex
;
370 * Accesses the synonym list directly
372 function getSynonym( $i ) {
373 return $this->mSynonyms
[$i];
377 * Returns true if the last call to replace() or substituteCallback()
378 * returned a modified text, otherwise false.
380 function getWasModified(){
381 return $this->mModified
;
385 * $magicarr is an associative array of (magic word ID => replacement)
386 * This method uses the php feature to do several replacements at the same time,
387 * thereby gaining some efficiency. The result is placed in the out variable
388 * $result. The return value is true if something was replaced.
391 function replaceMultiple( $magicarr, $subject, &$result ){
394 foreach( $magicarr as $id => $replacement ){
395 $mw = MagicWord
::get( $id );
396 $search[] = $mw->getRegex();
397 $replace[] = $replacement;
400 $result = preg_replace( $search, $replace, $subject );
401 return !($result === $subject);
405 * Adds all the synonyms of this MagicWord to an array, to allow quick
406 * lookup in a list of magic words
408 function addToArray( &$array, $value ) {
409 foreach ( $this->mSynonyms
as $syn ) {
410 $array[$syn] = $value;
416 * Used in matchAndRemove()
419 function pregRemoveAndRecord( $match ) {
420 global $wgMagicFound;
421 $wgMagicFound = true;