Merge "Fix Postgres support"
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index d21cc1d..2090d90 100644 (file)
@@ -203,6 +203,38 @@ function wfArrayDiff2_cmp( $a, $b ) {
        }
 }
 
+/**
+ * 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
  *
@@ -1191,7 +1223,8 @@ function wfLogProfilingData() {
        $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];
@@ -1199,9 +1232,7 @@ function wfLogProfilingData() {
                        $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 );
                }
@@ -1276,7 +1307,7 @@ function wfIncrStats( $key, $count = 1 ) {
  * @return bool
  */
 function wfReadOnly() {
-       return \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
+       return MediaWikiServices::getInstance()->getReadOnlyMode()
                ->isReadOnly();
 }
 
@@ -1289,7 +1320,7 @@ function wfReadOnly() {
  * @return string|bool String when in read-only mode; false otherwise
  */
 function wfReadOnlyReason() {
-       return \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
+       return MediaWikiServices::getInstance()->getReadOnlyMode()
                ->getReason();
 }
 
@@ -1300,7 +1331,7 @@ function wfReadOnlyReason() {
  * @since 1.27
  */
 function wfConfiguredReadOnlyReason() {
-       return \MediaWiki\MediaWikiServices::getInstance()->getConfiguredReadOnlyMode()
+       return MediaWikiServices::getInstance()->getConfiguredReadOnlyMode()
                ->getReason();
 }
 
@@ -2193,8 +2224,6 @@ function wfIniGetBool( $setting ) {
  * @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,
@@ -2309,8 +2338,6 @@ function wfShellExec( $cmd, &$retval = null, $environ = [],
        $includeStderr = isset( $options['duplicateStderr'] ) && $options['duplicateStderr'];
        $profileMethod = isset( $options['profileMethod'] ) ? $options['profileMethod'] : wfGetCaller();
 
-       wfInitShellLocale();
-
        $envcmd = '';
        foreach ( $environ as $k => $v ) {
                if ( wfIsWindows() ) {
@@ -2534,25 +2561,14 @@ function wfShellExecWithStderr( $cmd, &$retval = null, $environ = [], $limits =
 }
 
 /**
- * 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 );
 }
 
 /**
@@ -2962,6 +2978,7 @@ function wfGetPrecompiledData( $name ) {
 /**
  * Make a cache key for the local wiki.
  *
+ * @deprecated since 1.30 Call makeKey on a BagOStuff instance
  * @param string $args,...
  * @return string
  */
@@ -2998,6 +3015,7 @@ function wfForeignMemcKey( $db, $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
@@ -3079,9 +3097,9 @@ function wfGetDB( $db, $groups = [], $wiki = false ) {
  */
 function wfGetLB( $wiki = false ) {
        if ( $wiki === false ) {
-               return \MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancer();
+               return MediaWikiServices::getInstance()->getDBLoadBalancer();
        } else {
-               $factory = \MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                return $factory->getMainLB( $wiki );
        }
 }
@@ -3094,7 +3112,7 @@ function wfGetLB( $wiki = false ) {
  * @return \Wikimedia\Rdbms\LBFactory
  */
 function wfGetLBFactory() {
-       return \MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+       return MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
 }
 
 /**
@@ -3513,7 +3531,9 @@ function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
        }
 
        $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