r92674 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92673‎ | r92674 | r92675 >
Date:20:15, 20 July 2011
Author:werdna
Status:ok
Tags:
Comment:
Commit EditPageTracking extension
Modified paths:
  • /trunk/extensions/EditPageTracking (added) (history)
  • /trunk/extensions/EditPageTracking/EditPageTracking.php (added) (history)
  • /trunk/extensions/EditPageTracking/edit_page_tracking.sql (added) (history)

Diff [purge]

Index: trunk/extensions/EditPageTracking/edit_page_tracking.sql
@@ -0,0 +1,10 @@
 2+-- Tables for EditPageTracking extension
 3+
 4+CREATE TABLE /*_*/edit_page_tracking (
 5+ ept_user bigint unsigned not null primary key,
 6+ ept_timestamp varbinary(14) not null,
 7+ ept_namespace int not null,
 8+ ept_title varbinary(255) not null
 9+) /*$wgDBTableOptions*/;
 10+
 11+CREATE INDEX /*i*/user_timestamp ON /*_*/edit_page_tracking (ept_user, ept_timestamp);
Index: trunk/extensions/EditPageTracking/EditPageTracking.php
@@ -0,0 +1,134 @@
 2+<?php
 3+/**
 4+ * EditPageTracking extension
 5+ * Allows tracking when users first click on "edit page"
 6+ */
 7+
 8+$wgExtensionCredits['other'][] = array(
 9+ 'author' => array( 'Andrew Garrett' ),
 10+ 'descriptionmsg' => 'editpagetracking-desc',
 11+ 'name' => 'EditPageTracking',
 12+ 'url' => 'http://www.mediawiki.org/wiki/Extension:EditPageTracking',
 13+ 'version' => '0.1',
 14+ 'path' => __FILE__,
 15+);
 16+
 17+$wgExtensionMessagesFiles['EditPageTracking'] = dirname(__FILE__).'/EditPageTracking.i18n.php';
 18+
 19+$wgHooks['LoadExtensionSchemaUpdates'][] = 'wfEditPageTrackingSchema';
 20+$wgHooks['EditPage::showEditForm:initial'][] = 'wfEditPageTrackingEditForm';
 21+$wgHooks['UserLoadDefaults'][] = 'wfEditPageTrackingResetUser';
 22+
 23+/** Configuration **/
 24+
 25+/** The registration cutoff for recording this data **/
 26+$wgEditPageTrackingRegistrationCutoff = null;
 27+
 28+/**
 29+ * Applies EditPageTracking schema updates.
 30+ */
 31+function wfEditPageTrackingSchema( $updater = null ) {
 32+ $updater->addExtensionUpdate( array( 'addTable', 'edit_page_tracking',
 33+ dirname(__FILE__).'/edit_page_tracking.sql', true ) );
 34+
 35+ return true;
 36+}
 37+
 38+/**
 39+ * Monitors edit page usage
 40+ */
 41+function wfEditPageTrackingEditForm( $editPage ) {
 42+ global $wgUser;
 43+
 44+ // Anonymous users
 45+ if ( $wgUser->isAnon() ) {
 46+ return true;
 47+ }
 48+
 49+ if ( wfGetFirstEditPageUsage( $wgUser ) ) {
 50+ // Already stored.
 51+ return true;
 52+ }
 53+ global $wgEditPageTrackingRegistrationCutoff;
 54+
 55+ if ( $wgEditPageTrackingRegistrationCutoff &&
 56+ $wgUser->getRegistration() < $wgEditPageTrackingRegistrationCutoff )
 57+ {
 58+ // User registered before the cutoff
 59+ return true;
 60+ }
 61+
 62+ // Record it
 63+ $dbw = wfGetDB( DB_MASTER );
 64+
 65+ $title = $editPage->getArticle()->getTitle();
 66+
 67+ $row = array(
 68+ 'ept_user' => $wgUser->getId(),
 69+ 'ept_namespace' => $title->getNamespace(),
 70+ 'ept_title' => $title->getDBkey(),
 71+ 'ept_timestamp' => $dbw->timestamp( wfTimestampNow() ),
 72+ );
 73+
 74+ $dbw->insert( 'edit_page_tracking', $row, __METHOD__ );
 75+
 76+ $wgUser->mFirstEditPage = wfTimestampNow();
 77+ $wgUser->saveToCache();
 78+
 79+ return true;
 80+}
 81+
 82+/**
 83+ * Gets the first time a user opened an edit page
 84+ * @param $user The User to check.
 85+ * @return The timestamp of the first time the user opened an edit page.
 86+ * false for an anonymous user, null for a user who has never opened an edit page.
 87+ */
 88+function wfGetFirstEditPageUsage( $user ) {
 89+ if ( isset($user->mFirstEditPage) ) {
 90+ return $user->mFirstEditPage;
 91+ }
 92+
 93+ if ( $user->isAnon() ) {
 94+ return false;
 95+ }
 96+
 97+ global $wgMemc;
 98+ $cacheKey = wfMemcKey( 'first-edit-page', $user->getId() );
 99+ $cacheVal = $wgMemc->get($cacheKey);
 100+
 101+ if ( $cacheVal !== false ) {
 102+ $user->mFirstEditPage = $cacheVal;
 103+ return $cacheVal;
 104+ }
 105+
 106+ $dbr = wfGetDB( DB_SLAVE );
 107+
 108+ $res = $dbr->select( 'edit_page_tracking', 'ept_timestamp',
 109+ array( 'ept_user' => $user->getID() ),
 110+ __METHOD__, array( 'order by' => 'ept_timestamp desc' ) );
 111+
 112+ if ( $dbr->numRows($res) == 0 ) {
 113+ $user->mFirstEditPage = null;
 114+ $wgMemc->set( $cacheKey, null, 86400 );
 115+ return null;
 116+ }
 117+
 118+ $row = reset($res);
 119+
 120+ $user->mFirstEditPage = wfTimestamp( TS_MW, $row->ept_timestamp );
 121+ $wgMemc->set($cacheKey, $user->mFirstEditPage, 86400);
 122+
 123+ return $user->mFirstEditPage;
 124+}
 125+
 126+/**
 127+ * Handler for UserLoadDefaults
 128+ * Sets mFirstEditPage to false
 129+ * @param $user The User object
 130+ * @param $name The User's name
 131+ */
 132+function wfEditPageTrackingResetUser( $user, $name ) {
 133+ $user->mFirstEditPage = false;
 134+ return true;
 135+}

Status & tagging log