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',
87 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
88 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
90 for ( $i = 0; $i < count( $magicWords ); ++
$i )
91 define( $magicWords[$i], $i );
93 $wgVariableIDs = array(
96 MAG_CURRENTMONTHNAMEGEN
,
97 MAG_CURRENTMONTHABBREV
,
103 MAG_NUMBEROFARTICLES
,
128 MAG_SUBJECTPAGENAMEE
,
132 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
133 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
136 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
138 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
140 * Possible future improvements:
141 * * Simultaneous searching for a number of magic words
142 * * $wgMagicWords in shared memory
144 * Please avoid reading the data out of one of these objects and then writing
145 * special case code. If possible, add another match()-like function here.
153 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
154 var $mRegexStart, $mBaseRegex, $mVariableRegex;
158 function MagicWord($id = 0, $syn = '', $cs = false) {
160 $this->mSynonyms
= (array)$syn;
161 $this->mCaseSensitive
= $cs;
163 $this->mRegexStart
= '';
164 $this->mVariableRegex
= '';
165 $this->mVariableStartToEndRegex
= '';
166 $this->mModified
= false;
170 * Factory: creates an object representing an ID
173 function &get( $id ) {
174 global $wgMagicWords;
176 if ( !is_array( $wgMagicWords ) ) {
177 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
179 if (!array_key_exists( $id, $wgMagicWords ) ) {
180 $mw = new MagicWord();
182 $wgMagicWords[$id] = $mw;
184 return $wgMagicWords[$id];
187 # Initialises this object with an ID
188 function load( $id ) {
191 $wgContLang->getMagic( $this );
195 * Preliminary initialisation
198 function initRegex() {
199 #$variableClass = Title::legalChars();
200 # This was used for matching "$1" variables, but different uses of the feature will have
201 # different restrictions, which should be checked *after* the MagicWord has been matched,
205 foreach ( $this->mSynonyms
as $synonym )
206 // In case a magic word contains /, like that's going to happen;)
207 $escSyn[] = preg_quote( $synonym, '/' );
208 $this->mBaseRegex
= implode( '|', $escSyn );
210 $case = $this->mCaseSensitive ?
'' : 'i';
211 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
212 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
213 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
214 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
215 "/^(?:{$this->mBaseRegex})$/{$case}" );
219 * Gets a regex representing matching the word
221 function getRegex() {
222 if ($this->mRegex
== '' ) {
225 return $this->mRegex
;
229 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
230 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
231 * the complete expression
233 function getRegexCase() {
234 if ( $this->mRegex
=== '' )
237 return $this->mCaseSensitive ?
'' : 'i';
241 * Gets a regex matching the word, if it is at the string start
243 function getRegexStart() {
244 if ($this->mRegex
== '' ) {
247 return $this->mRegexStart
;
251 * regex without the slashes and what not
253 function getBaseRegex() {
254 if ($this->mRegex
== '') {
257 return $this->mBaseRegex
;
261 * Returns true if the text contains the word
264 function match( $text ) {
265 return preg_match( $this->getRegex(), $text );
269 * Returns true if the text starts with the word
272 function matchStart( $text ) {
273 return preg_match( $this->getRegexStart(), $text );
277 * Returns NULL if there's no match, the value of $1 otherwise
278 * The return code is the matched string, if there's no variable
279 * part in the regex and the matched variable part ($1) if there
282 function matchVariableStartToEnd( $text ) {
284 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
285 if ( $matchcount == 0 ) {
287 } elseif ( count($matches) == 1 ) {
290 # multiple matched parts (variable match); some will be empty because of synonyms
291 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
292 $matches = array_values(array_filter($matches));
299 * Returns true if the text matches the word, and alters the
300 * input string, removing all instances of the word
302 function matchAndRemove( &$text ) {
303 global $wgMagicFound;
304 $wgMagicFound = false;
305 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
306 return $wgMagicFound;
309 function matchStartAndRemove( &$text ) {
310 global $wgMagicFound;
311 $wgMagicFound = false;
312 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
313 return $wgMagicFound;
318 * Replaces the word with something else
320 function replace( $replacement, $subject ) {
321 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
322 $this->mModified
= !($res === $subject);
327 * Variable handling: {{SUBST:xxx}} style words
328 * Calls back a function to determine what to replace xxx with
329 * Input word must contain $1
331 function substituteCallback( $text, $callback ) {
332 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
333 $this->mModified
= !($res === $text);
338 * Matches the word, where $1 is a wildcard
340 function getVariableRegex() {
341 if ( $this->mVariableRegex
== '' ) {
344 return $this->mVariableRegex
;
348 * Matches the entire string, where $1 is a wildcard
350 function getVariableStartToEndRegex() {
351 if ( $this->mVariableStartToEndRegex
== '' ) {
354 return $this->mVariableStartToEndRegex
;
358 * Accesses the synonym list directly
360 function getSynonym( $i ) {
361 return $this->mSynonyms
[$i];
365 * Returns true if the last call to replace() or substituteCallback()
366 * returned a modified text, otherwise false.
368 function getWasModified(){
369 return $this->mModified
;
373 * $magicarr is an associative array of (magic word ID => replacement)
374 * This method uses the php feature to do several replacements at the same time,
375 * thereby gaining some efficiency. The result is placed in the out variable
376 * $result. The return value is true if something was replaced.
379 function replaceMultiple( $magicarr, $subject, &$result ){
382 foreach( $magicarr as $id => $replacement ){
383 $mw = MagicWord
::get( $id );
384 $search[] = $mw->getRegex();
385 $replace[] = $replacement;
388 $result = preg_replace( $search, $replace, $subject );
389 return !($result === $subject);
393 * Adds all the synonyms of this MagicWord to an array, to allow quick
394 * lookup in a list of magic words
396 function addToArray( &$array, $value ) {
397 foreach ( $this->mSynonyms
as $syn ) {
398 $array[$syn] = $value;
404 * Used in matchAndRemove()
407 function pregRemoveAndRecord( $match ) {
408 global $wgMagicFound;
409 $wgMagicFound = true;