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 a regex matching the word, if it is at the string start
204 function getRegexStart() {
205 if ($this->mRegex
== '' ) {
208 return $this->mRegexStart
;
212 * regex without the slashes and what not
214 function getBaseRegex() {
215 if ($this->mRegex
== '') {
218 return $this->mBaseRegex
;
222 * Returns true if the text contains the word
225 function match( $text ) {
226 return preg_match( $this->getRegex(), $text );
230 * Returns true if the text starts with the word
233 function matchStart( $text ) {
234 return preg_match( $this->getRegexStart(), $text );
238 * Returns NULL if there's no match, the value of $1 otherwise
239 * The return code is the matched string, if there's no variable
240 * part in the regex and the matched variable part ($1) if there
243 function matchVariableStartToEnd( $text ) {
245 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
246 if ( $matchcount == 0 ) {
248 } elseif ( count($matches) == 1 ) {
251 # multiple matched parts (variable match); some will be empty because of synonyms
252 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
253 $matches = array_values(array_filter($matches));
260 * Returns true if the text matches the word, and alters the
261 * input string, removing all instances of the word
263 function matchAndRemove( &$text ) {
264 global $wgMagicFound;
265 $wgMagicFound = false;
266 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
267 return $wgMagicFound;
270 function matchStartAndRemove( &$text ) {
271 global $wgMagicFound;
272 $wgMagicFound = false;
273 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
274 return $wgMagicFound;
279 * Replaces the word with something else
281 function replace( $replacement, $subject ) {
282 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
283 $this->mModified
= !($res === $subject);
288 * Variable handling: {{SUBST:xxx}} style words
289 * Calls back a function to determine what to replace xxx with
290 * Input word must contain $1
292 function substituteCallback( $text, $callback ) {
293 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
294 $this->mModified
= !($res === $text);
299 * Matches the word, where $1 is a wildcard
301 function getVariableRegex() {
302 if ( $this->mVariableRegex
== '' ) {
305 return $this->mVariableRegex
;
309 * Matches the entire string, where $1 is a wildcard
311 function getVariableStartToEndRegex() {
312 if ( $this->mVariableStartToEndRegex
== '' ) {
315 return $this->mVariableStartToEndRegex
;
319 * Accesses the synonym list directly
321 function getSynonym( $i ) {
322 return $this->mSynonyms
[$i];
326 * Returns true if the last call to replace() or substituteCallback()
327 * returned a modified text, otherwise false.
329 function getWasModified(){
330 return $this->mModified
;
334 * $magicarr is an associative array of (magic word ID => replacement)
335 * This method uses the php feature to do several replacements at the same time,
336 * thereby gaining some efficiency. The result is placed in the out variable
337 * $result. The return value is true if something was replaced.
340 function replaceMultiple( $magicarr, $subject, &$result ){
343 foreach( $magicarr as $id => $replacement ){
344 $mw = MagicWord
::get( $id );
345 $search[] = $mw->getRegex();
346 $replace[] = $replacement;
349 $result = preg_replace( $search, $replace, $subject );
350 return !($result === $subject);
354 * Adds all the synonyms of this MagicWord to an array, to allow quick
355 * lookup in a list of magic words
357 function addToArray( &$array, $value ) {
358 foreach ( $this->mSynonyms
as $syn ) {
359 $array[$syn] = $value;
365 * Used in matchAndRemove()
368 function pregRemoveAndRecord( $match ) {
369 global $wgMagicFound;
370 $wgMagicFound = true;