r76100 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76099‎ | r76100 | r76101 >
Date:15:02, 5 November 2010
Author:ashley
Status:deferred
Tags:
Comment:
SocialProfile: converted a few raw SQL queries to use the appropriate Database functions, removed a silly if() conditional that used a variable which was never set ($wgSystemGifts; system gifts/awards functionality is nowadays always enabled by default), updated some variable names, added docs
Modified paths:
  • /trunk/extensions/SocialProfile/UserStats/UserStatsClass.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SocialProfile/UserStats/UserStatsClass.php
@@ -83,7 +83,8 @@
8484 /**
8585 * Constructor
8686 * @param $user_id Integer: ID number of the user that we want to track stats for
87 - * @param $user_name Mixed: user's name; if not supplied, then the user ID will be used to get the user name from DB.
 87+ * @param $user_name Mixed: user's name; if not supplied, then the user ID
 88+ * will be used to get the user name from DB.
8889 */
8990 function __construct( $user_id, $user_name = '' ) {
9091 global $wgUserStatsPointValues;
@@ -145,8 +146,14 @@
146147 $wgMemc->delete( $key );
147148 }
148149
 150+ /**
 151+ * Increase a given social statistic field by $val.
 152+ *
 153+ * @param $field String: field name in user_stats database table
 154+ * @param $val Integer: increase $field by this amount, defaults to 1
 155+ */
