MediaWiki  1.27.1
StringUtils.php
Go to the documentation of this file.
1 <?php
26 class StringUtils {
41  static function isUtf8( $value ) {
42  $value = (string)$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 
74  static function hungryDelimiterReplace( $startDelim, $endDelim, $replace, $subject ) {
75  $segments = explode( $startDelim, $subject );
76  $output = array_shift( $segments );
77  foreach ( $segments as $s ) {
78  $endDelimPos = strpos( $s, $endDelim );
79  if ( $endDelimPos === false ) {
80  $output .= $startDelim . $s;
81  } else {
82  $output .= $replace . substr( $s, $endDelimPos + strlen( $endDelim ) );
83  }
84  }
85 
86  return $output;
87  }
88 
113  static function delimiterReplaceCallback( $startDelim, $endDelim, $callback,
114  $subject, $flags = ''
115  ) {
116  $inputPos = 0;
117  $outputPos = 0;
118  $output = '';
119  $foundStart = false;
120  $encStart = preg_quote( $startDelim, '!' );
121  $encEnd = preg_quote( $endDelim, '!' );
122  $strcmp = strpos( $flags, 'i' ) === false ? 'strcmp' : 'strcasecmp';
123  $endLength = strlen( $endDelim );
124  $m = [];
125 
126  while ( $inputPos < strlen( $subject ) &&
127  preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos )
128  ) {
129  $tokenOffset = $m[0][1];
130  if ( $m[1][0] != '' ) {
131  if ( $foundStart &&
132  $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0
133  ) {
134  # An end match is present at the same location
135  $tokenType = 'end';
136  $tokenLength = $endLength;
137  } else {
138  $tokenType = 'start';
139  $tokenLength = strlen( $m[0][0] );
140  }
141  } elseif ( $m[2][0] != '' ) {
142  $tokenType = 'end';
143  $tokenLength = strlen( $m[0][0] );
144  } else {
145  throw new InvalidArgumentException( 'Invalid delimiter given to ' . __METHOD__ );
146  }
147 
148  if ( $tokenType == 'start' ) {
149  # Only move the start position if we haven't already found a start
150  # This means that START START END matches outer pair
151  if ( !$foundStart ) {
152  # Found start
153  $inputPos = $tokenOffset + $tokenLength;
154  # Write out the non-matching section
155  $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
156  $outputPos = $tokenOffset;
157  $contentPos = $inputPos;
158  $foundStart = true;
159  } else {
160  # Move the input position past the *first character* of START,
161  # to protect against missing END when it overlaps with START
162  $inputPos = $tokenOffset + 1;
163  }
164  } elseif ( $tokenType == 'end' ) {
165  if ( $foundStart ) {
166  # Found match
167  $output .= call_user_func( $callback, [
168  substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
169  substr( $subject, $contentPos, $tokenOffset - $contentPos )
170  ] );
171  $foundStart = false;
172  } else {
173  # Non-matching end, write it out
174  $output .= substr( $subject, $inputPos, $tokenOffset + $tokenLength - $outputPos );
175  }
176  $inputPos = $outputPos = $tokenOffset + $tokenLength;
177  } else {
178  throw new InvalidArgumentException( 'Invalid delimiter given to ' . __METHOD__ );
179  }
180  }
181  if ( $outputPos < strlen( $subject ) ) {
182  $output .= substr( $subject, $outputPos );
183  }
184 
185  return $output;
186  }
187 
203  static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
204  $replacer = new RegexlikeReplacer( $replace );
205 
206  return self::delimiterReplaceCallback( $startDelim, $endDelim,
207  $replacer->cb(), $subject, $flags );
208  }
209 
217  static function explodeMarkup( $separator, $text ) {
218  $placeholder = "\x00";
219 
220  // Remove placeholder instances
221  $text = str_replace( $placeholder, '', $text );
222 
223  // Replace instances of the separator inside HTML-like tags with the placeholder
224  $replacer = new DoubleReplacer( $separator, $placeholder );
225  $cleaned = StringUtils::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
226 
227  // Explode, then put the replaced separators back in
228  $items = explode( $separator, $cleaned );
229  foreach ( $items as $i => $str ) {
230  $items[$i] = str_replace( $placeholder, $separator, $str );
231  }
232 
233  return $items;
234  }
235 
244  static function replaceMarkup( $search, $replace, $text ) {
245  $placeholder = "\x00";
246 
247  // Remove placeholder instances
248  $text = str_replace( $placeholder, '', $text );
249 
250  // Replace instances of the separator inside HTML-like tags with the placeholder
251  $replacer = new DoubleReplacer( $search, $placeholder );
252  $cleaned = StringUtils::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
253 
254  // Explode, then put the replaced separators back in
255  $cleaned = str_replace( $search, $replace, $cleaned );
256  $text = str_replace( $placeholder, $search, $cleaned );
257 
258  return $text;
259  }
260 
268  static function escapeRegexReplacement( $string ) {
269  $string = str_replace( '\\', '\\\\', $string );
270  $string = str_replace( '$', '\\$', $string );
271  return $string;
272  }
273 
281  static function explode( $separator, $subject ) {
282  if ( substr_count( $subject, $separator ) > 1000 ) {
283  return new ExplodeIterator( $separator, $subject );
284  } else {
285  return new ArrayIterator( explode( $separator, $subject ) );
286  }
287  }
288 }
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:177
$value
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2548
Class to perform secondary replacement within each replacement string.
static escapeRegexReplacement($string)
Escape a string to make it suitable for inclusion in a preg_replace() replacement parameter...
A collection of static methods to play with strings.
Definition: StringUtils.php:26
static isUtf8($value)
Test whether a string is valid UTF-8.
Definition: StringUtils.php:41
static hungryDelimiterReplace($startDelim, $endDelim, $replace, $subject)
Perform an operation equivalent to preg_replace()
Definition: StringUtils.php:74
static delimiterReplace($startDelim, $endDelim, $replace, $subject, $flags= '')
Perform an operation equivalent to preg_replace() with flags.
static replaceMarkup($search, $replace, $text)
More or less "markup-safe" str_replace() Ignores any instances of the separator inside <...
static delimiterReplaceCallback($startDelim, $endDelim, $callback, $subject, $flags= '')
Perform an operation equivalent to preg_replace_callback()
An iterator which works exactly like:
static explodeMarkup($separator, $text)
More or less "markup-safe" explode() Ignores any instances of the separator inside <...
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
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1004
Class to replace regex matches with a string similar to that used in preg_replace() ...
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
static explode($separator, $subject)
Workalike for explode() with limited memory usage.