MediaWiki  1.31.0
StringUtils.php
Go to the documentation of this file.
1 <?php
26 class StringUtils {
41  static function isUtf8( $value ) {
42  return mb_check_encoding( (string)$value, 'UTF-8' );
43  }
44 
56  static function delimiterExplode( $startDelim, $endDelim, $separator,
57  $subject, $nested = false ) {
58  $inputPos = 0;
59  $lastPos = 0;
60  $depth = 0;
61  $encStart = preg_quote( $startDelim, '!' );
62  $encEnd = preg_quote( $endDelim, '!' );
63  $encSep = preg_quote( $separator, '!' );
64  $len = strlen( $subject );
65  $m = [];
66  $exploded = [];
67  while (
68  $inputPos < $len &&
69  preg_match(
70  "!$encStart|$encEnd|$encSep!S", $subject, $m,
71  PREG_OFFSET_CAPTURE, $inputPos
72  )
73  ) {
74  $match = $m[0][0];
75  $matchPos = $m[0][1];
76  $inputPos = $matchPos + strlen( $match );
77  if ( $match === $separator ) {
78  if ( $depth === 0 ) {
79  $exploded[] = substr(
80  $subject, $lastPos, $matchPos - $lastPos
81  );
82  $lastPos = $inputPos;
83  }
84  } elseif ( $match === $startDelim ) {
85  if ( $depth === 0 || $nested ) {
86  $depth++;
87  }
88  } else {
89  $depth--;
90  }
91  }
92  $exploded[] = substr( $subject, $lastPos );
93  // This method could be rewritten in the future to avoid creating an
94  // intermediate array, since the return type is just an iterator.
95  return new ArrayIterator( $exploded );
96  }
97 
115  static function hungryDelimiterReplace( $startDelim, $endDelim, $replace, $subject ) {
116  $segments = explode( $startDelim, $subject );
117  $output = array_shift( $segments );
118  foreach ( $segments as $s ) {
119  $endDelimPos = strpos( $s, $endDelim );
120  if ( $endDelimPos === false ) {
121  $output .= $startDelim . $s;
122  } else {
123  $output .= $replace . substr( $s, $endDelimPos + strlen( $endDelim ) );
124  }
125  }
126 
127  return $output;
128  }
129 
154  static function delimiterReplaceCallback( $startDelim, $endDelim, $callback,
155  $subject, $flags = ''
156  ) {
157  $inputPos = 0;
158  $outputPos = 0;
159  $contentPos = 0;
160  $output = '';
161  $foundStart = false;
162  $encStart = preg_quote( $startDelim, '!' );
163  $encEnd = preg_quote( $endDelim, '!' );
164  $strcmp = strpos( $flags, 'i' ) === false ? 'strcmp' : 'strcasecmp';
165  $endLength = strlen( $endDelim );
166  $m = [];
167 
168  while ( $inputPos < strlen( $subject ) &&
169  preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos )
170  ) {
171  $tokenOffset = $m[0][1];
172  if ( $m[1][0] != '' ) {
173  if ( $foundStart &&
174  $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0
175  ) {
176  # An end match is present at the same location
177  $tokenType = 'end';
178  $tokenLength = $endLength;
179  } else {
180  $tokenType = 'start';
181  $tokenLength = strlen( $m[0][0] );
182  }
183  } elseif ( $m[2][0] != '' ) {
184  $tokenType = 'end';
185  $tokenLength = strlen( $m[0][0] );
186  } else {
187  throw new InvalidArgumentException( 'Invalid delimiter given to ' . __METHOD__ );
188  }
189 
190  if ( $tokenType == 'start' ) {
191  # Only move the start position if we haven't already found a start
192  # This means that START START END matches outer pair
193  if ( !$foundStart ) {
194  # Found start
195  $inputPos = $tokenOffset + $tokenLength;
196  # Write out the non-matching section
197  $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
198  $outputPos = $tokenOffset;
199  $contentPos = $inputPos;
200  $foundStart = true;
201  } else {
202  # Move the input position past the *first character* of START,
203  # to protect against missing END when it overlaps with START
204  $inputPos = $tokenOffset + 1;
205  }
206  } elseif ( $tokenType == 'end' ) {
207  if ( $foundStart ) {
208  # Found match
209  $output .= call_user_func( $callback, [
210  substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
211  substr( $subject, $contentPos, $tokenOffset - $contentPos )
212  ] );
213  $foundStart = false;
214  } else {
215  # Non-matching end, write it out
216  $output .= substr( $subject, $inputPos, $tokenOffset + $tokenLength - $outputPos );
217  }
218  $inputPos = $outputPos = $tokenOffset + $tokenLength;
219  } else {
220  throw new InvalidArgumentException( 'Invalid delimiter given to ' . __METHOD__ );
221  }
222  }
223  if ( $outputPos < strlen( $subject ) ) {
224  $output .= substr( $subject, $outputPos );
225  }
226 
227  return $output;
228  }
229 
245  static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
246  $replacer = new RegexlikeReplacer( $replace );
247 
248  return self::delimiterReplaceCallback( $startDelim, $endDelim,
249  $replacer->cb(), $subject, $flags );
250  }
251 
259  static function explodeMarkup( $separator, $text ) {
260  $placeholder = "\x00";
261 
262  // Remove placeholder instances
263  $text = str_replace( $placeholder, '', $text );
264 
265  // Replace instances of the separator inside HTML-like tags with the placeholder
266  $replacer = new DoubleReplacer( $separator, $placeholder );
267  $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
268 
269  // Explode, then put the replaced separators back in
270  $items = explode( $separator, $cleaned );
271  foreach ( $items as $i => $str ) {
272  $items[$i] = str_replace( $placeholder, $separator, $str );
273  }
274 
275  return $items;
276  }
277 
286  static function replaceMarkup( $search, $replace, $text ) {
287  $placeholder = "\x00";
288 
289  // Remove placeholder instances
290  $text = str_replace( $placeholder, '', $text );
291 
292  // Replace instances of the separator inside HTML-like tags with the placeholder
293  $replacer = new DoubleReplacer( $search, $placeholder );
294  $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
295 
296  // Explode, then put the replaced separators back in
297  $cleaned = str_replace( $search, $replace, $cleaned );
298  $text = str_replace( $placeholder, $search, $cleaned );
299 
300  return $text;
301  }
302 
310  static function escapeRegexReplacement( $string ) {
311  $string = str_replace( '\\', '\\\\', $string );
312  $string = str_replace( '$', '\\$', $string );
313  return $string;
314  }
315 
323  static function explode( $separator, $subject ) {
324  if ( substr_count( $subject, $separator ) > 1000 ) {
325  return new ExplodeIterator( $separator, $subject );
326  } else {
327  return new ArrayIterator( explode( $separator, $subject ) );
328  }
329  }
330 }
StringUtils\isUtf8
static isUtf8( $value)
Test whether a string is valid UTF-8.
Definition: StringUtils.php:41
StringUtils\hungryDelimiterReplace
static hungryDelimiterReplace( $startDelim, $endDelim, $replace, $subject)
Perform an operation equivalent to preg_replace()
Definition: StringUtils.php:115
StringUtils
A collection of static methods to play with strings.
Definition: StringUtils.php:26
StringUtils\escapeRegexReplacement
static escapeRegexReplacement( $string)
Escape a string to make it suitable for inclusion in a preg_replace() replacement parameter.
Definition: StringUtils.php:310
$s
$s
Definition: mergeMessageFileList.php:187
php
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:35
StringUtils\replaceMarkup
static replaceMarkup( $search, $replace, $text)
More or less "markup-safe" str_replace() Ignores any instances of the separator inside <....
Definition: StringUtils.php:286
ExplodeIterator
An iterator which works exactly like:
Definition: ExplodeIterator.php:30
StringUtils\explodeMarkup
static explodeMarkup( $separator, $text)
More or less "markup-safe" explode() Ignores any instances of the separator inside <....
Definition: StringUtils.php:259
StringUtils\explode
static explode( $separator, $subject)
Workalike for explode() with limited memory usage.
Definition: StringUtils.php:323
$output
$output
Definition: SyntaxHighlight.php:338
StringUtils\delimiterReplaceCallback
static delimiterReplaceCallback( $startDelim, $endDelim, $callback, $subject, $flags='')
Perform an operation equivalent to preg_replace_callback()
Definition: StringUtils.php:154
$value
$value
Definition: styleTest.css.php:45
StringUtils\delimiterExplode
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...
Definition: StringUtils.php:56
RegexlikeReplacer
Class to replace regex matches with a string similar to that used in preg_replace()
Definition: RegexlikeReplacer.php:24
DoubleReplacer
Class to perform secondary replacement within each replacement string.
Definition: DoubleReplacer.php:24
as
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
Definition: distributors.txt:9
StringUtils\delimiterReplace
static delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags='')
Perform an operation equivalent to preg_replace() with flags.
Definition: StringUtils.php:245