Index: trunk/extensions/SocialProfile/UserStats/SpecialUpdateEditCounts.php |
— | — | @@ -1,24 +1,27 @@ |
2 | 2 | <?php |
| 3 | +/** |
| 4 | + * A special page for updating users' point counts. |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + */ |
3 | 9 | |
4 | 10 | class UpdateEditCounts extends UnlistedSpecialPage { |
5 | 11 | |
6 | 12 | /** |
7 | | - * Constructor |
| 13 | + * Constructor -- set up the new special page |
8 | 14 | */ |
9 | 15 | public function __construct() { |
10 | | - parent::__construct( 'UpdateEditCounts' ); |
| 16 | + parent::__construct( 'UpdateEditCounts', 'updatepoints' ); |
11 | 17 | } |
12 | 18 | |
| 19 | + /** |
| 20 | + * Perform the queries necessary to update the social point counts and |
| 21 | + * purge memcached entries. |
| 22 | + */ |
13 | 23 | function updateMainEditsCount() { |
14 | | - global $wgOut, $wgUser, $wgNamespacesForEditPoints; |
| 24 | + global $wgOut, $wgNamespacesForEditPoints; |
15 | 25 | |
16 | | - $wgOut->setPageTitle( 'Update Edit Counts' ); |
17 | | - |
18 | | - if ( !$wgUser->isAllowed( 'updatepoints' ) ) { |
19 | | - $wgOut->errorpage( 'error', 'badaccess' ); |
20 | | - return false; |
21 | | - } |
22 | | - |
23 | 26 | $whereConds = array(); |
24 | 27 | $whereConds[] = 'rev_user <> 0'; |
25 | 28 | // If points are given out for editing non-main namespaces, take that |
— | — | @@ -47,9 +50,9 @@ |
48 | 51 | $user->loadFromId(); |
49 | 52 | |
50 | 53 | if ( !$user->isAllowed( 'bot' ) ) { |
51 | | - $edit_count = $row->the_count; |
| 54 | + $editCount = $row->the_count; |
52 | 55 | } else { |
53 | | - $edit_count = 0; |
| 56 | + $editCount = 0; |
54 | 57 | } |
55 | 58 | |
56 | 59 | $s = $dbw->selectRow( |
— | — | @@ -58,7 +61,7 @@ |
59 | 62 | array( 'stats_user_id' => $row->rev_user ), |
60 | 63 | __METHOD__ |
61 | 64 | ); |
62 | | - if ( !$s->stats_user_id ) { |
| 65 | + if ( !$s->stats_user_id || $s === false ) { |
63 | 66 | $dbw->insert( |
64 | 67 | 'user_stats', |
65 | 68 | array( |
— | — | @@ -70,11 +73,11 @@ |
71 | 74 | __METHOD__ |
72 | 75 | ); |
73 | 76 | } |
74 | | - $wgOut->addHTML( "<p>Updating {$row->rev_user_text} with {$edit_count} edits</p>" ); |
| 77 | + $wgOut->addHTML( "<p>Updating {$row->rev_user_text} with {$editCount} edits</p>" ); |
75 | 78 | |
76 | 79 | $dbw->update( |
77 | 80 | 'user_stats', |
78 | | - array( 'stats_edit_count = ' . $edit_count ), |
| 81 | + array( 'stats_edit_count = ' . $editCount ), |
79 | 82 | array( 'stats_user_id' => $row->rev_user ), |
80 | 83 | __METHOD__ |
81 | 84 | ); |
— | — | @@ -92,14 +95,31 @@ |
93 | 96 | * @param $par Mixed: parameter passed to the page or null |
94 | 97 | */ |
95 | 98 | public function execute( $par ) { |
96 | | - global $wgUser, $wgOut; |
97 | | - $dbr = wfGetDB( DB_MASTER ); |
| 99 | + global $wgOut, $wgUser; |
| 100 | + |
| 101 | + $wgOut->setPageTitle( 'Update Edit Counts' ); |
| 102 | + |
| 103 | + // Check permissions -- we must be allowed to access this special page |
| 104 | + // before we can run any database queries |
| 105 | + if ( !$wgUser->isAllowed( 'updatepoints' ) ) { |
| 106 | + $wgOut->errorpage( 'error', 'badaccess' ); |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + // And obviously the database needs to be writable before we start |
| 111 | + // running INSERT/UPDATE queries against it... |
| 112 | + if( wfReadOnly() ) { |
| 113 | + $wgOut->readOnlyPage(); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + $dbw = wfGetDB( DB_MASTER ); |
98 | 118 | $this->updateMainEditsCount(); |
99 | 119 | |
100 | 120 | global $wgUserLevels; |
101 | 121 | $wgUserLevels = ''; |
102 | 122 | |
103 | | - $res = $dbr->select( |
| 123 | + $res = $dbw->select( |
104 | 124 | 'user_stats', |
105 | 125 | array( 'stats_user_id', 'stats_user_name', 'stats_total_points' ), |
106 | 126 | array(), |
— | — | @@ -107,9 +127,13 @@ |
108 | 128 | array( 'ORDER BY' => 'stats_user_name' ) |
109 | 129 | ); |
110 | 130 | $out = ''; |
| 131 | + $x = 0; |
111 | 132 | foreach ( $res as $row ) { |
112 | 133 | $x++; |
113 | | - $stats = new UserStatsTrack( $row->stats_user_id, $row->stats_user_name ); |
| 134 | + $stats = new UserStatsTrack( |
| 135 | + $row->stats_user_id, |
| 136 | + $row->stats_user_name |
| 137 | + ); |
114 | 138 | $stats->updateTotalPoints(); |
115 | 139 | } |
116 | 140 | $out = "Updated stats for <b>{$x}</b> users"; |