MediaWiki  1.33.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 .= $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 = '' ) {
247  $startDelim, $endDelim,
248  function ( array $matches ) use ( $replace ) {
249  return strtr( $replace, [ '$0' => $matches[0], '$1' => $matches[1] ] );
250  },
251  $subject, $flags
252  );
253  }
254 
262  static function explodeMarkup( $separator, $text ) {
263  $placeholder = "\x00";
264 
265  // Remove placeholder instances
266  $text = str_replace( $placeholder, '', $text );
267 
268  // Replace instances of the separator inside HTML-like tags with the placeholder
270  '<', '>',
271  function ( array $matches ) use ( $separator, $placeholder ) {
272  return str_replace( $separator, $placeholder, $matches[0] );
273  },
274  $text
275  );
276 
277  // Explode, then put the replaced separators back in
278  $items = explode( $separator, $cleaned );
279  foreach ( $items as $i => $str ) {
280  $items[$i] = str_replace( $placeholder, $separator, $str );
281  }
282 
283  return $items;
284  }
285 
294  static function replaceMarkup( $search, $replace, $text ) {
295  $placeholder = "\x00";
296 
297  // Remove placeholder instances
298  $text = str_replace( $placeholder, '', $text );
299 
300  // Replace instances of the separator inside HTML-like tags with the placeholder
302  '<', '>',
303  function ( array $matches ) use ( $search, $placeholder ) {
304  return str_replace( $search, $placeholder, $matches[0] );
305  },
306  $text
307  );
308 
309  // Explode, then put the replaced separators back in
310  $cleaned = str_replace( $search, $replace, $cleaned );
311  $text = str_replace( $placeholder, $search, $cleaned );
312 
313  return $text;
314  }
315 
323  static function escapeRegexReplacement( $string ) {
324  $string = str_replace( '\\', '\\\\', $string );
325  $string = str_replace( '$', '\\$', $string );
326  return $string;
327  }
328 
336  static function explode( $separator, $subject ) {
337  if ( substr_count( $subject, $separator ) > 1000 ) {
338  return new ExplodeIterator( $separator, $subject );
339  } else {
340  return new ArrayIterator( explode( $separator, $subject ) );
341  }
342  }
343 }
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:323
$s
$s
Definition: mergeMessageFileList.php:186
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:294
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:262
$matches
$matches
Definition: NoLocalSettings.php:24
StringUtils\explode
static explode( $separator, $subject)
Workalike for explode() with limited memory usage.
Definition: StringUtils.php:336
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$output
$output
Definition: SyntaxHighlight.php:334
StringUtils\delimiterReplaceCallback
static delimiterReplaceCallback( $startDelim, $endDelim, $callback, $subject, $flags='')
Perform an operation equivalent to preg_replace_callback()
Definition: StringUtils.php:154
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$value
$value
Definition: styleTest.css.php:49
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
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