r94002 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94001‎ | r94002 | r94003 >
Date:02:26, 6 August 2011
Author:jlemley
Status:deferred
Tags:
Comment:
Added userpage tag parameter.
Modified paths:
  • /trunk/extensions/Favorites/FavParser.php (modified) (history)
  • /trunk/extensions/Favorites/Favorites.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Favorites/FavParser.php
@@ -4,14 +4,28 @@
55
66 function wfSpecialFavoritelist($argv, $parser) {
77
8 - global $wgUser, $wgOut, $wgLang, $wgRequest;
 8+ global $wgUser, $wgOut, $wgLang, $wgRequest, $wgArticle;
99 global $wgRCShowFavoritingUsers, $wgEnotifFavoritelist, $wgShowUpdatedMarker;
1010 $output = '';
11 -
 11+
1212 $skin = $wgUser->getSkin();
1313 $specialTitle = SpecialPage::getTitleFor( 'Favoritelist' );
1414 //$wgOut->setRobotPolicy( 'noindex,nofollow' );
15 -
 15+
 16+ $this->mTitle = $wgArticle->mTitle;
 17+
 18+ if ($this->mTitle->getNamespace() == NS_USER && array_key_exists('userpage', $argv) && $argv['userpage']) {
 19+ $parts = explode( '/', $this->mTitle->getText() );
 20+ $rootPart = $parts[0];
 21+ $user = User::newFromName( $rootPart, true /* don't allow IP users*/ );
 22+ //echo "Userpage: $user";
 23+ $output = $this->viewFavList($user, $output, $wgRequest, $argv);
 24+ $output .= $this->editlink($argv, $skin);
 25+ return $output ;
 26+ } else {
 27+ $user = $wgUser;
 28+ }
 29+
1630 # Anons don't get a favoritelist
1731 if( $wgUser->isAnon() ) {
1832 //$wgOut->setPageTitle( wfMsg( 'favoritenologin' ) );
@@ -22,16 +36,47 @@
2337 array( 'returnto' => $specialTitle->getPrefixedText() )
2438 );
2539 $output = wfMsgHtml( 'favoritelistanontext', $llink ) ;
26 -
27 -
2840 return $output ;
2941
3042 }
3143
32 - $output = $this->viewFavList($wgUser, $output, $wgRequest);
 44+ $output = $this->viewFavList($user, $output, $wgRequest, $argv);
 45+ $output .= $this->editlink($argv, $skin);
 46+
 47+ return $output ;
 48+ }
 49+
 50+
 51+ private function viewFavList ($user, $output, $request, $argv) {
 52+ global $wgOut, $wgLang, $wgRequest;
 53+ $output = $this->showNormalForm( $output, $user );
 54+ $uid=$user->getId();
 55+ $dbr = wfGetDB( DB_SLAVE, 'favoritelist' );
 56+
 57+ $favoritelistCount = $dbr->selectField( 'favoritelist', 'COUNT(*)',
 58+ array( 'fl_user' => $uid ), __METHOD__ );
 59+ $nitems = floor($favoritelistCount);
 60+
 61+ if( $nitems == 0 ) {
 62+ $output = wfmsg('nofavoritelist');
 63+
 64+ }
 65+
 66+ return $output;
 67+ }
 68+
 69+ /**
 70+ * Does the user want to display an editlink?
 71+ *
 72+ * @param $argv Array of values from the parser
 73+ * $param $skin User skin
 74+ * @return Output
 75+ */
 76+ private function editlink($argv, $skin) {
 77+ $output='';
3378 if ( array_key_exists('editlink', $argv) && $argv['editlink']) {
3479 # Add an edit link if you want it:
35 - $output .= "<div id='contentSub'><br>" .
 80+ $output = "<div id='contentSub'><br>" .
3681 $skin->link(
3782 SpecialPage::getTitleFor( 'Favoritelist', 'edit' ),
3883 wfMsgHtml( "favoritelisttools-edit" ),
@@ -40,17 +85,33 @@
4186 array( 'known', 'noclasses' )
4287 ) . "</div>";
4388 }
44 -
45 - return $output ;
 89+ return $output;
4690 }
47 -
4891
49 - private function viewFavList ($user, $output, $request) {
50 - global $wgUser, $wgOut, $wgLang, $wgRequest;
51 - $uid = $wgUser->getId();
52 - $output = $this->showNormalForm( $output, $user );
53 -
54 - return $output;
 92+
 93+ /**
 94+ * Extract a list of titles from a blob of text, returning
 95+ * (prefixed) strings; unfavoritable titles are ignored
 96+ *
 97+ * @param $list mixed
 98+ * @return array
 99+ */
 100+ private function extractTitles( $list ) {
 101+ $titles = array();
 102+ if( !is_array( $list ) ) {
 103+ $list = explode( "\n", trim( $list ) );
 104+ if( !is_array( $list ) )
 105+ return array();
 106+ }
 107+ foreach( $list as $text ) {
 108+ $text = trim( $text );
 109+ if( strlen( $text ) > 0 ) {
 110+ $title = Title::newFromText( $text );
 111+ //if( $title instanceof Title && $title->isFavoritable() )
 112+ $titles[] = $title->getPrefixedText();
 113+ }
 114+ }
 115+ return array_unique( $titles );
55116 }
56117
57118
@@ -62,13 +123,40 @@
63124 */
64125 private function countFavoritelist( $user ) {
65126 $dbr = wfGetDB( DB_MASTER );
66 - $res = $dbr->select( 'favoritelist', 'COUNT(fl_user) AS count', array( 'fl_user' => $user->getId() ), __METHOD__ );
 127+ $res = $dbr->select( 'favoritelist', 'COUNT(*) AS count', array( 'fl_user' => $user->getId() ), __METHOD__ );
67128 $row = $dbr->fetchObject( $res );
68 - return ceil( $row->count);
 129+ return ceil( $row->count); // Paranoia
69130 }
70131
 132+ /**
 133+ * Prepare a list of titles on a user's favoritelist (excluding talk pages)
 134+ * and return an array of (prefixed) strings
 135+ *
 136+ * @param $user User
 137+ * @return array
 138+ */
 139+ private function getFavoritelist( $user ) {
 140+ $list = array();
 141+ $dbr = wfGetDB( DB_MASTER );
 142+ $res = $dbr->select(
 143+ 'favoritelist',
 144+ '*',
 145+ array(
 146+ 'fl_user' => $user->getId(),
 147+ ),
 148+ __METHOD__
 149+ );
 150+ if( $res->numRows() > 0 ) {
 151+ while( $row = $res->fetchObject() ) {
 152+ $title = Title::makeTitleSafe( $row->fl_namespace, $row->fl_title );
 153+ if( $title instanceof Title && !$title->isTalkPage() )
 154+ $list[] = $title->getPrefixedText();
 155+ }
 156+ $res->free();
 157+ }
 158+ return $list;
 159+ }
71160
72 -
73161 /**
74162 * Get a list of titles on a user's favoritelist, excluding talk pages,
75163 * and return as a two-dimensional array with namespace, title and
@@ -106,24 +194,44 @@
107195 return $titles;
108196 }
109197
 198+ /**
 199+ * Show a message indicating the number of items on the user's favoritelist,
 200+ * and return this count for additional checking
 201+ *
 202+ * @param $output OutputPage
 203+ * @param $user User
 204+ * @return int
 205+ */
 206+ private function showItemCount( $output, $user ) {
 207+ if( ( $count = $this->countFavoritelist( $user ) ) > 0 ) {
 208+ //$output->addHTML( wfMsgExt( 'favoritelistedit-numitems', 'parse',
 209+ // $GLOBALS['wgLang']->formatNum( $count ) ) );
 210+ } else {
 211+ //$output->addHTML( wfMsg( 'favoritelistedit-noitems', 'parse' ) );
 212+ }
 213+ return $count;
 214+ }
110215
111216 /**
 217+ * Remove all titles from a user's favoritelist
 218+ *
 219+ * @param $user User
 220+// */
 221+
 222+
 223+ /**
112224 * Show the standard favoritelist
113225 *
114226 * @param $output OutputPage
115227 * @param $user User
116228 */
117229 private function showNormalForm( $output, $user ) {
118 - global $wgUser, $wgOut;
119 - $skin = $wgUser->getSkin();
120 -
121 - if ( $this->countFavoritelist($user ) > 0 ) {
 230+ global $wgOut;
 231+ $skin = $user->getSkin();
 232+ if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
122233 $form = $this->buildRemoveList( $user, $skin );
123234 $output .= $form ;
124235 return $output;
125 - } else {
126 - $output = wfmsg('nofavoritelist');
127 - return $output;
128236 }
129237 }
130238
@@ -167,3 +275,25 @@
168276 }
169277
170278 }
 279+/**
 280+ * Count the number of items on a user's favoritelist
 281+ *
 282+ * @param $talk Include talk pages
 283+ * @return integer
 284+ */
 285+function flCountItems( &$user, $talk = true ) {
 286+ $dbr = wfGetDB( DB_SLAVE, 'favoritelist' );
 287+
 288+ # Fetch the raw count
 289+ $res = $dbr->select( 'favoritelist', 'COUNT(*) AS count',
 290+ array( 'fl_user' => $user->mId ), 'flCountItems' );
 291+ $row = $dbr->fetchObject( $res );
 292+ $count = $row->count;
 293+ $dbr->freeResult( $res );
 294+
 295+ # Halve to remove talk pages if needed
 296+ if( !$talk )
 297+ $count = floor( $count);
 298+
 299+ return( $count );
 300+}
Index: trunk/extensions/Favorites/Favorites.php
@@ -22,7 +22,7 @@
2323 'name' => 'Favorites',
2424 'author' => 'Jeremy Lemley',
2525 'descriptionmsg' => 'favorites-desc',
26 - 'version' => '0.1.4',
 26+ 'version' => '0.2.0',
2727 'url' => "http://www.mediawiki.org/wiki/Extension:Favorites",
2828 );
2929

Status & tagging log