| Index: trunk/extensions/CentralNotice/CentralNotice.php |
| — | — | @@ -94,6 +94,9 @@ |
| 95 | 95 | 'scripts' => 'bannerstats.js', |
| 96 | 96 | ); |
| 97 | 97 | |
| | 98 | +// Temporary setting to configure salt for Harvard banner protocol |
| | 99 | +$wgNoticeBanner_Harvard2011_salt = 'default'; |
| | 100 | + |
| 98 | 101 | /** |
| 99 | 102 | * UnitTestsList hook handler |
| 100 | 103 | */ |
| — | — | @@ -243,12 +246,90 @@ |
| 244 | 247 | * MakeGlobalVariablesScript hook handler |
| 245 | 248 | */ |
| 246 | 249 | function efCentralNoticeDefaults( &$vars ) { |
| 247 | | - global $wgNoticeProject; |
| | 250 | + // Using global $wgUser for compatibility with 1.18 |
| | 251 | + global $wgNoticeProject, $wgUser, $wgRequest; |
| | 252 | + |
| 248 | 253 | // Initialize global Javascript variables. We initialize Geo with empty values so if the geo |
| 249 | 254 | // IP lookup fails we don't have any surprises. |
| 250 | 255 | $geo = array( 'city' => '', 'country' => '' ); |
| 251 | 256 | $vars['Geo'] = $geo; // change this to wgGeo as soon as Mark updates on his end |
| 252 | 257 | $vars['wgNoticeProject'] = $wgNoticeProject; |
| | 258 | + |
| | 259 | + // XXX: Temporary WMF-specific code for the 2011 Harvard survey invitation banner. |
| | 260 | + // Only do this for logged-in users, keeping anonymous user output equal (for Squid-cache). |
| | 261 | + // Also, don't run if the UserDailyContribs-extension isn't installed. |
| | 262 | + if ( $wgUser->isLoggedIn() && function_exists( 'getUserEditCountSince' ) ) { |
| | 263 | + |
| | 264 | + $cacheKey = wfMemcKey( 'CentralNotice', 'Harvard2011', $wgUser->getId() ); |
| | 265 | + $data = $wgMemc->get( $cacheKey ); |
| | 266 | + |
| | 267 | + // Cached ? |
| | 268 | + if ( is_null( $data ) ) { |
| | 269 | + /** |
| | 270 | + * To be eligible, the user must match all of the following: |
| | 271 | + * - have an account |
| | 272 | + * - not be a bot (userright=bot) |
| | 273 | + * .. and match one of the following: |
| | 274 | + * - be an admin (group=sysop) |
| | 275 | + * - have an editcount higher than 300, of which 20 within the last 180 days (on the launch date) |
| | 276 | + * - have had their account registered for less than 30 days (on to the launch date) |
| | 277 | + */ |
| | 278 | + if ( $wgUser->isAllowed( 'bot' ) ) { |
| | 279 | + $data = false; |
| | 280 | + |
| | 281 | + } else { |
| | 282 | + global $wgNoticeBanner_Harvard2011_salt; |
| | 283 | + |
| | 284 | + $launchTimestamp = wfTimestamp( TS_UNIX, '2011-12-06 00:00:00' ); |
| | 285 | + $groups = $wgUser->getGroups(); |
| | 286 | + $registrationDate = !$wgUser->getRegistration() ? 0 : $wgUser->getRegistration(); |
| | 287 | + $daysOld = floor( ( $launchTimestamp - wfTimestamp( TS_UNIX, $registrationDate ) ) / ( 60*60*24 ) ); |
| | 288 | + $salt = $wgNoticeBanner_Harvard2011_salt; |
| | 289 | + $metrics = array( |
| | 290 | + // "username" the user's username |
| | 291 | + 'username' => $wgUser->getName(), |
| | 292 | + |
| | 293 | + // "group" is the group name(s) of the user (comma-separated). |
| | 294 | + 'group' => join( ',', $groups ), |
| | 295 | + |
| | 296 | + // "duration" is the number of days since the user registered his (on the launching date). |
| | 297 | + // Note: Will be negative if user registered after launch date! |
| | 298 | + 'duration' => $daysOld, |
| | 299 | + |
| | 300 | + // "editcounts" is the user's total number of edits |
| | 301 | + 'editcounts' => $wgUser->getEditCount() == NULL ? 0 : $wgUser->getEditCount(), |
| | 302 | + |
| | 303 | + // "last6monthseditcount" is the user's total number of edits in the last 180 days (on the launching date) |
| | 304 | + 'last6monthseditcount' => getUserEditCountSince( |
| | 305 | + $launchTimestamp - ( 180*24*3600 ), |
| | 306 | + $wgUser, |
| | 307 | + $launchTimestamp |
| | 308 | + ), |
| | 309 | + ); |
| | 310 | + $realData = array( |
| | 311 | + 'id' => $wgUser->getId(), |
| | 312 | + 'metrics' => $metrics, |
| | 313 | + 'hash' => md5( $salt . serialize( $metrics ) ), |
| | 314 | + ); |
| | 315 | + |
| | 316 | + if ( |
| | 317 | + in_array( 'sysop', $groups ) |
| | 318 | + || ( $metrics['editcounts'] >= 300 && $metrics['last6monthseditcount'] >= 20 ) |
| | 319 | + || ( $metrics['duration'] < 30 ) |
| | 320 | + ) { |
| | 321 | + $data = $realData; |
| | 322 | + } else { |
| | 323 | + $data = false; |
| | 324 | + } |
| | 325 | + } |
| | 326 | + |
| | 327 | + $wgMemc->set( $cacheKey, $data, strtotime( '+10 days' ) ); |
| | 328 | + } |
| | 329 | + |
| | 330 | + $vars['wgNoticeBanner_Harvard2011'] = $data; |
| | 331 | + |
| | 332 | + } |
| | 333 | + |
| 253 | 334 | return true; |
| 254 | 335 | } |
| 255 | 336 | |