MediaWiki REL1_30
StringUtils.php
Go to the documentation of this file.
1<?php
41 static function isUtf8( $value ) {
43
44 // HHVM 3.4 and older come with an outdated version of libmbfl that
45 // incorrectly allows values above U+10FFFF, so we have to check
46 // for them separately. (This issue also exists in PHP 5.3 and
47 // older, which are no longer supported.)
48 static $newPHP;
49 if ( $newPHP === null ) {
50 $newPHP = !mb_check_encoding( "\xf4\x90\x80\x80", 'UTF-8' );
51 }
52
53 return mb_check_encoding( $value, 'UTF-8' ) &&
54 ( $newPHP || preg_match( "/\xf4[\x90-\xbf]|[\xf5-\xff]/S", $value ) === 0 );
55 }
56
68 static function delimiterExplode( $startDelim, $endDelim, $separator,
69 $subject, $nested = false ) {
70 $inputPos = 0;
71 $lastPos = 0;
72 $depth = 0;
73 $encStart = preg_quote( $startDelim, '!' );
74 $encEnd = preg_quote( $endDelim, '!' );
75 $encSep = preg_quote( $separator, '!' );
76 $len = strlen( $subject );
77 $m = [];
78 $exploded = [];
79 while (
80 $inputPos < $len &&
81 preg_match(
82 "!$encStart|$encEnd|$encSep!S", $subject, $m,
83 PREG_OFFSET_CAPTURE, $inputPos
84 )
85 ) {
86 $match = $m[0][0];
87 $matchPos = $m[0][1];
88 $inputPos = $matchPos + strlen( $match );
89 if ( $match === $separator ) {
90 if ( $depth === 0 ) {
91 $exploded[] = substr(
92 $subject, $lastPos, $matchPos - $lastPos
93 );
94 $lastPos = $inputPos;
95 }
96 } elseif ( $match === $startDelim ) {
97 if ( $depth === 0 || $nested ) {
98 $depth++;
99 }
100 } else {
101 $depth--;
102 }
103 }
104 $exploded[] = substr( $subject, $lastPos );
105 // This method could be rewritten in the future to avoid creating an
106 // intermediate array, since the return type is just an iterator.
107 return new ArrayIterator( $exploded );
108 }
109
127 static function hungryDelimiterReplace( $startDelim, $endDelim, $replace, $subject ) {
128 $segments = explode( $startDelim, $subject );
129 $output = array_shift( $segments );
130 foreach ( $segments as $s ) {
131 $endDelimPos = strpos( $s, $endDelim );
132 if ( $endDelimPos === false ) {
133 $output .= $startDelim . $s;
134 } else {
135 $output .= $replace . substr( $s, $endDelimPos + strlen( $endDelim ) );
136 }
137 }
138
139 return $output;
140 }
141
166 static function delimiterReplaceCallback( $startDelim, $endDelim, $callback,
167 $subject, $flags = ''
168 ) {
169 $inputPos = 0;
170 $outputPos = 0;
171 $contentPos = 0;
172 $output = '';
173 $foundStart = false;
174 $encStart = preg_quote( $startDelim, '!' );
175 $encEnd = preg_quote( $endDelim, '!' );
176 $strcmp = strpos( $flags, 'i' ) === false ? 'strcmp' : 'strcasecmp';
177 $endLength = strlen( $endDelim );
178 $m = [];
179
180 while ( $inputPos < strlen( $subject ) &&
181 preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos )
182 ) {
183 $tokenOffset = $m[0][1];
184 if ( $m[1][0] != '' ) {
185 if ( $foundStart &&
186 $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0
187 ) {
188 # An end match is present at the same location
189 $tokenType = 'end';
190 $tokenLength = $endLength;
191 } else {
192 $tokenType = 'start';
193 $tokenLength = strlen( $m[0][0] );
194 }
195 } elseif ( $m[2][0] != '' ) {
196 $tokenType = 'end';
197 $tokenLength = strlen( $m[0][0] );
198 } else {
199 throw new InvalidArgumentException( 'Invalid delimiter given to ' . __METHOD__ );
200 }
201
202 if ( $tokenType == 'start' ) {
203 # Only move the start position if we haven't already found a start
204 # This means that START START END matches outer pair
205 if ( !$foundStart ) {
206 # Found start
207 $inputPos = $tokenOffset + $tokenLength;
208 # Write out the non-matching section
209 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
210 $outputPos = $tokenOffset;
211 $contentPos = $inputPos;
212 $foundStart = true;
213 } else {
214 # Move the input position past the *first character* of START,
215 # to protect against missing END when it overlaps with START
216 $inputPos = $tokenOffset + 1;
217 }
218 } elseif ( $tokenType == 'end' ) {
219 if ( $foundStart ) {
220 # Found match
221 $output .= call_user_func( $callback, [
222 substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
223 substr( $subject, $contentPos, $tokenOffset - $contentPos )
224 ] );
225 $foundStart = false;
226 } else {
227 # Non-matching end, write it out
228 $output .= substr( $subject, $inputPos, $tokenOffset + $tokenLength - $outputPos );
229 }
230 $inputPos = $outputPos = $tokenOffset + $tokenLength;
231 } else {
232 throw new InvalidArgumentException( 'Invalid delimiter given to ' . __METHOD__ );
233 }
234 }
235 if ( $outputPos < strlen( $subject ) ) {
236 $output .= substr( $subject, $outputPos );
237 }
238
239 return $output;
240 }
241
257 static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
258 $replacer = new RegexlikeReplacer( $replace );
259
260 return self::delimiterReplaceCallback( $startDelim, $endDelim,
261 $replacer->cb(), $subject, $flags );
262 }
263
271 static function explodeMarkup( $separator, $text ) {
272 $placeholder = "\x00";
273
274 // Remove placeholder instances
275 $text = str_replace( $placeholder, '', $text );
276
277 // Replace instances of the separator inside HTML-like tags with the placeholder
278 $replacer = new DoubleReplacer( $separator, $placeholder );
279 $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
280
281 // Explode, then put the replaced separators back in
282 $items = explode( $separator, $cleaned );
283 foreach ( $items as $i => $str ) {
284 $items[$i] = str_replace( $placeholder, $separator, $str );
285 }
286
287 return $items;
288 }
289
298 static function replaceMarkup( $search, $replace, $text ) {
299 $placeholder = "\x00";
300
301 // Remove placeholder instances
302 $text = str_replace( $placeholder, '', $text );
303
304 // Replace instances of the separator inside HTML-like tags with the placeholder
305 $replacer = new DoubleReplacer( $search, $placeholder );
306 $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
307
308 // Explode, then put the replaced separators back in
309 $cleaned = str_replace( $search, $replace, $cleaned );
310 $text = str_replace( $placeholder, $search, $cleaned );
311
312 return $text;
313 }
314
322 static function escapeRegexReplacement( $string ) {
323 $string = str_replace( '\\', '\\\\', $string );
324 $string = str_replace( '$', '\\$', $string );
325 return $string;
326 }
327
335 static function explode( $separator, $subject ) {
336 if ( substr_count( $subject, $separator ) > 1000 ) {
337 return new ExplodeIterator( $separator, $subject );
338 } else {
339 return new ArrayIterator( explode( $separator, $subject ) );
340 }
341 }
342}
Class to perform secondary replacement within each replacement string.
An iterator which works exactly like:
Class to replace regex matches with a string similar to that used in preg_replace()
A collection of static methods to play with strings.
static hungryDelimiterReplace( $startDelim, $endDelim, $replace, $subject)
Perform an operation equivalent to preg_replace()
static delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags='')
Perform an operation equivalent to preg_replace() with flags.
static explodeMarkup( $separator, $text)
More or less "markup-safe" explode() Ignores any instances of the separator inside <....
static delimiterReplaceCallback( $startDelim, $endDelim, $callback, $subject, $flags='')
Perform an operation equivalent to preg_replace_callback()
static escapeRegexReplacement( $string)
Escape a string to make it suitable for inclusion in a preg_replace() replacement parameter.
static isUtf8( $value)
Test whether a string is valid UTF-8.
static replaceMarkup( $search, $replace, $text)
More or less "markup-safe" str_replace() Ignores any instances of the separator inside <....
static explode( $separator, $subject)
Workalike for explode() with limited memory usage.
static delimiterExplode( $startDelim, $endDelim, $separator, $subject, $nested=false)
Explode a string, but ignore any instances of the separator inside the given start and end delimiters...
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2225
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition hooks.txt:181
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition hooks.txt:2805