http://www.mediawiki.org/wiki/Special:Code/MediaWiki/63098
Revision: 63098 Author: ashley Date: 2010-02-28 21:22:06 +0000 (Sun, 28 Feb 2010) Log Message: ----------- SocialProfile: rewrote some DB queries, documented some functions and did general cleanup to SystemGifts Modified Paths: -------------- trunk/extensions/SocialProfile/SystemGifts/SpecialViewSystemGift.php trunk/extensions/SocialProfile/SystemGifts/SystemGiftsClass.php Modified: trunk/extensions/SocialProfile/SystemGifts/SpecialViewSystemGift.php =================================================================== --- trunk/extensions/SocialProfile/SystemGifts/SpecialViewSystemGift.php 2010-02-28 21:16:38 UTC (rev 63097) +++ trunk/extensions/SocialProfile/SystemGifts/SpecialViewSystemGift.php 2010-02-28 21:22:06 UTC (rev 63098) @@ -15,10 +15,10 @@ * @param $par Mixed: parameter passed to the page or null */ public function execute( $par ) { - global $wgUser, $wgOut, $wgRequest, $wgUploadPath, $wgSystemGiftsScripts, $wgDBprefix; + global $wgUser, $wgOut, $wgRequest, $wgUploadPath, $wgSystemGiftsScripts; wfLoadExtensionMessages( 'SystemGifts' ); - $wgOut->addStyle( '../..' . $wgSystemGiftsScripts . '/SystemGift.css' ); + $wgOut->addExtensionStyle( $wgSystemGiftsScripts . '/SystemGift.css' ); $output = ''; // Prevent E_NOTICE $user_name = ''; // Prevent E_NOTICE @@ -30,17 +30,15 @@ return false; } - if ( !$user_name ) $user_name = $wgUser->getName(); + if ( !$user_name ) { + $user_name = $wgUser->getName(); + } $gift = UserSystemGifts::getUserGift( $gift_id ); $id = User::idFromName( $user_name ); $user_safe = urlencode( $gift['user_name'] ); - // DB stuff - $dbr = wfGetDB( DB_MASTER ); - if ( $gift ) { - if ( $gift['status'] == 1 ) { if ( $gift['user_name'] == $wgUser->getName() ) { $g = new UserSystemGifts( $gift['user_name'] ); @@ -48,45 +46,65 @@ $g->decNewSystemGiftCount( $wgUser->getID() ); } } - $sql = "SELECT DISTINCT sg_user_name, sg_user_id, sg_gift_id, sg_date FROM " . $wgDBprefix . "user_system_gift WHERE sg_gift_id={$gift["gift_id"]} AND sg_user_name<>'" . addslashes( $gift['user_name'] ) . "' GROUP BY sg_user_name ORDER BY sg_date DESC LIMIT 0,6"; - $res = $dbr->query( $sql ); + // DB stuff + $dbr = wfGetDB( DB_MASTER ); + $res = $dbr->select( + 'user_system_gift', + array( + 'DISTINCT sg_user_name', 'sg_user_id', 'sg_gift_id', + 'sg_date' + ), + array( + "sg_gift_id = {$gift['gift_id']}", + "sg_user_name <> '" . $dbr->strencode( $gift['user_name'] ) . "'" + ), + __METHOD__, + array( + 'GROUP BY' => 'sg_user_name', + 'ORDER BY' => 'sg_date DESC', + 'OFFSET' => 0, + 'LIMIT' => 6 + ) + ); $output .= $wgOut->setPageTitle( wfMsg( 'ga-gift-title', $gift['user_name'], $gift['name'] ) ); - $output .= '<div class="back-links"> - ' . wfMsg( 'ga-back-link', Title::makeTitle( NS_USER, $gift['user_name'] )->escapeFullURL(), $gift['user_name'] ) . ' - </div>'; + $output .= '<div class="back-links">' + . wfMsg( 'ga-back-link', Title::makeTitle( NS_USER, $gift['user_name'] )->escapeFullURL(), $gift['user_name'] ) . + '</div>'; $message = $wgOut->parse( trim( $gift['description'] ), false ); $output .= '<div class="ga-description-container">'; - $gift_image = "<img src=\"{$wgUploadPath}/awards/" . SystemGifts::getGiftImage( $gift['gift_id'], 'l' ) . "\" border=\"0\" alt=\"\"/>"; + $gift_image = "<img src=\"{$wgUploadPath}/awards/" . SystemGifts::getGiftImage( $gift['gift_id'], 'l' ) . '" border="0" alt=""/>'; - $output .= "<div class=\"ga-description\"> + $output .= "<div class=\"ga-description\"> {$gift_image} - <div class=\"ga-name\">{$gift["name"]}</div> - <div class=\"ga-timestamp\">({$gift["timestamp"]})</div> + <div class=\"ga-name\">{$gift['name']}</div> + <div class=\"ga-timestamp\">({$gift['timestamp']})</div> <div class=\"ga-description-message\">\"{$message}\"</div>"; - $output .= '<div class="cleared"></div> + $output .= '<div class="cleared"></div> </div>'; - $output .= '<div class="ga-recent"> - <div class="ga-recent-title">' . wfMsg( 'ga-recent-recipients-award' ) . '</div> - <div class="ga-gift-count">' . wfMsgExt( 'ga-gift-given-count', 'parsemag', $gift['gift_count'] ) . '</div>'; + $output .= '<div class="ga-recent"> + <div class="ga-recent-title">' + . wfMsg( 'ga-recent-recipients-award' ) . + '</div> + <div class="ga-gift-count">' + . wfMsgExt( 'ga-gift-given-count', 'parsemag', $gift['gift_count'] ) . + '</div>'; - while ( $row = $dbr->fetchObject( $res ) ) { + foreach ( $res as $row ) { + $user_to_id = $row->sg_user_id; + $avatar = new wAvatar( $user_to_id, 'ml' ); + $user_name_link = Title::makeTitle( NS_USER, $row->sg_user_name ); - $user_to_id = $row->sg_user_id; - $avatar = new wAvatar( $user_to_id, 'ml' ); - $user_name_link = Title::makeTitle( NS_USER, $row->sg_user_name ); + $output .= '<a href="' . $user_name_link->escapeFullURL() . "\"> + {$avatar->getAvatarURL()} + </a>"; + } - $output .= "<a href=\"" . $user_name_link->escapeFullURL() . "\"> - {$avatar->getAvatarURL()} - </a>"; - - } - - $output .= '<div class="cleared"></div> + $output .= '<div class="cleared"></div> </div> </div>'; Modified: trunk/extensions/SocialProfile/SystemGifts/SystemGiftsClass.php =================================================================== --- trunk/extensions/SocialProfile/SystemGifts/SystemGiftsClass.php 2010-02-28 21:16:38 UTC (rev 63097) +++ trunk/extensions/SocialProfile/SystemGifts/SystemGiftsClass.php 2010-02-28 21:22:06 UTC (rev 63098) @@ -33,6 +33,10 @@ } + /** + * Adds awards for all registered users + * Special:PopulateAwards calls this function + */ public function update_system_gifts() { global $wgOut, $wgMemc; @@ -40,7 +44,8 @@ $stats = new UserStatsTrack( 1, '' ); $this->categories = array_flip( $this->categories ); - $res = $dbw->select( 'system_gift', + $res = $dbw->select( + 'system_gift', array( 'gift_id', 'gift_category', 'gift_threshold', 'gift_name' ), array(), __METHOD__, @@ -48,26 +53,27 @@ ); $x = 1; - while ( $row = $dbw->fetchObject( $res ) ) { - + foreach ( $res as $row ) { if ( $row->gift_category ) { - $res2 = $dbw->select( 'user_stats', + $res2 = $dbw->select( + 'user_stats', array( 'stats_user_id', 'stats_user_name' ), array( $stats->stats_fields[$this->categories[$row->gift_category]] . " >= {$row->gift_threshold}", 'stats_user_id<>0' ), __METHOD__ ); - while ( $row2 = $dbw->fetchObject( $res2 ) ) { + foreach ( $res2 as $row2 ) { if ( $this->doesUserHaveGift( $row2->stats_user_id, $row->gift_id ) == false ) { - - $dbw->insert( 'user_system_gift', + $dbw->insert( + 'user_system_gift', array( 'sg_gift_id' => $row->gift_id, 'sg_user_id' => $row2->stats_user_id, 'sg_user_name' => $row2->stats_user_name, 'sg_status' => 0, - 'sg_date' => date( "Y-m-d H:i:s", time() - ( 60 * 60 * 24 * 3 ) ), - ), __METHOD__ + 'sg_date' => date( 'Y-m-d H:i:s', time() - ( 60 * 60 * 24 * 3 ) ), + ), + __METHOD__ ); $sg_key = wfMemcKey( 'user', 'profile', 'system_gifts', "{$row2->stats_user_id}" ); @@ -84,7 +90,12 @@ public function doesUserHaveGift( $user_id, $gift_id ) { $dbr = wfGetDB( DB_SLAVE ); - $s = $dbr->selectRow( 'user_system_gift', array( 'sg_gift_id' ), array( 'sg_gift_id' => $gift_id, 'sg_user_id' => $user_id ), __METHOD__ ); + $s = $dbr->selectRow( + 'user_system_gift', + array( 'sg_gift_id' ), + array( 'sg_gift_id' => $gift_id, 'sg_user_id' => $user_id ), + __METHOD__ + ); if ( $s === false ) { return false; } else { @@ -92,37 +103,63 @@ } } - public function addGift( $gift_name, $gift_description, $gift_category, $gift_threshold ) { + /** + * Adds a new system gift to the database + * @param $name Mixed: gift name + * @param $description Mixed: gift description + * @param $category + * @param $threshold + */ + public function addGift( $name, $description, $category, $threshold ) { $dbw = wfGetDB( DB_MASTER ); - $dbw->insert( 'system_gift', + $dbw->insert( + 'system_gift', array( - 'gift_name' => $gift_name, - 'gift_description' => $gift_description, - 'gift_category' => $gift_category, - 'gift_threshold' => $gift_threshold, - 'gift_createdate' => date( "Y-m-d H:i:s" ), - ), __METHOD__ + 'gift_name' => $name, + 'gift_description' => $description, + 'gift_category' => $category, + 'gift_threshold' => $threshold, + 'gift_createdate' => date( 'Y-m-d H:i:s' ), + ), + __METHOD__ ); return $dbw->insertId(); } - public function updateGift( $id, $gift_name, $gift_description, $gift_category, $gift_threshold ) { + /** + * Updates data for a system gift + * @param $id Integer: system gift unique ID number + * @param $name Mixed: gift name + * @param $description Mixed: gift description + * @param $category + * @param $threshold + */ + public function updateGift( $id, $name, $description, $category, $threshold ) { $dbw = wfGetDB( DB_MASTER ); - $dbw->update( 'system_gift', - array( /* SET */ - 'gift_name' => $gift_name, - 'gift_description' => $gift_description, - 'gift_category' => $gift_category, - 'gift_threshold' => $gift_threshold, - ), array( /* WHERE */ - 'gift_id' => $id - ), __METHOD__ + $dbw->update( + 'system_gift', + /* SET */array( + 'gift_name' => $name, + 'gift_description' => $description, + 'gift_category' => $category, + 'gift_threshold' => $threshold, + ), + /* WHERE */array( 'gift_id' => $id ), + __METHOD__ ); } public function doesGiftExistForThreshold( $category, $threshold ) { $dbr = wfGetDB( DB_SLAVE ); - $s = $dbr->selectRow( 'system_gift', array( 'gift_id' ), array( 'gift_category' => $this->categories[$category], 'gift_threshold' => $threshold ), __METHOD__ ); + $s = $dbr->selectRow( + 'system_gift', + array( 'gift_id' ), + array( + 'gift_category' => $this->categories[$category], + 'gift_threshold' => $threshold + ), + __METHOD__ + ); if ( $s === false ) { return false; } else { @@ -130,10 +167,17 @@ } } + /** + * Fetches the system gift with the ID $id from the database + * @param $id Integer: ID number of the system gift to be fetched + */ static function getGift( $id ) { $dbr = wfGetDB( DB_SLAVE ); $res = $dbr->select( 'system_gift', - array( 'gift_id', 'gift_name', 'gift_description', 'gift_category', 'gift_threshold', 'gift_given_count' ), + array( + 'gift_id', 'gift_name', 'gift_description', 'gift_category', + 'gift_threshold', 'gift_given_count' + ), array( 'gift_id' => $id ), __METHOD__, array( 'LIMIT' => 1 ) @@ -150,37 +194,50 @@ return $gift; } + /** + * Gets the associated image for a system gift + * @param $id Integer: system gift ID number + * @param $size String: image size (s, m, ml or l) + */ static function getGiftImage( $id, $size ) { global $wgUploadDirectory; - $files = glob( $wgUploadDirectory . '/awards/sg_' . $id . '_' . $size . "*" ); + $files = glob( $wgUploadDirectory . '/awards/sg_' . $id . '_' . $size . '*' ); if ( !empty( $files[0] ) ) { $img = basename( $files[0] ); } else { $img = 'default_' . $size . '.gif'; } - return $img . "?r=" . rand(); + return $img . '?r=' . rand(); } static function getGiftList( $limit = 0, $page = 0 ) { - global $wgDBprefix; $dbr = wfGetDB( DB_SLAVE ); - $limit_sql = ''; // Prevent E_NOTICE + $limitvalue = 0; if ( $limit > 0 ) { - $limitvalue = 0; - if ( $page ) $limitvalue = $page * $limit - ( $limit ); - $limit_sql = " LIMIT {$limitvalue},{$limit} "; + if ( $page ) { + $limitvalue = $page * $limit - ( $limit ); + } } - $sql = "SELECT gift_id,gift_createdate,gift_name,gift_description,gift_category, gift_threshold, gift_given_count - FROM " . $wgDBprefix . "system_gift - ORDER BY gift_createdate DESC - {$limit_sql}"; + $res = $dbr->select( + 'system_gift', + array( + 'gift_id', 'gift_createdate', 'gift_name', 'gift_description', + 'gift_category', 'gift_threshold', 'gift_given_count' + ), + array(), + __METHOD__, + array( + 'ORDER BY' => 'gift_createdate DESC', + 'LIMIT' => $limit, + 'OFFSET' => $limitvalue + ) + ); - $res = $dbr->query( $sql ); - $gifts = ''; - while ( $row = $dbr->fetchObject( $res ) ) { + $gifts = array(); + foreach ( $res as $row ) { $gifts[] = array( 'id' => $row->gift_id, 'timestamp' => ( $row->gift_createdate ), @@ -194,11 +251,21 @@ return $gifts; } + /** + * Gets the amount of available system gifts from the database. + * @return integer + */ static function getGiftCount() { $dbr = wfGetDB( DB_SLAVE ); $gift_count = 0; - $s = $dbr->selectRow( 'system_gift', array( 'count(*) AS count' ), __METHOD__ ); - if ( $s !== false ) $gift_count = $s->count; + $s = $dbr->selectRow( + 'system_gift', + array( 'COUNT(*) AS count' ), + __METHOD__ + ); + if ( $s !== false ) { + $gift_count = $s->count; + } return $gift_count; } } _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs