Ori.livneh has uploaded a new change for review. https://gerrit.wikimedia.org/r/201375
Change subject: Make WebRequest objects time-aware ...................................................................... Make WebRequest objects time-aware * Deprecate $wgRequestTime in favor of $_SERVER['REQUEST_TIME_FLOAT'], which is more accurate. Because $_SERVER['REQUEST_TIME_FLOAT'] is only set for PHP 5.4+, set it to microtime( true ) in Setup.php for back-compatibility. * Modify the WebRequest constructor to take an optional request time parameter which defaults to microtime( true ) if unset. * Use that to provide WebRequest::getElapsedTime(), which gets the time since the request was initiated. * In wfLogProfilingData(), get the user and request objects from the context object rather than from global scope. Change-Id: I7e07e22eaf16b5141b80ad9f843285c542a127b7 --- M includes/GlobalFunctions.php M includes/Setup.php M includes/WebRequest.php M includes/WebStart.php 4 files changed, 56 insertions(+), 12 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/75/201375/1 diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index bc3a46b..70473ca 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -1237,10 +1237,10 @@ * @todo document */ function wfLogProfilingData() { - global $wgRequestTime, $wgDebugLogGroups, $wgDebugRawPage; - global $wgProfileLimit, $wgUser, $wgRequest; + global $wgDebugLogGroups, $wgDebugRawPage, $wgProfileLimit; $context = RequestContext::getMain(); + $request = $context->getRequest(); $config = $context->getConfig(); if ( $config->has( 'StatsdServer' ) ) { $statsdServer = explode( ':', $config->get( 'StatsdServer' ) ); @@ -1260,7 +1260,7 @@ // Get total page request time and only show pages that longer than // $wgProfileLimit time (default is 0) - $elapsed = microtime( true ) - $wgRequestTime; + $elapsed = $request->getElapsedTime(); if ( $elapsed <= $wgProfileLimit ) { return; } @@ -1296,16 +1296,13 @@ // Don't load $wgUser at this late stage just for statistics purposes // @todo FIXME: We can detect some anons even if it is not loaded. // See User::getId() - if ( $wgUser->isItemLoaded( 'id' ) && $wgUser->isAnon() ) { - $ctx['anon'] = true; - } else { - $ctx['anon'] = false; - } + $user = $context->getUser(); + $ctx['anon'] = $user->isItemLoaded( 'id' ) && $user->isAnon(); // Command line script uses a FauxRequest object which does not have // any knowledge about an URL and throw an exception instead. try { - $ctx['url'] = urldecode( $wgRequest->getRequestURL() ); + $ctx['url'] = urldecode( $request->getRequestURL() ); } catch ( Exception $ignored ) { // no-op } diff --git a/includes/Setup.php b/includes/Setup.php index e281768..6abe497 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -558,7 +558,7 @@ wfDebug( "\n\nStart command line script $self\n" ); } else { // Can't stub this one, it sets up $_GET and $_REQUEST in its constructor - $wgRequest = new WebRequest; + $wgRequest = new WebRequest( $_SERVER['REQUEST_TIME_FLOAT'] ); $debug = "\n\nStart request {$wgRequest->getMethod()} {$wgRequest->getRequestURL()}\n"; diff --git a/includes/WebRequest.php b/includes/WebRequest.php index df88b35..ba21f27 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -51,14 +51,33 @@ private $ip; /** + * The timestamp of the start of the request, with microsecond precision. + * @var float + */ + private $requestTime; + + /** * Cached URL protocol * @var string */ protected $protocol; - public function __construct() { + /** + * Constructor + * + * @param float|null $requestTime Time of request start, or null to default + * to current time. + * @since 1.25 Takes optional $requestTime parameter. + */ + public function __construct( $requestTime = null ) { if ( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) { throw new MWException( "MediaWiki does not function when magic quotes are enabled." ); + } + + if ( $requestTime !== null ) { + $this->requestTime = $requestTime; + } else { + $this->requestTime = microtime( true ); } // POST overrides GET data @@ -214,6 +233,17 @@ } else { return 'http'; } + } + + /** + * Get the number of seconds to have elapsed since request start, + * in fractional seconds, with microsecond resolution. + * + * @return float + * @since 1.25 + */ + public function getElapsedTime() { + return microtime( true ) - $this->requestTime; } /** @@ -1274,6 +1304,8 @@ public function __construct( $data = array(), $wasPosted = false, $session = null, $protocol = 'http' ) { + $this->requestTime = microtime( true ); + if ( is_array( $data ) ) { $this->data = $data; } else { @@ -1497,4 +1529,8 @@ public function getProtocol() { return $this->base->getProtocol(); } + + public function getElapsedTime() { + return $this->base->getElapsedTime(); + } } diff --git a/includes/WebStart.php b/includes/WebStart.php index da4bc87..e6a6744 100644 --- a/includes/WebStart.php +++ b/includes/WebStart.php @@ -39,7 +39,18 @@ # points and when $wgOut gets disabled or overridden. header( 'X-Content-Type-Options: nosniff' ); -$wgRequestTime = microtime( true ); +# Approximate $_SERVER['REQUEST_TIME_FLOAT'] for PHP<5.4 +if ( !isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ) { + $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true ); +} + +/** + * @var float Request start time as fractional seconds since epoch + * @deprecated since 1.25; use $_SERVER['REQUEST_TIME_FLOAT'] or + * WebRequest::getElapsedTime() instead. + */ +$wgRequestTime = $_SERVER['REQUEST_TIME_FLOAT']; + unset( $IP ); # Valid web server entry point, enable includes. -- To view, visit https://gerrit.wikimedia.org/r/201375 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I7e07e22eaf16b5141b80ad9f843285c542a127b7 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Ori.livneh <o...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits