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',
75 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
76 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
78 for ( $i = 0; $i < count( $magicWords ); ++
$i )
79 define( $magicWords[$i], $i );
81 $wgVariableIDs = array(
84 MAG_CURRENTMONTHNAMEGEN
,
85 MAG_CURRENTMONTHABBREV
,
108 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
109 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
112 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
114 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
116 * Possible future improvements:
117 * * Simultaneous searching for a number of magic words
118 * * $wgMagicWords in shared memory
120 * Please avoid reading the data out of one of these objects and then writing
121 * special case code. If possible, add another match()-like function here.
129 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
130 var $mRegexStart, $mBaseRegex, $mVariableRegex;
134 function MagicWord($id = 0, $syn = '', $cs = false) {
136 $this->mSynonyms
= (array)$syn;
137 $this->mCaseSensitive
= $cs;
139 $this->mRegexStart
= '';
140 $this->mVariableRegex
= '';
141 $this->mVariableStartToEndRegex
= '';
142 $this->mModified
= false;
146 * Factory: creates an object representing an ID
149 function &get( $id ) {
150 global $wgMagicWords;
152 if ( !is_array( $wgMagicWords ) ) {
153 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
155 if (!array_key_exists( $id, $wgMagicWords ) ) {
156 $mw = new MagicWord();
158 $wgMagicWords[$id] = $mw;
160 return $wgMagicWords[$id];
163 # Initialises this object with an ID
164 function load( $id ) {
167 $wgContLang->getMagic( $this );
171 * Preliminary initialisation
174 function initRegex() {
175 #$variableClass = Title::legalChars();
176 # This was used for matching "$1" variables, but different uses of the feature will have
177 # different restrictions, which should be checked *after* the MagicWord has been matched,
181 foreach ( $this->mSynonyms
as $synonym )
182 // In case a magic word contains /, like that's going to happen;)
183 $escSyn[] = preg_quote( $synonym, '/' );
184 $this->mBaseRegex
= implode( '|', $escSyn );
186 $case = $this->mCaseSensitive ?
'' : 'i';
187 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
188 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
189 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
190 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
191 "/^(?:{$this->mBaseRegex})$/{$case}" );
195 * Gets a regex representing matching the word
197 function getRegex() {
198 if ($this->mRegex
== '' ) {
201 return $this->mRegex
;
205 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
206 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
207 * the complete expression
209 function getRegexCase() {
210 if ( $this->mRegex
=== '' )
213 return $this->mCaseSensitive ?
'' : 'i';
217 * Gets a regex matching the word, if it is at the string start
219 function getRegexStart() {
220 if ($this->mRegex
== '' ) {
223 return $this->mRegexStart
;
227 * regex without the slashes and what not
229 function getBaseRegex() {
230 if ($this->mRegex
== '') {
233 return $this->mBaseRegex
;
237 * Returns true if the text contains the word
240 function match( $text ) {
241 return preg_match( $this->getRegex(), $text );
245 * Returns true if the text starts with the word
248 function matchStart( $text ) {
249 return preg_match( $this->getRegexStart(), $text );
253 * Returns NULL if there's no match, the value of $1 otherwise
254 * The return code is the matched string, if there's no variable
255 * part in the regex and the matched variable part ($1) if there
258 function matchVariableStartToEnd( $text ) {
260 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
261 if ( $matchcount == 0 ) {
263 } elseif ( count($matches) == 1 ) {
266 # multiple matched parts (variable match); some will be empty because of synonyms
267 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
268 $matches = array_values(array_filter($matches));
275 * Returns true if the text matches the word, and alters the
276 * input string, removing all instances of the word
278 function matchAndRemove( &$text ) {
279 global $wgMagicFound;
280 $wgMagicFound = false;
281 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
282 return $wgMagicFound;
285 function matchStartAndRemove( &$text ) {
286 global $wgMagicFound;
287 $wgMagicFound = false;
288 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
289 return $wgMagicFound;
294 * Replaces the word with something else
296 function replace( $replacement, $subject ) {
297 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
298 $this->mModified
= !($res === $subject);
303 * Variable handling: {{SUBST:xxx}} style words
304 * Calls back a function to determine what to replace xxx with
305 * Input word must contain $1
307 function substituteCallback( $text, $callback ) {
308 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
309 $this->mModified
= !($res === $text);
314 * Matches the word, where $1 is a wildcard
316 function getVariableRegex() {
317 if ( $this->mVariableRegex
== '' ) {
320 return $this->mVariableRegex
;
324 * Matches the entire string, where $1 is a wildcard
326 function getVariableStartToEndRegex() {
327 if ( $this->mVariableStartToEndRegex
== '' ) {
330 return $this->mVariableStartToEndRegex
;
334 * Accesses the synonym list directly
336 function getSynonym( $i ) {
337 return $this->mSynonyms
[$i];
341 * Returns true if the last call to replace() or substituteCallback()
342 * returned a modified text, otherwise false.
344 function getWasModified(){
345 return $this->mModified
;
349 * $magicarr is an associative array of (magic word ID => replacement)
350 * This method uses the php feature to do several replacements at the same time,
351 * thereby gaining some efficiency. The result is placed in the out variable
352 * $result. The return value is true if something was replaced.
355 function replaceMultiple( $magicarr, $subject, &$result ){
358 foreach( $magicarr as $id => $replacement ){
359 $mw = MagicWord
::get( $id );
360 $search[] = $mw->getRegex();
361 $replace[] = $replacement;
364 $result = preg_replace( $search, $replace, $subject );
365 return !($result === $subject);
369 * Adds all the synonyms of this MagicWord to an array, to allow quick
370 * lookup in a list of magic words
372 function addToArray( &$array, $value ) {
373 foreach ( $this->mSynonyms
as $syn ) {
374 $array[$syn] = $value;
380 * Used in matchAndRemove()
383 function pregRemoveAndRecord( $match ) {
384 global $wgMagicFound;
385 $wgMagicFound = true;