JeLuF's image resizing code
[lhc/web/wiklou.git] / includes / MagicWord.php
1 <?
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 $escSyn = array_map( "preg_quote", $this->mSynonyms );
58 $this->mBaseRegex = implode( "|", $escSyn );
59 $case = $this->mCaseSensitive ? "" : "i";
60 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
61 $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
62 $this->mVariableRegex = str_replace( "\\$1", "([A-Za-z0-9_\-]*)", $this->mRegex );
63 $this->mVariableStartToEndRegex = str_replace( "\\$1", "([A-Za-z0-9_\-]*)", "/^{$this->mBaseRegex}$/{$case}" );
64 }
65
66 # Gets a regex representing matching the word
67 function getRegex()
68 {
69 if ($this->mRegex == "" ) {
70 $this->initRegex();
71 }
72 return $this->mRegex;
73 }
74
75 # Gets a regex matching the word, if it is at the
76 # string start
77 function getRegexStart()
78 {
79 if ($this->mRegex == "" ) {
80 $this->initRegex();
81 }
82 return $this->mRegexStart;
83 }
84
85 # regex without the slashes and what not
86 function getBaseRegex()
87 {
88 if ($this->mRegex == "") {
89 $this->initRegex();
90 }
91 return $this->mBaseRegex;
92 }
93
94 # Returns true if the text contains the word
95 function match( $text ) {
96 return preg_match( $this->getRegex(), $text );
97 }
98
99 # Returns true if the text starts with the word
100 function matchStart( $text )
101 {
102 return preg_match( $this->getRegexStart(), $text );
103 }
104
105 # Returns NULL if there's no match, the value of $1 otherwise
106 # The return code is the matched string, if there's no variable
107 # part in the regex and the matched variable part ($1) if there
108 # is one.
109 function matchVariableStartToEnd( $text ) {
110 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
111 if ( $matchcount == 0 ) {
112 return NULL;
113 } elseif ( count($matches) == 1 ) {
114 return $matches[0];
115 } else {
116 return $matches[1];
117 }
118 }
119
120
121 # Returns true if the text matches the word, and alters the
122 # input string, removing all instances of the word
123 function matchAndRemove( &$text )
124 {
125 global $wgMagicFound;
126 $wgMagicFound = false;
127 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
128 return $wgMagicFound;
129 }
130
131 # Replaces the word with something else
132 function replace( $replacement, $subject )
133 {
134 $res = preg_replace( $this->getRegex(), $replacement, $subject );
135 $this->mModified = !($res === $subject);
136 return $res;
137 }
138
139 # Variable handling: {{SUBST:xxx}} style words
140 # Calls back a function to determine what to replace xxx with
141 # Input word must contain $1
142 function substituteCallback( $text, $callback ) {
143 $regex = $this->getVariableRegex();
144 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
145 $this->mModified = !($res === $text);
146 return $res;
147 }
148
149 # Matches the word, where $1 is a wildcard
150 function getVariableRegex()
151 {
152 if ( $this->mVariableRegex == "" ) {
153 $this->initRegex();
154 }
155 return $this->mVariableRegex;
156 }
157
158 # Matches the entire string, where $1 is a wildcard
159 function getVariableStartToEndRegex()
160 {
161 if ( $this->mVariableStartToEndRegex == "" ) {
162 $this->initRegex();
163 }
164 return $this->mVariableStartToEndRegex;
165 }
166
167 # Accesses the synonym list directly
168 function getSynonym( $i ) {
169 return $this->mSynonyms[$i];
170 }
171
172 # Returns true if the last call to replace() or substituteCallback()
173 # returned a modified text, otherwise false.
174 function getWasModified(){
175 return $this->mModified;
176 }
177
178 # $magicarr is an associative array of (magic word ID => replacement)
179 # This method uses the php feature to do several replacements at the same time,
180 # thereby gaining some efficiency. The result is placed in the out variable
181 # $result. The return value is true if something was replaced.
182
183 /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
184 $search = array();
185 $replace = array();
186 foreach( $magicarr as $id => $replacement ){
187 $mw = MagicWord::get( $id );
188 $search[] = $mw->getRegex();
189 $replace[] = $replacement;
190 }
191
192 $result = preg_replace( $search, $replace, $subject );
193 return !($result === $subject);
194 }
195 }
196
197 # Used in matchAndRemove()
198 /*private*/ function pregRemoveAndRecord( $match )
199 {
200 global $wgMagicFound;
201 $wgMagicFound = true;
202 return "";
203 }
204
205 ?>