r105588 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105587‎ | r105588 | r105589 >
Date:20:57, 8 December 2011
Author:reedy
Status:ok
Tags:
Comment:
CentralNotice MFT r105239, 105241, r105267, r105450, r105452, r105453, r105586
Modified paths:
  • /branches/wmf/1.18wmf1/extensions/CentralNotice (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/CentralNotice/CentralNotice.db.php (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/CentralNotice/CentralNotice.php (modified) (history)

Diff [purge]

Index: branches/wmf/1.18wmf1/extensions/CentralNotice/CentralNotice.php
@@ -89,6 +89,17 @@
9090 'messages' => 'centralnotice-documentwrite-error'
9191 );
9292
 93+// Temporary setting to enable and configure info for Harvard banner on en.wikipedia.org
 94+$wgNoticeBanner_Harvard2011 = array(
 95+ 'enable' => false,
 96+ 'salt' => 'default',
 97+);
 98+
 99+/**
 100+ * UnitTestsList hook handler
 101+ * @param $files array
 102+ * @return bool
 103+ */
93104 function efCentralNoticeUnitTests( &$files ) {
94105 $files[] = dirname( __FILE__ ) . '/tests/CentralNoticeTest.php';
95106 return true;
@@ -146,6 +157,11 @@
147158 }
148159 }
149160
 161+/**
 162+ * LoadExtensionSchemaUpdates hook handler
 163+ * @param $updater DatabaseUpdater|null
 164+ * @return bool
 165+ */
