Added placeholders for text injection by hooks to EditPage.php
[lhc/web/wiklou.git] / includes / StringUtils.php
1 <?php
2
3 class StringUtils {
4 /**
5 * Perform an operation equivalent to
6 *
7 * preg_replace( "!$startDelim(.*?)$endDelim!", $replace, $subject );
8 *
9 * except that it's worst-case O(N) instead of O(N^2)
10 *
11 * Compared to delimiterReplace(), this implementation is fast but memory-
12 * hungry and inflexible. The memory requirements are such that I don't
13 * recommend using it on anything but guaranteed small chunks of text.
14 */
15 static function hungryDelimiterReplace( $startDelim, $endDelim, $replace, $subject ) {
16 $segments = explode( $startDelim, $subject );
17 $output = array_shift( $segments );
18 foreach ( $segments as $s ) {
19 $endDelimPos = strpos( $s, $endDelim );
20 if ( $endDelimPos === false ) {
21 $output .= $startDelim . $s;
22 } else {
23 $output .= $replace . substr( $s, $endDelimPos + strlen( $endDelim ) );
24 }
25 }
26 return $output;
27 }
28
29 /**
30 * Perform an operation equivalent to
31 *
32 * preg_replace_callback( "!$startDelim(.*)$endDelim!s$flags", $callback, $subject )
33 *
34 * This implementation is slower than hungryDelimiterReplace but uses far less
35 * memory. The delimiters are literal strings, not regular expressions.
36 *
37 * @param string $flags Regular expression flags
38 */
39 # If the start delimiter ends with an initial substring of the end delimiter,
40 # e.g. in the case of C-style comments, the behaviour differs from the model
41 # regex. In this implementation, the end must share no characters with the
42 # start, so e.g. /*/ is not considered to be both the start and end of a
43 # comment. /*/xy/*/ is considered to be a single comment with contents /xy/.
44 static function delimiterReplaceCallback( $startDelim, $endDelim, $callback, $subject, $flags = '' ) {
45 $inputPos = 0;
46 $outputPos = 0;
47 $output = '';
48 $foundStart = false;
49 $encStart = preg_quote( $startDelim, '!' );
50 $encEnd = preg_quote( $endDelim, '!' );
51 $strcmp = strpos( $flags, 'i' ) === false ? 'strcmp' : 'strcasecmp';
52 $endLength = strlen( $endDelim );
53
54 while ( $inputPos < strlen( $subject ) &&
55 preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos ) )
56 {
57 $tokenOffset = $m[0][1];
58 if ( $m[1][0] != '' ) {
59 if ( $foundStart &&
60 $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0 )
61 {
62 # An end match is present at the same location
63 $tokenType = 'end';
64 $tokenLength = $endLength;
65 } else {
66 $tokenType = 'start';
67 $tokenLength = strlen( $m[0][0] );
68 }
69 } elseif ( $m[2][0] != '' ) {
70 $tokenType = 'end';
71 $tokenLength = strlen( $m[0][0] );
72 } else {
73 throw new MWException( 'Invalid delimiter given to ' . __METHOD__ );
74 }
75
76 if ( $tokenType == 'start' ) {
77 $inputPos = $tokenOffset + $tokenLength;
78 # Only move the start position if we haven't already found a start
79 # This means that START START END matches outer pair
80 if ( !$foundStart ) {
81 # Found start
82 # Write out the non-matching section
83 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
84 $outputPos = $tokenOffset;
85 $contentPos = $inputPos;
86 $foundStart = true;
87 }
88 } elseif ( $tokenType == 'end' ) {
89 if ( $foundStart ) {
90 # Found match
91 $output .= call_user_func( $callback, array(
92 substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
93 substr( $subject, $contentPos, $tokenOffset - $contentPos )
94 ));
95 $foundStart = false;
96 } else {
97 # Non-matching end, write it out
98 $output .= substr( $subject, $inputPos, $tokenOffset + $tokenLength - $outputPos );
99 }
100 $inputPos = $outputPos = $tokenOffset + $tokenLength;
101 } else {
102 throw new MWException( 'Invalid delimiter given to ' . __METHOD__ );
103 }
104 }
105 if ( $outputPos < strlen( $subject ) ) {
106 $output .= substr( $subject, $outputPos );
107 }
108 return $output;
109 }
110
111 /*
112 * Perform an operation equivalent to
113 *
114 * preg_replace( "!$startDelim(.*)$endDelim!$flags", $replace, $subject )
115 *
116 * @param string $startDelim Start delimiter regular expression
117 * @param string $endDelim End delimiter regular expression
118 * @param string $replace Replacement string. May contain $1, which will be
119 * replaced by the text between the delimiters
120 * @param string $subject String to search
121 * @return string The string with the matches replaced
122 */
123 static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
124 $replacer = new RegexlikeReplacer( $replace );
125 return self::delimiterReplaceCallback( $startDelim, $endDelim,
126 $replacer->cb(), $subject, $flags );
127 }
128
129 /**
130 * More or less "markup-safe" explode()
131 * Ignores any instances of the separator inside <...>
132 * @param string $separator
133 * @param string $text
134 * @return array
135 */
136 static function explodeMarkup( $separator, $text ) {
137 $placeholder = "\x00";
138
139 // Remove placeholder instances
140 $text = str_replace( $placeholder, '', $text );
141
142 // Replace instances of the separator inside HTML-like tags with the placeholder
143 $replacer = new DoubleReplacer( $separator, $placeholder );
144 $cleaned = StringUtils::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
145
146 // Explode, then put the replaced separators back in
147 $items = explode( $separator, $cleaned );
148 foreach( $items as $i => $str ) {
149 $items[$i] = str_replace( $placeholder, $separator, $str );
150 }
151
152 return $items;
153 }
154
155 /**
156 * Escape a string to make it suitable for inclusion in a preg_replace()
157 * replacement parameter.
158 *
159 * @param string $string
160 * @return string
161 */
162 static function escapeRegexReplacement( $string ) {
163 $string = str_replace( '\\', '\\\\', $string );
164 $string = str_replace( '$', '\\$', $string );
165 return $string;
166 }
167 }
168
169 /**
170 * Base class for "replacers", objects used in preg_replace_callback() and
171 * StringUtils::delimiterReplaceCallback()
172 */
173 class Replacer {
174 function cb() {
175 return array( &$this, 'replace' );
176 }
177 }
178
179 /**
180 * Class to replace regex matches with a string similar to that used in preg_replace()
181 */
182 class RegexlikeReplacer extends Replacer {
183 var $r;
184 function __construct( $r ) {
185 $this->r = $r;
186 }
187
188 function replace( $matches ) {
189 $pairs = array();
190 foreach ( $matches as $i => $match ) {
191 $pairs["\$$i"] = $match;
192 }
193 return strtr( $this->r, $pairs );
194 }
195
196 }
197
198 /**
199 * Class to perform secondary replacement within each replacement string
200 */
201 class DoubleReplacer extends Replacer {
202 function __construct( $from, $to, $index = 0 ) {
203 $this->from = $from;
204 $this->to = $to;
205 $this->index = $index;
206 }
207
208 function replace( $matches ) {
209 return str_replace( $this->from, $this->to, $matches[$this->index] );
210 }
211 }
212
213 /**
214 * Class to perform replacement based on a simple hashtable lookup
215 */
216 class HashtableReplacer extends Replacer {
217 var $table, $index;
218
219 function __construct( $table, $index = 0 ) {
220 $this->table = $table;
221 $this->index = $index;
222 }
223
224 function replace( $matches ) {
225 return $this->table[$matches[$this->index]];
226 }
227 }
228
229 /**
230 * Replacement array for FSS with fallback to strtr()
231 * Supports lazy initialisation of FSS resource
232 */
233 class ReplacementArray {
234 /*mostly private*/ var $data = false;
235 /*mostly private*/ var $fss = false;
236
237 /**
238 * Create an object with the specified replacement array
239 * The array should have the same form as the replacement array for strtr()
240 */
241 function __construct( $data = array() ) {
242 $this->data = $data;
243 }
244
245 function __sleep() {
246 return array( 'data' );
247 }
248
249 function __wakeup() {
250 $this->fss = false;
251 }
252
253 /**
254 * Set the whole replacement array at once
255 */
256 function setArray( $data ) {
257 $this->data = $data;
258 $this->fss = false;
259 }
260
261 function getArray() {
262 return $this->data;
263 }
264
265 /**
266 * Set an element of the replacement array
267 */
268 function setPair( $from, $to ) {
269 $this->data[$from] = $to;
270 $this->fss = false;
271 }
272
273 function mergeArray( $data ) {
274 $this->data = array_merge( $this->data, $data );
275 $this->fss = false;
276 }
277
278 function merge( $other ) {
279 $this->data = array_merge( $this->data, $other->data );
280 $this->fss = false;
281 }
282
283 function replace( $subject ) {
284 if ( function_exists( 'fss_prep_replace' ) ) {
285 wfProfileIn( __METHOD__.'-fss' );
286 if ( $this->fss === false ) {
287 $this->fss = fss_prep_replace( $this->data );
288 }
289 $result = fss_exec_replace( $this->fss, $subject );
290 wfProfileOut( __METHOD__.'-fss' );
291 } else {
292 wfProfileIn( __METHOD__.'-strtr' );
293 $result = strtr( $subject, $this->data );
294 wfProfileOut( __METHOD__.'-strtr' );
295 }
296 return $result;
297 }
298 }
299
300 ?>