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',
73 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
74 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
76 for ( $i = 0; $i < count( $magicWords ); ++
$i )
77 define( $magicWords[$i], $i );
79 $wgVariableIDs = array(
82 MAG_CURRENTMONTHNAMEGEN
,
83 MAG_CURRENTMONTHABBREV
,
105 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
106 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
109 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
111 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
113 * Possible future improvements:
114 * * Simultaneous searching for a number of magic words
115 * * $wgMagicWords in shared memory
117 * Please avoid reading the data out of one of these objects and then writing
118 * special case code. If possible, add another match()-like function here.
126 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
127 var $mRegexStart, $mBaseRegex, $mVariableRegex;
131 function MagicWord($id = 0, $syn = '', $cs = false) {
133 $this->mSynonyms
= (array)$syn;
134 $this->mCaseSensitive
= $cs;
136 $this->mRegexStart
= '';
137 $this->mVariableRegex
= '';
138 $this->mVariableStartToEndRegex
= '';
139 $this->mModified
= false;
143 * Factory: creates an object representing an ID
146 function &get( $id ) {
147 global $wgMagicWords;
149 if ( !is_array( $wgMagicWords ) ) {
150 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
152 if (!array_key_exists( $id, $wgMagicWords ) ) {
153 $mw = new MagicWord();
155 $wgMagicWords[$id] = $mw;
157 return $wgMagicWords[$id];
160 # Initialises this object with an ID
161 function load( $id ) {
164 $wgContLang->getMagic( $this );
168 * Preliminary initialisation
171 function initRegex() {
172 #$variableClass = Title::legalChars();
173 # This was used for matching "$1" variables, but different uses of the feature will have
174 # different restrictions, which should be checked *after* the MagicWord has been matched,
178 foreach ( $this->mSynonyms
as $synonym )
179 // In case a magic word contains /, like that's going to happen;)
180 $escSyn[] = preg_quote( $synonym, '/' );
181 $this->mBaseRegex
= implode( '|', $escSyn );
183 $case = $this->mCaseSensitive ?
'' : 'i';
184 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
185 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
186 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
187 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
188 "/^(?:{$this->mBaseRegex})$/{$case}" );
192 * Gets a regex representing matching the word
194 function getRegex() {
195 if ($this->mRegex
== '' ) {
198 return $this->mRegex
;
202 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
203 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
204 * the complete expression
206 function getRegexCase() {
207 if ( $this->mRegex
=== '' )
210 return $this->mCaseSensitive ?
'' : 'i';
214 * Gets a regex matching the word, if it is at the string start
216 function getRegexStart() {
217 if ($this->mRegex
== '' ) {
220 return $this->mRegexStart
;
224 * regex without the slashes and what not
226 function getBaseRegex() {
227 if ($this->mRegex
== '') {
230 return $this->mBaseRegex
;
234 * Returns true if the text contains the word
237 function match( $text ) {
238 return preg_match( $this->getRegex(), $text );
242 * Returns true if the text starts with the word
245 function matchStart( $text ) {
246 return preg_match( $this->getRegexStart(), $text );
250 * Returns NULL if there's no match, the value of $1 otherwise
251 * The return code is the matched string, if there's no variable
252 * part in the regex and the matched variable part ($1) if there
255 function matchVariableStartToEnd( $text ) {
257 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
258 if ( $matchcount == 0 ) {
260 } elseif ( count($matches) == 1 ) {
263 # multiple matched parts (variable match); some will be empty because of synonyms
264 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
265 $matches = array_values(array_filter($matches));
272 * Returns true if the text matches the word, and alters the
273 * input string, removing all instances of the word
275 function matchAndRemove( &$text ) {
276 global $wgMagicFound;
277 $wgMagicFound = false;
278 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
279 return $wgMagicFound;
282 function matchStartAndRemove( &$text ) {
283 global $wgMagicFound;
284 $wgMagicFound = false;
285 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
286 return $wgMagicFound;
291 * Replaces the word with something else
293 function replace( $replacement, $subject ) {
294 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
295 $this->mModified
= !($res === $subject);
300 * Variable handling: {{SUBST:xxx}} style words
301 * Calls back a function to determine what to replace xxx with
302 * Input word must contain $1
304 function substituteCallback( $text, $callback ) {
305 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
306 $this->mModified
= !($res === $text);
311 * Matches the word, where $1 is a wildcard
313 function getVariableRegex() {
314 if ( $this->mVariableRegex
== '' ) {
317 return $this->mVariableRegex
;
321 * Matches the entire string, where $1 is a wildcard
323 function getVariableStartToEndRegex() {
324 if ( $this->mVariableStartToEndRegex
== '' ) {
327 return $this->mVariableStartToEndRegex
;
331 * Accesses the synonym list directly
333 function getSynonym( $i ) {
334 return $this->mSynonyms
[$i];
338 * Returns true if the last call to replace() or substituteCallback()
339 * returned a modified text, otherwise false.
341 function getWasModified(){
342 return $this->mModified
;
346 * $magicarr is an associative array of (magic word ID => replacement)
347 * This method uses the php feature to do several replacements at the same time,
348 * thereby gaining some efficiency. The result is placed in the out variable
349 * $result. The return value is true if something was replaced.
352 function replaceMultiple( $magicarr, $subject, &$result ){
355 foreach( $magicarr as $id => $replacement ){
356 $mw = MagicWord
::get( $id );
357 $search[] = $mw->getRegex();
358 $replace[] = $replacement;
361 $result = preg_replace( $search, $replace, $subject );
362 return !($result === $subject);
366 * Adds all the synonyms of this MagicWord to an array, to allow quick
367 * lookup in a list of magic words
369 function addToArray( &$array, $value ) {
370 foreach ( $this->mSynonyms
as $syn ) {
371 $array[$syn] = $value;
377 * Used in matchAndRemove()
380 function pregRemoveAndRecord( $match ) {
381 global $wgMagicFound;
382 $wgMagicFound = true;