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',
86 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
87 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
89 for ( $i = 0; $i < count( $magicWords ); ++
$i )
90 define( $magicWords[$i], $i );
92 $wgVariableIDs = array(
95 MAG_CURRENTMONTHNAMEGEN
,
96 MAG_CURRENTMONTHABBREV
,
102 MAG_NUMBEROFARTICLES
,
127 MAG_SUBJECTPAGENAMEE
,
130 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
131 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
134 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
136 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
138 * Possible future improvements:
139 * * Simultaneous searching for a number of magic words
140 * * $wgMagicWords in shared memory
142 * Please avoid reading the data out of one of these objects and then writing
143 * special case code. If possible, add another match()-like function here.
151 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
152 var $mRegexStart, $mBaseRegex, $mVariableRegex;
156 function MagicWord($id = 0, $syn = '', $cs = false) {
158 $this->mSynonyms
= (array)$syn;
159 $this->mCaseSensitive
= $cs;
161 $this->mRegexStart
= '';
162 $this->mVariableRegex
= '';
163 $this->mVariableStartToEndRegex
= '';
164 $this->mModified
= false;
168 * Factory: creates an object representing an ID
171 function &get( $id ) {
172 global $wgMagicWords;
174 if ( !is_array( $wgMagicWords ) ) {
175 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
177 if (!array_key_exists( $id, $wgMagicWords ) ) {
178 $mw = new MagicWord();
180 $wgMagicWords[$id] = $mw;
182 return $wgMagicWords[$id];
185 # Initialises this object with an ID
186 function load( $id ) {
189 $wgContLang->getMagic( $this );
193 * Preliminary initialisation
196 function initRegex() {
197 #$variableClass = Title::legalChars();
198 # This was used for matching "$1" variables, but different uses of the feature will have
199 # different restrictions, which should be checked *after* the MagicWord has been matched,
203 foreach ( $this->mSynonyms
as $synonym )
204 // In case a magic word contains /, like that's going to happen;)
205 $escSyn[] = preg_quote( $synonym, '/' );
206 $this->mBaseRegex
= implode( '|', $escSyn );
208 $case = $this->mCaseSensitive ?
'' : 'i';
209 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
210 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
211 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
212 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
213 "/^(?:{$this->mBaseRegex})$/{$case}" );
217 * Gets a regex representing matching the word
219 function getRegex() {
220 if ($this->mRegex
== '' ) {
223 return $this->mRegex
;
227 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
228 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
229 * the complete expression
231 function getRegexCase() {
232 if ( $this->mRegex
=== '' )
235 return $this->mCaseSensitive ?
'' : 'i';
239 * Gets a regex matching the word, if it is at the string start
241 function getRegexStart() {
242 if ($this->mRegex
== '' ) {
245 return $this->mRegexStart
;
249 * regex without the slashes and what not
251 function getBaseRegex() {
252 if ($this->mRegex
== '') {
255 return $this->mBaseRegex
;
259 * Returns true if the text contains the word
262 function match( $text ) {
263 return preg_match( $this->getRegex(), $text );
267 * Returns true if the text starts with the word
270 function matchStart( $text ) {
271 return preg_match( $this->getRegexStart(), $text );
275 * Returns NULL if there's no match, the value of $1 otherwise
276 * The return code is the matched string, if there's no variable
277 * part in the regex and the matched variable part ($1) if there
280 function matchVariableStartToEnd( $text ) {
282 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
283 if ( $matchcount == 0 ) {
285 } elseif ( count($matches) == 1 ) {
288 # multiple matched parts (variable match); some will be empty because of synonyms
289 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
290 $matches = array_values(array_filter($matches));
297 * Returns true if the text matches the word, and alters the
298 * input string, removing all instances of the word
300 function matchAndRemove( &$text ) {
301 global $wgMagicFound;
302 $wgMagicFound = false;
303 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
304 return $wgMagicFound;
307 function matchStartAndRemove( &$text ) {
308 global $wgMagicFound;
309 $wgMagicFound = false;
310 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
311 return $wgMagicFound;
316 * Replaces the word with something else
318 function replace( $replacement, $subject ) {
319 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
320 $this->mModified
= !($res === $subject);
325 * Variable handling: {{SUBST:xxx}} style words
326 * Calls back a function to determine what to replace xxx with
327 * Input word must contain $1
329 function substituteCallback( $text, $callback ) {
330 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
331 $this->mModified
= !($res === $text);
336 * Matches the word, where $1 is a wildcard
338 function getVariableRegex() {
339 if ( $this->mVariableRegex
== '' ) {
342 return $this->mVariableRegex
;
346 * Matches the entire string, where $1 is a wildcard
348 function getVariableStartToEndRegex() {
349 if ( $this->mVariableStartToEndRegex
== '' ) {
352 return $this->mVariableStartToEndRegex
;
356 * Accesses the synonym list directly
358 function getSynonym( $i ) {
359 return $this->mSynonyms
[$i];
363 * Returns true if the last call to replace() or substituteCallback()
364 * returned a modified text, otherwise false.
366 function getWasModified(){
367 return $this->mModified
;
371 * $magicarr is an associative array of (magic word ID => replacement)
372 * This method uses the php feature to do several replacements at the same time,
373 * thereby gaining some efficiency. The result is placed in the out variable
374 * $result. The return value is true if something was replaced.
377 function replaceMultiple( $magicarr, $subject, &$result ){
380 foreach( $magicarr as $id => $replacement ){
381 $mw = MagicWord
::get( $id );
382 $search[] = $mw->getRegex();
383 $replace[] = $replacement;
386 $result = preg_replace( $search, $replace, $subject );
387 return !($result === $subject);
391 * Adds all the synonyms of this MagicWord to an array, to allow quick
392 * lookup in a list of magic words
394 function addToArray( &$array, $value ) {
395 foreach ( $this->mSynonyms
as $syn ) {
396 $array[$syn] = $value;
402 * Used in matchAndRemove()
405 function pregRemoveAndRecord( $match ) {
406 global $wgMagicFound;
407 $wgMagicFound = true;