Merge "Add protection for User: JSON pages in the same manner as JS & CSS ones"
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 1a33b76..513f593 100644 (file)
@@ -24,7 +24,6 @@ if ( !defined( 'MEDIAWIKI' ) ) {
        die( "This file is part of MediaWiki, it is not a valid entry point" );
 }
 
-use Liuggio\StatsdClient\Sender\SocketSender;
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\ProcOpenError;
 use MediaWiki\Session\SessionManager;
@@ -583,7 +582,7 @@ function wfAppendQuery( $url, $query ) {
  * like "subdir/foo.html", etc.
  *
  * @param string $url Either fully-qualified or a local path + query
- * @param string $defaultProto One of the PROTO_* constants. Determines the
+ * @param string|int|null $defaultProto One of the PROTO_* constants. Determines the
  *    protocol to use if $url or $wgServer is protocol-relative
  * @return string|false Fully-qualified URL, current-path-relative URL or false if
  *    no valid URL can be constructed
@@ -878,9 +877,9 @@ function wfParseUrl( $url ) {
        if ( $wasRelative ) {
                $url = "http:$url";
        }
-       MediaWiki\suppressWarnings();
+       Wikimedia\suppressWarnings();
        $bits = parse_url( $url );
-       MediaWiki\restoreWarnings();
+       Wikimedia\restoreWarnings();
        // parse_url() returns an array without scheme for some invalid URLs, e.g.
        // parse_url("%0Ahttp://example.com") == [ 'host' => '%0Ahttp', 'path' => 'example.com' ]
        if ( !$bits || !isset( $bits['scheme'] ) ) {
@@ -1011,7 +1010,7 @@ function wfMakeUrlIndexes( $url ) {
 
 /**
  * Check whether a given URL has a domain that occurs in a given set of domains
- * @param string $url URL
+ * @param string $url
  * @param array $domains Array of domains (strings)
  * @return bool True if the host part of $url ends in one of the strings in $domains
  */
@@ -1051,7 +1050,7 @@ function wfMatchesDomainList( $url, $domains ) {
  */
 function wfDebug( $text, $dest = 'all', array $context = [] ) {
        global $wgDebugRawPage, $wgDebugLogPrefix;
-       global $wgDebugTimestamps, $wgRequestTime;
+       global $wgDebugTimestamps;
 
        if ( !$wgDebugRawPage && wfIsDebugRawPage() ) {
                return;
@@ -1062,7 +1061,7 @@ function wfDebug( $text, $dest = 'all', array $context = [] ) {
        if ( $wgDebugTimestamps ) {
                $context['seconds_elapsed'] = sprintf(
                        '%6.4f',
-                       microtime( true ) - $wgRequestTime
+                       microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT']
                );
                $context['memory_used'] = sprintf(
                        '%5.1fM',
@@ -1231,6 +1230,7 @@ function wfErrorLog( $text, $file, array $context = [] ) {
 
 /**
  * @todo document
+ * @todo Move logic to MediaWiki.php
  */
 function wfLogProfilingData() {
        global $wgDebugLogGroups, $wgDebugRawPage;
@@ -1242,23 +1242,13 @@ function wfLogProfilingData() {
        $profiler->setContext( $context );
        $profiler->logData();
 
-       $config = $context->getConfig();
-       $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
-       if ( $config->get( 'StatsdServer' ) && $stats->hasData() ) {
-               try {
-                       $statsdServer = explode( ':', $config->get( 'StatsdServer' ) );
-                       $statsdHost = $statsdServer[0];
-                       $statsdPort = isset( $statsdServer[1] ) ? $statsdServer[1] : 8125;
-                       $statsdSender = new SocketSender( $statsdHost, $statsdPort );
-                       $statsdClient = new SamplingStatsdClient( $statsdSender, true, false );
-                       $statsdClient->setSamplingRates( $config->get( 'StatsdSamplingRates' ) );
-                       $statsdClient->send( $stats->getData() );
-               } catch ( Exception $ex ) {
-                       MWExceptionHandler::logException( $ex );
-               }
-       }
+       // Send out any buffered statsd metrics as needed
+       MediaWiki::emitBufferedStatsdData(
+               MediaWikiServices::getInstance()->getStatsdDataFactory(),
+               $context->getConfig()
+       );
 
-       # Profiling must actually be enabled...
+       // Profiling must actually be enabled...
        if ( $profiler instanceof ProfilerStub ) {
                return;
        }
@@ -1524,9 +1514,11 @@ function wfHostname() {
  * @return string
  */
 function wfReportTime() {
-       global $wgRequestTime, $wgShowHostnames;
+       global $wgShowHostnames;
 
-       $responseTime = round( ( microtime( true ) - $wgRequestTime ) * 1000 );
+       $elapsed = ( microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT'] );
+       // seconds to milliseconds
+       $responseTime = round( $elapsed * 1000 );
        $reportVars = [ 'wgBackendResponseTime' => $responseTime ];
        if ( $wgShowHostnames ) {
                $reportVars['wgHostname'] = wfHostname();
@@ -1853,7 +1845,7 @@ function wfHttpError( $code, $label, $desc ) {
 function wfResetOutputBuffers( $resetGzipEncoding = true ) {
        if ( $resetGzipEncoding ) {
                // Suppress Content-Encoding and Content-Length
-               // headers from 1.10+s wfOutputHandler
+               // headers from OutputHandler::handle.
                global $wgDisableOutputCompression;
                $wgDisableOutputCompression = true;
        }
@@ -2019,19 +2011,19 @@ function wfNegotiateType( $cprefs, $sprefs ) {
 /**
  * Reference-counted warning suppression
  *
- * @deprecated since 1.26, use MediaWiki\suppressWarnings() directly
+ * @deprecated since 1.26, use Wikimedia\suppressWarnings() directly
  * @param bool $end
  */
 function wfSuppressWarnings( $end = false ) {
-       MediaWiki\suppressWarnings( $end );
+       Wikimedia\suppressWarnings( $end );
 }
 
 /**
- * @deprecated since 1.26, use MediaWiki\restoreWarnings() directly
+ * @deprecated since 1.26, use Wikimedia\restoreWarnings() directly
  * Restore error level to previous value
  */
 function wfRestoreWarnings() {
-       MediaWiki\suppressWarnings( true );
+       Wikimedia\restoreWarnings();
 }
 
 /**
@@ -2098,6 +2090,16 @@ function wfIsHHVM() {
        return defined( 'HHVM_VERSION' );
 }
 
+/**
+ * Check if we are running from the commandline
+ *
+ * @since 1.31
+ * @return bool
+ */
+function wfIsCLI() {
+       return PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg';
+}
+
 /**
  * Tries to get the system directory for temporary files. First
  * $wgTmpDirectory is checked, and then the TMPDIR, TMP, and TEMP
@@ -2150,9 +2152,9 @@ function wfMkdirParents( $dir, $mode = null, $caller = null ) {
        }
 
        // Turn off the normal warning, we're doing our own below
-       MediaWiki\suppressWarnings();
+       Wikimedia\suppressWarnings();
        $ok = mkdir( $dir, $mode, true ); // PHP5 <3
-       MediaWiki\restoreWarnings();
+       Wikimedia\restoreWarnings();
 
        if ( !$ok ) {
                // directory may have been created on another request since we last checked
@@ -2412,9 +2414,9 @@ function wfMerge( $old, $mine, $yours, &$result, &$mergeAttemptResult = null ) {
 
        # This check may also protect against code injection in
        # case of broken installations.
-       MediaWiki\suppressWarnings();
+       Wikimedia\suppressWarnings();
        $haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
-       MediaWiki\restoreWarnings();
+       Wikimedia\restoreWarnings();
 
        if ( !$haveDiff3 ) {
                wfDebug( "diff3 not found\n" );
@@ -2496,9 +2498,9 @@ function wfDiff( $before, $after, $params = '-u' ) {
        }
 
        global $wgDiff;
-       MediaWiki\suppressWarnings();
+       Wikimedia\suppressWarnings();
        $haveDiff = $wgDiff && file_exists( $wgDiff );
-       MediaWiki\restoreWarnings();
+       Wikimedia\restoreWarnings();
 
        # This check may also protect against code injection in
        # case of broken installations.
@@ -2686,30 +2688,6 @@ function wfRelativePath( $path, $from ) {
        return implode( DIRECTORY_SEPARATOR, $pieces );
 }
 
-/**
- * Convert an arbitrarily-long digit string from one numeric base
- * to another, optionally zero-padding to a minimum column width.
- *
- * Supports base 2 through 36; digit values 10-36 are represented
- * as lowercase letters a-z. Input is case-insensitive.
- *
- * @deprecated since 1.27 Use Wikimedia\base_convert() directly
- *
- * @param string $input Input number
- * @param int $sourceBase Base of the input number
- * @param int $destBase Desired base of the output
- * @param int $pad Minimum number of digits in the output (pad with zeroes)
- * @param bool $lowercase Whether to output in lowercase or uppercase
- * @param string $engine Either "gmp", "bcmath", or "php"
- * @return string|bool The output number as a string, or false on error
- */
-function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1,
-       $lowercase = true, $engine = 'auto'
-) {
-       wfDeprecated( __FUNCTION__, '1.27' );
-       return Wikimedia\base_convert( $input, $sourceBase, $destBase, $pad, $lowercase, $engine );
-}
-
 /**
  * Reset the session id
  *
@@ -2754,7 +2732,7 @@ function wfSetupSession( $sessionId = false ) {
        if ( session_id() !== $session->getId() ) {
                session_id( $session->getId() );
        }
-       MediaWiki\quietCall( 'session_start' );
+       Wikimedia\quietCall( 'session_start' );
 }
 
 /**
@@ -2920,7 +2898,7 @@ function wfGetLBFactory() {
  * Find a file.
  * Shortcut for RepoGroup::singleton()->findFile()
  *
- * @param string $title String or Title object
+ * @param string|Title $title String or Title object
  * @param array $options Associative array of options (see RepoGroup::findFile)
  * @return File|bool File, or false if the file does not exist
  */
@@ -3041,7 +3019,7 @@ function wfWaitForSlaves(
        $ifWritesSince = null, $wiki = false, $cluster = false, $timeout = null
 ) {
        if ( $timeout === null ) {
-               $timeout = ( PHP_SAPI === 'cli' ) ? 86400 : 10;
+               $timeout = wfIsCLI() ? 60 : 10;
        }
 
        if ( $cluster === '*' ) {
@@ -3052,7 +3030,8 @@ function wfWaitForSlaves(
        }
 
        try {
-               wfGetLBFactory()->waitForReplication( [
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $lbFactory->waitForReplication( [
                        'wiki' => $wiki,
                        'cluster' => $cluster,
                        'timeout' => $timeout,
@@ -3122,15 +3101,15 @@ function wfMemoryLimit() {
                $conflimit = wfShorthandToInteger( $wgMemoryLimit );
                if ( $conflimit == -1 ) {
                        wfDebug( "Removing PHP's memory limit\n" );
-                       MediaWiki\suppressWarnings();
+                       Wikimedia\suppressWarnings();
                        ini_set( 'memory_limit', $conflimit );
-                       MediaWiki\restoreWarnings();
+                       Wikimedia\restoreWarnings();
                        return $conflimit;
                } elseif ( $conflimit > $memlimit ) {
                        wfDebug( "Raising PHP's memory limit to $conflimit bytes\n" );
-                       MediaWiki\suppressWarnings();
+                       Wikimedia\suppressWarnings();
                        ini_set( 'memory_limit', $conflimit );
-                       MediaWiki\restoreWarnings();
+                       Wikimedia\restoreWarnings();
                        return $conflimit;
                }
        }
@@ -3283,9 +3262,9 @@ function wfUnpack( $format, $data, $length = false ) {
                }
        }
 
-       MediaWiki\suppressWarnings();
+       Wikimedia\suppressWarnings();
        $result = unpack( $format, $data );
-       MediaWiki\restoreWarnings();
+       Wikimedia\restoreWarnings();
 
        if ( $result === false ) {
                // If it cannot extract the packed data.
@@ -3527,19 +3506,3 @@ function wfGetRusage() {
                return getrusage( 0 /* RUSAGE_SELF */ );
        }
 }
-
-/**
- * Begin profiling of a function
- * @param string $functionname Name of the function we will profile
- * @deprecated since 1.25
- */
-function wfProfileIn( $functionname ) {
-}
-
-/**
- * Stop profiling of a function
- * @param string $functionname Name of the function we have profiled
- * @deprecated since 1.25
- */
-function wfProfileOut( $functionname = 'missing' ) {
-}