r50604 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50603‎ | r50604 | r50605 >
Date:21:04, 14 May 2009
Author:ashley
Status:deferred
Tags:
Comment:
SocialProfile: some fixes for UserStats:
*added 'updatepoints' user right; required to access Special:UpdateEditCounts (previously required the user to be a member of staff user group)
*converted raw SQL in EditCount.php and SpecialUpdateEditCounts.php to use Database class
*fixed indentation in SpecialUpdateEditCounts.php
*spacing tweaks
Modified paths:
  • /trunk/extensions/SocialProfile/UserStats/EditCount.php (modified) (history)
  • /trunk/extensions/SocialProfile/UserStats/SpecialUpdateEditCounts.php (modified) (history)
  • /trunk/extensions/SocialProfile/UserStats/UserStats.i18n.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SocialProfile/UserStats/UserStats.i18n.php
@@ -41,6 +41,7 @@
4242 'top-fans-stats-comment-score-negative-given' => '{{PLURAL:$1|Thumb down given|Thumbs down given}}',
4343 'top-fans-stats-gifts-rec-count' => '{{PLURAL:$1|Gift received|Gifts received}}',
4444 'top-fans-stats-gifts-sent-count' => '{{PLURAL:$1|Gift sent|Gifts sent}}',
 45+ 'right-updatepoints' => 'Update edit counts',
