Remove internal use of deprecated $wgRequestTime
authorKevin Israel <pleasestand@live.com>
Fri, 12 Aug 2016 10:33:37 +0000 (06:33 -0400)
committerKrinkle <krinklemail@gmail.com>
Sun, 18 Mar 2018 04:41:12 +0000 (04:41 +0000)
* Use $_SERVER['REQUEST_TIME_FLOAT'] unconditionally in WebRequest.php
  and libs/Timing.php. WebStart.php was doing this already without issue.
  The key existst since PHP 5.4, for both Web and CLI (we require 5.5).

* In wfDebug() and wfReportTime(), use $_SERVER['REQUEST_TIME_FLOAT'] instead.

* In ApiFormatBase and MWDebug, use WebRequest::getElapsedTime() instead.

* In Maintenance.php, remove setting of $wgRequestTime.

* In rebuildFileCache.php, update mocking to $_SERVER['REQUEST_TIME_FLOAT']
  so that we avoid re-introducing bug T24852.

Change-Id: I1b647da2862f815029caa533b592ec8a05b33806

includes/GlobalFunctions.php
includes/WebRequest.php
includes/api/ApiFormatBase.php
includes/debug/MWDebug.php
includes/libs/Timing.php
maintenance/Maintenance.php
maintenance/rebuildFileCache.php
tests/phpunit/includes/WebRequestTest.php

