Spage has uploaded a new change for review. https://gerrit.wikimedia.org/r/67587
Change subject: Add campaign support ...................................................................... Add campaign support On any wiki page, ?campaign=someName in the query string triggers loading a JS module that a) logs to a Campaigns schema, and b) if valid also sets a mediaWiki.campaign session cookie. TODO: update EventLogging's ServerSideAccountCreation to log campaign Add a $wgEventLoggingSchemaRevs array of revisions, and only log Campaign (and existing PageContentSaveComplete and ServerSideAccountCreation events) if its revision is set. The revisions on Meta are in the extension's code rather than wmf-config files for ease of deployment See https://www.mediawiki.org/wiki/Extension:EventLogging/Campaigns and the schema https://meta.wikimedia.org/wiki/Schema:Campaigns Change-Id: Ife949daf51210d68a3328fc7b4bfef581afe80cd --- A Campaigns.hooks.php A Campaigns.i18n.php A Campaigns.php A modules/ext.campaigns.js 4 files changed, 185 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Campaigns refs/changes/87/67587/1 diff --git a/Campaigns.hooks.php b/Campaigns.hooks.php new file mode 100644 index 0000000..79ab4c6 --- /dev/null +++ b/Campaigns.hooks.php @@ -0,0 +1,39 @@ +<?php +/** + * Hooks for Campaigns extension. + * + * @file + * + * @ingroup Extensions + * @ingroup EventLogging + * + * @author S Page <[email protected]> + */ + +class CampaignsHooks { + + /** + * If there's a ?campaign=someName in the query string, then adds a module + * to the page to process it. + * + * @param OutputPage $out output page + * @param Skin $skin current skin + * @return bool + */ + public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) { + + $user = $out->getUser(); + // XXX (spage, 2013-05-17) This user pref is a poor signal for opt-out + // behavior, but it's all we have. + if ( !$user->isAnon() && $user->getOption( 'vector-noexperiments' ) ) { + return true; + } + + if ( ( $campaign = $out->getRequest()->getVal( 'campaign', '' ) ) !== '' ) { + $out->addModules( 'ext.campaigns' ); + $out->addJsConfigVars( 'wgCampaignsCampaign', $campaign ); + } + return true; + } + +} diff --git a/Campaigns.i18n.php b/Campaigns.i18n.php new file mode 100644 index 0000000..7356409 --- /dev/null +++ b/Campaigns.i18n.php @@ -0,0 +1,24 @@ +<?php +/** + * Internationalisation for Campaigns extension + * + * @file + * @ingroup Extensions + */ + +$messages = array(); + +/** English + * @author S Page + */ +$messages['en'] = array( + 'campaigns-desc' => 'Logs event and sets session cookie in response to ?campaign=someName', +); + +/** Message documentation (Message documentation) + * @author S Page + */ +$messages['qqq'] = array( + 'campaigns-desc' => '{{desc|name=Campaigns|url=https://www.mediawiki.org/wiki/Extension:Campaigns}}', +); + diff --git a/Campaigns.php b/Campaigns.php new file mode 100644 index 0000000..65806c2 --- /dev/null +++ b/Campaigns.php @@ -0,0 +1,56 @@ +<?php +/** + * Campaigns extension + * + * @file + * @ingroup Extensions + * + * @author S Page <[email protected]> + * + * @license GPL v2 or later + * @version 0.1.1 + */ + +$wgExtensionCredits['other'][] = array( + 'path' => __FILE__, + 'name' => 'Campaigns', + 'version' => '0.0.1', + 'url' => 'https://www.mediawiki.org/wiki/Extension:Campaigns', + 'author' => array( + 'S Page', + ), + 'descriptionmsg' => 'campaigns-desc', +); + + +// Classes + +$wgAutoloadClasses['CampaignsHooks'] = __DIR__ . '/Campaigns.hooks.php'; + + +// Messages + +$wgExtensionMessagesFiles[ 'Campaigns' ] = __DIR__ . '/Campaigns.i18n.php'; + + +// Modules + +$wgResourceModules += array( + 'schema.Campaigns' => array( + 'class' => 'ResourceLoaderSchemaModule', + 'schema' => 'Campaigns', + 'revision' => 5487321, + ), + 'ext.campaigns' => array( + 'scripts' => 'ext.campaigns.js', + 'localBasePath' => __DIR__ . '/modules', + 'remoteExtPath' => 'Campaigns/modules', + 'dependencies' => array( + 'mediawiki.user', + 'jquery.cookie', + 'schema.Campaigns', + ) + ), +); + +$wgHooks[ 'BeforePageDisplay' ][] = 'CampaignsHooks::onBeforePageDisplay'; diff --git a/modules/ext.campaigns.js b/modules/ext.campaigns.js new file mode 100644 index 0000000..f8a229a --- /dev/null +++ b/modules/ext.campaigns.js @@ -0,0 +1,66 @@ +/** + * If server detected a campaign, log an event and set a cookie. + * + * @module ext.campaigns.js + * @author S Page <[email protected]> + */ +( function ( mw, $ ) { + 'use strict'; + + var cfg, + campaign, + logEvt, + isValid; + + cfg = mw.config.get( [ + 'wgCampaignsCampaign', 'wgUserId', 'wgCookiePrefix', + 'wgNamespaceNumber', 'wgCanonicalSpecialPageName', 'wgPageName' + ] ); + + campaign = cfg.wgCampaignsCampaign; + if ( !campaign ) { + mw.eventLog.warn( 'No campaign, why did server load me?' ); + return; + } + + // Log the event. + + logEvt = { + campaign: campaign, + pageNs: cfg.wgNamespaceNumber, + // This is "Userlogin" even for Special:UserLogin?type=signup. + pageTitle: cfg.wgCanonicalSpecialPageName || cfg.wgPageName + }; + if ( mw.user.isAnon() ) { + logEvt.token = mw.user.sessionId(); + } else { + logEvt.userId = cfg.wgUserId; + } + mw.eventLog.logEvent( 'Campaigns', logEvt ); + + // Set the session cookie to the campaign, ONLY if the event is valid + // (i.e. the campaign value is enumerated in the current schema). + // This prevents spurious ?campaign=HAHAHA rickroll links from logging, and + // allows campaigns to expire despite external links with old campaigns in + // them. + // + // FIXME (spage, 2013-05-14) logEvent() above already performed validation, + // but there's no easy way to get the isValid status from it. + // We could test if ( mw.eventLog.isValid( logEvt, 'Campaigns' ), + // but instead just inspect EventLogging internals. + try { + isValid = ( $.inArray( campaign, + mw.eventLog.schemas.Campaigns.schema.properties.campaign['enum'] ) !== -1 ); + } catch ( e ) { + isValid = false; + } + if ( isValid ) { + $.cookie( cfg.wgCookiePrefix +'.campaign', + campaign, { 'expires': null, 'path': '/' } ); + } + + // XXX (spage, 2013-05-14) We could remove the query string parameter from + // the browser history state, so users don't unintentionally propagate + // campaigns by bookmarking or sharing the landing page. + +} ( mediaWiki, jQuery ) ); -- To view, visit https://gerrit.wikimedia.org/r/67587 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ife949daf51210d68a3328fc7b4bfef581afe80cd Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Campaigns Gerrit-Branch: master Gerrit-Owner: Spage <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
