http://www.mediawiki.org/wiki/Special:Code/MediaWiki/93951
Revision: 93951 Author: jlemley Date: 2011-08-05 02:33:56 +0000 (Fri, 05 Aug 2011) Log Message: ----------- Cleaned up some code for efficiency. Modified Paths: -------------- trunk/extensions/Favorites/FavParser.php trunk/extensions/Favorites/SpecialFavoritelist.php Modified: trunk/extensions/Favorites/FavParser.php =================================================================== --- trunk/extensions/Favorites/FavParser.php 2011-08-05 02:31:11 UTC (rev 93950) +++ trunk/extensions/Favorites/FavParser.php 2011-08-05 02:33:56 UTC (rev 93951) @@ -49,48 +49,12 @@ global $wgUser, $wgOut, $wgLang, $wgRequest; $uid = $wgUser->getId(); $output = $this->showNormalForm( $output, $user ); - - $dbr = wfGetDB( DB_SLAVE, 'favoritelist' ); - - $favoritelistCount = $dbr->selectField( 'favoritelist', 'COUNT(*)', - array( 'fl_user' => $uid ), __METHOD__ ); - $nitems = floor($favoritelistCount); - - if( $nitems == 0 ) { - $output = wfmsg('nofavoritelist'); - - } + return $output; } /** - * Extract a list of titles from a blob of text, returning - * (prefixed) strings; unfavoritable titles are ignored - * - * @param $list mixed - * @return array - */ - private function extractTitles( $list ) { - $titles = array(); - if( !is_array( $list ) ) { - $list = explode( "\n", trim( $list ) ); - if( !is_array( $list ) ) - return array(); - } - foreach( $list as $text ) { - $text = trim( $text ); - if( strlen( $text ) > 0 ) { - $title = Title::newFromText( $text ); - //if( $title instanceof Title && $title->isFavoritable() ) - $titles[] = $title->getPrefixedText(); - } - } - return array_unique( $titles ); - } - - - /** * Count the number of titles on a user's favoritelist, excluding talk pages * * @param $user User @@ -98,40 +62,13 @@ */ private function countFavoritelist( $user ) { $dbr = wfGetDB( DB_MASTER ); - $res = $dbr->select( 'favoritelist', 'COUNT(*) AS count', array( 'fl_user' => $user->getId() ), __METHOD__ ); + $res = $dbr->select( 'favoritelist', 'COUNT(fl_user) AS count', array( 'fl_user' => $user->getId() ), __METHOD__ ); $row = $dbr->fetchObject( $res ); - return ceil( $row->count); // Paranoia + return ceil( $row->count); } - /** - * Prepare a list of titles on a user's favoritelist (excluding talk pages) - * and return an array of (prefixed) strings - * - * @param $user User - * @return array - */ - private function getFavoritelist( $user ) { - $list = array(); - $dbr = wfGetDB( DB_MASTER ); - $res = $dbr->select( - 'favoritelist', - '*', - array( - 'fl_user' => $user->getId(), - ), - __METHOD__ - ); - if( $res->numRows() > 0 ) { - while( $row = $res->fetchObject() ) { - $title = Title::makeTitleSafe( $row->fl_namespace, $row->fl_title ); - if( $title instanceof Title && !$title->isTalkPage() ) - $list[] = $title->getPrefixedText(); - } - $res->free(); - } - return $list; - } + /** * Get a list of titles on a user's favoritelist, excluding talk pages, * and return as a two-dimensional array with namespace, title and @@ -169,32 +106,8 @@ return $titles; } - /** - * Show a message indicating the number of items on the user's favoritelist, - * and return this count for additional checking - * - * @param $output OutputPage - * @param $user User - * @return int - */ - private function showItemCount( $output, $user ) { - if( ( $count = $this->countFavoritelist( $user ) ) > 0 ) { - //$output->addHTML( wfMsgExt( 'favoritelistedit-numitems', 'parse', - // $GLOBALS['wgLang']->formatNum( $count ) ) ); - } else { - //$output->addHTML( wfMsg( 'favoritelistedit-noitems', 'parse' ) ); - } - return $count; - } /** - * Remove all titles from a user's favoritelist - * - * @param $user User -// */ - - - /** * Show the standard favoritelist * * @param $output OutputPage @@ -203,10 +116,14 @@ private function showNormalForm( $output, $user ) { global $wgUser, $wgOut; $skin = $wgUser->getSkin(); - if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) { + + if ( $this->countFavoritelist($user ) > 0 ) { $form = $this->buildRemoveList( $user, $skin ); $output .= $form ; return $output; + } else { + $output = wfmsg('nofavoritelist'); + return $output; } } @@ -250,25 +167,3 @@ } } -/** - * Count the number of items on a user's favoritelist - * - * @param $talk Include talk pages - * @return integer - */ -function flCountItems( &$user, $talk = true ) { - $dbr = wfGetDB( DB_SLAVE, 'favoritelist' ); - - # Fetch the raw count - $res = $dbr->select( 'favoritelist', 'COUNT(*) AS count', - array( 'fl_user' => $user->mId ), 'flCountItems' ); - $row = $dbr->fetchObject( $res ); - $count = $row->count; - $dbr->freeResult( $res ); - - # Halve to remove talk pages if needed - if( !$talk ) - $count = floor( $count); - - return( $count ); -} Modified: trunk/extensions/Favorites/SpecialFavoritelist.php =================================================================== --- trunk/extensions/Favorites/SpecialFavoritelist.php 2011-08-05 02:31:11 UTC (rev 93950) +++ trunk/extensions/Favorites/SpecialFavoritelist.php 2011-08-05 02:33:56 UTC (rev 93951) @@ -105,7 +105,7 @@ $dbr = wfGetDB( DB_SLAVE, 'favoritelist' ); // $recentchanges = $dbr->tableName( 'recentchanges' ); - $favoritelistCount = $dbr->selectField( 'favoritelist', 'COUNT(*)', + $favoritelistCount = $dbr->selectField( 'favoritelist', 'COUNT(fl_user)', array( 'fl_user' => $uid ), __METHOD__ ); // Adjust for page X, talk:page X, which are both stored separately, // but treated together @@ -199,7 +199,7 @@ */ private function countFavoritelist( $user ) { $dbr = wfGetDB( DB_MASTER ); - $res = $dbr->select( 'favoritelist', 'COUNT(*) AS count', array( 'fl_user' => $user->getId() ), __METHOD__ ); + $res = $dbr->select( 'favoritelist', 'COUNT(fl_user) AS count', array( 'fl_user' => $user->getId() ), __METHOD__ ); $row = $dbr->fetchObject( $res ); return ceil( $row->count ); // Paranoia } @@ -216,8 +216,11 @@ $dbr = wfGetDB( DB_MASTER ); $res = $dbr->select( 'favoritelist', - '*', array( + 'fl_namespace', + 'fl_title' + ), + array( 'fl_user' => $user->getId(), ), __METHOD__ @@ -270,35 +273,8 @@ return $titles; } - /** - * Show a message indicating the number of items on the user's favoritelist, - * and return this count for additional checking - * - * @param $output OutputPage - * @param $user User - * @return int - */ - private function showItemCount( $output, $user ) { - if( ( $count = $this->countFavoritelist( $user ) ) > 0 ) { - //$output->addHTML( wfMsgExt( 'favoritelistedit-numitems', 'parse', - // $GLOBALS['wgLang']->formatNum( $count ) ) ); - } else { - //$output->addHTML( wfMsgExt( 'favoritelistedit-noitems', 'parse' ) ); - } - return $count; - } - /** - * Remove all titles from a user's favoritelist - * - * @param $user User - */ - private function clearFavoritelist( $user ) { - $dbw = wfGetDB( DB_MASTER ); - $dbw->delete( 'favoritelist', array( 'fl_user' => $user->getId() ), __METHOD__ ); - } - - /** +/** * Add a list of titles to a user's favoritelist * * $titles can be an array of strings or Title objects; the former @@ -316,12 +292,6 @@ if( $title instanceof Title ) { $rows[] = array( 'fl_user' => $user->getId(), - 'fl_namespace' => ( $title->getNamespace() & ~1 ), - 'fl_title' => $title->getDBkey(), - 'fl_notificationtimestamp' => null, - ); - $rows[] = array( - 'fl_user' => $user->getId(), 'fl_namespace' => ( $title->getNamespace() | 1 ), 'fl_title' => $title->getDBkey(), 'fl_notificationtimestamp' => null, @@ -350,15 +320,6 @@ 'favoritelist', array( 'fl_user' => $user->getId(), - 'fl_namespace' => ( $title->getNamespace() & ~1 ), - 'fl_title' => $title->getDBkey(), - ), - __METHOD__ - ); - $dbw->delete( - 'favoritelist', - array( - 'fl_user' => $user->getId(), 'fl_namespace' => ( $title->getNamespace() | 1 ), 'fl_title' => $title->getDBkey(), ), @@ -378,7 +339,7 @@ */ private function showNormalForm( $output, $user ) { global $wgUser; - if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) { + if( ( $count = $this->countFavoritelist($user ) ) > 0 ) { $self = SpecialPage::getTitleFor( 'Favoritelist' ); $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl( array( 'action' => 'edit' ) ) ) ); @@ -494,7 +455,7 @@ */ public function showRawForm( $output, $user ) { global $wgUser; - $this->showItemCount( $output, $user ); + $this->countFavoritelist( $user ); $self = SpecialPage::getTitleFor( 'Favoritelist' ); $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl( array( 'action' => 'raw' ) ) ) ); _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs