}
}
+/**
+ * Like array_filter with ARRAY_FILTER_USE_BOTH, but works pre-5.6.
+ *
+ * @param array $arr
+ * @param callable $callback Will be called with the array value and key (in that order) and
+ * should return a bool which will determine whether the array element is kept.
+ * @return array
+ */
+function wfArrayFilter( array $arr, callable $callback ) {
+ if ( defined( 'ARRAY_FILTER_USE_BOTH' ) ) {
+ return array_filter( $arr, $callback, ARRAY_FILTER_USE_BOTH );
+ }
+ $filteredKeys = array_filter( array_keys( $arr ), function ( $key ) use ( $arr, $callback ) {
+ return call_user_func( $callback, $arr[$key], $key );
+ } );
+ return array_intersect_key( $arr, array_fill_keys( $filteredKeys, true ) );
+}
+
+/**
+ * Like array_filter with ARRAY_FILTER_USE_KEY, but works pre-5.6.
+ *
+ * @param array $arr
+ * @param callable $callback Will be called with the array key and should return a bool which
+ * will determine whether the array element is kept.
+ * @return array
+ */
+function wfArrayFilterByKey( array $arr, callable $callback ) {
+ return wfArrayFilter( $arr, function ( $val, $key ) use ( $callback ) {
+ return call_user_func( $callback, $key );
+ } );
+}
+
/**
* Appends to second array if $value differs from that in $default
*
* @param string|int $key
* @param mixed $value
* @param mixed $default
- * @param array $changed Array to alter
+ * @param array &$changed Array to alter
* @throws MWException
*/
function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) {
* 1) Does not raise warnings on bad URLs (just returns false).
* 2) Handles protocols that don't use :// (e.g., mailto: and news:, as well as
* protocol-relative URLs) correctly.
- * 3) Adds a "delimiter" element to the array, either '://', ':' or '//' (see (2)).
+ * 3) Adds a "delimiter" element to the array (see (2)).
+ * 4) Verifies that the protocol is on the $wgUrlProtocols whitelist.
+ * 5) Rejects some invalid URLs that parse_url doesn't, e.g. the empty string or URLs starting with
+ * a line feed character.
*
* @param string $url A URL to parse
- * @return string[]|bool Bits of the URL in an associative array, per PHP docs, false on failure
+ * @return string[]|bool Bits of the URL in an associative array, or false on failure.
+ * Possible fields:
+ * - scheme: URI scheme (protocol), e.g. 'http', 'mailto'. Lowercase, always present, but can
+ * be an empty string for protocol-relative URLs.
+ * - delimiter: either '://', ':' or '//'. Always present.
+ * - host: domain name / IP. Always present, but could be an empty string, e.g. for file: URLs.
+ * - user: user name, e.g. for HTTP Basic auth URLs such as http://user:pass@example.com/
+ * Missing when there is no username.
+ * - pass: password, same as above.
+ * - path: path including the leading /. Will be missing when empty (e.g. 'http://example.com')
+ * - query: query string (as a string; see wfCgiToArray() for parsing it), can be missing.
+ * - fragment: the part after #, can be missing.
*/
function wfParseUrl( $url ) {
global $wgUrlProtocols; // Allow all protocols defined in DefaultSettings/LocalSettings.php
$profiler->logData();
$config = $context->getConfig();
- if ( $config->get( 'StatsdServer' ) ) {
+ $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
+ if ( $config->get( 'StatsdServer' ) && $stats->hasData() ) {
try {
$statsdServer = explode( ':', $config->get( 'StatsdServer' ) );
$statsdHost = $statsdServer[0];
$statsdSender = new SocketSender( $statsdHost, $statsdPort );
$statsdClient = new SamplingStatsdClient( $statsdSender, true, false );
$statsdClient->setSamplingRates( $config->get( 'StatsdSamplingRates' ) );
- $statsdClient->send(
- MediaWikiServices::getInstance()->getStatsdDataFactory()->getBuffer()
- );
+ $statsdClient->send( $stats->getData() );
} catch ( Exception $ex ) {
MWExceptionHandler::logException( $ex );
}
* If source is NULL, it just returns the value, it doesn't set the variable
* If force is true, it will set the value even if source is NULL
*
- * @param mixed $dest
+ * @param mixed &$dest
* @param mixed $source
* @param bool $force
* @return mixed
/**
* As for wfSetVar except setting a bit
*
- * @param int $dest
+ * @param int &$dest
* @param int $bit
* @param bool $state
*
* (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
* PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
*
- * @param string ... strings to escape and glue together, or a single array of strings parameter
+ * @param string $args,... strings to escape and glue together,
+ * or a single array of strings parameter
* @return string
*/
function wfEscapeShellArg( /*...*/ ) {
- wfInitShellLocale();
-
$args = func_get_args();
if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
// If only one argument has been passed, and that argument is an array,
$includeStderr = isset( $options['duplicateStderr'] ) && $options['duplicateStderr'];
$profileMethod = isset( $options['profileMethod'] ) ? $options['profileMethod'] : wfGetCaller();
- wfInitShellLocale();
-
$envcmd = '';
foreach ( $environ as $k => $v ) {
if ( wfIsWindows() ) {
}
/**
- * Set the locale for locale-sensitive operations
+ * Formerly set the locale for locale-sensitive operations
*
- * Sets LC_ALL to a known value to work around issues like the following:
- * - https://bugs.php.net/bug.php?id=45132 escapeshellarg() destroys non-ASCII
- * characters if LANG is not a UTF-8 locale
- * - T107128 Scribunto string comparison works case insensitive while the
- * standard Lua case sensitive
+ * This is now done in Setup.php.
*
+ * @deprecated since 1.30, no longer needed
* @see $wgShellLocale
*/
function wfInitShellLocale() {
- static $done = false;
- if ( $done ) {
- return;
- }
- $done = true;
- global $wgShellLocale;
- putenv( "LC_ALL=$wgShellLocale" );
- setlocale( LC_ALL, $wgShellLocale );
}
/**
* @param string $old
* @param string $mine
* @param string $yours
- * @param string $result
+ * @param string &$result
* @return bool
*/
function wfMerge( $old, $mine, $yours, &$result ) {
}
/**
- * @deprecated since 1.30 Call makeKey on a ObjectCache instance
- *
* Make a cache key for the local wiki.
*
+ * @deprecated since 1.30 Call makeKey on a BagOStuff instance
* @param string $args,...
* @return string
*/
}
/**
- * @deprecated since 1.30 Call makeGlobalKey on a ObjectCache instance
- *
* Make a cache key with database-agnostic prefix.
*
* Doesn't have a wiki-specific namespace. Uses a generic 'global' prefix
* instead. Must have a prefix as otherwise keys that use a database name
* in the first segment will clash with wfMemcKey/wfForeignMemcKey.
*
+ * @deprecated since 1.30 Call makeGlobalKey on a BagOStuff instance
* @since 1.26
* @param string $args,...
* @return string
/**
* Get the cache object used by the parser cache
*
+ * @deprecated since 1.30, use MediaWikiServices::getParserCache()->getCacheStorage()
* @return BagOStuff
*/
function wfGetParserCacheStorage() {
}
$cache = ObjectCache::getLocalServerInstance( 'hash' );
- $key = wfMemcKey( 'bad-image-list', ( $blacklist === null ) ? 'default' : md5( $blacklist ) );
+ $key = $cache->makeKey(
+ 'bad-image-list', ( $blacklist === null ) ? 'default' : md5( $blacklist )
+ );
$badImages = $cache->get( $key );
if ( $badImages === false ) { // cache miss
* @since 1.25
*/
function wfIsInfinity( $str ) {
+ // These are hardcoded elsewhere in MediaWiki (e.g. mediawiki.special.block.js).
$infinityValues = [ 'infinite', 'indefinite', 'infinity', 'never' ];
return in_array( $str, $infinityValues );
}