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',
88 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
89 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
91 for ( $i = 0; $i < count( $magicWords ); ++
$i )
92 define( $magicWords[$i], $i );
94 $wgVariableIDs = array(
97 MAG_CURRENTMONTHNAMEGEN
,
98 MAG_CURRENTMONTHABBREV
,
104 MAG_NUMBEROFARTICLES
,
129 MAG_SUBJECTPAGENAMEE
,
134 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
135 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
138 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
140 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
142 * Possible future improvements:
143 * * Simultaneous searching for a number of magic words
144 * * $wgMagicWords in shared memory
146 * Please avoid reading the data out of one of these objects and then writing
147 * special case code. If possible, add another match()-like function here.
155 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
156 var $mRegexStart, $mBaseRegex, $mVariableRegex;
160 function MagicWord($id = 0, $syn = '', $cs = false) {
162 $this->mSynonyms
= (array)$syn;
163 $this->mCaseSensitive
= $cs;
165 $this->mRegexStart
= '';
166 $this->mVariableRegex
= '';
167 $this->mVariableStartToEndRegex
= '';
168 $this->mModified
= false;
172 * Factory: creates an object representing an ID
175 function &get( $id ) {
176 global $wgMagicWords;
178 if ( !is_array( $wgMagicWords ) ) {
179 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
181 if (!array_key_exists( $id, $wgMagicWords ) ) {
182 $mw = new MagicWord();
184 $wgMagicWords[$id] = $mw;
186 return $wgMagicWords[$id];
189 # Initialises this object with an ID
190 function load( $id ) {
193 $wgContLang->getMagic( $this );
197 * Preliminary initialisation
200 function initRegex() {
201 #$variableClass = Title::legalChars();
202 # This was used for matching "$1" variables, but different uses of the feature will have
203 # different restrictions, which should be checked *after* the MagicWord has been matched,
207 foreach ( $this->mSynonyms
as $synonym )
208 // In case a magic word contains /, like that's going to happen;)
209 $escSyn[] = preg_quote( $synonym, '/' );
210 $this->mBaseRegex
= implode( '|', $escSyn );
212 $case = $this->mCaseSensitive ?
'' : 'i';
213 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
214 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
215 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
216 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
217 "/^(?:{$this->mBaseRegex})$/{$case}" );
221 * Gets a regex representing matching the word
223 function getRegex() {
224 if ($this->mRegex
== '' ) {
227 return $this->mRegex
;
231 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
232 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
233 * the complete expression
235 function getRegexCase() {
236 if ( $this->mRegex
=== '' )
239 return $this->mCaseSensitive ?
'' : 'i';
243 * Gets a regex matching the word, if it is at the string start
245 function getRegexStart() {
246 if ($this->mRegex
== '' ) {
249 return $this->mRegexStart
;
253 * regex without the slashes and what not
255 function getBaseRegex() {
256 if ($this->mRegex
== '') {
259 return $this->mBaseRegex
;
263 * Returns true if the text contains the word
266 function match( $text ) {
267 return preg_match( $this->getRegex(), $text );
271 * Returns true if the text starts with the word
274 function matchStart( $text ) {
275 return preg_match( $this->getRegexStart(), $text );
279 * Returns NULL if there's no match, the value of $1 otherwise
280 * The return code is the matched string, if there's no variable
281 * part in the regex and the matched variable part ($1) if there
284 function matchVariableStartToEnd( $text ) {
286 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
287 if ( $matchcount == 0 ) {
289 } elseif ( count($matches) == 1 ) {
292 # multiple matched parts (variable match); some will be empty because of synonyms
293 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
294 $matches = array_values(array_filter($matches));
301 * Returns true if the text matches the word, and alters the
302 * input string, removing all instances of the word
304 function matchAndRemove( &$text ) {
305 global $wgMagicFound;
306 $wgMagicFound = false;
307 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
308 return $wgMagicFound;
311 function matchStartAndRemove( &$text ) {
312 global $wgMagicFound;
313 $wgMagicFound = false;
314 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
315 return $wgMagicFound;
320 * Replaces the word with something else
322 function replace( $replacement, $subject ) {
323 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
324 $this->mModified
= !($res === $subject);
329 * Variable handling: {{SUBST:xxx}} style words
330 * Calls back a function to determine what to replace xxx with
331 * Input word must contain $1
333 function substituteCallback( $text, $callback ) {
334 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
335 $this->mModified
= !($res === $text);
340 * Matches the word, where $1 is a wildcard
342 function getVariableRegex() {
343 if ( $this->mVariableRegex
== '' ) {
346 return $this->mVariableRegex
;
350 * Matches the entire string, where $1 is a wildcard
352 function getVariableStartToEndRegex() {
353 if ( $this->mVariableStartToEndRegex
== '' ) {
356 return $this->mVariableStartToEndRegex
;
360 * Accesses the synonym list directly
362 function getSynonym( $i ) {
363 return $this->mSynonyms
[$i];
367 * Returns true if the last call to replace() or substituteCallback()
368 * returned a modified text, otherwise false.
370 function getWasModified(){
371 return $this->mModified
;
375 * $magicarr is an associative array of (magic word ID => replacement)
376 * This method uses the php feature to do several replacements at the same time,
377 * thereby gaining some efficiency. The result is placed in the out variable
378 * $result. The return value is true if something was replaced.
381 function replaceMultiple( $magicarr, $subject, &$result ){
384 foreach( $magicarr as $id => $replacement ){
385 $mw = MagicWord
::get( $id );
386 $search[] = $mw->getRegex();
387 $replace[] = $replacement;
390 $result = preg_replace( $search, $replace, $subject );
391 return !($result === $subject);
395 * Adds all the synonyms of this MagicWord to an array, to allow quick
396 * lookup in a list of magic words
398 function addToArray( &$array, $value ) {
399 foreach ( $this->mSynonyms
as $syn ) {
400 $array[$syn] = $value;
406 * Used in matchAndRemove()
409 function pregRemoveAndRecord( $match ) {
410 global $wgMagicFound;
411 $wgMagicFound = true;