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,
176 $escSyn = array_map( 'preg_quote', $this->mSynonyms
);
177 $this->mBaseRegex
= implode( '|', $escSyn );
178 $case = $this->mCaseSensitive ?
'' : 'i';
179 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
180 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
181 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
182 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
183 "/^(?:{$this->mBaseRegex})$/{$case}" );
187 * Gets a regex representing matching the word
189 function getRegex() {
190 if ($this->mRegex
== '' ) {
193 return $this->mRegex
;
197 * Gets a regex matching the word, if it is at the string start
199 function getRegexStart() {
200 if ($this->mRegex
== '' ) {
203 return $this->mRegexStart
;
207 * regex without the slashes and what not
209 function getBaseRegex() {
210 if ($this->mRegex
== '') {
213 return $this->mBaseRegex
;
217 * Returns true if the text contains the word
220 function match( $text ) {
221 return preg_match( $this->getRegex(), $text );
225 * Returns true if the text starts with the word
228 function matchStart( $text ) {
229 return preg_match( $this->getRegexStart(), $text );
233 * Returns NULL if there's no match, the value of $1 otherwise
234 * The return code is the matched string, if there's no variable
235 * part in the regex and the matched variable part ($1) if there
238 function matchVariableStartToEnd( $text ) {
240 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
241 if ( $matchcount == 0 ) {
243 } elseif ( count($matches) == 1 ) {
246 # multiple matched parts (variable match); some will be empty because of synonyms
247 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
248 $matches = array_values(array_filter($matches));
255 * Returns true if the text matches the word, and alters the
256 * input string, removing all instances of the word
258 function matchAndRemove( &$text ) {
259 global $wgMagicFound;
260 $wgMagicFound = false;
261 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
262 return $wgMagicFound;
265 function matchStartAndRemove( &$text ) {
266 global $wgMagicFound;
267 $wgMagicFound = false;
268 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
269 return $wgMagicFound;
274 * Replaces the word with something else
276 function replace( $replacement, $subject ) {
277 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
278 $this->mModified
= !($res === $subject);
283 * Variable handling: {{SUBST:xxx}} style words
284 * Calls back a function to determine what to replace xxx with
285 * Input word must contain $1
287 function substituteCallback( $text, $callback ) {
288 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
289 $this->mModified
= !($res === $text);
294 * Matches the word, where $1 is a wildcard
296 function getVariableRegex() {
297 if ( $this->mVariableRegex
== '' ) {
300 return $this->mVariableRegex
;
304 * Matches the entire string, where $1 is a wildcard
306 function getVariableStartToEndRegex() {
307 if ( $this->mVariableStartToEndRegex
== '' ) {
310 return $this->mVariableStartToEndRegex
;
314 * Accesses the synonym list directly
316 function getSynonym( $i ) {
317 return $this->mSynonyms
[$i];
321 * Returns true if the last call to replace() or substituteCallback()
322 * returned a modified text, otherwise false.
324 function getWasModified(){
325 return $this->mModified
;
329 * $magicarr is an associative array of (magic word ID => replacement)
330 * This method uses the php feature to do several replacements at the same time,
331 * thereby gaining some efficiency. The result is placed in the out variable
332 * $result. The return value is true if something was replaced.
335 function replaceMultiple( $magicarr, $subject, &$result ){
338 foreach( $magicarr as $id => $replacement ){
339 $mw = MagicWord
::get( $id );
340 $search[] = $mw->getRegex();
341 $replace[] = $replacement;
344 $result = preg_replace( $search, $replace, $subject );
345 return !($result === $subject);
349 * Adds all the synonyms of this MagicWord to an array, to allow quick
350 * lookup in a list of magic words
352 function addToArray( &$array, $value ) {
353 foreach ( $this->mSynonyms
as $syn ) {
354 $array[$syn] = $value;
360 * Used in matchAndRemove()
363 function pregRemoveAndRecord( $match ) {
364 global $wgMagicFound;
365 $wgMagicFound = true;