506046981eb08410699388d231f1b821aa123dea
[lhc/web/wiklou.git] / includes / MagicWord.php
1 <?php
2
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
10 #
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.
13
14 /*private*/ $wgMagicFound = false;
15
16 class MagicWord {
17 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
18 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
19 /*private*/ var $mModified;
20
21 function MagicWord($id = 0, $syn = "", $cs = false)
22 {
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;
31 }
32
33 # Factory: creates an object representing an ID
34 /*static*/ function &get( $id )
35 {
36 global $wgMagicWords;
37
38 if (!array_key_exists( $id, $wgMagicWords ) ) {
39 $mw = new MagicWord();
40 $mw->load( $id );
41 $wgMagicWords[$id] = $mw;
42 }
43 return $wgMagicWords[$id];
44 }
45
46 # Initialises this object with an ID
47 function load( $id )
48 {
49 global $wgLang;
50 $this->mId = $id;
51 $wgLang->getMagic( $this );
52 }
53
54 # Preliminary initialisation
55 /* private */ function initRegex()
56 {
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}" );
66 }
67
68 # Gets a regex representing matching the word
69 function getRegex()
70 {
71 if ($this->mRegex == "" ) {
72 $this->initRegex();
73 }
74 return $this->mRegex;
75 }
76
77 # Gets a regex matching the word, if it is at the
78 # string start
79 function getRegexStart()
80 {
81 if ($this->mRegex == "" ) {
82 $this->initRegex();
83 }
84 return $this->mRegexStart;
85 }
86
87 # regex without the slashes and what not
88 function getBaseRegex()
89 {
90 if ($this->mRegex == "") {
91 $this->initRegex();
92 }
93 return $this->mBaseRegex;
94 }
95
96 # Returns true if the text contains the word
97 function match( $text ) {
98 return preg_match( $this->getRegex(), $text );
99 }
100
101 # Returns true if the text starts with the word
102 function matchStart( $text )
103 {
104 return preg_match( $this->getRegexStart(), $text );
105 }
106
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];
119 }
120 }
121
122
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 )
126 {
127 global $wgMagicFound;
128 $wgMagicFound = false;
129 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
130 return $wgMagicFound;
131 }
132
133 # Replaces the word with something else
134 function replace( $replacement, $subject )
135 {
136 $res = preg_replace( $this->getRegex(), $replacement, $subject );
137 $this->mModified = !($res === $subject);
138 return $res;
139 }
140
141 # Variable handling: {{SUBST:xxx}} style words
142 # Calls back a function to determine what to replace xxx with
143 # Input word must contain $1
144 function substituteCallback( $text, $callback ) {
145 $regex = $this->getVariableRegex();
146 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
147 $this->mModified = !($res === $text);
148 return $res;
149 }
150
151 # Matches the word, where $1 is a wildcard
152 function getVariableRegex()
153 {
154 if ( $this->mVariableRegex == "" ) {
155 $this->initRegex();
156 }
157 return $this->mVariableRegex;
158 }
159
160 # Matches the entire string, where $1 is a wildcard
161 function getVariableStartToEndRegex()
162 {
163 if ( $this->mVariableStartToEndRegex == "" ) {
164 $this->initRegex();
165 }
166 return $this->mVariableStartToEndRegex;
167 }
168
169 # Accesses the synonym list directly
170 function getSynonym( $i ) {
171 return $this->mSynonyms[$i];
172 }
173
174 # Returns true if the last call to replace() or substituteCallback()
175 # returned a modified text, otherwise false.
176 function getWasModified(){
177 return $this->mModified;
178 }
179
180 # $magicarr is an associative array of (magic word ID => replacement)
181 # This method uses the php feature to do several replacements at the same time,
182 # thereby gaining some efficiency. The result is placed in the out variable
183 # $result. The return value is true if something was replaced.
184
185 /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
186 $search = array();
187 $replace = array();
188 foreach( $magicarr as $id => $replacement ){
189 $mw = MagicWord::get( $id );
190 $search[] = $mw->getRegex();
191 $replace[] = $replacement;
192 }
193
194 $result = preg_replace( $search, $replace, $subject );
195 return !($result === $subject);
196 }
197 }
198
199 # Used in matchAndRemove()
200 /*private*/ function pregRemoveAndRecord( $match )
201 {
202 global $wgMagicFound;
203 $wgMagicFound = true;
204 return "";
205 }
206
207 ?>