Legoktm has uploaded a new change for review.

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

Change subject: Allow extensions to add items at runtime with 
XAnalytics::addItem()
......................................................................

Allow extensions to add items at runtime with XAnalytics::addItem()

Change-Id: Ib479ae59a172da9619ce15fa1f21207f50283a5b
---
M XAnalytics.class.php
1 file changed, 32 insertions(+), 4 deletions(-)


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

diff --git a/XAnalytics.class.php b/XAnalytics.class.php
index f59bad3..9d58875 100644
--- a/XAnalytics.class.php
+++ b/XAnalytics.class.php
@@ -1,6 +1,19 @@
 <?php
 
 class XAnalytics {
+
+       /**
+        * Items to add to the header
+        *
+        * @var array
+        */
+       private static $items = array();
+       /**
+        * Whether the header has already been added
+        *
+        * @var bool
+        */
+       private static $addedHeader = false;
        /**
         * Set X-Analytics header before the output buffer is flushed.
         *
@@ -11,7 +24,8 @@
         * responses is done in one place, however, so for that use-case, the 
code is
         * reliable.
         *
-        * X-Analytics items can be declared by hooking into 
'XAnalyticsSetHeader'.
+        * X-Analytics items can be declared by hooking into 
'XAnalyticsSetHeader' or
+        * by calling XAnalytics;:addItem().
         *
         * @see https://wikitech.wikimedia.org/wiki/X-Analytics
         */
@@ -20,15 +34,15 @@
        }
 
        private static function generateHeader( OutputPage $out ) {
-               static $called = null;
-               if ( $called === true ) {
+               if ( self::$addedHeader === true ) {
                        // Only run once for API requests that use OutputPage
                        return;
                }
-               $called = true;
+               self::$addedHeader = true;
                $response = $out->getRequest()->response();
                $currentHeader = $response->getHeader( 'X-Analytics' );
                parse_str( preg_replace( '/; */', '&', $currentHeader ), 
$headerItems );
+               $headerItems = array_merge( $headerItems, self::$items );
                Hooks::run( 'XAnalyticsSetHeader', array( $out, &$headerItems ) 
);
 
                if ( count( $headerItems ) ) {
@@ -40,4 +54,18 @@
        public static function onAPIAfterExecute( ApiBase &$module ) {
                self::generateHeader( $module->getOutput() );
        }
+
+       /**
+        * Add an item to the X-Analytics header that will be output
+        * @param string $name
+        * @param string $value
+        */
+       public static function addItem( $name, $value ) {
+               if ( self::$addedHeader ) {
+                       throw new RuntimeException(
+                               "The X-Analytics header has already been added, 
it is too late to add new items"
+                       );
+               }
+               self::$items[$name] = $value;
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib479ae59a172da9619ce15fa1f21207f50283a5b
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