Special:Watchlist: Don't try to render empty row
[lhc/web/wiklou.git] / includes / specials / SpecialWatchlist.php
1 <?php
2 /**
3 * Implements Special:Watchlist
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that lists last changes made to the wiki,
26 * limited to user-defined list of titles.
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialWatchlist extends ChangesListSpecialPage {
31 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
32 parent::__construct( $page, $restriction );
33 }
34
35 /**
36 * Main execution point
37 *
38 * @param string $subpage
39 */
40 function execute( $subpage ) {
41 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
42
43 // Anons don't get a watchlist
44 $this->requireLogin( 'watchlistanontext' );
45
46 $output = $this->getOutput();
47 $request = $this->getRequest();
48
49 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
50 if ( $mode !== false ) {
51 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
52 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
53 } elseif ( $mode === SpecialEditWatchlist::EDIT_CLEAR ) {
54 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'clear' );
55 } else {
56 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
57 }
58
59 $output->redirect( $title->getLocalURL() );
60
61 return;
62 }
63
64 $this->checkPermissions();
65
66 $user = $this->getUser();
67 $opts = $this->getOptions();
68
69 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker )
70 && $request->getVal( 'reset' )
71 && $request->wasPosted()
72 ) {
73 $user->clearAllNotifications();
74 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
75
76 return;
77 }
78
79 parent::execute( $subpage );
80 }
81
82 /**
83 * Return an array of subpages beginning with $search that this special page will accept.
84 *
85 * @param string $search Prefix to search for
86 * @param integer $limit Maximum number of results to return
87 * @return string[] Matching subpages
88 */
89 public function prefixSearchSubpages( $search, $limit = 10 ) {
90 // See also SpecialEditWatchlist::prefixSearchSubpages
91 $subpages = array( 'clear', 'edit', 'raw' );
92 $escaped = preg_quote( $search );
93 return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit );
94 }
95
96 /**
97 * Get a FormOptions object containing the default options
98 *
99 * @return FormOptions
100 */
101 public function getDefaultOptions() {
102 $opts = parent::getDefaultOptions();
103 $user = $this->getUser();
104
105 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
106
107 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
108 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
109 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
110 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
111 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
112 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
113
114 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
115
116 return $opts;
117 }
118
119 /**
120 * Get custom show/hide filters
121 *
122 * @return array Map of filter URL param names to properties (msg/default)
123 */
124 protected function getCustomFilters() {
125 if ( $this->customFilters === null ) {
126 $this->customFilters = parent::getCustomFilters();
127 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ), '1.23' );
128 }
129
130 return $this->customFilters;
131 }
132
133 /**
134 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
135 *
136 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
137 * to the current ones.
138 *
139 * @param FormOptions $opts
140 * @return FormOptions
141 */
142 protected function fetchOptionsFromRequest( $opts ) {
143 static $compatibilityMap = array(
144 'hideMinor' => 'hideminor',
145 'hideBots' => 'hidebots',
146 'hideAnons' => 'hideanons',
147 'hideLiu' => 'hideliu',
148 'hidePatrolled' => 'hidepatrolled',
149 'hideOwn' => 'hidemyself',
150 );
151
152 $params = $this->getRequest()->getValues();
153 foreach ( $compatibilityMap as $from => $to ) {
154 if ( isset( $params[$from] ) ) {
155 $params[$to] = $params[$from];
156 unset( $params[$from] );
157 }
158 }
159
160 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
161 // methods defined on WebRequest and removing this dependency would cause some code duplication.
162 $request = new DerivativeRequest( $this->getRequest(), $params );
163 $opts->fetchValuesFromRequest( $request );
164
165 return $opts;
166 }
167
168 /**
169 * Return an array of conditions depending of options set in $opts
170 *
171 * @param FormOptions $opts
172 * @return array
173 */
174 public function buildMainQueryConds( FormOptions $opts ) {
175 $dbr = $this->getDB();
176 $conds = parent::buildMainQueryConds( $opts );
177
178 // Calculate cutoff
179 if ( $opts['days'] > 0 ) {
180 $conds[] = 'rc_timestamp > ' .
181 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
182 }
183
184 return $conds;
185 }
186
187 /**
188 * Process the query
189 *
190 * @param array $conds
191 * @param FormOptions $opts
192 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
193 */
194 public function doMainQuery( $conds, $opts ) {
195 global $wgShowUpdatedMarker;
196
197 $dbr = $this->getDB();
198 $user = $this->getUser();
199
200 # Toggle watchlist content (all recent edits or just the latest)
201 if ( $opts['extended'] ) {
202 $limitWatchlist = $user->getIntOption( 'wllimit' );
203 $usePage = false;
204 } else {
205 # Top log Ids for a page are not stored
206 $nonRevisionTypes = array( RC_LOG );
207 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
208 if ( $nonRevisionTypes ) {
209 $conds[] = $dbr->makeList(
210 array(
211 'rc_this_oldid=page_latest',
212 'rc_type' => $nonRevisionTypes,
213 ),
214 LIST_OR
215 );
216 }
217 $limitWatchlist = 0;
218 $usePage = true;
219 }
220
221 $tables = array( 'recentchanges', 'watchlist' );
222 $fields = RecentChange::selectFields();
223 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
224 $join_conds = array(
225 'watchlist' => array(
226 'INNER JOIN',
227 array(
228 'wl_user' => $user->getId(),
229 'wl_namespace=rc_namespace',
230 'wl_title=rc_title'
231 ),
232 ),
233 );
234
235 if ( $wgShowUpdatedMarker ) {
236 $fields[] = 'wl_notificationtimestamp';
237 }
238 if ( $limitWatchlist ) {
239 $query_options['LIMIT'] = $limitWatchlist;
240 }
241
242 $rollbacker = $user->isAllowed( 'rollback' );
243 if ( $usePage || $rollbacker ) {
244 $tables[] = 'page';
245 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
246 if ( $rollbacker ) {
247 $fields[] = 'page_latest';
248 }
249 }
250
251 // Log entries with DELETED_ACTION must not show up unless the user has
252 // the necessary rights.
253 if ( !$user->isAllowed( 'deletedhistory' ) ) {
254 $bitmask = LogPage::DELETED_ACTION;
255 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
256 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
257 } else {
258 $bitmask = 0;
259 }
260 if ( $bitmask ) {
261 $conds[] = $dbr->makeList( array(
262 'rc_type != ' . RC_LOG,
263 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
264 ), LIST_OR );
265 }
266
267 ChangeTags::modifyDisplayQuery(
268 $tables,
269 $fields,
270 $conds,
271 $join_conds,
272 $query_options,
273 ''
274 );
275
276 wfRunHooks( 'SpecialWatchlistQuery',
277 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
278 '1.23' );
279
280 return $dbr->select(
281 $tables,
282 $fields,
283 $conds,
284 __METHOD__,
285 $query_options,
286 $join_conds
287 );
288 }
289
290 /**
291 * Return a DatabaseBase object for reading
292 *
293 * @return DatabaseBase
294 */
295 protected function getDB() {
296 return wfGetDB( DB_SLAVE, 'watchlist' );
297 }
298
299 /**
300 * Output feed links.
301 */
302 public function outputFeedLinks() {
303 $user = $this->getUser();
304 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
305 if ( $wlToken ) {
306 $this->addFeedLinks( array(
307 'action' => 'feedwatchlist',
308 'allrev' => 1,
309 'wlowner' => $user->getName(),
310 'wltoken' => $wlToken,
311 ) );
312 }
313 }
314
315 /**
316 * Build and output the actual changes list.
317 *
318 * @param ResultWrapper $rows Database rows
319 * @param FormOptions $opts
320 */
321 public function outputChangesList( $rows, $opts ) {
322 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
323
324 $dbr = $this->getDB();
325 $user = $this->getUser();
326 $output = $this->getOutput();
327
328 # Show a message about slave lag, if applicable
329 $lag = wfGetLB()->safeGetLag( $dbr );
330 if ( $lag > 0 ) {
331 $output->showLagWarning( $lag );
332 }
333
334 # If no rows to display, show message before try to render the list
335 if ( $rows->numRows() == 0 ) {
336 $output->wrapWikiMsg(
337 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
338 );
339 return;
340 }
341
342 $dbr->dataSeek( $rows, 0 );
343
344 $list = ChangesList::newFromContext( $this->getContext() );
345 $list->setWatchlistDivs();
346 $list->initChangesListRows( $rows );
347 $dbr->dataSeek( $rows, 0 );
348
349 $s = $list->beginRecentChangesList();
350 $counter = 1;
351 foreach ( $rows as $obj ) {
352 # Make RC entry
353 $rc = RecentChange::newFromRow( $obj );
354 $rc->counter = $counter++;
355
356 if ( $wgShowUpdatedMarker ) {
357 $updated = $obj->wl_notificationtimestamp;
358 } else {
359 $updated = false;
360 }
361
362 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
363 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
364 'COUNT(*)',
365 array(
366 'wl_namespace' => $obj->rc_namespace,
367 'wl_title' => $obj->rc_title,
368 ),
369 __METHOD__ );
370 } else {
371 $rc->numberofWatchingusers = 0;
372 }
373
374 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
375 if ( $changeLine !== false ) {
376 $s .= $changeLine;
377 }
378 }
379 $s .= $list->endRecentChangesList();
380
381 $output->addHTML( $s );
382 }
383
384 /**
385 * Return the text to be displayed above the changes
386 *
387 * @param FormOptions $opts
388 * @return string XHTML
389 */
390 public function doHeader( $opts ) {
391 $user = $this->getUser();
392
393 $this->getOutput()->addSubtitle(
394 $this->msg( 'watchlistfor2', $user->getName() )
395 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
396 );
397
398 $this->setTopText( $opts );
399
400 $lang = $this->getLanguage();
401 $wlInfo = '';
402 if ( $opts['days'] > 0 ) {
403 $timestamp = wfTimestampNow();
404 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
405 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
406 )->parse() . "<br />\n";
407 }
408
409 $nondefaults = $opts->getChangedValues();
410 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
411
412 # Spit out some control panel links
413 $filters = array(
414 'hideminor' => 'rcshowhideminor',
415 'hidebots' => 'rcshowhidebots',
416 'hideanons' => 'rcshowhideanons',
417 'hideliu' => 'rcshowhideliu',
418 'hidemyself' => 'rcshowhidemine',
419 'hidepatrolled' => 'rcshowhidepatr'
420 );
421 foreach ( $this->getCustomFilters() as $key => $params ) {
422 $filters[$key] = $params['msg'];
423 }
424 // Disable some if needed
425 if ( !$user->useNPPatrol() ) {
426 unset( $filters['hidepatrolled'] );
427 }
428
429 $links = array();
430 foreach ( $filters as $name => $msg ) {
431 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
432 }
433
434 $hiddenFields = $nondefaults;
435 unset( $hiddenFields['namespace'] );
436 unset( $hiddenFields['invert'] );
437 unset( $hiddenFields['associated'] );
438
439 # Create output
440 $form = '';
441
442 # Namespace filter and put the whole form together.
443 $form .= $wlInfo;
444 $form .= $cutofflinks;
445 $form .= $lang->pipeList( $links ) . "\n";
446 $form .= "<hr />\n<p>";
447 $form .= Html::namespaceSelector(
448 array(
449 'selected' => $opts['namespace'],
450 'all' => '',
451 'label' => $this->msg( 'namespace' )->text()
452 ), array(
453 'name' => 'namespace',
454 'id' => 'namespace',
455 'class' => 'namespaceselector',
456 )
457 ) . '&#160;';
458 $form .= Xml::checkLabel(
459 $this->msg( 'invert' )->text(),
460 'invert',
461 'nsinvert',
462 $opts['invert'],
463 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
464 ) . '&#160;';
465 $form .= Xml::checkLabel(
466 $this->msg( 'namespace_association' )->text(),
467 'associated',
468 'nsassociated',
469 $opts['associated'],
470 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
471 ) . '&#160;';
472 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
473 foreach ( $hiddenFields as $key => $value ) {
474 $form .= Html::hidden( $key, $value ) . "\n";
475 }
476 $form .= Xml::closeElement( 'fieldset' ) . "\n";
477 $form .= Xml::closeElement( 'form' ) . "\n";
478 $this->getOutput()->addHTML( $form );
479
480 $this->setBottomText( $opts );
481 }
482
483 function setTopText( FormOptions $opts ) {
484 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
485
486 $nondefaults = $opts->getChangedValues();
487 $form = "";
488 $user = $this->getUser();
489
490 $dbr = $this->getDB();
491 $numItems = $this->countItems( $dbr );
492
493 // Show watchlist header
494 $form .= "<p>";
495 if ( $numItems == 0 ) {
496 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
497 } else {
498 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
499 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
500 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
501 }
502 if ( $wgShowUpdatedMarker ) {
503 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
504 }
505 }
506 $form .= "</p>";
507
508 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
509 $form .= Xml::openElement( 'form', array( 'method' => 'post',
510 'action' => $this->getPageTitle()->getLocalURL(),
511 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
512 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
513 Html::hidden( 'reset', 'all' ) . "\n";
514 foreach ( $nondefaults as $key => $value ) {
515 $form .= Html::hidden( $key, $value ) . "\n";
516 }
517 $form .= Xml::closeElement( 'form' ) . "\n";
518 }
519
520 $form .= Xml::openElement( 'form', array(
521 'method' => 'post',
522 'action' => $this->getPageTitle()->getLocalURL(),
523 'id' => 'mw-watchlist-form'
524 ) );
525 $form .= Xml::fieldset(
526 $this->msg( 'watchlist-options' )->text(),
527 false,
528 array( 'id' => 'mw-watchlist-options' )
529 );
530
531 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
532
533 $this->getOutput()->addHTML( $form );
534 }
535
536 protected function showHideLink( $options, $message, $name, $value ) {
537 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
538 $options[$name] = 1 - (int)$value;
539
540 return $this->msg( $message )
541 ->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
542 ->escaped();
543 }
544
545 protected function hoursLink( $h, $options = array() ) {
546 $options['days'] = ( $h / 24.0 );
547
548 return Linker::linkKnown(
549 $this->getPageTitle(),
550 $this->getLanguage()->formatNum( $h ),
551 array(),
552 $options
553 );
554 }
555
556 protected function daysLink( $d, $options = array() ) {
557 $options['days'] = $d;
558 $message = $d ? $this->getLanguage()->formatNum( $d )
559 : $this->msg( 'watchlistall2' )->escaped();
560
561 return Linker::linkKnown(
562 $this->getPageTitle(),
563 $message,
564 array(),
565 $options
566 );
567 }
568
569 /**
570 * Returns html
571 *
572 * @param int $days This gets overwritten, so is not used
573 * @param array $options Query parameters for URL
574 * @return string
575 */
576 protected function cutoffLinks( $days, $options = array() ) {
577 $hours = array( 1, 2, 6, 12 );
578 $days = array( 1, 3, 7 );
579 $i = 0;
580 foreach ( $hours as $h ) {
581 $hours[$i++] = $this->hoursLink( $h, $options );
582 }
583 $i = 0;
584 foreach ( $days as $d ) {
585 $days[$i++] = $this->daysLink( $d, $options );
586 }
587
588 return $this->msg( 'wlshowlast' )->rawParams(
589 $this->getLanguage()->pipeList( $hours ),
590 $this->getLanguage()->pipeList( $days ),
591 $this->daysLink( 0, $options ) )->parse();
592 }
593
594 /**
595 * Count the number of items on a user's watchlist
596 *
597 * @param DatabaseBase $dbr A database connection
598 * @return int
599 */
600 protected function countItems( $dbr ) {
601 # Fetch the raw count
602 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
603 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
604 $row = $dbr->fetchObject( $rows );
605 $count = $row->count;
606
607 return floor( $count / 2 );
608 }
609 }