http://www.mediawiki.org/wiki/Special:Code/MediaWiki/63101

Revision: 63101
Author:   ashley
Date:     2010-02-28 21:40:31 +0000 (Sun, 28 Feb 2010)

Log Message:
-----------
SocialProfile: rewrote UserGifts' DB queries

Modified Paths:
--------------
    trunk/extensions/SocialProfile/UserGifts/GiftsClass.php
    trunk/extensions/SocialProfile/UserGifts/UserGiftsClass.php

Modified: trunk/extensions/SocialProfile/UserGifts/GiftsClass.php
===================================================================
--- trunk/extensions/SocialProfile/UserGifts/GiftsClass.php     2010-02-28 
21:28:04 UTC (rev 63100)
+++ trunk/extensions/SocialProfile/UserGifts/GiftsClass.php     2010-02-28 
21:40:31 UTC (rev 63101)
@@ -1,7 +1,8 @@
 <?php
 /**
  * Gifts class
- * Functions for managing individual social gifts (add to/fetch/remove from 
database etc.)
+ * Functions for managing individual social gifts
+ * (add to/fetch/remove from database etc.)
  */
 class Gifts {
 
@@ -35,11 +36,12 @@
 
                $dbw = wfGetDB( DB_MASTER );
 
-               $dbw->insert( 'gift',
+               $dbw->insert(
+                       'gift',
                        array(
                                'gift_name' => $gift_name,
                                'gift_description' => $gift_description,
-                               'gift_createdate' => date( "Y-m-d H:i:s" ),
+                               'gift_createdate' => date( 'Y-m-d H:i:s' ),
                                'gift_creator_user_id' => $wgUser->getID(),
                                'gift_creator_user_name' => $wgUser->getName(),
                                'gift_access' => $gift_access,
@@ -74,15 +76,22 @@
         * @return Gift information, including ID number, name, description, 
creator's user name and ID and gift access
         */
        static function getGift( $id ) {
-               if ( !is_numeric( $id ) )
+               if ( !is_numeric( $id ) ) {
                        return '';
+               }
                $dbr = wfGetDB( DB_SLAVE );
-               $sql = "SELECT gift_id, gift_name, gift_description,
-                       gift_creator_user_id, gift_creator_user_name, 
gift_access
-                       FROM {$dbr->tableName( 'gift' )} WHERE gift_id = {$id} 
LIMIT 0,1";
-               $res = $dbr->query( $sql );
+               $res = $dbr->select(
+                       'gift',
+                       array(
+                               'gift_id', 'gift_name', 'gift_description',
+                               'gift_creator_user_id', 
'gift_creator_user_name', 'gift_access'
+                       ),
+                       array( "gift_id = {$id}" ),
+                       __METHOD__,
+                       array( 'LIMIT' => 1, 'OFFSET' => 0 )
+               );
                $row = $dbr->fetchObject( $res );
-               $gift = '';
+               $gift = array();
                if ( $row ) {
                        $gift['gift_id'] = $row->gift_id;
                        $gift['gift_name'] = $row->gift_name;
@@ -110,22 +119,31 @@
                global $wgUser;
 
                $dbr = wfGetDB( DB_SLAVE );
+               $params = array();
 
                if ( $limit > 0 ) {
                        $limitvalue = 0;
-                       if ( $page ) $limitvalue = $page * $limit - ( $limit );
-                       $limit_sql = " LIMIT {$limitvalue},{$limit} ";
+                       if ( $page ) {
+                               $limitvalue = $page * $limit - ( $limit );
+                       }
+                       $params['LIMIT'] = $limit;
+                       $params['OFFSET'] = $limitvalue;
                }
 
-               $sql = "SELECT 
gift_id,gift_createdate,gift_name,gift_description,gift_given_count
-                       FROM {$dbr->tableName( 'gift' )}
-                       WHERE gift_access=0 OR gift_creator_user_id = 
{$wgUser->getID()}
-                       ORDER BY {$order}
-                       {$limit_sql}";
+               $params['ORDER BY'] = $order;
+               $res = $dbr->select(
+                       'gift',
+                       array(
+                               'gift_id', 'gift_createdate', 'gift_name', 
'gift_description',
+                               'gift_given_count'
+                       ),
+                       array( "gift_access = 0 OR gift_creator_user_id = 
{$wgUser->getID()}" ),
+                       __METHOD__,
+                       $params
+               );
 
-               $res = $dbr->query( $sql );
                $gifts = array();
-               while ( $row = $dbr->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $gifts[] = array(
                                'id' => $row->gift_id,
                                'timestamp' => ( $row->gift_createdate ),
@@ -143,24 +161,32 @@
 
                $where = ''; // Prevent E_NOTICE
                $params['ORDER BY'] = 'gift_createdate';
-               if ( $limit )
+               if ( $limit ) {
                        $params['LIMIT'] = $limit;
+               }
 
-               // If the user isn't in giftadmin group and isn't allowed to 
delete pages, only show them the gifts they've created
-               if ( !in_array( 'giftadmin', ( $wgUser->getGroups() ) ) && 
!$wgUser->isAllowed( 'delete' ) ) {
+               // If the user isn't allowed to perform administrative tasks to 
gifts
+               // and isn't allowed to delete pages, only show them the gifts 
they've
+               // created
+               if ( !$wgUser->isAllowed( 'giftadmin' ) && !$wgUser->isAllowed( 
'delete' ) ) {
                        $where = array( 'gift_creator_user_id' => 
$wgUser->getID() );
                }
 
                $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( 'gift',
-                       array( 'gift_id', 'gift_createdate', 'gift_name', 
'gift_description', 'gift_given_count',
-                               'gift_access', 'gift_creator_user_id', 
'gift_creator_user_name' ),
-                       $where, __METHOD__,
+               $res = $dbr->select(
+                       'gift',
+                       array(
+                               'gift_id', 'gift_createdate', 'gift_name', 
'gift_description',
+                               'gift_given_count', 'gift_access', 
'gift_creator_user_id',
+                               'gift_creator_user_name'
+                       ),
+                       $where,
+                       __METHOD__,
                        $params
                );
 
                $gifts = array();
-               while ( $row = $dbr->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $gifts[] = array(
                                'id' => $row->gift_id,
                                'timestamp' => ( $row->gift_createdate ),
@@ -175,16 +201,30 @@
        static function getCustomCreatedGiftCount( $user_id ) {
                $dbr = wfGetDB( DB_SLAVE );
                $gift_count = 0;
-               $s = $dbr->selectRow( 'gift', array( 'count(*) AS count' ), 
array( 'gift_creator_user_id' => $user_id ), __METHOD__ );
-               if ( $s !== false ) $gift_count = $s->count;
+               $s = $dbr->selectRow(
+                       'gift',
+                       array( 'COUNT(*) AS count' ),
+                       array( 'gift_creator_user_id' => $user_id ),
+                       __METHOD__
+               );
+               if ( $s !== false ) {
+                       $gift_count = $s->count;
+               }
                return $gift_count;
        }
 
        static function getGiftCount() {
                $dbr = wfGetDB( DB_SLAVE );
                $gift_count = 0;
-               $s = $dbr->selectRow( 'gift', array( 'count(*) AS count' ), 
array( 'gift_given_count' => $gift_count ), __METHOD__ );
-               if ( $s !== false ) $gift_count = $s->count;
+               $s = $dbr->selectRow(
+                       'gift',
+                       array( 'COUNT(*) AS count' ),
+                       array( 'gift_given_count' => $gift_count ),
+                       __METHOD__
+               );
+               if ( $s !== false ) {
+                       $gift_count = $s->count;
+               }
                return $gift_count;
        }
 }

Modified: trunk/extensions/SocialProfile/UserGifts/UserGiftsClass.php
===================================================================
--- trunk/extensions/SocialProfile/UserGifts/UserGiftsClass.php 2010-02-28 
21:28:04 UTC (rev 63100)
+++ trunk/extensions/SocialProfile/UserGifts/UserGiftsClass.php 2010-02-28 
21:40:31 UTC (rev 63101)
@@ -26,11 +26,20 @@
                $this->user_id = User::idFromName( $this->user_name );
        }
 
+       /**
+        * Sends a gift to the specified user.
+        *
+        * @param $user_to Integer: user ID of the recipient
+        * @param $gift_id Integer: gift ID number
+        * @param $type Integer: gift type
+        * @param $message Mixed: message as supplied by the sender
+        */
        public function sendGift( $user_to, $gift_id, $type, $message ) {
                $user_id_to = User::idFromName( $user_to );
                $dbw = wfGetDB( DB_MASTER );
 
-               $dbw->insert( 'user_gift',
+               $dbw->insert(
+                       'user_gift',
                        array(
                                'ug_gift_id' => $gift_id,
                                'ug_user_id_from' => $this->user_id,
@@ -40,7 +49,7 @@
                                'ug_type' => $type,
                                'ug_status' => 1,
                                'ug_message' => $message,
-                               'ug_date' => date( "Y-m-d H:i:s" ),
+                               'ug_date' => date( 'Y-m-d H:i:s' ),
                        ), __METHOD__
                );
                $ug_gift_id = $dbw->insertId();
@@ -109,7 +118,12 @@
         */
        public function doesUserOwnGift( $user_id, $ug_id ) {
                $dbr = wfGetDB( DB_SLAVE );
-               $s = $dbr->selectRow( 'user_gift', array( 'ug_user_id_to' ), 
array( 'ug_id' => $ug_id ), __METHOD__ );
+               $s = $dbr->selectRow(
+                       'user_gift',
+                       array( 'ug_user_id_to' ),
+                       array( 'ug_id' => $ug_id ),
+                       __METHOD__
+               );
                if ( $s !== false ) {
                        if ( $user_id == $s->ug_user_id_to ) {
                                return true;
@@ -128,15 +142,24 @@
        }
 
        static function getUserGift( $id ) {
-               if ( !is_numeric( $id ) )
+               if ( !is_numeric( $id ) ) {
                        return '';
+               }
 
                $dbr = wfGetDB( DB_SLAVE );
-               $sql = "SELECT ug_id, ug_user_id_from, ug_user_name_from, 
ug_user_id_to,ug_user_name_to,ug_message,gift_id, ug_date,
-                       ug_status,gift_name, gift_description, gift_given_count
-                       FROM {$dbr->tableName( 'user_gift' )} INNER JOIN 
{$dbr->tableName( 'gift' )} ON ug_gift_id=gift_id
-                       WHERE ug_id = {$id} LIMIT 0,1";
-               $res = $dbr->query( $sql );
+               $res = $dbr->select(
+                       array( 'user_gift', 'gift' ),
+                       array(
+                               'ug_id', 'ug_user_id_from', 'ug_user_name_from',
+                               'ug_user_id_to', 'ug_user_name_to', 
'ug_message', 'gift_id',
+                               'ug_date', 'ug_status', 'gift_name', 
'gift_description',
+                               'gift_given_count'
+                       ),
+                       array( "ug_id = {$id}" ),
+                       __METHOD__,
+                       array( 'LIMIT' => 1, 'OFFSET' => 0 ),
+                       array( 'gift' => array( 'INNER JOIN', 'ug_gift_id = 
gift_id' ) )
+               );
                $row = $dbr->fetchObject( $res );
                if ( $row ) {
                        $gift['id'] = $row->ug_id;
@@ -202,9 +225,15 @@
                $key = wfMemcKey( 'user_gifts', 'new_count', $user_id );
                $dbr = wfGetDB( DB_SLAVE );
                $new_gift_count = 0;
-               $s = $dbr->selectRow( 'user_gift', array( 'count(*) AS count' 
), array( 'ug_user_id_to' => $user_id, 'ug_status' => 1 ), __METHOD__ );
-               if ( $s !== false )
+               $s = $dbr->selectRow(
+                       'user_gift',
+                       array( 'COUNT(*) AS count' ),
+                       array( 'ug_user_id_to' => $user_id, 'ug_status' => 1 ),
+                       __METHOD__
+               );
+               if ( $s !== false ) {
                        $new_gift_count = $s->count;
+               }
 
                $wgMemc->set( $key, $new_gift_count );
 
@@ -213,23 +242,33 @@
 
        public function getUserGiftList( $type, $limit = 0, $page = 0 ) {
                $dbr = wfGetDB( DB_SLAVE );
+               $params = array();
 
                if ( $limit > 0 ) {
                        $limitvalue = 0;
-                       if ( $page ) $limitvalue = $page * $limit - ( $limit );
-                       $limit_sql = " LIMIT {$limitvalue},{$limit} ";
+                       if ( $page ) {
+                               $limitvalue = $page * $limit - ( $limit );
+                       }
+                       $params['LIMIT'] = $limit;
+                       $params['OFFSET'] = $limitvalue;
                }
 
-               $sql = "SELECT ug_id, ug_user_id_from, ug_user_name_from, 
ug_gift_id, ug_date, ug_status,
-                       gift_name, gift_description, gift_given_count, 
UNIX_TIMESTAMP(ug_date) AS unix_time
-                       FROM {$dbr->tableName( 'user_gift' )} INNER JOIN 
{$dbr->tableName( 'gift' )} ON ug_gift_id=gift_id
-                       WHERE ug_user_id_to = {$this->user_id}
-                       ORDER BY ug_id DESC
-                       {$limit_sql}";
-               $res = $dbr->query( $sql );
+               $params['ORDER BY'] = 'ug_id DESC';
+               $res = $dbr->select(
+                       array( 'user_gift', 'gift' ),
+                       array(
+                               'ug_id', 'ug_user_id_from', 
'ug_user_name_from', 'ug_gift_id',
+                               'ug_date', 'ug_status', 'gift_name', 
'gift_description',
+                               'gift_given_count', 'UNIX_TIMESTAMP(ug_date) AS 
unix_time'
+                       ),
+                       array( "ug_user_id_to = {$this->user_id}" ),
+                       __METHOD__,
+                       $params,
+                       array( 'gift' => array( 'INNER JOIN', 'ug_gift_id = 
gift_id' ) )
+               );
 
                $requests = array();
-               while ( $row = $dbr->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $requests[] = array(
                                'id' => $row->ug_id,
                                'gift_id' => $row->ug_gift_id,
@@ -248,22 +287,33 @@
 
        public function getAllGiftList( $limit = 10, $page = 0 ) {
                $dbr = wfGetDB( DB_SLAVE );
+               $params = array();
 
+               $params['ORDER BY'] = 'ug_id DESC';
                if ( $limit > 0 ) {
                        $limitvalue = 0;
-                       if ( $page ) $limitvalue = $page * $limit - ( $limit );
-                       $limit_sql = " LIMIT {$limitvalue},{$limit} ";
+                       if ( $page ) {
+                               $limitvalue = $page * $limit - ( $limit );
+                       }
+                       $params['LIMIT'] = $limit;
+                       $params['OFFSET'] = $limitvalue;
                }
 
-               $sql = "SELECT ug_id, ug_user_id_from, ug_user_name_from, 
ug_gift_id, ug_date, ug_status,
-                       gift_name, gift_description, gift_given_count, 
UNIX_TIMESTAMP(ug_date) AS unix_time
-                       FROM {$dbr->tableName( 'user_gift' )} INNER JOIN 
{$dbr->tableName( 'gift' )} ON ug_gift_id=gift_id
-                       ORDER BY ug_id DESC
-                       {$limit_sql}";
-               $res = $dbr->query( $sql );
+               $res = $dbr->select(
+                       array( 'user_gift', 'gift' ),
+                       array(
+                               'ug_id', 'ug_user_id_from', 
'ug_user_name_from', 'ug_gift_id',
+                               'ug_date', 'ug_status', 'gift_name', 
'gift_description',
+                               'gift_given_count', 'UNIX_TIMESTAMP(ug_date) AS 
unix_time'
+                       ),
+                       array(),
+                       __METHOD__,
+                       $params,
+                       array( 'gift' => array( 'INNER JOIN', 'ug_gift_id = 
gift_id' ) )
+               );
 
                $requests = array();
-               while ( $row = $dbr->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $requests[] = array(
                                'id' => $row->ug_id,
                                'gift_id' => $row->ug_gift_id,
@@ -280,23 +330,35 @@
                return $requests;
        }
 
+       /**
+        * Update the counter that tracks how many times a gift has been given 
out.
+        * @param $gift_id Integer: ID number of the gift that we're tracking
+        */
        private function incGiftGivenCount( $gift_id ) {
                $dbw = wfGetDB( DB_MASTER );
-               $dbw->update( 'gift',
+               $dbw->update(
+                       'gift',
                        array( 'gift_given_count=gift_given_count+1' ),
                        array( 'gift_id' => $gift_id ),
                        __METHOD__
                );
        }
 
+       /**
+        * Gets the amount of gifts a user has.
+        * @param $user_name Mixed: username whose gift count we're looking up
+        * @return integer
+        */
        static function getGiftCountByUsername( $user_name ) {
                $dbr = wfGetDB( DB_SLAVE );
                $user_id = User::idFromName( $user_name );
-               $sql = "SELECT count(*) AS count
-                       FROM {$dbr->tableName( 'user_gift' )}
-                       WHERE ug_user_id_to = {$user_id}
-                       LIMIT 0,1";
-               $res = $dbr->query( $sql );
+               $res = $dbr->select(
+                       'user_gift',
+                       'COUNT(*) AS count',
+                       array( "ug_user_id_to = {$user_id}" ),
+                       __METHOD__,
+                       array( 'LIMIT' => 1, 'OFFSET' => 0 )
+               );
                $row = $dbr->fetchObject( $res );
                $gift_count = 0;
                if ( $row ) {



_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to