Ottomata has submitted this change and it was merged. Change subject: Initial commit ......................................................................
Initial commit This commit introduces a trivial MediaWiki extension for emitting an X-Analytics headers from a MediaWiki hook. See <https://wikitech.wikimedia.org/wiki/X-Analytics> Change-Id: I9137b255a105b6b6cf16764225252b0bc288b737 --- A XAnalytics.php A i18n/en.json A i18n/qqq.json 3 files changed, 67 insertions(+), 0 deletions(-) Approvals: Ottomata: Verified; Looks good to me, approved diff --git a/XAnalytics.php b/XAnalytics.php new file mode 100644 index 0000000..d149f40 --- /dev/null +++ b/XAnalytics.php @@ -0,0 +1,51 @@ +<?php +/** + * Emit structured analytics data via an X-Analytics HTTP header. + * + * @see https://wikitech.wikimedia.org/wiki/X-Analytics + * @author Ori Livneh <[email protected]> + * @license GPLv2 + * @version 0.1 + */ + +$wgExtensionCredits['other'][] = array( + 'path' => __FILE__, + 'name' => 'XAnalytics', + 'version' => '0.1', + 'url' => 'https://wikitech.wikimedia.org/wiki/X-Analytics', + 'author' => 'Ori Livneh', + 'descriptionmsg' => 'xanalytics-desc', +); + +// Messages + +$wgMessagesDirs['XAnalytics'] = __DIR__ . '/i18n'; + +// Hooks + +/** + * Set X-Analytics header before the output buffer is flushed. + * + * The PHP output buffer is flushed from multiple places in the MediaWiki + * codebase (and the codebase of MediaWiki extensions), making it difficult to + * ensure that the header is reliably injected into every response generated by + * MediaWiki. This should be fixed. The output buffer of normal page view + * responses is done in one place, however, so for that use-case, the code is + * reliable. + * + * X-Analytics items can be declared by adding items to the configuration + * variable $wgXAnalyticsHeaderItems. + * + * @see https://wikitech.wikimedia.org/wiki/X-Analytics + */ +$wgHooks['BeforePageDisplay'][] = function ( OutputPage &$out, Skin &$skin ) { + $response = $out->getRequest()->response(); + $currentHeader = $response->getHeader( 'X-Analytics' ); + parse_str( preg_replace( '/; */', '&', $currentHeader ), $headerItems ); + wfRunHooks( 'XAnalyticsSetHeader', array( $out, &$headerItems ) ); + + if ( count( $headerItems ) ) { + $headerValue = http_build_query( $headerItems, null, '; ' ); + $response->header( 'X-Analytics: ' . $headerValue, true ); + } +}; diff --git a/i18n/en.json b/i18n/en.json new file mode 100644 index 0000000..0ead301 --- /dev/null +++ b/i18n/en.json @@ -0,0 +1,8 @@ +{ + "@metadata": { + "authors": [ + "Ori Livneh" + ] + }, + "xanalytics-desc": "Emit analytics data via an X-Analytics header" +} diff --git a/i18n/qqq.json b/i18n/qqq.json new file mode 100644 index 0000000..429aa22 --- /dev/null +++ b/i18n/qqq.json @@ -0,0 +1,8 @@ +{ + "@metadata": { + "authors": [ + "Ori Livneh" + ] + }, + "xanalytics-desc": "{{desc|name=XAnalytics|url=https://wikitech.wikimedia.org/wiki/X-Analytics}}" +} -- To view, visit https://gerrit.wikimedia.org/r/157841 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I9137b255a105b6b6cf16764225252b0bc288b737 Gerrit-PatchSet: 9 Gerrit-Project: mediawiki/extensions/XAnalytics Gerrit-Branch: master Gerrit-Owner: Ori.livneh <[email protected]> Gerrit-Reviewer: Giuseppe Lavagetto <[email protected]> Gerrit-Reviewer: Legoktm <[email protected]> Gerrit-Reviewer: Mark Bergsma <[email protected]> Gerrit-Reviewer: Ori.livneh <[email protected]> Gerrit-Reviewer: Ottomata <[email protected]> Gerrit-Reviewer: Siebrand <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
