Index: trunk/extensions/SocialProfile/UserStats/UserStatsClass.php |
— | — | @@ -83,7 +83,8 @@ |
84 | 84 | /** |
85 | 85 | * Constructor |
86 | 86 | * @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. |
88 | 89 | */ |
89 | 90 | function __construct( $user_id, $user_name = '' ) { |
90 | 91 | global $wgUserStatsPointValues; |
— | — | @@ -145,8 +146,14 @@ |
146 | 147 | $wgMemc->delete( $key ); |
147 | 148 | } |
148 | 149 | |
| 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 | + */ |
149 | 156 | function incStatField( $field, $val = 1 ) { |
150 | | - global $wgUser, $wgMemc, $wgSystemGifts, $wgUserStatsTrackWeekly, $wgUserStatsTrackMonthly, $wgUserStatsPointValues; |
| 157 | + global $wgUser, $wgMemc, $wgSystemGifts, $wgUserStatsTrackWeekly, $wgUserStatsTrackMonthly; |
151 | 158 | if ( !$wgUser->isAllowed( 'bot' ) && !$wgUser->isAnon() && $this->stats_fields[$field] ) { |
152 | 159 | $dbw = wfGetDB( DB_MASTER ); |
153 | 160 | $dbw->update( |
— | — | @@ -169,38 +176,42 @@ |
170 | 177 | } |
171 | 178 | } |
172 | 179 | |
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; |
182 | 188 | |
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 ); |
185 | 191 | |
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 ); |
195 | 200 | } |
| 201 | + } |
196 | 202 | |
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 ); |
201 | 206 | } |
202 | 207 | } |
203 | 208 | } |
204 | 209 | |
| 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 | + */ |
205 | 216 | function decStatField( $field, $val = 1 ) { |
206 | 217 | global $wgUser, $wgUserStatsTrackWeekly, $wgUserStatsTrackMonthly; |
207 | 218 | if ( !$wgUser->isAllowed( 'bot' ) && !$wgUser->isAnon() && $this->stats_fields[$field] ) { |
— | — | @@ -232,7 +243,7 @@ |
233 | 244 | $dbw = wfGetDB( DB_MASTER ); |
234 | 245 | $sql = "UPDATE {$dbw->tableName( 'user_stats' )} SET "; |
235 | 246 | $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; |
237 | 248 | $sql .= ")"; |
238 | 249 | $sql .= " WHERE stats_user_id = " . $this->user_id; |
239 | 250 | $res = $dbw->query( $sql, __METHOD__ ); |
— | — | @@ -290,20 +301,27 @@ |
291 | 302 | } |
292 | 303 | } |
293 | 304 | |
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 ) { |
295 | 313 | global $wgUser; |
296 | 314 | if ( $this->user_id != 0 ) { |
297 | 315 | $dbw = wfGetDB( DB_MASTER ); |
298 | 316 | $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='; |
301 | 319 | } else { |
302 | | - $sql .= 'stats_comment_score_negative_rec='; |
| 320 | + $sql .= 'stats_comment_score_negative_rec='; |
303 | 321 | } |
304 | 322 | $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; |
308 | 326 | $res = $dbw->query( $sql, __METHOD__ ); |
309 | 327 | |
310 | 328 | $this->clearCache(); |
— | — | @@ -350,21 +368,23 @@ |
351 | 369 | } |
352 | 370 | |
353 | 371 | /** |
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 |
356 | 376 | */ |
357 | | - function updateRelationshipCount( $rel_type ) { |
| 377 | + function updateRelationshipCount( $relType ) { |
358 | 378 | global $wgUser; |
359 | 379 | if ( !$wgUser->isAnon() ) { |
360 | 380 | $dbw = wfGetDB( DB_MASTER ); |
361 | | - if ( $rel_type == 1 ) { |
| 381 | + if ( $relType == 1 ) { |
362 | 382 | $col = 'stats_friends_count'; |
363 | 383 | } else { |
364 | 384 | $col = 'stats_foe_count'; |
365 | 385 | } |
366 | 386 | $sql = "UPDATE LOW_PRIORITY {$dbw->tableName( 'user_stats' )} SET {$col}= |
367 | 387 | (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} |
369 | 389 | ) |
370 | 390 | WHERE stats_user_id = {$this->user_id}"; |
371 | 391 | $res = $dbw->query( $sql, __METHOD__ ); |
— | — | @@ -440,6 +460,10 @@ |
441 | 461 | ); |
442 | 462 | } |
443 | 463 | |
| 464 | + /** |
| 465 | + * Adds a record about the current user to the user_points_weekly database |
| 466 | + * table. |
| 467 | + */ |
444 | 468 | public function addWeekly() { |
445 | 469 | $dbw = wfGetDB( DB_MASTER ); |
446 | 470 | $dbw->insert( |
— | — | @@ -473,6 +497,10 @@ |
474 | 498 | ); |
475 | 499 | } |
476 | 500 | |
| 501 | + /** |
| 502 | + * Adds a record about the current user to the user_points_monthly database |
| 503 | + * table. |
| 504 | + */ |
477 | 505 | public function addMonthly() { |
478 | 506 | $dbw = wfGetDB( DB_MASTER ); |
479 | 507 | $dbw->insert( |
— | — | @@ -485,6 +513,11 @@ |
486 | 514 | ); |
487 | 515 | } |
488 | 516 | |
| 517 | + /** |
| 518 | + * Updates the total amount of points the user has. |
| 519 | + * |
| 520 | + * @return Array |
| 521 | + */ |
489 | 522 | public function updateTotalPoints() { |
490 | 523 | global $wgEnableFacebook, $wgUserLevels; |
491 | 524 | |
— | — | @@ -615,7 +648,7 @@ |
616 | 649 | $key = wfMemcKey( 'user', 'stats', $this->user_id ); |
617 | 650 | $data = $wgMemc->get( $key ); |
618 | 651 | 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" ); |
620 | 653 | return $data; |
621 | 654 | } |
622 | 655 | } |
— | — | @@ -628,8 +661,16 @@ |
629 | 662 | |
630 | 663 | wfDebug( "Got user stats for {$this->user_name} from DB\n" ); |
631 | 664 | $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 | + ); |
634 | 675 | $row = $dbr->fetchObject( $res ); |
635 | 676 | $stats['edits'] = number_format( isset( $row->stats_edit_count ) ? $row->stats_edit_count : 0 ); |
636 | 677 | $stats['votes'] = number_format( isset( $row->stats_vote_count ) ? $row->stats_vote_count : 0 ); |
— | — | @@ -667,25 +708,28 @@ |
668 | 709 | return $stats; |
669 | 710 | } |
670 | 711 | |
| 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 | + */ |
671 | 719 | static function getTopFansList( $limit = 10 ) { |
672 | 720 | $dbr = wfGetDB( DB_MASTER ); |
673 | 721 | |
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 | + ); |
681 | 732 | |
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 | | - |
688 | 733 | $list = array(); |
689 | | - $res = $dbr->query( $sql, __METHOD__ ); |
690 | 734 | foreach ( $res as $row ) { |
691 | 735 | $list[] = array( |
692 | 736 | 'user_id' => $row->stats_user_id, |
— | — | @@ -696,29 +740,33 @@ |
697 | 741 | return $list; |
698 | 742 | } |
699 | 743 | |
| 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 | + */ |
700 | 752 | 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 | | - } |
710 | 753 | if ( $period == 'monthly' ) { |
711 | | - $points_table = 'user_points_monthly'; |
| 754 | + $pointsTable = 'user_points_monthly'; |
712 | 755 | } else { |
713 | | - $points_table = 'user_points_weekly'; |
| 756 | + $pointsTable = 'user_points_weekly'; |
714 | 757 | } |
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 | + ); |
720 | 769 | |
721 | 770 | $list = array(); |
722 | | - $res = $dbr->query( $sql, __METHOD__ ); |
723 | 771 | foreach ( $res as $row ) { |
724 | 772 | $list[] = array( |
725 | 773 | 'user_id' => $row->up_user_id, |
— | — | @@ -729,17 +777,20 @@ |
730 | 778 | return $list; |
731 | 779 | } |
732 | 780 | |
| 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 | + */ |
733 | 794 | 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 | | - |
744 | 795 | if ( $condition == 1 ) { |
745 | 796 | $op = '>'; |
746 | 797 | $sort = 'ASC'; |
— | — | @@ -747,15 +798,26 @@ |
748 | 799 | $op = '<'; |
749 | 800 | $sort = 'DESC'; |
750 | 801 | } |
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 | + ); |
758 | 821 | $list = array(); |
759 | | - $res = $dbr->query( $sql, __METHOD__ ); |
760 | 822 | foreach ( $res as $row ) { |
761 | 823 | $list[] = array( |
762 | 824 | 'user_id' => $row->stats_user_id, |
— | — | @@ -838,12 +900,12 @@ |
839 | 901 | |
840 | 902 | /** |
841 | 903 | * @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 |
848 | 910 | * @param $count |
849 | 911 | * @param $page_title |
850 | 912 | */ |