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