6af049b3ecbd524fc32138c8fe521fe29e1cd112
11 $wgMagicFound = false;
13 /** Actual keyword to be used is set in Language.php */
20 'MAG_CURRENTMONTHNAME',
21 'MAG_CURRENTMONTHNAMEGEN',
22 'MAG_CURRENTMONTHABBREV',
27 'MAG_NUMBEROFARTICLES',
54 'MAG_NOCONTENTCONVERT',
61 'MAG_IMG_MANUALTHUMB',
72 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
73 wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
75 for ( $i = 0; $i < count( $magicWords ); ++
$i )
76 define( $magicWords[$i], $i );
78 $wgVariableIDs = array(
81 MAG_CURRENTMONTHNAMEGEN
,
82 MAG_CURRENTMONTHABBREV
,
103 if ( ! defined( 'MEDIAWIKI_INSTALL' ) )
104 wfRunHooks( 'MagicWordwgVariableIDs', array( &$wgVariableIDs ) );
107 * This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
109 * if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
111 * Possible future improvements:
112 * * Simultaneous searching for a number of magic words
113 * * $wgMagicWords in shared memory
115 * Please avoid reading the data out of one of these objects and then writing
116 * special case code. If possible, add another match()-like function here.
124 var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
125 var $mRegexStart, $mBaseRegex, $mVariableRegex;
129 function MagicWord($id = 0, $syn = '', $cs = false) {
131 $this->mSynonyms
= (array)$syn;
132 $this->mCaseSensitive
= $cs;
134 $this->mRegexStart
= '';
135 $this->mVariableRegex
= '';
136 $this->mVariableStartToEndRegex
= '';
137 $this->mModified
= false;
141 * Factory: creates an object representing an ID
144 function &get( $id ) {
145 global $wgMagicWords;
147 if ( !is_array( $wgMagicWords ) ) {
148 wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" );
150 if (!array_key_exists( $id, $wgMagicWords ) ) {
151 $mw = new MagicWord();
153 $wgMagicWords[$id] = $mw;
155 return $wgMagicWords[$id];
158 # Initialises this object with an ID
159 function load( $id ) {
162 $wgContLang->getMagic( $this );
166 * Preliminary initialisation
169 function initRegex() {
170 #$variableClass = Title::legalChars();
171 # This was used for matching "$1" variables, but different uses of the feature will have
172 # different restrictions, which should be checked *after* the MagicWord has been matched,
174 $escSyn = array_map( 'preg_quote', $this->mSynonyms
);
175 $this->mBaseRegex
= implode( '|', $escSyn );
176 $case = $this->mCaseSensitive ?
'' : 'i';
177 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
178 $this->mRegexStart
= "/^(?:{$this->mBaseRegex})/{$case}";
179 $this->mVariableRegex
= str_replace( "\\$1", "(.*?)", $this->mRegex
);
180 $this->mVariableStartToEndRegex
= str_replace( "\\$1", "(.*?)",
181 "/^(?:{$this->mBaseRegex})$/{$case}" );
185 * Gets a regex representing matching the word
187 function getRegex() {
188 if ($this->mRegex
== '' ) {
191 return $this->mRegex
;
195 * Gets a regex matching the word, if it is at the string start
197 function getRegexStart() {
198 if ($this->mRegex
== '' ) {
201 return $this->mRegexStart
;
205 * regex without the slashes and what not
207 function getBaseRegex() {
208 if ($this->mRegex
== '') {
211 return $this->mBaseRegex
;
215 * Returns true if the text contains the word
218 function match( $text ) {
219 return preg_match( $this->getRegex(), $text );
223 * Returns true if the text starts with the word
226 function matchStart( $text ) {
227 return preg_match( $this->getRegexStart(), $text );
231 * Returns NULL if there's no match, the value of $1 otherwise
232 * The return code is the matched string, if there's no variable
233 * part in the regex and the matched variable part ($1) if there
236 function matchVariableStartToEnd( $text ) {
238 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
239 if ( $matchcount == 0 ) {
241 } elseif ( count($matches) == 1 ) {
244 # multiple matched parts (variable match); some will be empty because of synonyms
245 # the variable will be the second non-empty one so remove any blank elements and re-sort the indices
246 $matches = array_values(array_filter($matches));
253 * Returns true if the text matches the word, and alters the
254 * input string, removing all instances of the word
256 function matchAndRemove( &$text ) {
257 global $wgMagicFound;
258 $wgMagicFound = false;
259 $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text );
260 return $wgMagicFound;
263 function matchStartAndRemove( &$text ) {
264 global $wgMagicFound;
265 $wgMagicFound = false;
266 $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text );
267 return $wgMagicFound;
272 * Replaces the word with something else
274 function replace( $replacement, $subject ) {
275 $res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject );
276 $this->mModified
= !($res === $subject);
281 * Variable handling: {{SUBST:xxx}} style words
282 * Calls back a function to determine what to replace xxx with
283 * Input word must contain $1
285 function substituteCallback( $text, $callback ) {
286 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
287 $this->mModified
= !($res === $text);
292 * Matches the word, where $1 is a wildcard
294 function getVariableRegex() {
295 if ( $this->mVariableRegex
== '' ) {
298 return $this->mVariableRegex
;
302 * Matches the entire string, where $1 is a wildcard
304 function getVariableStartToEndRegex() {
305 if ( $this->mVariableStartToEndRegex
== '' ) {
308 return $this->mVariableStartToEndRegex
;
312 * Accesses the synonym list directly
314 function getSynonym( $i ) {
315 return $this->mSynonyms
[$i];
319 * Returns true if the last call to replace() or substituteCallback()
320 * returned a modified text, otherwise false.
322 function getWasModified(){
323 return $this->mModified
;
327 * $magicarr is an associative array of (magic word ID => replacement)
328 * This method uses the php feature to do several replacements at the same time,
329 * thereby gaining some efficiency. The result is placed in the out variable
330 * $result. The return value is true if something was replaced.
333 function replaceMultiple( $magicarr, $subject, &$result ){
336 foreach( $magicarr as $id => $replacement ){
337 $mw = MagicWord
::get( $id );
338 $search[] = $mw->getRegex();
339 $replace[] = $replacement;
342 $result = preg_replace( $search, $replace, $subject );
343 return !($result === $subject);
347 * Adds all the synonyms of this MagicWord to an array, to allow quick
348 * lookup in a list of magic words
350 function addToArray( &$array, $value ) {
351 foreach ( $this->mSynonyms
as $syn ) {
352 $array[$syn] = $value;
358 * Used in matchAndRemove()
361 function pregRemoveAndRecord( $match ) {
362 global $wgMagicFound;
363 $wgMagicFound = true;