r94020 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94019‎ | r94020 | r94021 >
Date:11:37, 6 August 2011
Author:jlemley
Status:deferred
Tags:
Comment:
Code cleanup. Added userlist tag argument.
Modified paths:
  • /branches/REL1_17/extensions/Favorites/FavParser.php (modified) (history)
  • /branches/REL1_17/extensions/Favorites/Favorites.php (modified) (history)
  • /branches/REL1_17/extensions/Favorites/SpecialFavoritelist.php (modified) (history)

Diff [purge]

Index: branches/REL1_17/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,34 +36,21 @@
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);
33 - if ( array_key_exists('editlink', $argv) && $argv['editlink']) {
34 - # Add an edit link if you want it:
35 - $output .= "<div id='contentSub'><br>" .
36 - $skin->link(
37 - SpecialPage::getTitleFor( 'Favoritelist', 'edit' ),
38 - wfMsgHtml( "favoritelisttools-edit" ),
39 - array(),
40 - array(),
41 - array( 'known', 'noclasses' )
42 - ) . "</div>";
43 - }
 44+ $output = $this->viewFavList($user, $output, $wgRequest, $argv);
 45+ $output .= $this->editlink($argv, $skin);
4446
4547 return $output ;
4648 }
4749
4850
49 - private function viewFavList ($user, $output, $request) {
50 - global $wgUser, $wgOut, $wgLang, $wgRequest;
51 - $uid = $wgUser->getId();
 51+ private function viewFavList ($user, $output, $request, $argv) {
 52+ global $wgOut, $wgLang, $wgRequest;
5253 $output = $this->showNormalForm( $output, $user );
53 -
 54+ $uid=$user->getId();
5455 $dbr = wfGetDB( DB_SLAVE, 'favoritelist' );
5556
5657 $favoritelistCount = $dbr->selectField( 'favoritelist', 'COUNT(*)',
@@ -60,11 +61,35 @@
6162 $output = wfmsg('nofavoritelist');
6263
6364 }
 65+
6466 return $output;
6567 }
6668
67 -
6869 /**
 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='';
 78+ if ( array_key_exists('editlink', $argv) && $argv['editlink']) {
 79+ # Add an edit link if you want it:
 80+ $output = "<div id='contentSub'><br>" .
 81+ $skin->link(
 82+ SpecialPage::getTitleFor( 'Favoritelist', 'edit' ),
 83+ wfMsgHtml( "favoritelisttools-edit" ),
 84+ array(),
 85+ array(),
 86+ array( 'known', 'noclasses' )
 87+ ) . "</div>";
 88+ }
 89+ return $output;
 90+ }
 91+
 92+
 93+ /**
6994 * Extract a list of titles from a blob of text, returning
7095 * (prefixed) strings; unfavoritable titles are ignored
7196 *
@@ -201,8 +226,8 @@
202227 * @param $user User
203228 */
204229 private function showNormalForm( $output, $user ) {
205 - global $wgUser, $wgOut;
206 - $skin = $wgUser->getSkin();
 230+ global $wgOut;
 231+ $skin = $user->getSkin();
207232 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
208233 $form = $this->buildRemoveList( $user, $skin );
209234 $output .= $form ;
Index: branches/REL1_17/extensions/Favorites/SpecialFavoritelist.php
@@ -105,7 +105,7 @@
106106 $dbr = wfGetDB( DB_SLAVE, 'favoritelist' );
107107 // $recentchanges = $dbr->tableName( 'recentchanges' );
108108
109 - $favoritelistCount = $dbr->selectField( 'favoritelist', 'COUNT(*)',
 109+ $favoritelistCount = $dbr->selectField( 'favoritelist', 'COUNT(fl_user)',
110110 array( 'fl_user' => $uid ), __METHOD__ );
111111 // Adjust for page X, talk:page X, which are both stored separately,
112112 // but treated together
@@ -199,7 +199,7 @@
200200 */
201201 private function countFavoritelist( $user ) {
202202 $dbr = wfGetDB( DB_MASTER );
203 - $res = $dbr->select( 'favoritelist', 'COUNT(*) AS count', array( 'fl_user' => $user->getId() ), __METHOD__ );
 203+ $res = $dbr->select( 'favoritelist', 'COUNT(fl_user) AS count', array( 'fl_user' => $user->getId() ), __METHOD__ );
204204 $row = $dbr->fetchObject( $res );
205205 return ceil( $row->count ); // Paranoia
206206 }
@@ -216,8 +216,11 @@
217217 $dbr = wfGetDB( DB_MASTER );
218218 $res = $dbr->select(
219219 'favoritelist',
220 - '*',
221220 array(
 221+ 'fl_namespace',
 222+ 'fl_title'
 223+ ),
 224+ array(
222225 'fl_user' => $user->getId(),
223226 ),
224227 __METHOD__
@@ -270,35 +273,8 @@
271274 return $titles;
272275 }
273276
274 - /**
275 - * Show a message indicating the number of items on the user's favoritelist,
276 - * and return this count for additional checking
277 - *
278 - * @param $output OutputPage
279 - * @param $user User
280 - * @return int
281 - */
282 - private function showItemCount( $output, $user ) {
283 - if( ( $count = $this->countFavoritelist( $user ) ) > 0 ) {
284 - //$output->addHTML( wfMsgExt( 'favoritelistedit-numitems', 'parse',
285 - // $GLOBALS['wgLang']->formatNum( $count ) ) );
286 - } else {
287 - //$output->addHTML( wfMsgExt( 'favoritelistedit-noitems', 'parse' ) );
288 - }
289 - return $count;
290 - }
291277
292 - /**
293 - * Remove all titles from a user's favoritelist
294 - *
295 - * @param $user User
296 - */
297 - private function clearFavoritelist( $user ) {
298 - $dbw = wfGetDB( DB_MASTER );
299 - $dbw->delete( 'favoritelist', array( 'fl_user' => $user->getId() ), __METHOD__ );
300 - }
301 -
302 - /**
 278+/**
303279 * Add a list of titles to a user's favoritelist
304280 *
305281 * $titles can be an array of strings or Title objects; the former
@@ -316,12 +292,6 @@
317293 if( $title instanceof Title ) {
318294 $rows[] = array(
319295 'fl_user' => $user->getId(),
320 - 'fl_namespace' => ( $title->getNamespace() & ~1 ),
321 - 'fl_title' => $title->getDBkey(),
322 - 'fl_notificationtimestamp' => null,
323 - );
324 - $rows[] = array(
325 - 'fl_user' => $user->getId(),
326296 'fl_namespace' => ( $title->getNamespace() | 1 ),
327297 'fl_title' => $title->getDBkey(),
328298 'fl_notificationtimestamp' => null,
@@ -350,15 +320,6 @@
351321 'favoritelist',
352322 array(
353323 'fl_user' => $user->getId(),
354 - 'fl_namespace' => ( $title->getNamespace() & ~1 ),
355 - 'fl_title' => $title->getDBkey(),
356 - ),
357 - __METHOD__
358 - );
359 - $dbw->delete(
360 - 'favoritelist',
361 - array(
362 - 'fl_user' => $user->getId(),
363324 'fl_namespace' => ( $title->getNamespace() | 1 ),
364325 'fl_title' => $title->getDBkey(),
365326 ),
@@ -378,7 +339,7 @@
379340 */
380341 private function showNormalForm( $output, $user ) {
381342 global $wgUser;
382 - if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
 343+ if( ( $count = $this->countFavoritelist($user ) ) > 0 ) {
383344 $self = SpecialPage::getTitleFor( 'Favoritelist' );
384345 $form = Xml::openElement( 'form', array( 'method' => 'post',
385346 'action' => $self->getLocalUrl( array( 'action' => 'edit' ) ) ) );
@@ -494,7 +455,7 @@
495456 */
496457 public function showRawForm( $output, $user ) {
497458 global $wgUser;
498 - $this->showItemCount( $output, $user );
 459+ $this->countFavoritelist( $user );
499460 $self = SpecialPage::getTitleFor( 'Favoritelist' );
500461 $form = Xml::openElement( 'form', array( 'method' => 'post',
501462 'action' => $self->getLocalUrl( array( 'action' => 'raw' ) ) ) );
Index: branches/REL1_17/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