Index: trunk/extensions/Favorites/FavParser.php |
— | — | @@ -4,14 +4,28 @@ |
5 | 5 | |
6 | 6 | function wfSpecialFavoritelist($argv, $parser) { |
7 | 7 | |
8 | | - global $wgUser, $wgOut, $wgLang, $wgRequest; |
| 8 | + global $wgUser, $wgOut, $wgLang, $wgRequest, $wgArticle; |
9 | 9 | global $wgRCShowFavoritingUsers, $wgEnotifFavoritelist, $wgShowUpdatedMarker; |
10 | 10 | $output = ''; |
11 | | - |
| 11 | + |
12 | 12 | $skin = $wgUser->getSkin(); |
13 | 13 | $specialTitle = SpecialPage::getTitleFor( 'Favoritelist' ); |
14 | 14 | //$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 | + |
16 | 30 | # Anons don't get a favoritelist |
17 | 31 | if( $wgUser->isAnon() ) { |
18 | 32 | //$wgOut->setPageTitle( wfMsg( 'favoritenologin' ) ); |
— | — | @@ -22,16 +36,47 @@ |
23 | 37 | array( 'returnto' => $specialTitle->getPrefixedText() ) |
24 | 38 | ); |
25 | 39 | $output = wfMsgHtml( 'favoritelistanontext', $llink ) ; |
26 | | - |
27 | | - |
28 | 40 | return $output ; |
29 | 41 | |
30 | 42 | } |
31 | 43 | |
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=''; |
33 | 78 | if ( array_key_exists('editlink', $argv) && $argv['editlink']) { |
34 | 79 | # Add an edit link if you want it: |
35 | | - $output .= "<div id='contentSub'><br>" . |
| 80 | + $output = "<div id='contentSub'><br>" . |
36 | 81 | $skin->link( |
37 | 82 | SpecialPage::getTitleFor( 'Favoritelist', 'edit' ), |
38 | 83 | wfMsgHtml( "favoritelisttools-edit" ), |
— | — | @@ -40,17 +85,33 @@ |
41 | 86 | array( 'known', 'noclasses' ) |
42 | 87 | ) . "</div>"; |
43 | 88 | } |
44 | | - |
45 | | - return $output ; |
| 89 | + return $output; |
46 | 90 | } |
47 | | - |
48 | 91 | |
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 ); |
55 | 116 | } |
56 | 117 | |
57 | 118 | |
— | — | @@ -62,13 +123,40 @@ |
63 | 124 | */ |
64 | 125 | private function countFavoritelist( $user ) { |
65 | 126 | $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__ ); |
67 | 128 | $row = $dbr->fetchObject( $res ); |
68 | | - return ceil( $row->count); |
| 129 | + return ceil( $row->count); // Paranoia |
69 | 130 | } |
70 | 131 | |
| 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 | + } |
71 | 160 | |
72 | | - |
73 | 161 | /** |
74 | 162 | * Get a list of titles on a user's favoritelist, excluding talk pages, |
75 | 163 | * and return as a two-dimensional array with namespace, title and |
— | — | @@ -106,24 +194,44 @@ |
107 | 195 | return $titles; |
108 | 196 | } |
109 | 197 | |
| 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 | + } |
110 | 215 | |
111 | 216 | /** |
| 217 | + * Remove all titles from a user's favoritelist |
| 218 | + * |
| 219 | + * @param $user User |
| 220 | +// */ |
| 221 | + |
| 222 | + |
| 223 | + /** |
112 | 224 | * Show the standard favoritelist |
113 | 225 | * |
114 | 226 | * @param $output OutputPage |
115 | 227 | * @param $user User |
116 | 228 | */ |
117 | 229 | 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 ) { |
122 | 233 | $form = $this->buildRemoveList( $user, $skin ); |
123 | 234 | $output .= $form ; |
124 | 235 | return $output; |
125 | | - } else { |
126 | | - $output = wfmsg('nofavoritelist'); |
127 | | - return $output; |
128 | 236 | } |
129 | 237 | } |
130 | 238 | |
— | — | @@ -167,3 +275,25 @@ |
168 | 276 | } |
169 | 277 | |
170 | 278 | } |
| 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 @@ |
23 | 23 | 'name' => 'Favorites', |
24 | 24 | 'author' => 'Jeremy Lemley', |
25 | 25 | 'descriptionmsg' => 'favorites-desc', |
26 | | - 'version' => '0.1.4', |
| 26 | + 'version' => '0.2.0', |
27 | 27 | 'url' => "http://www.mediawiki.org/wiki/Extension:Favorites", |
28 | 28 | ); |
29 | 29 | |