From: Kevin Israel Date: Fri, 12 Aug 2016 10:33:37 +0000 (-0400) Subject: Remove internal use of deprecated $wgRequestTime X-Git-Tag: 1.31.0-rc.0~359^2 X-Git-Url: http://git.cyclocoop.org//%22javascript:ModifierStyle%28%27%22.%24id.%22%27%29/%22?a=commitdiff_plain;h=06ba5ca383e29262cbf248bd42859e09acc6563f;p=lhc%2Fweb%2Fwiklou.git Remove internal use of deprecated $wgRequestTime * 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 --- diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 1d61996f79..7a5a5d8489 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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(); diff --git a/includes/WebRequest.php b/includes/WebRequest.php index 0a7f416cff..26e2d455d8 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -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... diff --git a/includes/api/ApiFormatBase.php b/includes/api/ApiFormatBase.php index 18c36deb07..4b93b31991 100644 --- a/includes/api/ApiFormatBase.php +++ b/includes/api/ApiFormatBase.php @@ -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 ), diff --git a/includes/debug/MWDebug.php b/includes/debug/MWDebug.php index 012837fdcd..7479841487 100644 --- a/includes/debug/MWDebug.php +++ b/includes/debug/MWDebug.php @@ -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, diff --git a/includes/libs/Timing.php b/includes/libs/Timing.php index 57c253d50e..7b1a9140df 100644 --- a/includes/libs/Timing.php +++ b/includes/libs/Timing.php @@ -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, ], ]; diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 1778a79990..0c79bd51e5 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -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 ); diff --git a/maintenance/rebuildFileCache.php b/maintenance/rebuildFileCache.php index ecdec29a95..1f89426e7a 100644 --- a/maintenance/rebuildFileCache.php +++ b/maintenance/rebuildFileCache.php @@ -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 ) { diff --git a/tests/phpunit/includes/WebRequestTest.php b/tests/phpunit/includes/WebRequestTest.php index 936f4f5bd6..9583921d51 100644 --- a/tests/phpunit/includes/WebRequestTest.php +++ b/tests/phpunit/includes/WebRequestTest.php @@ -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; + } }