Merge "mediawiki.special/mediawiki.special.apisandbox: Use 'trash' icon which still...
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 3c3cdb8..c6ccf31 100644 (file)
@@ -27,7 +27,9 @@ if ( !defined( 'MEDIAWIKI' ) ) {
 use Liuggio\StatsdClient\Sender\SocketSender;
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\Session\SessionManager;
+use MediaWiki\MediaWikiServices;
 use Wikimedia\ScopedCallback;
+use Wikimedia\Rdbms\DBReplicationWaitError;
 
 // Hide compatibility functions from Doxygen
 /// @cond
@@ -222,18 +224,18 @@ function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) {
 /**
  * Merge arrays in the style of getUserPermissionsErrors, with duplicate removal
  * e.g.
- *     wfMergeErrorArrays(
- *             [ [ 'x' ] ],
- *             [ [ 'x', '2' ] ],
- *             [ [ 'x' ] ],
- *             [ [ 'y' ] ]
- *     );
+ *     wfMergeErrorArrays(
+ *       [ [ 'x' ] ],
+ *       [ [ 'x', '2' ] ],
+ *       [ [ 'x' ] ],
+ *       [ [ 'y' ] ]
+ *     );
  * returns:
- *             [
- *             [ 'x', '2' ],
- *             [ 'x' ],
- *             [ 'y' ]
- *     ]
+ *     [
+ *       [ 'x', '2' ],
+ *       [ 'x' ],
+ *       [ 'y' ]
+ *     ]
  *
  * @param array $array1,...
  * @return array
@@ -1189,7 +1191,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];
@@ -1197,7 +1200,7 @@ function wfLogProfilingData() {
                        $statsdSender = new SocketSender( $statsdHost, $statsdPort );
                        $statsdClient = new SamplingStatsdClient( $statsdSender, true, false );
                        $statsdClient->setSamplingRates( $config->get( 'StatsdSamplingRates' ) );
-                       $statsdClient->send( $context->getStats()->getBuffer() );
+                       $statsdClient->send( $stats->getData() );
                } catch ( Exception $ex ) {
                        MWExceptionHandler::logException( $ex );
                }
@@ -1262,7 +1265,7 @@ function wfLogProfilingData() {
  * @return void
  */
 function wfIncrStats( $key, $count = 1 ) {
-       $stats = RequestContext::getMain()->getStats();
+       $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
        $stats->updateCount( $key, $count );
 }
 
@@ -1272,7 +1275,8 @@ function wfIncrStats( $key, $count = 1 ) {
  * @return bool
  */
 function wfReadOnly() {
-       return wfReadOnlyReason() !== false;
+       return MediaWikiServices::getInstance()->getReadOnlyMode()
+               ->isReadOnly();
 }
 
 /**
@@ -1284,19 +1288,8 @@ function wfReadOnly() {
  * @return string|bool String when in read-only mode; false otherwise
  */
 function wfReadOnlyReason() {
-       $readOnly = wfConfiguredReadOnlyReason();
-       if ( $readOnly !== false ) {
-               return $readOnly;
-       }
-
-       static $lbReadOnly = null;
-       if ( $lbReadOnly === null ) {
-               // Callers use this method to be aware that data presented to a user
-               // may be very stale and thus allowing submissions can be problematic.
-               $lbReadOnly = wfGetLB()->getReadOnlyReason();
-       }
-
-       return $lbReadOnly;
+       return MediaWikiServices::getInstance()->getReadOnlyMode()
+               ->getReason();
 }
 
 /**
@@ -1306,18 +1299,8 @@ function wfReadOnlyReason() {
  * @since 1.27
  */
 function wfConfiguredReadOnlyReason() {
-       global $wgReadOnly, $wgReadOnlyFile;
-
-       if ( $wgReadOnly === null ) {
-               // Set $wgReadOnly for faster access next time
-               if ( is_file( $wgReadOnlyFile ) && filesize( $wgReadOnlyFile ) > 0 ) {
-                       $wgReadOnly = file_get_contents( $wgReadOnlyFile );
-               } else {
-                       $wgReadOnly = false;
-               }
-       }
-
-       return $wgReadOnly;
+       return MediaWikiServices::getInstance()->getConfiguredReadOnlyMode()
+               ->getReason();
 }
 
 /**
@@ -1452,7 +1435,6 @@ function wfMsgReplaceArgs( $message, $args ) {
 function wfHostname() {
        static $host;
        if ( is_null( $host ) ) {
-
                # Hostname overriding
                global $wgOverrideHostname;
                if ( $wgOverrideHostname !== false ) {
@@ -1787,6 +1769,7 @@ function wfHttpError( $code, $label, $desc ) {
                $wgOut->sendCacheControl();
        }
 
+       MediaWiki\HeaderCallback::warnIfHeadersSent();
        header( 'Content-type: text/html; charset=utf-8' );
        print '<!DOCTYPE html>' .
                '<html><head><title>' .
@@ -2209,8 +2192,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,
@@ -2325,8 +2306,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() ) {
@@ -2550,18 +2529,14 @@ function wfShellExecWithStderr( $cmd, &$retval = null, $environ = [], $limits =
 }
 
 /**
- * Workaround for https://bugs.php.net/bug.php?id=45132
- * escapeshellarg() destroys non-ASCII characters if LANG is not a UTF-8 locale
+ * Formerly set the locale for locale-sensitive operations
+ *
+ * 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_CTYPE=$wgShellLocale" );
-       setlocale( LC_CTYPE, $wgShellLocale );
 }
 
 /**
@@ -2572,8 +2547,8 @@ function wfInitShellLocale() {
  * @param string $script MediaWiki cli script path
  * @param array $parameters Arguments and options to the script
  * @param array $options Associative array of options:
- *             'php': The path to the php executable
- *             'wrapper': Path to a PHP wrapper to handle the maintenance script
+ *     'php': The path to the php executable
+ *     'wrapper': Path to a PHP wrapper to handle the maintenance script
  * @return string
  */
 function wfShellWikiCmd( $script, array $parameters = [], array $options = [] ) {
@@ -2971,6 +2946,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
  */
@@ -3007,6 +2983,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
@@ -3071,7 +3048,7 @@ function wfSplitWikiID( $wiki ) {
  * @todo Replace calls to wfGetDB with calls to LoadBalancer::getConnection()
  *       on an injected instance of LoadBalancer.
  *
- * @return Database
+ * @return \Wikimedia\Rdbms\Database
  */
 function wfGetDB( $db, $groups = [], $wiki = false ) {
        return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki );
@@ -3088,9 +3065,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 );
        }
 }
@@ -3103,7 +3080,7 @@ function wfGetLB( $wiki = false ) {
  * @return \Wikimedia\Rdbms\LBFactory
  */
 function wfGetLBFactory() {
-       return \MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+       return MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
 }
 
 /**
@@ -3522,7 +3499,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