149156 function incStatField( $field, $val = 1 ) {
150 - global $wgUser, $wgMemc, $wgSystemGifts, $wgUserStatsTrackWeekly, $wgUserStatsTrackMonthly, $wgUserStatsPointValues;
 157+ global $wgUser, $wgMemc, $wgSystemGifts, $wgUserStatsTrackWeekly, $wgUserStatsTrackMonthly;
151158 if ( !$wgUser->isAllowed( 'bot' ) && !$wgUser->isAnon() && $this->stats_fields[$field] ) {
152159 $dbw = wfGetDB( DB_MASTER );
153160 $dbw->update(
@@ -169,38 +176,42 @@
170177 }
171178 }
172179
173 - if ( $wgSystemGifts ) {
174 - $s = $dbw->selectRow(
175 - 'user_stats',
176 - array( $this->stats_fields[$field] ),
177 - array( 'stats_user_id' => $this->user_id ),
178 - __METHOD__
179 - );
180 - $stat_field = $this->stats_fields[$field];
181 - $field_count = $s->$stat_field;
 180+ $s = $dbw->selectRow(
 181+ 'user_stats',
 182+ array( $this->stats_fields[$field] ),
 183+ array( 'stats_user_id' => $this->user_id ),
 184+ __METHOD__
 185+ );
 186+ $stat_field = $this->stats_fields[$field];
 187+ $field_count = $s->$stat_field;
182188
183 - $key = wfMemcKey( 'system_gift', 'id', $field . '-' . $field_count );
184 - $data = $wgMemc->get( $key );
 189+ $key = wfMemcKey( 'system_gift', 'id', $field . '-' . $field_count );
 190+ $data = $wgMemc->get( $key );
185191
186 - if ( $data ) {
187 - wfDebug( "Got system gift id from cache\n" );
188 - $system_gift_id = $data;
189 - } else {
190 - $g = new SystemGifts();
191 - $system_gift_id = $g->doesGiftExistForThreshold( $field, $field_count );
192 - if ( $system_gift_id ) {
193 - $wgMemc->set( $key, $system_gift_id, 60 * 30 );
194 - }
 192+ if ( $data ) {
 193+ wfDebug( "Got system gift ID from cache\n" );
 194+ $systemGiftID = $data;
 195+ } else {
 196+ $g = new SystemGifts();
 197+ $systemGiftID = $g->doesGiftExistForThreshold( $field, $field_count );
 198+ if ( $systemGiftID ) {
 199+ $wgMemc->set( $key, $systemGiftID, 60 * 30 );
195200 }
 201+ }
196202
197 - if ( $system_gift_id ) {
198 - $sg = new UserSystemGifts( $this->user_name );
199 - $sg->sendSystemGift( $system_gift_id );
200 - }
 203+ if ( $systemGiftID ) {
 204+ $sg = new UserSystemGifts( $this->user_name );
 205+ $sg->sendSystemGift( $systemGiftID );
201206 }
202207 }
203208 }
204209
 210+ /**
 211+ * Decrease a given social statistic field by $val.
 212+ *
 213+ * @param $field String: field name in user_stats database table
 214+ * @param $val Integer: decrease $field by this amount, defaults to 1
 215+ */
205216 function decStatField( $field, $val = 1 ) {
206217 global $wgUser, $wgUserStatsTrackWeekly, $wgUserStatsTrackMonthly;
207218 if ( !$wgUser->isAllowed( 'bot' ) && !$wgUser->isAnon() && $this->stats_fields[$field] ) {
@@ -232,7 +243,7 @@
233244 $dbw = wfGetDB( DB_MASTER );
234245 $sql = "UPDATE {$dbw->tableName( 'user_stats' )} SET ";
235246 $sql .= 'stats_comment_count=';
236 - $sql .= "(SELECT COUNT(*) AS CommentCount FROM {$dbw->tableName( 'Comments' )} WHERE Comment_user_id = " . $this->user_id;
 247+ $sql .= "(SELECT COUNT(*) AS CommentCount FROM {$dbw->tableName( 'Comments' )} WHERE Comment_user_id = " . $this->user_id;
237248 $sql .= ")";
238249 $sql .= " WHERE stats_user_id = " . $this->user_id;
239250 $res = $dbw->query( $sql, __METHOD__ );
@@ -290,20 +301,27 @@
291302 }
292303 }
293304
294 - function updateCommentScoreRec( $vote_type ) {
 305+ /**
 306+ * Updates the comment scores for the current user.
 307+ *
 308+ * @param $voteType Integer: if 1, sets the amount of positive comment
 309+ * scores, else sets the amount of negative
 310+ * comment scores
 311+ */
 312+ function updateCommentScoreRec( $voteType ) {
295313 global $wgUser;
296314 if ( $this->user_id != 0 ) {
297315 $dbw = wfGetDB( DB_MASTER );
298316 $sql = "UPDATE {$dbw->tableName( 'user_stats' )} SET ";
299 - if ( $vote_type == 1 ) {
300 - $sql .= 'stats_comment_score_positive_rec=';
 317+ if ( $voteType == 1 ) {
 318+ $sql .= 'stats_comment_score_positive_rec=';
301319 } else {
302 - $sql .= 'stats_comment_score_negative_rec=';
 320+ $sql .= 'stats_comment_score_negative_rec=';
303321 }
304322 $sql .= "(SELECT COUNT(*) AS CommentVoteCount FROM {$dbw->tableName( 'Comments_Vote' )} WHERE Comment_Vote_ID IN (
305 - SELECT CommentID FROM {$dbw->tableName( 'Comments' )} WHERE Comment_user_id = " . $this->user_id . ") AND Comment_Vote_Score=" . $vote_type;
306 - $sql .= ")";
307 - $sql .= " WHERE stats_user_id = " . $this->user_id;
 323+ SELECT CommentID FROM {$dbw->tableName( 'Comments' )} WHERE Comment_user_id = " . $this->user_id . ") AND Comment_Vote_Score=" . $voteType;
 324+ $sql .= ')';
 325+ $sql .= ' WHERE stats_user_id = ' . $this->user_id;
308326 $res = $dbw->query( $sql, __METHOD__ );
309327
310328 $this->clearCache();
@@ -350,21 +368,23 @@
351369 }
352370
353371 /**
354 - * Updates the amount of relationships (friends or foes) if the user isn't an anon
355 - * @param $rel_type Integer: 1 for updating friends
 372+ * Updates the amount of relationships (friends or foes) if the user isn't
 373+ * an anonymous one.
 374+ *
 375+ * @param $relType Integer: 1 for updating friends
356376 */
357 - function updateRelationshipCount( $rel_type ) {
 377+ function updateRelationshipCount( $relType ) {
358378 global $wgUser;
359379 if ( !$wgUser->isAnon() ) {
360380 $dbw = wfGetDB( DB_MASTER );
361 - if ( $rel_type == 1 ) {
 381+ if ( $relType == 1 ) {
362382 $col = 'stats_friends_count';
363383 } else {
364384 $col = 'stats_foe_count';
365385 }
366386 $sql = "UPDATE LOW_PRIORITY {$dbw->tableName( 'user_stats' )} SET {$col}=
367387 (SELECT COUNT(*) AS rel_count FROM {$dbw->tableName( 'user_relationship' )} WHERE
368 - r_user_id = {$this->user_id} AND r_type={$rel_type}
 388+ r_user_id = {$this->user_id} AND r_type={$relType}
369389 )
370390 WHERE stats_user_id = {$this->user_id}";
371391 $res = $dbw->query( $sql, __METHOD__ );
@@ -440,6 +460,10 @@
441461 );
442462 }
443463
 464+ /**
 465+ * Adds a record about the current user to the user_points_weekly database
 466+ * table.
 467+ */
444468 public function addWeekly() {
445469 $dbw = wfGetDB( DB_MASTER );
446470 $dbw->insert(
@@ -473,6 +497,10 @@
474498 );
475499 }
476500
 501+ /**
 502+ * Adds a record about the current user to the user_points_monthly database
 503+ * table.
 504+ */
477505 public function addMonthly() {
478506 $dbw = wfGetDB( DB_MASTER );
479507 $dbw->insert(
@@ -485,6 +513,11 @@
486514 );
487515 }
488516
 517+ /**
 518+ * Updates the total amount of points the user has.
 519+ *
 520+ * @return Array
 521+ */
489522 public function updateTotalPoints() {
490523 global $wgEnableFacebook, $wgUserLevels;
491524
@@ -615,7 +648,7 @@
616649 $key = wfMemcKey( 'user', 'stats', $this->user_id );
617650 $data = $wgMemc->get( $key );
618651 if ( $data ) {
619 - wfDebug( "Got user stats for {$this->user_name} from cache\n" );
 652+ wfDebug( "Got user stats for {$this->user_name} from cache\n" );
620653 return $data;
621654 }
622655 }
@@ -628,8 +661,16 @@
629662
630663 wfDebug( "Got user stats for {$this->user_name} from DB\n" );
631664 $dbr = wfGetDB( DB_MASTER );
632 - $sql = "SELECT * FROM {$dbr->tableName( 'user_stats' )} WHERE stats_user_id = {$this->user_id} LIMIT 0,1";
633 - $res = $dbr->query( $sql, __METHOD__ );
 665+ $res = $dbr->select(
 666+ 'user_stats',
 667+ '*',
 668+ array( 'stats_user_id' => $this->user_id ),
 669+ __METHOD__,
 670+ array(
 671+ 'LIMIT' => 1,
 672+ 'OFFSET' => 0
 673+ )
 674+ );
634675 $row = $dbr->fetchObject( $res );
635676 $stats['edits'] = number_format( isset( $row->stats_edit_count ) ? $row->stats_edit_count : 0 );
636677 $stats['votes'] = number_format( isset( $row->stats_vote_count ) ? $row->stats_vote_count : 0 );
@@ -667,25 +708,28 @@
668709 return $stats;
669710 }
670711
 712+ /**
 713+ * Get the list of top users, based on social statistics.
 714+ *
 715+ * @param $limit Integer: LIMIT for SQL query, defaults to 10
 716+ * @return Array: list of top users, contains the user IDs, names and
 717+ * amount of points the user has
 718+ */
671719 static function getTopFansList( $limit = 10 ) {
672720 $dbr = wfGetDB( DB_MASTER );
673721
674 - if ( $limit > 0 ) {
675 - $limitvalue = 0;
676 - if ( $page ) {
677 - $limitvalue = $page * $limit - ( $limit );
678 - }
679 - $limit_sql = " LIMIT {$limitvalue},{$limit} ";
680 - }
 722+ $res = $dbr->select(
 723+ 'user_stats',
 724+ array( 'stats_user_id', 'stats_user_name', 'stats_total_points' ),
 725+ array( 'stats_user_id <> 0' ),
 726+ __METHOD__,
 727+ array(
 728+ 'ORDER BY' => 'stats_total_points DESC',
 729+ 'LIMIT' => $limit
 730+ )
 731+ );
681732
682 - $sql = "SELECT stats_user_id, stats_user_name, stats_total_points
683 - FROM {$dbr->tableName( 'user_stats' )}
684 - WHERE stats_user_id <> 0
685 - ORDER BY stats_total_points DESC
686 - {$limit_sql}";
687 -
688733 $list = array();
689 - $res = $dbr->query( $sql, __METHOD__ );
690734 foreach ( $res as $row ) {
691735 $list[] = array(
692736 'user_id' => $row->stats_user_id,
@@ -696,29 +740,33 @@
697741 return $list;
698742 }
699743
 744+ /**
 745+ * Get the top users for a given period.
 746+ *
 747+ * @param $limit Integer: LIMIT for SQL query, defaults to 10
 748+ * @param $period String: period for which we're getting the top users, can
 749+ * be either 'weekly' or 'monthly'
 750+ * @return Array: list of top users
 751+ */
700752 static function getTopFansListPeriod( $limit = 10, $period = 'weekly' ) {
701 - $dbr = wfGetDB( DB_SLAVE );
702 -
703 - if ( $limit > 0 ) {
704 - $limitvalue = 0;
705 - if ( $page ) {
706 - $limitvalue = $page * $limit - ( $limit );
707 - }
708 - $limit_sql = " LIMIT {$limitvalue},{$limit} ";
709 - }
710753 if ( $period == 'monthly' ) {
711 - $points_table = 'user_points_monthly';
 754+ $pointsTable = 'user_points_monthly';
712755 } else {
713 - $points_table = 'user_points_weekly';
 756+ $pointsTable = 'user_points_weekly';
714757 }
715 - $sql = "SELECT up_user_id, up_user_name, up_points
716 - FROM {$points_table}
717 - WHERE up_user_id <> 0
718 - ORDER BY up_points DESC
719 - {$limit_sql}";
 758+ $dbr = wfGetDB( DB_SLAVE );
 759+ $res = $dbr->select(
 760+ $pointsTable,
 761+ array( 'up_user_id', 'up_user_name', 'up_points' ),
 762+ array( 'up_user_id <> 0' ),
 763+ __METHOD__,
 764+ array(
 765+ 'ORDER BY' => 'up_points DESC',
 766+ 'LIMIT' => $limit
 767+ )
 768+ );
720769
721770 $list = array();
722 - $res = $dbr->query( $sql, __METHOD__ );
723771 foreach ( $res as $row ) {
724772 $list[] = array(
725773 'user_id' => $row->up_user_id,
@@ -729,17 +777,20 @@
730778 return $list;
731779 }
732780
 781+ /**
 782+ * Gets the amount of friends relative to points.
 783+ *
 784+ * @param $user_id Integer: user ID
 785+ * @param $points Integer:
 786+ * @param $limit Integer: LIMIT for SQL queries, defaults to 3
 787+ * @param $condition Integer: if 1, the query operator for ORDER BY clause
 788+ * will be set to > and the results are
 789+ * ordered in ascending order, otherwise it'll
 790+ * be set to < and results are ordered in
 791+ * descending order
 792+ * @return Array
 793+ */
733794 static function getFriendsRelativeToPoints( $user_id, $points, $limit = 3, $condition = 1 ) {
734 - $dbr = wfGetDB( DB_SLAVE );
735 -
736 - if ( $limit > 0 ) {
737 - $limitvalue = 0;
738 - if ( $page ) {
739 - $limitvalue = $page * $limit - ( $limit );
740 - }
741 - $limit_sql = " LIMIT {$limitvalue},{$limit} ";
742 - }
743 -
744795 if ( $condition == 1 ) {
745796 $op = '>';
746797 $sort = 'ASC';
@@ -747,15 +798,26 @@
748799 $op = '<';
749800 $sort = 'DESC';
750801 }
751 - $sql = "SELECT stats_user_id, stats_user_name, stats_total_points
752 - FROM {$dbr->tableName( 'user_stats' )}
753 - INNER JOIN {$dbr->tableName( 'user_relationship' )} ON stats_user_id = r_user_id_relation
754 - WHERE r_user_id = {$user_id} AND stats_total_points {$op} {$points}
755 - ORDER BY stats_total_points {$sort}
756 - {$limit_sql}";
757 -
 802+ $dbr = wfGetDB( DB_SLAVE );
 803+ $res = $dbr->select(
 804+ array( 'user_stats', 'user_relationship' ),
 805+ array( 'stats_user_id', 'stats_user_name', 'stats_total_points' ),
 806+ array(
 807+ 'r_user_id' => $user_id,
 808+ "stats_total_points {$op} {$points}"
 809+ ),
 810+ __METHOD__,
 811+ array(
 812+ 'ORDER BY' => "stats_total_points {$sort}",
 813+ 'LIMIT' => $limit
 814+ ),
 815+ array(
 816+ 'user_relationship' => array(
 817+ 'INNER JOIN', 'stats_user_id = r_user_id_relation'
 818+ )
 819+ )
 820+ );
758821 $list = array();
759 - $res = $dbr->query( $sql, __METHOD__ );
760822 foreach ( $res as $row ) {
761823 $list[] = array(
762824 'user_id' => $row->stats_user_id,
@@ -838,12 +900,12 @@
839901
840902 /**
841903 * @param $type Integer: one of the following:
842 - 1 = Invite - Email Contacts sucker
843 - 2 = Invite - CVS Contacts importer
844 - 3 = Invite - Manually Address enter
845 - 4 = Invite to Read - Manually Address enter
846 - 5 = Invite to Edit - Manually Address enter
847 - 6 = Invite to Rate - Manually Address enter
 904+ * 1 = Invite - Email Contacts sucker
 905+ * 2 = Invite - CVS Contacts importer
 906+ * 3 = Invite - Manually Address enter
 907+ * 4 = Invite to Read - Manually Address enter
 908+ * 5 = Invite to Edit - Manually Address enter
 909+ * 6 = Invite to Rate - Manually Address enter
848910 * @param $count
849911 * @param $page_title
850912 */

Status & tagging log