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',
74 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
75 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
77 for ( $i = 0; $i < count( $magicWords ); ++
$i )
78 define( $magicWords[$i], $i );
80 $wgVariableIDs = array(
83 MAG_CURRENTMONTHNAMEGEN
,
84 MAG_CURRENTMONTHABBREV
,
106 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
107 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
110 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
112 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
114 * Possible future improvements:
115 * * Simultaneous searching for a number of magic words
116 * * $wgMagicWords in shared memory
118 * Please avoid reading the data out of one of these objects and then writing
119 * special case code. If possible, add another match()-like function here.
127 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
128 var $mRegexStart, $mBaseRegex, $mVariableRegex;
132 function MagicWord($id = 0, $syn = '', $cs = false) {
134 $this->mSynonyms
= (array)$syn;
135 $this->mCaseSensitive
= $cs;
137 $this->mRegexStart
= '';
138 $this->mVariableRegex
= '';
139 $this->mVariableStartToEndRegex
= '';
140 $this->mModified
= false;
144 * Factory: creates an object representing an ID
147 function &get( $id ) {
148 global $wgMagicWords;
150 if ( !is_array( $wgMagicWords ) ) {
151 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
153 if (!array_key_exists( $id, $wgMagicWords ) ) {
154 $mw = new MagicWord();
156 $wgMagicWords[$id] = $mw;
158 return $wgMagicWords[$id];
161 # Initialises this object with an ID
162 function load( $id ) {
165 $wgContLang->getMagic( $this );
169 * Preliminary initialisation
172 function initRegex() {
173 #$variableClass = Title::legalChars();
174 # This was used for matching "$1" variables, but different uses of the feature will have
175 # different restrictions, which should be checked *after* the MagicWord has been matched,
179 foreach ( $this->mSynonyms
as $synonym )
180 // In case a magic word contains /, like that's going to happen;)
181 $escSyn[] = preg_quote( $synonym, '/' );
182 $this->mBaseRegex
= implode( '|', $escSyn );
184 $case = $this->mCaseSensitive ?
'' : 'i';
185 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
186 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
187 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
188 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
189 "/^(?:{$this->mBaseRegex})$/{$case}" );
193 * Gets a regex representing matching the word
195 function getRegex() {
196 if ($this->mRegex
== '' ) {
199 return $this->mRegex
;
203 * Gets the regexp case modifier to use, i.e. i or nothing, to be used if
204 * one is using MagicWord::getBaseRegex(), otherwise it'll be included in
205 * the complete expression
207 function getRegexCase() {
208 if ( $this->mRegex
=== '' )
211 return $this->mCaseSensitive ?
'' : 'i';
215 * Gets a regex matching the word, if it is at the string start
217 function getRegexStart() {
218 if ($this->mRegex
== '' ) {
221 return $this->mRegexStart
;
225 * regex without the slashes and what not
227 function getBaseRegex() {
228 if ($this->mRegex
== '') {
231 return $this->mBaseRegex
;
235 * Returns true if the text contains the word
238 function match( $text ) {
239 return preg_match( $this->getRegex(), $text );
243 * Returns true if the text starts with the word
246 function matchStart( $text ) {
247 return preg_match( $this->getRegexStart(), $text );
251 * Returns NULL if there's no match, the value of $1 otherwise
252 * The return code is the matched string, if there's no variable
253 * part in the regex and the matched variable part ($1) if there
256 function matchVariableStartToEnd( $text ) {
258 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
259 if ( $matchcount == 0 ) {
261 } elseif ( count($matches) == 1 ) {
264 # multiple matched parts (variable match); some will be empty because of synonyms
265 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
266 $matches = array_values(array_filter($matches));
273 * Returns true if the text matches the word, and alters the
274 * input string, removing all instances of the word
276 function matchAndRemove( &$text ) {
277 global $wgMagicFound;
278 $wgMagicFound = false;
279 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
280 return $wgMagicFound;
283 function matchStartAndRemove( &$text ) {
284 global $wgMagicFound;
285 $wgMagicFound = false;
286 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
287 return $wgMagicFound;
292 * Replaces the word with something else
294 function replace( $replacement, $subject ) {
295 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
296 $this->mModified
= !($res === $subject);
301 * Variable handling: {{SUBST:xxx}} style words
302 * Calls back a function to determine what to replace xxx with
303 * Input word must contain $1
305 function substituteCallback( $text, $callback ) {
306 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
307 $this->mModified
= !($res === $text);
312 * Matches the word, where $1 is a wildcard
314 function getVariableRegex() {
315 if ( $this->mVariableRegex
== '' ) {
318 return $this->mVariableRegex
;
322 * Matches the entire string, where $1 is a wildcard
324 function getVariableStartToEndRegex() {
325 if ( $this->mVariableStartToEndRegex
== '' ) {
328 return $this->mVariableStartToEndRegex
;
332 * Accesses the synonym list directly
334 function getSynonym( $i ) {
335 return $this->mSynonyms
[$i];
339 * Returns true if the last call to replace() or substituteCallback()
340 * returned a modified text, otherwise false.
342 function getWasModified(){
343 return $this->mModified
;
347 * $magicarr is an associative array of (magic word ID => replacement)
348 * This method uses the php feature to do several replacements at the same time,
349 * thereby gaining some efficiency. The result is placed in the out variable
350 * $result. The return value is true if something was replaced.
353 function replaceMultiple( $magicarr, $subject, &$result ){
356 foreach( $magicarr as $id => $replacement ){
357 $mw = MagicWord
::get( $id );
358 $search[] = $mw->getRegex();
359 $replace[] = $replacement;
362 $result = preg_replace( $search, $replace, $subject );
363 return !($result === $subject);
367 * Adds all the synonyms of this MagicWord to an array, to allow quick
368 * lookup in a list of magic words
370 function addToArray( &$array, $value ) {
371 foreach ( $this->mSynonyms
as $syn ) {
372 $array[$syn] = $value;
378 * Used in matchAndRemove()
381 function pregRemoveAndRecord( $match ) {
382 global $wgMagicFound;
383 $wgMagicFound = true;