API: Recognize an "Api-User-Agent" header
authorBrad Jorsch <bjorsch@wikimedia.org>
Tue, 18 Nov 2014 19:33:09 +0000 (14:33 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Tue, 18 Nov 2014 19:40:14 +0000 (14:40 -0500)
XMLHttpRequest does not allow overriding the "User-Agent" header. To
allow such clients to make effective use of the api-feature-usage log,
let's give them a different header they can use.

Change-Id: Ia1e4f52f35f8d9a8e9f5590a00854d14c11f7cc9

includes/api/ApiBase.php
includes/api/ApiMain.php

index 3f84f2a..99c0fb4 100644 (file)
@@ -2277,7 +2277,7 @@ abstract class ApiBase extends ContextSource {
                        ' "' . wfUrlencode( str_replace( ' ', '_', $this->getUser()->getName() ) ) . '"' .
                        ' "' . $request->getIP() . '"' .
                        ' "' . addslashes( $request->getHeader( 'Referer' ) ) . '"' .
-                       ' "' . addslashes( $request->getHeader( 'User-agent' ) ) . '"';
+                       ' "' . addslashes( $this->getMain()->getUserAgent() ) . '"';
                wfDebugLog( 'api-feature-usage', $s, 'private' );
        }
 
index 10a99c9..004bfae 100644 (file)
@@ -1242,6 +1242,21 @@ class ApiMain extends ApiBase {
                return $this->mModuleMgr;
        }
 
+       /**
+        * Fetches the user agent used for this request
+        *
+        * The value will be the combination of the 'Api-User-Agent' header (if
+        * any) and the standard User-Agent header (if any).
+        *
+        * @return string
+        */
+       public function getUserAgent() {
+               return trim(
+                       $this->getRequest()->getHeader( 'Api-user-agent' ) . ' ' .
+                       $this->getRequest()->getHeader( 'User-agent' )
+               );
+       }
+
        /************************************************************************//**
         * @name   Deprecated
         * @{