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',
97 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
98 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
100 for ( $i = 0; $i < count( $magicWords ); ++
$i )
101 define( $magicWords[$i], $i );
103 $wgVariableIDs = array(
105 MAG_CURRENTMONTHNAME
,
106 MAG_CURRENTMONTHNAMEGEN
,
107 MAG_CURRENTMONTHABBREV
,
113 MAG_NUMBEROFARTICLES
,
138 MAG_SUBJECTPAGENAMEE
,
147 MAG_CURRENTTIMESTAMP
,
152 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
153 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
156 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
158 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
160 * Possible future improvements:
161 * * Simultaneous searching for a number of magic words
162 * * $wgMagicWords in shared memory
164 * Please avoid reading the data out of one of these objects and then writing
165 * special case code. If possible, add another match()-like function here.
173 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
174 var $mRegexStart, $mBaseRegex, $mVariableRegex;
178 function MagicWord($id = 0, $syn = '', $cs = false) {
180 $this->mSynonyms
= (array)$syn;
181 $this->mCaseSensitive
= $cs;
183 $this->mRegexStart
= '';
184 $this->mVariableRegex
= '';
185 $this->mVariableStartToEndRegex
= '';
186 $this->mModified
= false;
190 * Factory: creates an object representing an ID
193 function &get( $id ) {
194 global $wgMagicWords;
196 if ( !is_array( $wgMagicWords ) ) {
197 throw new MWException( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
199 if (!array_key_exists( $id, $wgMagicWords ) ) {
200 $mw = new MagicWord();
202 $wgMagicWords[$id] = $mw;
204 return $wgMagicWords[$id];
207 # Initialises this object with an ID
208 function load( $id ) {
211 $wgContLang->getMagic( $this );
215 * Preliminary initialisation
218 function initRegex() {
219 #$variableClass = Title::legalChars();
220 # This was used for matching "$1" variables, but different uses of the feature will have
221 # different restrictions, which should be checked *after* the MagicWord has been matched,
225 foreach ( $this->mSynonyms
as $synonym )
226 // In case a magic word contains /, like that's going to happen;)
227 $escSyn[] = preg_quote( $synonym, '/' );
228 $this->mBaseRegex
= implode( '|', $escSyn );
230 $case = $this->mCaseSensitive ?
'' : 'i';
231 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
232 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
233 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
234 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
235 "/^(?:{$this->mBaseRegex})$/{$case}" );
239 * Gets a regex representing matching the word
241 function getRegex() {
242 if ($this->mRegex
== '' ) {
245 return $this->mRegex
;
249 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
250 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
251 * the complete expression
253 function getRegexCase() {
254 if ( $this->mRegex
=== '' )
257 return $this->mCaseSensitive ?
'' : 'i';
261 * Gets a regex matching the word, if it is at the string start
263 function getRegexStart() {
264 if ($this->mRegex
== '' ) {
267 return $this->mRegexStart
;
271 * regex without the slashes and what not
273 function getBaseRegex() {
274 if ($this->mRegex
== '') {
277 return $this->mBaseRegex
;
281 * Returns true if the text contains the word
284 function match( $text ) {
285 return preg_match( $this->getRegex(), $text );
289 * Returns true if the text starts with the word
292 function matchStart( $text ) {
293 return preg_match( $this->getRegexStart(), $text );
297 * Returns NULL if there's no match, the value of $1 otherwise
298 * The return code is the matched string, if there's no variable
299 * part in the regex and the matched variable part ($1) if there
302 function matchVariableStartToEnd( $text ) {
304 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
305 if ( $matchcount == 0 ) {
307 } elseif ( count($matches) == 1 ) {
310 # multiple matched parts (variable match); some will be empty because of synonyms
311 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
312 $matches = array_values(array_filter($matches));
319 * Returns true if the text matches the word, and alters the
320 * input string, removing all instances of the word
322 function matchAndRemove( &$text ) {
323 global $wgMagicFound;
324 $wgMagicFound = false;
325 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
326 return $wgMagicFound;
329 function matchStartAndRemove( &$text ) {
330 global $wgMagicFound;
331 $wgMagicFound = false;
332 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
333 return $wgMagicFound;
338 * Replaces the word with something else
340 function replace( $replacement, $subject, $limit=-1 ) {
341 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
342 $this->mModified
= !($res === $subject);
347 * Variable handling: {{SUBST:xxx}} style words
348 * Calls back a function to determine what to replace xxx with
349 * Input word must contain $1
351 function substituteCallback( $text, $callback ) {
352 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
353 $this->mModified
= !($res === $text);
358 * Matches the word, where $1 is a wildcard
360 function getVariableRegex() {
361 if ( $this->mVariableRegex
== '' ) {
364 return $this->mVariableRegex
;
368 * Matches the entire string, where $1 is a wildcard
370 function getVariableStartToEndRegex() {
371 if ( $this->mVariableStartToEndRegex
== '' ) {
374 return $this->mVariableStartToEndRegex
;
378 * Accesses the synonym list directly
380 function getSynonym( $i ) {
381 return $this->mSynonyms
[$i];
385 * Returns true if the last call to replace() or substituteCallback()
386 * returned a modified text, otherwise false.
388 function getWasModified(){
389 return $this->mModified
;
393 * $magicarr is an associative array of (magic word ID => replacement)
394 * This method uses the php feature to do several replacements at the same time,
395 * thereby gaining some efficiency. The result is placed in the out variable
396 * $result. The return value is true if something was replaced.
399 function replaceMultiple( $magicarr, $subject, &$result ){
402 foreach( $magicarr as $id => $replacement ){
403 $mw = MagicWord
::get( $id );
404 $search[] = $mw->getRegex();
405 $replace[] = $replacement;
408 $result = preg_replace( $search, $replace, $subject );
409 return !($result === $subject);
413 * Adds all the synonyms of this MagicWord to an array, to allow quick
414 * lookup in a list of magic words
416 function addToArray( &$array, $value ) {
417 foreach ( $this->mSynonyms
as $syn ) {
418 $array[$syn] = $value;
424 * Used in matchAndRemove()
427 function pregRemoveAndRecord( $match ) {
428 global $wgMagicFound;
429 $wgMagicFound = true;