Index: trunk/extensions/EditPageTracking/EditPageTracking.php |
— | — | @@ -24,105 +24,107 @@ |
25 | 25 | $wgEditPageTrackingRegistrationCutoff = null; |
26 | 26 | |
27 | 27 | abstract class EditPageTracking { |
28 | | - |
| 28 | + |
29 | 29 | /** |
30 | 30 | * Applies EditPageTracking schema updates. |
| 31 | + * |
| 32 | + * @param $updater DatabaseUpdater |
31 | 33 | */ |
32 | 34 | public static function doSchemaUpdates( $updater = null ) { |
33 | 35 | $updater->addExtensionUpdate( array( 'addTable', 'edit_page_tracking', |
34 | 36 | dirname(__FILE__).'/edit_page_tracking.sql', true ) ); |
35 | | - |
| 37 | + |
36 | 38 | return true; |
37 | 39 | } |
38 | | - |
| 40 | + |
39 | 41 | /** |
40 | 42 | * Monitors edit page usage |
41 | 43 | */ |
42 | 44 | public static function onEditForm( EditPage $editPage ) { |
43 | 45 | global $wgUser, $wgEditPageTrackingRegistrationCutoff, $wgMemc; |
44 | | - |
| 46 | + |
45 | 47 | // Anonymous users |
46 | 48 | if ( $wgUser->isAnon() ) { |
47 | 49 | return true; |
48 | 50 | } |
49 | | - |
| 51 | + |
50 | 52 | if ( $wgEditPageTrackingRegistrationCutoff && |
51 | 53 | $wgUser->getRegistration() < $wgEditPageTrackingRegistrationCutoff ) |
52 | 54 | { |
53 | 55 | // User registered before the cutoff |
54 | 56 | return true; |
55 | 57 | } |
56 | | - |
| 58 | + |
57 | 59 | if ( EditPageTracking::getFirstEditPage( $wgUser ) ) { |
58 | 60 | // Already stored. |
59 | 61 | return true; |
60 | 62 | } |
61 | | - |
| 63 | + |
62 | 64 | // Record it |
63 | 65 | $dbw = wfGetDB( DB_MASTER ); |
64 | | - |
| 66 | + |
65 | 67 | $title = $editPage->getArticle()->getTitle(); |
66 | 68 | $timestamp = wfTimestampNow(); |
67 | | - |
| 69 | + |
68 | 70 | $row = array( |
69 | 71 | 'ept_user' => $wgUser->getId(), |
70 | 72 | 'ept_namespace' => $title->getNamespace(), |
71 | 73 | 'ept_title' => $title->getDBkey(), |
72 | 74 | 'ept_timestamp' => $dbw->timestamp( $timestamp ), |
73 | 75 | ); |
74 | | - |
| 76 | + |
75 | 77 | $dbw->insert( 'edit_page_tracking', $row, __METHOD__ ); |
76 | | - |
| 78 | + |
77 | 79 | $wgUser->mFirstEditPage = $timestamp; |
78 | | - |
| 80 | + |
79 | 81 | $cacheKey = wfMemcKey( 'first-edit-page', $wgUser->getId() ); |
80 | 82 | $wgMemc->set($cacheKey, $timestamp, 86400); |
81 | | - |
| 83 | + |
82 | 84 | return true; |
83 | 85 | } |
84 | | - |
85 | | - /** |
| 86 | + |
| 87 | + /** |
86 | 88 | * Gets the first time a user opened an edit page |
87 | | - * @param $user The User to check. |
| 89 | + * @param $user User The User to check. |
88 | 90 | * @return The timestamp of the first time the user opened an edit page. |
89 | 91 | * false for an anonymous user, null for a user who has never opened an edit page. |
90 | 92 | */ |
91 | 93 | public static function getFirstEditPage( $user ) { |
92 | 94 | global $wgMemc; |
93 | | - |
| 95 | + |
94 | 96 | if ( isset($user->mFirstEditPage) ) { |
95 | 97 | return $user->mFirstEditPage; |
96 | 98 | } |
97 | | - |
| 99 | + |
98 | 100 | if ( $user->isAnon() ) { |
99 | 101 | return false; |
100 | 102 | } |
101 | | - |
| 103 | + |
102 | 104 | $cacheKey = wfMemcKey( 'first-edit-page', $user->getId() ); |
103 | 105 | $cacheVal = $wgMemc->get($cacheKey); |
104 | | - |
| 106 | + |
105 | 107 | if ( $cacheVal !== false ) { |
106 | 108 | $user->mFirstEditPage = $cacheVal; |
107 | 109 | return $cacheVal; |
108 | 110 | } |
109 | | - |
| 111 | + |
110 | 112 | $dbr = wfGetDB( DB_SLAVE ); |
111 | | - |
| 113 | + |
112 | 114 | $res = $dbr->select( 'edit_page_tracking', 'ept_timestamp', |
113 | 115 | array( 'ept_user' => $user->getID() ), |
114 | 116 | __METHOD__, array( 'ORDER BY' => 'ept_timestamp asc' ) ); |
115 | | - |
| 117 | + |
116 | 118 | if ( $dbr->numRows($res) == 0 ) { |
117 | 119 | $user->mFirstEditPage = null; |
118 | 120 | $wgMemc->set( $cacheKey, null, 86400 ); |
119 | 121 | return null; |
120 | 122 | } |
121 | | - |
| 123 | + |
122 | 124 | $row = $dbr->fetchObject( $res ); |
123 | | - |
| 125 | + |
124 | 126 | $user->mFirstEditPage = wfTimestamp( TS_MW, $row->ept_timestamp ); |
125 | 127 | $wgMemc->set($cacheKey, $user->mFirstEditPage, 86400); |
126 | | - |
| 128 | + |
127 | 129 | return $user->mFirstEditPage; |
128 | 130 | } |
129 | 131 | |