Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/202800

Change subject: Add X-Analytics header for API requests too
......................................................................

Add X-Analytics header for API requests too

Add a hook handler for APIAfterExecute, which runs after an API request.

"*fm" API formats use OutputPage, so they were already calling the
BeforePageDisplay hook. To avoid duplicating information,
XAnalytics::generateHeader() can only be called once per request.

Bug: T92875
Change-Id: I15a7fd78f13870f86d907f72ed7bdf2372e42aa1
---
M XAnalytics.class.php
M XAnalytics.php
2 files changed, 15 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/XAnalytics 
refs/changes/00/202800/1

diff --git a/XAnalytics.class.php b/XAnalytics.class.php
index 46df7cb..f59bad3 100644
--- a/XAnalytics.class.php
+++ b/XAnalytics.class.php
@@ -16,6 +16,16 @@
         * @see https://wikitech.wikimedia.org/wiki/X-Analytics
         */
        public static function onBeforePageDisplay( OutputPage &$out, Skin 
&$skin ) {
+               self::generateHeader( $out );
+       }
+
+       private static function generateHeader( OutputPage $out ) {
+               static $called = null;
+               if ( $called === true ) {
+                       // Only run once for API requests that use OutputPage
+                       return;
+               }
+               $called = true;
                $response = $out->getRequest()->response();
                $currentHeader = $response->getHeader( 'X-Analytics' );
                parse_str( preg_replace( '/; */', '&', $currentHeader ), 
$headerItems );
@@ -26,4 +36,8 @@
                        $response->header( 'X-Analytics: ' . $headerValue, true 
);
                }
        }
+
+       public static function onAPIAfterExecute( ApiBase &$module ) {
+               self::generateHeader( $module->getOutput() );
+       }
 }
diff --git a/XAnalytics.php b/XAnalytics.php
index 73c769d..9389c63 100644
--- a/XAnalytics.php
+++ b/XAnalytics.php
@@ -28,3 +28,4 @@
 // Hooks
 
 $wgHooks['BeforePageDisplay'][] = 'XAnalytics::onBeforePageDisplay';
+$wgHooks['APIAfterExecute'][] = 'XAnalytics::onAPIAfterExecute';

-- 
To view, visit https://gerrit.wikimedia.org/r/202800
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I15a7fd78f13870f86d907f72ed7bdf2372e42aa1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/XAnalytics
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to