index 1d61996..7a5a5d8 100644 (file)
@@ -1050,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;
@@ -1061,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',
@@ -1514,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();
index 0a7f416..26e2d45 100644 (file)
@@ -88,8 +88,7 @@ class WebRequest {
         * @codeCoverageIgnore
         */
        public function __construct() {
-               $this->requestTime = isset( $_SERVER['REQUEST_TIME_FLOAT'] )
-                       ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime( true );
+               $this->requestTime = $_SERVER['REQUEST_TIME_FLOAT'];
 
                // POST overrides GET data
                // We don't use $_REQUEST here to avoid interference from cookies...
index 18c36de..4b93b31 100644 (file)
@@ -298,7 +298,7 @@ abstract class ApiFormatBase extends ApiBase {
 
                        if ( $this->getIsWrappedHtml() ) {
                                // This is a special output mode mainly intended for ApiSandbox use
-                               $time = microtime( true ) - $this->getConfig()->get( 'RequestTime' );
+                               $time = $this->getMain()->getRequest()->getElapsedTime();
                                $json = FormatJson::encode(
                                        [
                                                'status' => (int)( $this->mHttpStatus ?: 200 ),
index 012837f..7479841 100644 (file)
@@ -517,7 +517,7 @@ class MWDebug {
                        return [];
                }
 
-               global $wgVersion, $wgRequestTime;
+               global $wgVersion;
                $request = $context->getRequest();
 
                // HHVM's reported memory usage from memory_get_peak_usage()
@@ -540,7 +540,7 @@ class MWDebug {
                        'gitRevision' => GitInfo::headSHA1(),
                        'gitBranch' => $branch,
                        'gitViewUrl' => GitInfo::headViewUrl(),
-                       'time' => microtime( true ) - $wgRequestTime,
+                       'time' => $request->getElapsedTime(),
                        'log' => self::$log,
                        'debugLog' => self::$debug,
                        'queries' => self::$query,
index 57c253d..7b1a914 100644 (file)
@@ -94,9 +94,7 @@ class Timing implements LoggerAwareInterface {
                                'requestStart' => [
                                        'name'      => 'requestStart',
                                        'entryType' => 'mark',
-                                       'startTime' => isset( $_SERVER['REQUEST_TIME_FLOAT'] )
-                                               ? $_SERVER['REQUEST_TIME_FLOAT']
-                                               : $_SERVER['REQUEST_TIME'],
+                                       'startTime' => $_SERVER['REQUEST_TIME_FLOAT'],
                                        'duration'  => 0,
                                ],
                        ];
index 1778a79..0c79bd5 100644 (file)
@@ -678,7 +678,7 @@ abstract class Maintenance {
         * Do some sanity checking and basic setup
         */
        public function setup() {
-               global $IP, $wgCommandLineMode, $wgRequestTime;
+               global $IP, $wgCommandLineMode;
 
                # Abort if called from a web server
                # wfIsCLI() is not available yet
@@ -715,8 +715,6 @@ abstract class Maintenance {
                # But sometimes this doesn't seem to be the case.
                ini_set( 'max_execution_time', 0 );
 
-               $wgRequestTime = microtime( true );
-
                # Define us as being in MediaWiki
                define( 'MEDIAWIKI', true );
 
index ecdec29..1f89426 100644 (file)
@@ -57,8 +57,6 @@ class RebuildFileCache extends Maintenance {
        }
 
        public function execute() {
-               global $wgRequestTime;
-
                if ( !$this->enabled ) {
                        $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
                }
@@ -90,7 +88,8 @@ class RebuildFileCache extends Maintenance {
                        $this->fatalError( "Nothing to do." );
                }
 
-               $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
+               // Mock request (hack, no real client)
+               $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
 
                # Do remaining chunk
                $end += $batchSize - 1;
@@ -141,22 +140,27 @@ class RebuildFileCache extends Maintenance {
                                        }
 
                                        Wikimedia\suppressWarnings(); // header notices
-                                       // Cache ?action=view
-                                       $wgRequestTime = microtime( true ); # T24852
+
+                                       // 1. Cache ?action=view
+                                       // Be sure to reset the mocked request time (T24852)
+                                       $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
                                        ob_start();
                                        $article->view();
                                        $context->getOutput()->output();
                                        $context->getOutput()->clearHTML();
                                        $viewHtml = ob_get_clean();
                                        $viewCache->saveToFileCache( $viewHtml );
-                                       // Cache ?action=history
-                                       $wgRequestTime = microtime( true ); # T24852
+
+                                       // 2. Cache ?action=history
+                                       // Be sure to reset the mocked request time (T24852)
+                                       $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
                                        ob_start();
                                        Action::factory( 'history', $article, $context )->show();
                                        $context->getOutput()->output();
                                        $context->getOutput()->clearHTML();
                                        $historyHtml = ob_get_clean();
                                        $historyCache->saveToFileCache( $historyHtml );
+
                                        Wikimedia\restoreWarnings();
 
                                        if ( $rebuilt ) {
index 936f4f5..9583921 100644 (file)
@@ -26,7 +26,7 @@ class WebRequestTest extends MediaWikiTestCase {
        public function testDetectServer( $expected, $input, $description ) {
                $this->setMwGlobals( 'wgAssumeProxiesUseDefaultProtocolPorts', true );
 
-               $_SERVER = $input;
+               $this->setServerVars( $input );
                $result = WebRequest::detectServer();
                $this->assertEquals( $expected, $result, $description );
        }
@@ -363,7 +363,7 @@ class WebRequestTest extends MediaWikiTestCase {
         * @covers WebRequest::getIP
         */
        public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
-               $_SERVER = $input;
+               $this->setServerVars( $input );
                $this->setMwGlobals( [
                        'wgUsePrivateIPs' => $private,
                        'wgHooks' => [
@@ -608,8 +608,19 @@ class WebRequestTest extends MediaWikiTestCase {
         * @covers WebRequest::getAcceptLang
         */
        public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
-               $_SERVER = [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ];
+               $this->setServerVars( [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ] );
                $request = new WebRequest();
                $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
        }
+
+       protected function setServerVars( $vars ) {
+               // Don't remove vars which should be available in all SAPI.
+               if ( !isset( $vars['REQUEST_TIME_FLOAT'] ) ) {
+                       $vars['REQUEST_TIME_FLOAT'] = $_SERVER['REQUEST_TIME_FLOAT'];
+               }
+               if ( !isset( $vars['REQUEST_TIME'] ) ) {
+                       $vars['REQUEST_TIME'] = $_SERVER['REQUEST_TIME'];
+               }
+               $_SERVER = $vars;
+       }
 }