Ori.livneh has submitted this change and it was merged.

Change subject: Add campaign support
......................................................................


Add campaign support

?campaign=someName in the query string of the Create account form
sends JavaScript to client that logs a dbName-campaign session cookie.

Upon successful account creation, it logs this campaign
cookie as part of the ServerSideAccountCreation event.

See https://www.mediawiki.org/wiki/Extension:Campaigns

The corresponding commit that removes the 'AddNewAccount' code from
EventLogging.hooks.php is Ic6e8f24e6d758309d201dc95c16016e823943a5a

PS 1 implemented logging and cookie in client-side JavaScript.
PS 2 implemented logging and cookie in server-side PHP, including
     Matt Flaschen's reimplementation of mw.user.sessionId() token.
PS 4 only set session cookie in PHP.
PS 5 set cookie on client; ServerSideAccountCreation logging moved here
     from Extension:EventLogging.

Change-Id: Ife949daf51210d68a3328fc7b4bfef581afe80cd
---
A .gitignore
A Campaigns.i18n.php
A Campaigns.php
A modules/ext.campaigns.js
4 files changed, 179 insertions(+), 0 deletions(-)

Approvals:
  Ori.livneh: Verified; Looks good to me, approved



diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..98b092a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+.svn
+*~
+*.kate-swp
+.*.swp
diff --git a/Campaigns.i18n.php b/Campaigns.i18n.php
new file mode 100644
index 0000000..04a8062
--- /dev/null
+++ b/Campaigns.i18n.php
@@ -0,0 +1,22 @@
+<?php
+/**
+ * Internationalisation for Campaigns extension
+ *
+ * @ingroup Extensions
+ */
+
+$messages = array();
+
+/** English
+ * @author S Page
+ */
+$messages['en'] = array(
+       'campaigns-desc' => 'Sets a session cookie in response to certain 
URLs.',
+);
+
+/** 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..9c4d380
--- /dev/null
+++ b/Campaigns.php
@@ -0,0 +1,123 @@
+<?php
+/**
+ * Campaigns extension
+ *
+ * @ingroup Extensions
+ *
+ * @author S Page <[email protected]>
+ *
+ * @license GPL v2 or later
+ */
+
+$wgExtensionCredits['other'][] = array(
+       'path' => __FILE__,
+       'name' => 'Campaigns',
+       'version' => '0.1.0',
+       'url' => 'https://www.mediawiki.org/wiki/Extension:Campaigns',
+       'author' => 'S Page',
+       'descriptionmsg' => 'campaigns-desc',
+);
+
+
+// Messages
+
+$wgExtensionMessagesFiles['Campaigns'] = __DIR__ . '/Campaigns.i18n.php';
+
+
+// Modules
+
+$wgResourceModules['ext.campaigns'] = array(
+       'scripts'       => 'ext.campaigns.js',
+       'localBasePath' => __DIR__ . '/modules',
+       'remoteExtPath' => 'Campaigns/modules',
+       'dependencies'  => array(
+               'mediawiki.user',
+               'jquery.cookie',
+       )
+);
+
+
+// Hooks
+
+/**
+ * If there's a ?campaign=someName in the query string and the user is not
+ * logged in, send JavaScript with the page to process campaign.
+ *
+ * Don't set the cookie in PHP because we believe the Squid cache will not
+ * send the Set-Cookie header along with a cached version of the page.  The
+ * Squid cache fragments on query string, hence the right campaign value and
+ * JS module will be sent to the client for different ?campaign=foo parameters.
+ *
+ * @param &$template template instance for the form
+ * @return bool True
+ */
+$wgHooks['UserCreateForm'][] = function( &$template ) {
+       global $wgCookiePrefix;
+       $maxCampaignLen = 40;
+
+       $skin = $template->getSkin();
+
+       $request = $skin->getRequest();
+       $campaign = $request->getVal( 'campaign', '' );
+       if ( $campaign === '' || strlen( $campaign ) > $maxCampaignLen ) {
+               return true;
+       }
+
+       // The version of the page for a logged-in user will not be cached and
+       // served to anonymous users, so it's OK to check on server.
+       if ( !$skin->getUser()->isAnon() ) {
+               return true;
+       };
+
+       $out = $skin->getOutput();
+       $out->addJsConfigVars( 'wgCampaignsCampaign', $campaign );
+       $out->addModules( 'ext.campaigns' );
+
+       return true;
+};
+
+/**
+ * Log account creation.
+ * @see https://www.mediawiki.org/wiki/Manual:Hooks/AddNewAccount
+ *
+ * @param User $user The User object that was created.
+ * @param boolean $byEmail The form has a [By e-mail] button.
+ * @return bool True
+ */
+$wgHooks['AddNewAccount'][] = function( $user, $byEmail ) {
+       global $wgEventLoggingSchemaRevs, $wgRequest, $wgUser, $wgCookiePrefix;
+
+       $userId = $user->getId();
+       $creatorUserId = $wgUser->getId();
+
+       // MediaWiki allows existing users to create accounts on behalf
+       // of others. In such cases the ID of the newly-created user and
+       // the ID of the user making this web request are different.
+       $isSelfMade = ( $userId && $userId === $creatorUserId );
+
+       $displayMobile = class_exists( 'MobileContext' ) &&
+               MobileContext::singleton()->shouldDisplayMobileView();
+
+       $event = array(
+               'token' => $wgRequest->getCookie( 'mediaWiki.user.id', '', '' ),
+               'userId' => $userId,
+               'userName' => $user->getName(),
+               'isSelfMade' => $isSelfMade,
+               'campaign' =>  $wgRequest->getCookie( '-campaign', 
$wgCookiePrefix, '' ),
+               'userBuckets' => $wgRequest->getCookie( 'userbuckets', '', '' ),
+               'displayMobile' => $displayMobile,
+       );
+
+       $returnTo = $wgRequest->getVal( 'returnto' );
+       if ( $returnTo !== null ) {
+               $event[ 'returnTo' ] = $returnTo;
+       }
+
+       $returnToQuery = $wgRequest->getVal( 'returntoquery' );
+       if ( $returnToQuery !== null ) {
+               $event[ 'returnToQuery' ] = $returnToQuery;
+       }
+
+       efLogServerSideEvent( 'ServerSideAccountCreation', 5487345, $event );
+       return true;
+};
diff --git a/modules/ext.campaigns.js b/modules/ext.campaigns.js
new file mode 100644
index 0000000..379b391
--- /dev/null
+++ b/modules/ext.campaigns.js
@@ -0,0 +1,30 @@
+/**
+ * If server detected a campaign, set a cookie.
+ *
+ * @module ext.campaigns.js
+ * @author S Page <[email protected]>
+ */
+( function ( mw, $ ) {
+       'use strict';
+
+       var campaign = mw.config.get( 'wgCampaignsCampaign' );
+
+       if ( !campaign ) {
+               mw.log.warn( 'No campaign, why did server load me?' );
+               return;
+       }
+
+       if ( !mw.user.isAnon() ) {
+               mw.log.warn( 'User is logged-in, why did server load me?' );
+               return;
+       }
+
+       // Set a campaign session cookie.
+       $.cookie( mw.config.get( 'wgCookiePrefix' ) +'-campaign', campaign,
+               { 'expires': null, 'path': '/' } );
+
+       // TODO (spage, 2013-06-11) 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: merged
Gerrit-Change-Id: Ife949daf51210d68a3328fc7b4bfef581afe80cd
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Campaigns
Gerrit-Branch: master
Gerrit-Owner: Spage <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: Spage <[email protected]>
Gerrit-Reviewer: Swalling <[email protected]>

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

Reply via email to