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',
90 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
91 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
93 for ( $i = 0; $i < count( $magicWords ); ++
$i )
94 define( $magicWords[$i], $i );
96 $wgVariableIDs = array(
99 MAG_CURRENTMONTHNAMEGEN
,
100 MAG_CURRENTMONTHABBREV
,
106 MAG_NUMBEROFARTICLES
,
131 MAG_SUBJECTPAGENAMEE
,
138 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
139 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
142 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
144 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
146 * Possible future improvements:
147 * * Simultaneous searching for a number of magic words
148 * * $wgMagicWords in shared memory
150 * Please avoid reading the data out of one of these objects and then writing
151 * special case code. If possible, add another match()-like function here.
159 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
160 var $mRegexStart, $mBaseRegex, $mVariableRegex;
164 function MagicWord($id = 0, $syn = '', $cs = false) {
166 $this->mSynonyms
= (array)$syn;
167 $this->mCaseSensitive
= $cs;
169 $this->mRegexStart
= '';
170 $this->mVariableRegex
= '';
171 $this->mVariableStartToEndRegex
= '';
172 $this->mModified
= false;
176 * Factory: creates an object representing an ID
179 function &get( $id ) {
180 global $wgMagicWords;
182 if ( !is_array( $wgMagicWords ) ) {
183 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
185 if (!array_key_exists( $id, $wgMagicWords ) ) {
186 $mw = new MagicWord();
188 $wgMagicWords[$id] = $mw;
190 return $wgMagicWords[$id];
193 # Initialises this object with an ID
194 function load( $id ) {
197 $wgContLang->getMagic( $this );
201 * Preliminary initialisation
204 function initRegex() {
205 #$variableClass = Title::legalChars();
206 # This was used for matching "$1" variables, but different uses of the feature will have
207 # different restrictions, which should be checked *after* the MagicWord has been matched,
211 foreach ( $this->mSynonyms
as $synonym )
212 // In case a magic word contains /, like that's going to happen;)
213 $escSyn[] = preg_quote( $synonym, '/' );
214 $this->mBaseRegex
= implode( '|', $escSyn );
216 $case = $this->mCaseSensitive ?
'' : 'i';
217 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
218 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
219 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
220 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
221 "/^(?:{$this->mBaseRegex})$/{$case}" );
225 * Gets a regex representing matching the word
227 function getRegex() {
228 if ($this->mRegex
== '' ) {
231 return $this->mRegex
;
235 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
236 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
237 * the complete expression
239 function getRegexCase() {
240 if ( $this->mRegex
=== '' )
243 return $this->mCaseSensitive ?
'' : 'i';
247 * Gets a regex matching the word, if it is at the string start
249 function getRegexStart() {
250 if ($this->mRegex
== '' ) {
253 return $this->mRegexStart
;
257 * regex without the slashes and what not
259 function getBaseRegex() {
260 if ($this->mRegex
== '') {
263 return $this->mBaseRegex
;
267 * Returns true if the text contains the word
270 function match( $text ) {
271 return preg_match( $this->getRegex(), $text );
275 * Returns true if the text starts with the word
278 function matchStart( $text ) {
279 return preg_match( $this->getRegexStart(), $text );
283 * Returns NULL if there's no match, the value of $1 otherwise
284 * The return code is the matched string, if there's no variable
285 * part in the regex and the matched variable part ($1) if there
288 function matchVariableStartToEnd( $text ) {
290 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
291 if ( $matchcount == 0 ) {
293 } elseif ( count($matches) == 1 ) {
296 # multiple matched parts (variable match); some will be empty because of synonyms
297 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
298 $matches = array_values(array_filter($matches));
305 * Returns true if the text matches the word, and alters the
306 * input string, removing all instances of the word
308 function matchAndRemove( &$text ) {
309 global $wgMagicFound;
310 $wgMagicFound = false;
311 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
312 return $wgMagicFound;
315 function matchStartAndRemove( &$text ) {
316 global $wgMagicFound;
317 $wgMagicFound = false;
318 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
319 return $wgMagicFound;
324 * Replaces the word with something else
326 function replace( $replacement, $subject ) {
327 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
328 $this->mModified
= !($res === $subject);
333 * Variable handling: {{SUBST:xxx}} style words
334 * Calls back a function to determine what to replace xxx with
335 * Input word must contain $1
337 function substituteCallback( $text, $callback ) {
338 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
339 $this->mModified
= !($res === $text);
344 * Matches the word, where $1 is a wildcard
346 function getVariableRegex() {
347 if ( $this->mVariableRegex
== '' ) {
350 return $this->mVariableRegex
;
354 * Matches the entire string, where $1 is a wildcard
356 function getVariableStartToEndRegex() {
357 if ( $this->mVariableStartToEndRegex
== '' ) {
360 return $this->mVariableStartToEndRegex
;
364 * Accesses the synonym list directly
366 function getSynonym( $i ) {
367 return $this->mSynonyms
[$i];
371 * Returns true if the last call to replace() or substituteCallback()
372 * returned a modified text, otherwise false.
374 function getWasModified(){
375 return $this->mModified
;
379 * $magicarr is an associative array of (magic word ID => replacement)
380 * This method uses the php feature to do several replacements at the same time,
381 * thereby gaining some efficiency. The result is placed in the out variable
382 * $result. The return value is true if something was replaced.
385 function replaceMultiple( $magicarr, $subject, &$result ){
388 foreach( $magicarr as $id => $replacement ){
389 $mw = MagicWord
::get( $id );
390 $search[] = $mw->getRegex();
391 $replace[] = $replacement;
394 $result = preg_replace( $search, $replace, $subject );
395 return !($result === $subject);
399 * Adds all the synonyms of this MagicWord to an array, to allow quick
400 * lookup in a list of magic words
402 function addToArray( &$array, $value ) {
403 foreach ( $this->mSynonyms
as $syn ) {
404 $array[$syn] = $value;
410 * Used in matchAndRemove()
413 function pregRemoveAndRecord( $match ) {
414 global $wgMagicFound;
415 $wgMagicFound = true;