150166 function efCentralNoticeSchema( $updater = null ) {
151167 $base = dirname( __FILE__ );
152168 if ( $updater === null ) {
@@ -200,26 +216,43 @@
201217 return true;
202218 }
203219
 220+/**
 221+ * BeforePageDisplay hook handler
 222+ * @param $out OutputPage
 223+ * @param $skin Skin
 224+ * @return bool
 225+ */
204226 function efCentralNoticeLoader( $out, $skin ) {
205 - global $wgOut;
206 -
207227 // Include '.js' to exempt script from squid cache expiration override
208228 $centralLoader = SpecialPage::getTitleFor( 'BannerController' )->getLocalUrl( 'cache=/cn.js' );
209229
210230 // Insert the banner controller Javascript into the page
211 - $wgOut->addScriptFile( $centralLoader );
 231+ $out->addScriptFile( $centralLoader );
212232
213233 return true;
214234 }
215235
 236+/**
 237+ * SkinAfterBottomScripts hook handler
 238+ * @param $skin Skin
 239+ * @param $text string
 240+ * @return bool
 241+ */
216242 function efCentralNoticeGeoLoader( $skin, &$text ) {
217243 // Insert the geo IP lookup
218244 $text .= Html::linkedScript( "//geoiplookup.wikimedia.org/" );
219245 return true;
220246 }
221247
 248+/**
 249+ * MakeGlobalVariablesScript hook handler
 250+ * @param $vars array
 251+ * @return bool
 252+ */
222253 function efCentralNoticeDefaults( &$vars ) {
223 - global $wgNoticeProject;
 254+ // Using global $wgUser for compatibility with 1.18
 255+ global $wgNoticeProject, $wgUser, $wgMemc, $wgNoticeBanner_Harvard2011, $wgContLang;
 256+
224257 // Initialize global Javascript variables. We initialize Geo with empty values so if the geo
225258 // IP lookup fails we don't have any surprises.
226259 $geo = (object)array();
@@ -227,9 +260,98 @@
228261 $geo->{'country'} = '';
229262 $vars['Geo'] = $geo; // change this to wgGeo as soon as Mark updates on his end
230263 $vars['wgNoticeProject'] = $wgNoticeProject;
 264+
 265+ // XXX: Temporary WMF-specific code for the 2011 Harvard survey invitation banner.
 266+ // Only do this for logged-in users, keeping anonymous user output equal (for Squid-cache).
 267+ // Also, don't run if the UserDailyContribs-extension isn't installed.
 268+ if ( $wgNoticeBanner_Harvard2011['enable'] && $wgUser->isLoggedIn() && function_exists( 'getUserEditCountSince' ) ) {
 269+
 270+ $cacheKey = wfMemcKey( 'CentralNotice', 'Harvard2011', 'v1', $wgUser->getId() );
 271+ $value = $wgMemc->get( $cacheKey );
 272+
 273+ // Cached ?
 274+ if ( !$value ) {
 275+ /**
 276+ * To be eligible, the user must match all of the following:
 277+ * - have an account
 278+ * - not be a bot (userright=bot)
 279+ * .. and match one of the following:
 280+ * - be an admin (group=sysop)
 281+ * - have an editcount higher than 300, of which 20 within the last 180 days (on the launch date)
 282+ * - have had their account registered for less than 30 days (on to the launch date)
 283+ */
 284+ if ( $wgUser->isAllowed( 'bot' ) ) {
 285+ $value = false;
 286+
 287+ } else {
 288+
 289+ $launchTimestamp = wfTimestamp( TS_UNIX, '2011-12-08 00:00:00' );
 290+ $groups = $wgUser->getGroups();
 291+ $registrationDate = $wgUser->getRegistration() ? $wgUser->getRegistration() : 0;
 292+ $daysOld = floor( ( $launchTimestamp - wfTimestamp( TS_UNIX, $registrationDate ) ) / ( 60*60*24 ) );
 293+ $salt = $wgNoticeBanner_Harvard2011['salt'];
 294+
 295+ // Variables
 296+ $hashData = array(
 297+ // "login"
 298+ 'login' => intval( $wgUser->getId() ),
 299+
 300+ // "group" is the group name(s) of the user (comma-separated).
 301+ 'group' => implode( ',', $groups ),
 302+
 303+ // "duration" is the number of days since the user registered his (on the launching date).
 304+ // Note: Will be negative if user registered after launch date!
 305+ 'duration' => intval( $daysOld ),
 306+
 307+ // "editcounts" is the user's total number of edits
 308+ 'editcounts' => $wgUser->getEditCount() == null ? 0 : intval( $wgUser->getEditCount() ),
 309+
 310+ // "last6monthseditcount" is the user's total number of edits in the last 180 days (on the launching date)
 311+ 'last6monthseditcount' => getUserEditCountSince(
 312+ $launchTimestamp - ( 180*24*3600 ),
 313+ $wgUser,
 314+ $launchTimestamp
 315+ ),
 316+ );
 317+
 318+ $postData = $hashData;
 319+
 320+ // "username" the user's username
 321+ $postData['username'] = $wgUser->getName();
 322+
 323+ // Security checksum. Prevent users from entering the survey with invalid metrics
 324+ $postData['secretkey'] = md5( $salt . serialize( $hashData ) );
 325+
 326+ // MD5 hash
 327+ $postData['lang'] = $wgContLang->getCode();
 328+ echo $salt . serialize( $hashData )."\n\n";
 329+
 330+ if (
 331+ in_array( 'sysop', $groups )
 332+ || ( $postData['duration'] >= 180 && $postData['editcounts'] >= 300 && $postData['last6monthseditcount'] >= 20 )
 333+ || ( $postData['duration'] < 30 )
 334+ ) {
 335+ $value = $postData;
 336+ } else {
 337+ $value = false;
 338+ }
 339+ }
 340+
 341+ $wgMemc->set( $cacheKey, $value, strtotime( '+10 days' ) );
 342+ }
 343+
 344+ $vars['wgNoticeBanner_Harvard2011'] = $value;
 345+
 346+ }
 347+
231348 return true;
232349 }
233350
 351+/**
 352+ * SiteNoticeAfter hook handler
 353+ * @param $notice string
 354+ * @return bool
 355+ */
234356 function efCentralNoticeDisplay( &$notice ) {
235357 // setup siteNotice div
236358 $notice =
Property changes on: branches/wmf/1.18wmf1/extensions/CentralNotice/CentralNotice.php
___________________________________________________________________
Modified: svn:mergeinfo
237359 Merged /trunk/extensions/CentralNotice/CentralNotice.php:r105239,105241,105267,105450,105452-105453,105586
Index: branches/wmf/1.18wmf1/extensions/CentralNotice/CentralNotice.db.php
@@ -14,6 +14,12 @@
1515 /**
1616 * Return campaigns in the system within given constraints
1717 * By default returns enabled campaigns, if $enabled set to false, returns both enabled and disabled campaigns
 18+ * @param $project string
 19+ * @param $language string
 20+ * @param $date string
 21+ * @param $enabled bool
 22+ * @param $preferred string
 23+ * @param $location string
1824 * @return an array of ids
1925 */
2026 static function getCampaigns( $project = false, $language = false, $date = false, $enabled = true, $preferred = false, $location = false ) {
@@ -289,7 +295,10 @@
290296 * Lookup function for active banners under a given language/project/location. This function is
291297 * called by SpecialBannerListLoader::getJsonList() in order to build the banner list JSON for
292298 * each project.
293 - * @return a 2D array of running banners with associated weights and settings
 299+ * @param $project string
 300+ * @param $language string
 301+ * @param $location string
 302+ * @return array a 2D array of running banners with associated weights and settings
294303 */
295304 static function getBannersByTarget( $project, $language, $location = null ) {
296305 global $wgCentralDBname;
@@ -371,6 +380,8 @@
372381
373382 /**
374383 * See if a given campaign exists in the database
 384+ * @param $campaignName string
 385+ * @return bool
375386 */
376387 public static function campaignExists( $campaignName ) {
377388 global $wgCentralDBname;
@@ -382,6 +393,8 @@
383394
384395 /**
385396 * See if a given banner exists in the database
 397+ * @param $bannerName string
 398+ * @return bool
386399 */
387400 public static function bannerExists( $bannerName ) {
388401 global $wgCentralDBname;
@@ -399,6 +412,7 @@
400413 /**
401414 * Return all of the available countries for geotargeting
402415 * (This should probably be moved to a core database table at some point.)
 416+ * @return array
403417 */
404418 static function getCountriesList() {
405419 return array(
Property changes on: branches/wmf/1.18wmf1/extensions/CentralNotice/CentralNotice.db.php
___________________________________________________________________
Modified: svn:mergeinfo
406420 Merged /trunk/extensions/CentralNotice/CentralNotice.db.php:r105586
Property changes on: branches/wmf/1.18wmf1/extensions/CentralNotice
___________________________________________________________________
Modified: svn:mergeinfo
407421 Merged /trunk/extensions/CentralNotice:r105239,105241,105267,105450,105452-105453,105586

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r105239[HarvardResearch][CentralNotice] Temporary wgNoticeBanner_Harvard2011 data....krinkle22:54, 5 December 2011
r105267[HarvardResearch] Swap ternary operator in registration fallback to zero...krinkle01:02, 6 December 2011
r105450[HarvardResearch] Update output according new protocol and banner-javascript...krinkle19:05, 7 December 2011
r105452[HarvardResearch] Another tiny update...krinkle19:17, 7 December 2011
r105453[HarvardResearch] Disable by default....krinkle19:23, 7 December 2011
r105586Fix a few minor issues with Harvard related code...reedy20:41, 8 December 2011

Status & tagging log