4546 'level-advance-subject' => 'You are now a "$1" on {{SITENAME}}!',
4647 'level-advance-body' => 'Hi $1.
4748
@@ -518,7 +519,7 @@
519520 );
520521
521522 /** Finnish (Suomi)
522 - * @author Jack Phoenix
 523+ * @author Jack Phoenix <jack@countervandalism.net>
523524 */
524525 $messages['fi'] = array(
525526 'user-stats-alltime-title' => 'Kaikkien aikojen suurimmat pistemäärät',
@@ -540,6 +541,7 @@
541542 'top-fans-stats-foe-count' => '{{PLURAL:$1|vihollinen|vihollista}}',
542543 'top-fans-stats-gifts-rec-count' => '{{PLURAL:$1|saatu lahja|saatua lahjaa}}',
543544 'top-fans-stats-gifts-sent-count' => '{{PLURAL:$1|lähetetty lahja|lähetettyä lahjaa}}',
 545+ 'right-updatepoints' => 'Päivittää muokkausmääriä',
544546 'level-advance-subject' => 'Olet nyt "$1" {{GRAMMAR:inessive|{{SITENAME}}}}!',
545547 'level-advance-body' => 'Hei $1:
546548
Index: trunk/extensions/SocialProfile/UserStats/EditCount.php
@@ -1,4 +1,12 @@
22 <?php
 3+/**
 4+ * Protect against register_globals vulnerabilities.
 5+ * This line must be present before any global variable is referenced.
 6+ */
 7+if ( !defined( 'MEDIAWIKI' ) ){
 8+ die( "This is not a valid entry point.\n" );
 9+}
 10+
311 $wgHooks['NewRevisionFromEditComplete'][] = 'incEditCount';
412
513 function incEditCount( &$article, $revision, $baseRevId ) {
@@ -7,7 +15,7 @@
816 // only keep tally for allowable namespaces
917 if( !is_array( $wgNamespacesForEditPoints ) || in_array( $wgTitle->getNamespace(), $wgNamespacesForEditPoints ) ){
1018 $stats = new UserStatsTrack( $wgUser->getID(), $wgUser->getName() );
11 - $stats->incStatField('edit');
 19+ $stats->incStatField( 'edit' );
1220 }
1321 return true;
1422 }
@@ -15,14 +23,18 @@
1624 $wgHooks['ArticleDelete'][] = 'removeDeletedEdits';
1725
1826 function removeDeletedEdits( &$article, &$user, &$reason ){
19 - global $wgUser, $wgTitle, $wgDBprefix, $wgNamespacesForEditPoints;
 27+ global $wgUser, $wgTitle, $wgNamespacesForEditPoints;
2028
2129 // only keep tally for allowable namespaces
2230 if( !is_array( $wgNamespacesForEditPoints ) || in_array( $wgTitle->getNamespace(), $wgNamespacesForEditPoints ) ){
2331
2432 $dbr = wfGetDB( DB_MASTER );
25 - $sql = "SELECT rev_user_text, rev_user, count(*) AS the_count FROM ".$wgDBprefix."revision WHERE rev_page = {$article->getID()} AND rev_user <> 0 GROUP BY rev_user_text";
26 - $res = $dbr->query($sql);
 33+ $res = $dbr->select( 'revision',
 34+ array( 'rev_user_text', 'rev_user', 'COUNT(*) AS the_count' ),
 35+ array( 'rev_page' => $article->getID(), 'rev_user <> 0' ),
 36+ __METHOD__,
 37+ array( 'GROUP BY' => 'rev_user_text' )
 38+ );
2739 while( $row = $dbr->fetchObject( $res ) ) {
2840 $stats = new UserStatsTrack( $row->rev_user, $row->rev_user_text );
2941 $stats->decStatField( 'edit', $row->the_count );
@@ -34,14 +46,18 @@
3547 $wgHooks['ArticleUndelete'][] = 'restoreDeletedEdits';
3648
3749 function restoreDeletedEdits( &$title, $new ){
38 - global $wgUser, $wgDBprefix, $wgNamespacesForEditPoints;
 50+ global $wgUser, $wgNamespacesForEditPoints;
3951
4052 // only keep tally for allowable namespaces
4153 if( !is_array( $wgNamespacesForEditPoints ) || in_array( $title->getNamespace(), $wgNamespacesForEditPoints ) ){
4254
4355 $dbr = wfGetDB( DB_MASTER );
44 - $sql = "SELECT rev_user_text, rev_user, count(*) AS the_count FROM ".$wgDBprefix."revision WHERE rev_page = {$title->getArticleID()} AND rev_user <> 0 GROUP BY rev_user_text";
45 - $res = $dbr->query($sql);
 56+ $res = $dbr->select( 'revision',
 57+ array( 'rev_user_text', 'rev_user', 'COUNT(*) AS the_count' ),
 58+ array( 'rev_page' => $title->getArticleID(), 'rev_user <> 0' ),
 59+ __METHOD__,
 60+ array( 'GROUP BY' => 'rev_user_text' )
 61+ );
4662 while( $row = $dbr->fetchObject( $res ) ) {
4763 $stats = new UserStatsTrack( $row->rev_user, $row->rev_user_text );
4864 $stats->incStatField( 'edit', $row->the_count );
Index: trunk/extensions/SocialProfile/UserStats/SpecialUpdateEditCounts.php
@@ -5,53 +5,59 @@
66 /**
77 * Constructor
88 */
9 - function __construct(){
 9+ public function __construct(){
1010 parent::__construct( 'UpdateEditCounts' );
1111 }
1212
1313 function updateMainEditsCount(){
14 - global $wgOut, $wgUser, $wgDBprefix;
 14+ global $wgOut, $wgUser;
1515
16 - $wgOut->setPageTitle('Update Edit Counts');
 16+ $wgOut->setPageTitle( 'Update Edit Counts' );
1717
18 - if( !in_array( 'staff', ( $wgUser->getGroups() ) ) ){
 18+ if( !$wgUser->isAllowed( 'updatepoints' ) ){
1919 $wgOut->errorpage( 'error', 'badaccess' );
2020 return false;
2121 }
2222
2323 $dbw = wfGetDB( DB_MASTER );
24 - $sql = "SELECT rev_user_text, rev_user, count(*) AS the_count FROM ".$wgDBprefix."revision INNER JOIN ".$wgDBprefix."page ON page_id = rev_page WHERE page_namespace = 0 AND rev_user <> 0 GROUP BY rev_user_text ";
25 - $res = $dbw->query($sql);
 24+ $res = $dbw->select(
 25+ array( 'revision', 'page' ),
 26+ array( 'rev_user_text', 'rev_user', 'COUNT(*) AS the_count' ),
 27+ array( 'page_namespace = 0', 'rev_user <> 0' ),
 28+ __METHOD__,
 29+ array( 'GROUP BY' => 'rev_user_text' ),
 30+ array( 'page' => array( 'INNER JOIN', 'page_id = rev_page' ) )
 31+ );
2632 while( $row = $dbw->fetchObject( $res ) ) {
2733
28 - $user = User::newFromId($row->rev_user);
29 - $user->loadFromId();
 34+ $user = User::newFromId( $row->rev_user );
 35+ $user->loadFromId();
3036
31 - if( !$user->isAllowed( 'bot' ) ){
32 - $edit_count = $row->the_count;
33 - } else {
34 - $edit_count = 0;
35 - }
 37+ if( !$user->isAllowed( 'bot' ) ){
 38+ $edit_count = $row->the_count;
 39+ } else {
 40+ $edit_count = 0;
 41+ }
3642
37 - $s = $dbw->selectRow( 'user_stats', array( 'stats_user_id' ), array( 'stats_user_id' => $row->rev_user ), __METHOD__ );
38 - if ( !$s->stats_user_id ) {
 43+ $s = $dbw->selectRow( 'user_stats', array( 'stats_user_id' ), array( 'stats_user_id' => $row->rev_user ), __METHOD__ );
 44+ if ( !$s->stats_user_id ) {
 45+ $dbw->insert( 'user_stats',
 46+ array(
 47+ 'stats_year_id' => 0,
 48+ 'stats_user_id' => $row->rev_user,
 49+ 'stats_user_name' => $row->rev_user_text,
 50+ 'stats_total_points' => 1000
 51+ ), __METHOD__
 52+ );
 53+ }
 54+ $wgOut->addHTML("<p>Updating {$row->rev_user_text} with {$edit_count} edits</p>");
3955
40 - $dbw->insert( 'user_stats',
41 - array(
42 - 'stats_year_id' => 0,
43 - 'stats_user_id' => $row->rev_user,
44 - 'stats_user_name' => $row->rev_user_text,
45 - 'stats_total_points' => 1000
46 - ), __METHOD__
 56+ $dbw->update( 'user_stats',
 57+ array( 'stats_edit_count = ' . $edit_count ),
 58+ array( 'stats_user_id' => $row->rev_user ),
 59+ __METHOD__
4760 );
48 - }
49 - $wgOut->addHTML("<p>Updating {$row->rev_user_text} with {$edit_count} edits</p>");
5061
51 - $dbw->update( 'user_stats',
52 - array( "stats_edit_count=".$edit_count ),
53 - array( 'stats_user_id' => $row->rev_user ),
54 - __METHOD__ );
55 -
5662 global $wgMemc;
5763 // clear stats cache for current user
5864 $key = wfMemcKey( 'user', 'stats', $row->rev_user );
@@ -66,22 +72,26 @@
6773 * @param $par Mixed: parameter passed to the page or null
6874 */
6975 public function execute( $par ){
70 - global $wgUser, $wgOut, $wgDBprefix;
 76+ global $wgUser, $wgOut;
7177 $dbr = wfGetDB( DB_MASTER );
7278 $this->updateMainEditsCount();
7379
7480 global $wgUserLevels;
7581 $wgUserLevels = '';
7682
77 - $sql = "SELECT stats_user_id,stats_user_name, stats_total_points FROM ".$wgDBprefix."user_stats ORDER BY stats_user_name";
78 - $res = $dbr->query($sql);
 83+ $res = $dbr->select( 'user_stats',
 84+ array( 'stats_user_id', 'stats_user_name', 'stats_total_points' ),
 85+ array(),
 86+ __METHOD__,
 87+ array( 'ORDER BY' => 'stats_user_name' )
 88+ );
7989 $out = '';
8090 while ( $row = $dbr->fetchObject( $res ) ) {
8191 $x++;
82 - $stats = new UserStatsTrack($row->stats_user_id, $row->stats_user_name);
 92+ $stats = new UserStatsTrack( $row->stats_user_id, $row->stats_user_name );
8393 $stats->updateTotalPoints();
8494 }
8595 $out = "Updated stats for <b>{$x}</b> users";
86 - $wgOut->addHTML($out);
 96+ $wgOut->addHTML( $out );
8797 }
8898 }

Status & tagging log