fixed section anchors
[mediawiki.git] / includes / MagicWord.php
blob57e79b88ac9a7d74143e463dc153df9b8b41e7b2
1 <?php
3 # This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
4 # Usage:
5 # if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
6 #
7 # Possible future improvements:
8 # * Simultaneous searching for a number of magic words
9 # * $wgMagicWords in shared memory
11 # Please avoid reading the data out of one of these objects and then writing
12 # special case code. If possible, add another match()-like function here.
14 /*private*/ $wgMagicFound = false;
16 class MagicWord {
17 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
18 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
19 /*private*/ var $mModified;
21 function MagicWord($id = 0, $syn = "", $cs = false)
23 $this->mId = $id;
24 $this->mSynonyms = (array)$syn;
25 $this->mCaseSensitive = $cs;
26 $this->mRegex = "";
27 $this->mRegexStart = "";
28 $this->mVariableRegex = "";
29 $this->mVariableStartToEndRegex = "";
30 $this->mModified = false;
33 # Factory: creates an object representing an ID
34 /*static*/ function &get( $id )
36 global $wgMagicWords;
38 if (!array_key_exists( $id, $wgMagicWords ) ) {
39 $mw = new MagicWord();
40 $mw->load( $id );
41 $wgMagicWords[$id] = $mw;
43 return $wgMagicWords[$id];
46 # Initialises this object with an ID
47 function load( $id )
49 global $wgLang;
50 $this->mId = $id;
51 $wgLang->getMagic( $this );
54 # Preliminary initialisation
55 /* private */ function initRegex()
57 $variableClass = Title::legalChars();
58 $escSyn = array_map( "preg_quote", $this->mSynonyms );
59 $this->mBaseRegex = implode( "|", $escSyn );
60 $case = $this->mCaseSensitive ? "" : "i";
61 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
62 $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
63 $this->mVariableRegex = str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex );
64 $this->mVariableStartToEndRegex = str_replace( "\\$1", "([$variableClass]*?)",
65 "/^{$this->mBaseRegex}$/{$case}" );
68 # Gets a regex representing matching the word
69 function getRegex()
71 if ($this->mRegex == "" ) {
72 $this->initRegex();
74 return $this->mRegex;
77 # Gets a regex matching the word, if it is at the
78 # string start
79 function getRegexStart()
81 if ($this->mRegex == "" ) {
82 $this->initRegex();
84 return $this->mRegexStart;
87 # regex without the slashes and what not
88 function getBaseRegex()
90 if ($this->mRegex == "") {
91 $this->initRegex();
93 return $this->mBaseRegex;
96 # Returns true if the text contains the word
97 function match( $text ) {
98 return preg_match( $this->getRegex(), $text );
101 # Returns true if the text starts with the word
102 function matchStart( $text )
104 return preg_match( $this->getRegexStart(), $text );
107 # Returns NULL if there's no match, the value of $1 otherwise
108 # The return code is the matched string, if there's no variable
109 # part in the regex and the matched variable part ($1) if there
110 # is one.
111 function matchVariableStartToEnd( $text ) {
112 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
113 if ( $matchcount == 0 ) {
114 return NULL;
115 } elseif ( count($matches) == 1 ) {
116 return $matches[0];
117 } else {
118 return $matches[1];
123 # Returns true if the text matches the word, and alters the
124 # input string, removing all instances of the word
125 function matchAndRemove( &$text )
127 global $wgMagicFound;
128 $wgMagicFound = false;
129 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
130 return $wgMagicFound;
133 function matchStartAndRemove( &$text ) {
134 global $wgMagicFound;
135 $wgMagicFound = false;
136 $text = preg_replace_callback( $this->getRegexStart(), "pregRemoveAndRecord", $text );
137 return $wgMagicFound;
141 # Replaces the word with something else
142 function replace( $replacement, $subject )
144 $res = preg_replace( $this->getRegex(), $replacement, $subject );
145 $this->mModified = !($res === $subject);
146 return $res;
149 # Variable handling: {{SUBST:xxx}} style words
150 # Calls back a function to determine what to replace xxx with
151 # Input word must contain $1
152 function substituteCallback( $text, $callback ) {
153 $regex = $this->getVariableRegex();
154 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
155 $this->mModified = !($res === $text);
156 return $res;
159 # Matches the word, where $1 is a wildcard
160 function getVariableRegex()
162 if ( $this->mVariableRegex == "" ) {
163 $this->initRegex();
165 return $this->mVariableRegex;
168 # Matches the entire string, where $1 is a wildcard
169 function getVariableStartToEndRegex()
171 if ( $this->mVariableStartToEndRegex == "" ) {
172 $this->initRegex();
174 return $this->mVariableStartToEndRegex;
177 # Accesses the synonym list directly
178 function getSynonym( $i ) {
179 return $this->mSynonyms[$i];
182 # Returns true if the last call to replace() or substituteCallback()
183 # returned a modified text, otherwise false.
184 function getWasModified(){
185 return $this->mModified;
188 # $magicarr is an associative array of (magic word ID => replacement)
189 # This method uses the php feature to do several replacements at the same time,
190 # thereby gaining some efficiency. The result is placed in the out variable
191 # $result. The return value is true if something was replaced.
193 /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
194 $search = array();
195 $replace = array();
196 foreach( $magicarr as $id => $replacement ){
197 $mw = MagicWord::get( $id );
198 $search[] = $mw->getRegex();
199 $replace[] = $replacement;
202 $result = preg_replace( $search, $replace, $subject );
203 return !($result === $subject);
206 # Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words
207 function addToArray( &$array, $value )
209 foreach ( $this->mSynonyms as $syn ) {
210 $array[$syn] = $value;
215 # Used in matchAndRemove()
216 /*private*/ function pregRemoveAndRecord( $match )
218 global $wgMagicFound;
219 $wgMagicFound = true;
220 return "";