Big commit: kill almost every freeResult() call as useless
[lhc/web/wiklou.git] / includes / specials / SpecialWatchlist.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage Watchlist
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 */
21
22 /**
23 * Constructor
24 *
25 * @param $par Parameter passed to the page
26 */
27 function wfSpecialWatchlist( $par ) {
28 global $wgUser, $wgOut, $wgLang, $wgRequest;
29 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
30
31 // Add feed links
32 $wlToken = $wgUser->getOption( 'watchlisttoken' );
33 if (!$wlToken) {
34 $wlToken = sha1( mt_rand() . microtime( true ) );
35 $wgUser->setOption( 'watchlisttoken', $wlToken );
36 $wgUser->saveSettings();
37 }
38
39 global $wgFeedClasses;
40 $apiParams = array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
41 'wlowner' => $wgUser->getName(), 'wltoken' => $wlToken );
42 $feedTemplate = wfScript('api').'?';
43
44 foreach( $wgFeedClasses as $format => $class ) {
45 $theseParams = $apiParams + array( 'feedformat' => $format );
46 $url = $feedTemplate . wfArrayToCGI( $theseParams );
47 $wgOut->addFeedLink( $format, $url );
48 }
49
50 $skin = $wgUser->getSkin();
51 $specialTitle = SpecialPage::getTitleFor( 'Watchlist' );
52 $wgOut->setRobotPolicy( 'noindex,nofollow' );
53
54 # Anons don't get a watchlist
55 if( $wgUser->isAnon() ) {
56 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
57 $llink = $skin->linkKnown(
58 SpecialPage::getTitleFor( 'Userlogin' ),
59 wfMsgHtml( 'loginreqlink' ),
60 array(),
61 array( 'returnto' => $specialTitle->getPrefixedText() )
62 );
63 $wgOut->addHTML( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
64 return;
65 }
66
67 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
68
69 $sub = wfMsgExt( 'watchlistfor', 'parseinline', $wgUser->getName() );
70 $sub .= '<br />' . WatchlistEditor::buildTools( $wgUser->getSkin() );
71 $wgOut->setSubtitle( $sub );
72
73 if( ( $mode = WatchlistEditor::getMode( $wgRequest, $par ) ) !== false ) {
74 $editor = new WatchlistEditor();
75 $editor->execute( $wgUser, $wgOut, $wgRequest, $mode );
76 return;
77 }
78
79 $uid = $wgUser->getId();
80 if( ($wgEnotifWatchlist || $wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) &&
81 $wgRequest->wasPosted() )
82 {
83 $wgUser->clearAllNotifications( $uid );
84 $wgOut->redirect( $specialTitle->getFullUrl() );
85 return;
86 }
87
88 $defaults = array(
89 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
90 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
91 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
92 /* bool */ 'hideAnons' => (int)$wgUser->getBoolOption( 'watchlisthideanons' ),
93 /* bool */ 'hideLiu' => (int)$wgUser->getBoolOption( 'watchlisthideliu' ),
94 /* bool */ 'hidePatrolled' => (int)$wgUser->getBoolOption( 'watchlisthidepatrolled' ),
95 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
96 /* ? */ 'namespace' => 'all',
97 /* ? */ 'invert' => false,
98 );
99
100 extract($defaults);
101
102 # Extract variables from the request, falling back to user preferences or
103 # other default values if these don't exist
104 $prefs['days'] = floatval( $wgUser->getOption( 'watchlistdays' ) );
105 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
106 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
107 $prefs['hideanons'] = $wgUser->getBoolOption( 'watchlisthideanon' );
108 $prefs['hideliu'] = $wgUser->getBoolOption( 'watchlisthideliu' );
109 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
110 $prefs['hidepatrolled' ] = $wgUser->getBoolOption( 'watchlisthidepatrolled' );
111
112 # Get query variables
113 $days = $wgRequest->getVal( 'days' , $prefs['days'] );
114 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
115 $hideBots = $wgRequest->getBool( 'hideBots' , $prefs['hidebots'] );
116 $hideAnons = $wgRequest->getBool( 'hideAnons', $prefs['hideanons'] );
117 $hideLiu = $wgRequest->getBool( 'hideLiu' , $prefs['hideliu'] );
118 $hideOwn = $wgRequest->getBool( 'hideOwn' , $prefs['hideown'] );
119 $hidePatrolled = $wgRequest->getBool( 'hidePatrolled' , $prefs['hidepatrolled'] );
120
121 # Get namespace value, if supplied, and prepare a WHERE fragment
122 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
123 $invert = $wgRequest->getIntOrNull( 'invert' );
124 if( !is_null( $nameSpace ) ) {
125 $nameSpace = intval( $nameSpace );
126 if( $invert && $nameSpace !== 'all' )
127 $nameSpaceClause = "rc_namespace != $nameSpace";
128 else
129 $nameSpaceClause = "rc_namespace = $nameSpace";
130 } else {
131 $nameSpace = '';
132 $nameSpaceClause = '';
133 }
134
135 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
136 $recentchanges = $dbr->tableName( 'recentchanges' );
137
138 $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
139 array( 'wl_user' => $uid ), __METHOD__ );
140 // Adjust for page X, talk:page X, which are both stored separately,
141 // but treated together
142 $nitems = floor($watchlistCount / 2);
143
144 if( is_null($days) || !is_numeric($days) ) {
145 $big = 1000; /* The magical big */
146 if($nitems > $big) {
147 # Set default cutoff shorter
148 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
149 } else {
150 $days = $defaults['days']; # default cutoff for shortlisters
151 }
152 } else {
153 $days = floatval($days);
154 }
155
156 // Dump everything here
157 $nondefaults = array();
158
159 wfAppendToArrayIfNotDefault( 'days' , $days , $defaults, $nondefaults);
160 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
161 wfAppendToArrayIfNotDefault( 'hideBots' , (int)$hideBots , $defaults, $nondefaults);
162 wfAppendToArrayIfNotDefault( 'hideAnons', (int)$hideAnons, $defaults, $nondefaults );
163 wfAppendToArrayIfNotDefault( 'hideLiu' , (int)$hideLiu , $defaults, $nondefaults );
164 wfAppendToArrayIfNotDefault( 'hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
165 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace , $defaults, $nondefaults);
166 wfAppendToArrayIfNotDefault( 'hidePatrolled', (int)$hidePatrolled, $defaults, $nondefaults );
167
168 if( $nitems == 0 ) {
169 $wgOut->addWikiMsg( 'nowatchlist' );
170 return;
171 }
172
173 # Possible where conditions
174 $conds = array();
175
176 if( $days <= 0 ) {
177 $andcutoff = '';
178 } else {
179 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
180 }
181
182 # If the watchlist is relatively short, it's simplest to zip
183 # down its entirety and then sort the results.
184
185 # If it's relatively long, it may be worth our while to zip
186 # through the time-sorted page list checking for watched items.
187
188 # Up estimate of watched items by 15% to compensate for talk pages...
189
190 # Toggles
191 if( $hideOwn ) {
192 $conds[] = "rc_user != $uid";
193 }
194 if( $hideBots ) {
195 $conds[] = 'rc_bot = 0';
196 }
197 if( $hideMinor ) {
198 $conds[] = 'rc_minor = 0';
199 }
200 if( $hideLiu ) {
201 $conds[] = 'rc_user = 0';
202 }
203 if( $hideAnons ) {
204 $conds[] = 'rc_user != 0';
205 }
206 if ( $wgUser->useRCPatrol() && $hidePatrolled ) {
207 $conds[] = 'rc_patrolled != 1';
208 }
209 if( $nameSpaceClause ) {
210 $conds[] = $nameSpaceClause;
211 }
212
213 # Toggle watchlist content (all recent edits or just the latest)
214 if( $wgUser->getOption( 'extendwatchlist' )) {
215 $limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
216 $usePage = false;
217 } else {
218 # Top log Ids for a page are not stored
219 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
220 $limitWatchlist = 0;
221 $usePage = true;
222 }
223
224 # Show a message about slave lag, if applicable
225 if( ( $lag = $dbr->getLag() ) > 0 )
226 $wgOut->showLagWarning( $lag );
227
228 # Create output form
229 $form = Xml::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
230
231 # Show watchlist header
232 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
233
234 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
235 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
236 }
237 if( $wgShowUpdatedMarker ) {
238 $form .= Xml::openElement( 'form', array( 'method' => 'post',
239 'action' => $specialTitle->getLocalUrl(),
240 'id' => 'mw-watchlist-resetbutton' ) ) .
241 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
242 Xml::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
243 Xml::hidden( 'reset', 'all' ) .
244 Xml::closeElement( 'form' );
245 }
246 $form .= '<hr />';
247
248 $tables = array( 'recentchanges', 'watchlist' );
249 $fields = array( "{$recentchanges}.*" );
250 $join_conds = array(
251 'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
252 );
253 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
254 if( $wgShowUpdatedMarker ) {
255 $fields[] = 'wl_notificationtimestamp';
256 }
257 if( $limitWatchlist ) {
258 $options['LIMIT'] = $limitWatchlist;
259 }
260
261 $rollbacker = $wgUser->isAllowed('rollback');
262 if ( $usePage || $rollbacker ) {
263 $tables[] = 'page';
264 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
265 if ($rollbacker)
266 $fields[] = 'page_latest';
267 }
268
269 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
270 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
271
272 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
273 $numRows = $dbr->numRows( $res );
274
275 /* Start bottom header */
276
277 $wlInfo = '';
278 if( $days >= 1 ) {
279 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
280 $wgLang->formatNum( $numRows ),
281 $wgLang->formatNum( $days ),
282 $wgLang->timeAndDate( wfTimestampNow(), true ),
283 $wgLang->date( wfTimestampNow(), true ),
284 $wgLang->time( wfTimestampNow(), true )
285 ) . '<br />';
286 } elseif( $days > 0 ) {
287 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
288 $wgLang->formatNum( $numRows ),
289 $wgLang->formatNum( round($days*24) )
290 ) . '<br />';
291 }
292
293 $cutofflinks = "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
294
295 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
296
297 # Spit out some control panel links
298 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideminor', 'hideMinor', $hideMinor );
299 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidebots', 'hideBots', $hideBots );
300 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideanons', 'hideAnons', $hideAnons );
301 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideliu', 'hideLiu', $hideLiu );
302 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidemine', 'hideOwn', $hideOwn );
303
304 if( $wgUser->useRCPatrol() ) {
305 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
306 }
307
308 # Namespace filter and put the whole form together.
309 $form .= $wlInfo;
310 $form .= $cutofflinks;
311 $form .= $wgLang->pipeList( $links );
312 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
313 $form .= '<hr /><p>';
314 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;';
315 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&#160;';
316 $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&#160;';
317 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
318 $form .= Xml::hidden( 'days', $days );
319 if( $hideMinor )
320 $form .= Xml::hidden( 'hideMinor', 1 );
321 if( $hideBots )
322 $form .= Xml::hidden( 'hideBots', 1 );
323 if( $hideAnons )
324 $form .= Xml::hidden( 'hideAnons', 1 );
325 if( $hideLiu )
326 $form .= Xml::hidden( 'hideLiu', 1 );
327 if( $hideOwn )
328 $form .= Xml::hidden( 'hideOwn', 1 );
329 $form .= Xml::closeElement( 'form' );
330 $form .= Xml::closeElement( 'fieldset' );
331 $wgOut->addHTML( $form );
332
333 $wgOut->addHTML( ChangesList::flagLegend() );
334
335 # If there's nothing to show, stop here
336 if( $numRows == 0 ) {
337 $wgOut->addWikiMsg( 'watchnochange' );
338 return;
339 }
340
341 /* End bottom header */
342
343 /* Do link batch query */
344 $linkBatch = new LinkBatch;
345 while ( $row = $dbr->fetchObject( $res ) ) {
346 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
347 if ( $row->rc_user != 0 ) {
348 $linkBatch->add( NS_USER, $userNameUnderscored );
349 }
350 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
351
352 $linkBatch->add( $row->rc_namespace, $row->rc_title );
353 }
354 $linkBatch->execute();
355 $dbr->dataSeek( $res, 0 );
356
357 $list = ChangesList::newFromUser( $wgUser );
358 $list->setWatchlistDivs();
359
360 $s = $list->beginRecentChangesList();
361 $counter = 1;
362 while ( $obj = $dbr->fetchObject( $res ) ) {
363 # Make RC entry
364 $rc = RecentChange::newFromRow( $obj );
365 $rc->counter = $counter++;
366
367 if ( $wgShowUpdatedMarker ) {
368 $updated = $obj->wl_notificationtimestamp;
369 } else {
370 $updated = false;
371 }
372
373 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
374 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
375 'COUNT(*)',
376 array(
377 'wl_namespace' => $obj->rc_namespace,
378 'wl_title' => $obj->rc_title,
379 ),
380 __METHOD__ );
381 } else {
382 $rc->numberofWatchingusers = 0;
383 }
384
385 $s .= $list->recentChangesLine( $rc, $updated, $counter );
386 }
387 $s .= $list->endRecentChangesList();
388
389 $wgOut->addHTML( $s );
390 }
391
392 function wlShowHideLink( $options, $message, $name, $value ) {
393 global $wgUser;
394
395 $showLinktext = wfMsgHtml( 'show' );
396 $hideLinktext = wfMsgHtml( 'hide' );
397 $title = SpecialPage::getTitleFor( 'Watchlist' );
398 $skin = $wgUser->getSkin();
399
400 $label = $value ? $showLinktext : $hideLinktext;
401 $options[$name] = 1 - (int) $value;
402
403 return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
404 }
405
406
407 function wlHoursLink( $h, $page, $options = array() ) {
408 global $wgUser, $wgLang, $wgContLang;
409
410 $sk = $wgUser->getSkin();
411 $title = Title::newFromText( $wgContLang->specialPage( $page ) );
412 $options['days'] = ($h / 24.0);
413
414 $s = $sk->linkKnown(
415 $title,
416 $wgLang->formatNum( $h ),
417 array(),
418 $options
419 );
420
421 return $s;
422 }
423
424 function wlDaysLink( $d, $page, $options = array() ) {
425 global $wgUser, $wgLang, $wgContLang;
426
427 $sk = $wgUser->getSkin();
428 $title = Title::newFromText( $wgContLang->specialPage( $page ) );
429 $options['days'] = $d;
430 $message = ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
431
432 $s = $sk->linkKnown(
433 $title,
434 $message,
435 array(),
436 $options
437 );
438
439 return $s;
440 }
441
442 /**
443 * Returns html
444 */
445 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
446 global $wgLang;
447
448 $hours = array( 1, 2, 6, 12 );
449 $days = array( 1, 3, 7 );
450 $i = 0;
451 foreach( $hours as $h ) {
452 $hours[$i++] = wlHoursLink( $h, $page, $options );
453 }
454 $i = 0;
455 foreach( $days as $d ) {
456 $days[$i++] = wlDaysLink( $d, $page, $options );
457 }
458 return wfMsgExt('wlshowlast',
459 array('parseinline', 'replaceafter'),
460 $wgLang->pipeList( $hours ),
461 $wgLang->pipeList( $days ),
462 wlDaysLink( 0, $page, $options ) );
463 }
464
465 /**
466 * Count the number of items on a user's watchlist
467 *
468 * @param $user User object
469 * @param $talk Boolean: include talk pages
470 * @return Integer
471 */
472 function wlCountItems( &$user, $talk = true ) {
473 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
474
475 # Fetch the raw count
476 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
477 array( 'wl_user' => $user->mId ), 'wlCountItems' );
478 $row = $dbr->fetchObject( $res );
479 $count = $row->count;
480
481 # Halve to remove talk pages if needed
482 if( !$talk )
483 $count = floor( $count / 2 );
484
485 return( $count );
486 }