MediaWiki REL1_29
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 = StringUtils::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 = StringUtils::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...
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition hooks.txt:1108
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:183
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition hooks.txt:2753
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37