Merge "RequestContext: Load the request object for getRequest on first call"
[lhc/web/wiklou.git] / includes / context / RequestContext.php
index d1bc597..36c644a 100644 (file)
  * @file
  */
 
+use MediaWiki\Logger\LoggerFactory;
+
 /**
  * Group all the pieces relevant to the context of a request into one instance
  */
-class RequestContext implements IContextSource {
+class RequestContext implements IContextSource, MutableContext {
        /**
         * @var WebRequest
         */
@@ -62,10 +64,15 @@ class RequestContext implements IContextSource {
        private $skin;
 
        /**
-        * @var StatsdDataFactory
+        * @var \Liuggio\StatsdClient\Factory\StatsdDataFactory
         */
        private $stats;
 
+       /**
+        * @var Timing
+        */
+       private $timing;
+
        /**
         * @var Config
         */
@@ -116,8 +123,13 @@ class RequestContext implements IContextSource {
         */
        public function getRequest() {
                if ( $this->request === null ) {
-                       global $wgRequest; # fallback to $wg till we can improve this
-                       $this->request = $wgRequest;
+                       global $wgCommandLineMode;
+                       // create the WebRequest object on the fly
+                       if ( $wgCommandLineMode ) {
+                               $this->request = new FauxRequest( array() );
+                       } else {
+                               $this->request = new WebRequest();
+                       }
                }
 
                return $this->request;
@@ -130,15 +142,26 @@ class RequestContext implements IContextSource {
         */
        public function getStats() {
                if ( $this->stats === null ) {
-                       $config = $this->getConfig();
-                       $prefix = $config->has( 'StatsdMetricPrefix' )
-                               ? rtrim( $config->get( 'StatsdMetricPrefix' ), '.' )
-                               : 'MediaWiki';
+                       $prefix = rtrim( $this->getConfig()->get( 'StatsdMetricPrefix' ), '.' );
                        $this->stats = new BufferingStatsdDataFactory( $prefix );
                }
                return $this->stats;
        }
 
+       /**
+        * Get the timing object
+        *
+        * @return Timing
+        */
+       public function getTiming() {
+               if ( $this->timing === null ) {
+                       $this->timing = new Timing( array(
+                               'logger' => LoggerFactory::getInstance( 'Timing' )
+                       ) );
+               }
+               return $this->timing;
+       }
+
        /**
         * Set the Title object
         *
@@ -159,7 +182,10 @@ class RequestContext implements IContextSource {
                if ( $this->title === null ) {
                        global $wgTitle; # fallback to $wg till we can improve this
                        $this->title = $wgTitle;
-                       wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' . wfGetAllCallers( 5 ) . ' with no title set.' );
+                       wfDebugLog(
+                               'GlobalTitleFail',
+                               __METHOD__ . ' called by ' . wfGetAllCallers( 5 ) . ' with no title set.'
+                       );
                }
 
                return $this->title;
@@ -427,6 +453,7 @@ class RequestContext implements IContextSource {
         * Get a Message object with context set
         * Parameters are the same as wfMessage()
         *
+        * @param mixed ...
         * @return Message
         */
        public function msg() {
@@ -494,15 +521,17 @@ class RequestContext implements IContextSource {
        /**
         * Import an client IP address, HTTP headers, user ID, and session ID
         *
-        * This sets the current session and sets $wgUser and $wgRequest.
+        * This sets the current session, $wgUser, and $wgRequest from $params.
         * Once the return value falls out of scope, the old context is restored.
-        * This method should only be called in contexts (CLI or HTTP job runners)
-        * where there is no session ID or end user receiving the response. This
+        * This method should only be called in contexts where there is no session
+        * ID or end user receiving the response (CLI or HTTP job runners). This
         * is partly enforced, and is done so to avoid leaking cookies if certain
         * error conditions arise.
         *
-        * This will setup the session from the given ID. This is useful when
-        * background scripts inherit context when acting on behalf of a user.
+        * This is useful when background scripts inherit context when acting on
+        * behalf of a user. In general the 'sessionId' parameter should be set
+        * to an empty string unless session importing is *truly* needed. This
+        * feature is somewhat deprecated.
         *
         * @note suhosin.session.encrypt may interfere with this method.
         *