r92677 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92676‎ | r92677 | r92678 >
Date:20:36, 20 July 2011
Author:werdna
Status:ok
Tags:
Comment:
Fixes:
* Stick everything in an abstract class to avoid namespace pollution.
* Remove primary key, make explicit the fact that the table might be used for non-first edit form views in the future.
* Improve comments
* Fix a bug in fetching.
Modified paths:
  • /trunk/extensions/EditPageTracking/EditPageTracking.php (modified) (history)
  • /trunk/extensions/EditPageTracking/edit_page_tracking.sql (modified) (history)

Diff [purge]

Index: trunk/extensions/EditPageTracking/edit_page_tracking.sql
@@ -3,9 +3,9 @@
44 CREATE TABLE /*_*/edit_page_tracking (
55 -- User ID
66 ept_user bigint unsigned not null,
7 - -- Timestamp when the edit form was first viewed
 7+ -- Timestamp when the edit form was viewed
88 ept_timestamp varbinary(14) not null,
9 - -- Page that the edit form was first viewed for
 9+ -- Page that the edit form was viewed for
1010 -- Not used at the moment, but useful for statistics
1111 ept_namespace int not null,
1212 ept_title varbinary(255) not null
Index: trunk/extensions/EditPageTracking/EditPageTracking.php
@@ -15,120 +15,113 @@
1616
1717 $wgExtensionMessagesFiles['EditPageTracking'] = dirname(__FILE__).'/EditPageTracking.i18n.php';
1818
19 -$wgHooks['LoadExtensionSchemaUpdates'][] = 'wfEditPageTrackingSchema';
20 -$wgHooks['EditPage::showEditForm:initial'][] = 'wfEditPageTrackingEditForm';
21 -$wgHooks['UserLoadDefaults'][] = 'wfEditPageTrackingResetUser';
 19+$wgHooks['LoadExtensionSchemaUpdates'][] = 'EditPageTracking::doSchemaUpdates';
 20+$wgHooks['EditPage::showEditForm:initial'][] = 'EditPageTracking::onEditForm';
2221
2322 /** Configuration **/
2423
2524 /** The registration cutoff for recording this data **/
2625 $wgEditPageTrackingRegistrationCutoff = null;
2726
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;
 27+abstract class EditPageTracking {
4328
44 - // Anonymous users
45 - if ( $wgUser->isAnon() ) {
 29+ /**
 30+ * Applies EditPageTracking schema updates.
 31+ */
 32+ public static function doSchemaUpdates( $updater = null ) {
 33+ $updater->addExtensionUpdate( array( 'addTable', 'edit_page_tracking',
 34+ dirname(__FILE__).'/edit_page_tracking.sql', true ) );
 35+
4636 return true;
4737 }
4838
49 - if ( wfGetFirstEditPageUsage( $wgUser ) ) {
50 - // Already stored.
 39+ /**
 40+ * Monitors edit page usage
 41+ */
 42+ public static function onEditForm( EditPage $editPage ) {
 43+ global $wgUser;
 44+
 45+ // Anonymous users
 46+ if ( $wgUser->isAnon() ) {
 47+ return true;
 48+ }
 49+
 50+ if ( EditPageTracking::getFirstEditPage( $wgUser ) ) {
 51+ // Already stored.
 52+ return true;
 53+ }
 54+ global $wgEditPageTrackingRegistrationCutoff;
 55+
 56+ if ( $wgEditPageTrackingRegistrationCutoff &&
 57+ $wgUser->getRegistration() < $wgEditPageTrackingRegistrationCutoff )
 58+ {
 59+ // User registered before the cutoff
 60+ return true;
 61+ }
 62+
 63+ // Record it
 64+ $dbw = wfGetDB( DB_MASTER );
 65+
 66+ $title = $editPage->getArticle()->getTitle();
 67+ $timestamp = wfTimestampNow();
 68+
 69+ $row = array(
 70+ 'ept_user' => $wgUser->getId(),
 71+ 'ept_namespace' => $title->getNamespace(),
 72+ 'ept_title' => $title->getDBkey(),
 73+ 'ept_timestamp' => $dbw->timestamp( $timestamp ),
 74+ );
 75+
 76+ $dbw->insert( 'edit_page_tracking', $row, __METHOD__ );
 77+
 78+ $wgUser->mFirstEditPage = $timestamp;
 79+ $wgUser->saveToCache();
 80+
5181 return true;
5282 }
53 - global $wgEditPageTrackingRegistrationCutoff;
5483
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) ) {
 84+ /**
 85+ * Gets the first time a user opened an edit page
 86+ * @param $user The User to check.
 87+ * @return The timestamp of the first time the user opened an edit page.
 88+ * false for an anonymous user, null for a user who has never opened an edit page.
 89+ */
 90+ public static function getFirstEditPage( $user ) {
 91+ if ( isset($user->mFirstEditPage) ) {
 92+ return $user->mFirstEditPage;
 93+ }
 94+
 95+ if ( $user->isAnon() ) {
 96+ return false;
 97+ }
 98+
 99+ global $wgMemc;
 100+ $cacheKey = wfMemcKey( 'first-edit-page', $user->getId() );
 101+ $cacheVal = $wgMemc->get($cacheKey);
 102+
 103+ if ( $cacheVal !== false ) {
 104+ $user->mFirstEditPage = $cacheVal;
 105+ return $cacheVal;
 106+ }
 107+
 108+ $dbr = wfGetDB( DB_SLAVE );
 109+
 110+ $res = $dbr->select( 'edit_page_tracking', 'ept_timestamp',
 111+ array( 'ept_user' => $user->getID() ),
 112+ __METHOD__, array( 'ORDER BY' => 'ept_timestamp asc' ) );
 113+
 114+ if ( $dbr->numRows($res) == 0 ) {
 115+ $user->mFirstEditPage = null;
 116+ $wgMemc->set( $cacheKey, null, 86400 );
 117+ return null;
 118+ }
 119+
 120+ $row = $dbr->fetchObject( $res );
 121+
 122+ $user->mFirstEditPage = wfTimestamp( TS_MW, $row->ept_timestamp );
 123+ $wgMemc->set($cacheKey, $user->mFirstEditPage, 86400);
 124+
90125 return $user->mFirstEditPage;
91126 }
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 -}
125127
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;
135128 }

Status & tagging log