Merge "ChangesListSpecialPage: Implement execute()"
[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 /**
32 * Constructor
33 */
34 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
35 parent::__construct( $page, $restriction );
36 }
37
38 /**
39 * Main execution point
40 *
41 * @param string $subpage
42 */
43 function execute( $subpage ) {
44 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
45
46 // Anons don't get a watchlist
47 $this->requireLogin( 'watchlistanontext' );
48
49 $output = $this->getOutput();
50 $request = $this->getRequest();
51
52 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
53 if ( $mode !== false ) {
54 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
55 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
56 } else {
57 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
58 }
59
60 $output->redirect( $title->getLocalURL() );
61 return;
62 }
63
64 $this->checkPermissions();
65
66 $user = $this->getUser();
67 $opts = $this->getOptions();
68
69 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
70 $request->wasPosted() )
71 {
72 $user->clearAllNotifications();
73 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
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 return $opts;
149 }
150
151 /**
152 * Return an array of conditions depending of options set in $opts
153 *
154 * @param FormOptions $opts
155 * @return array
156 */
157 public function buildMainQueryConds( FormOptions $opts ) {
158 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
159 $conds = array();
160 $user = $this->getUser();
161
162 // Calculate cutoff
163 if ( $opts['days'] > 0 ) {
164 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
165 }
166
167 # Toggles
168 if ( $opts['hidemyself'] ) {
169 $conds[] = 'rc_user != ' . $user->getId();
170 }
171 if ( $opts['hidebots'] ) {
172 $conds[] = 'rc_bot = 0';
173 }
174 if ( $opts['hideminor'] ) {
175 $conds[] = 'rc_minor = 0';
176 }
177 if ( $opts['hideliu'] ) {
178 $conds[] = 'rc_user = 0';
179 }
180 if ( $opts['hideanons'] ) {
181 $conds[] = 'rc_user != 0';
182 }
183 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
184 $conds[] = 'rc_patrolled != 1';
185 }
186
187 # Namespace filtering
188 if ( $opts['namespace'] !== '' ) {
189 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
190 $operator = $opts['invert'] ? '!=' : '=';
191 $boolean = $opts['invert'] ? 'AND' : 'OR';
192
193 # namespace association (bug 2429)
194 if ( !$opts['associated'] ) {
195 $condition = "rc_namespace $operator $selectedNS";
196 } else {
197 # Also add the associated namespace
198 $associatedNS = $dbr->addQuotes(
199 MWNamespace::getAssociated( $opts['namespace'] )
200 );
201 $condition = "(rc_namespace $operator $selectedNS "
202 . $boolean
203 . " rc_namespace $operator $associatedNS)";
204 }
205
206 $conds[] = $condition;
207 }
208
209 return $conds;
210 }
211
212 /**
213 * Process the query
214 *
215 * @param array $conds
216 * @param FormOptions $opts
217 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
218 */
219 public function doMainQuery( $conds, $opts ) {
220 global $wgShowUpdatedMarker;
221
222 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
223 $user = $this->getUser();
224 # Toggle watchlist content (all recent edits or just the latest)
225 if ( $opts['extended'] ) {
226 $limitWatchlist = $user->getIntOption( 'wllimit' );
227 $usePage = false;
228 } else {
229 # Top log Ids for a page are not stored
230 $nonRevisionTypes = array( RC_LOG );
231 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
232 if ( $nonRevisionTypes ) {
233 $conds[] = $dbr->makeList(
234 array(
235 'rc_this_oldid=page_latest',
236 'rc_type' => $nonRevisionTypes,
237 ),
238 LIST_OR
239 );
240 }
241 $limitWatchlist = 0;
242 $usePage = true;
243 }
244
245 $tables = array( 'recentchanges', 'watchlist' );
246 $fields = RecentChange::selectFields();
247 $join_conds = array(
248 'watchlist' => array(
249 'INNER JOIN',
250 array(
251 'wl_user' => $user->getId(),
252 'wl_namespace=rc_namespace',
253 'wl_title=rc_title'
254 ),
255 ),
256 );
257 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
258 if ( $wgShowUpdatedMarker ) {
259 $fields[] = 'wl_notificationtimestamp';
260 }
261 if ( $limitWatchlist ) {
262 $options['LIMIT'] = $limitWatchlist;
263 }
264
265 $rollbacker = $user->isAllowed( 'rollback' );
266 if ( $usePage || $rollbacker ) {
267 $tables[] = 'page';
268 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
269 if ( $rollbacker ) {
270 $fields[] = 'page_latest';
271 }
272 }
273
274 // Log entries with DELETED_ACTION must not show up unless the user has
275 // the necessary rights.
276 if ( !$user->isAllowed( 'deletedhistory' ) ) {
277 $bitmask = LogPage::DELETED_ACTION;
278 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
279 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
280 } else {
281 $bitmask = 0;
282 }
283 if ( $bitmask ) {
284 $conds[] = $dbr->makeList( array(
285 'rc_type != ' . RC_LOG,
286 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
287 ), LIST_OR );
288 }
289
290
291 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
292 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
293
294 return $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
295 }
296
297 /**
298 * Send output to the OutputPage object, only called if not used feeds
299 *
300 * @param ResultWrapper $rows Database rows
301 * @param FormOptions $opts
302 */
303 public function webOutput( $rows, $opts ) {
304 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
305
306 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
307 $user = $this->getUser();
308
309 $dbr->dataSeek( $rows, 0 );
310
311 $list = ChangesList::newFromContext( $this->getContext() );
312 $list->setWatchlistDivs();
313
314 $s = $list->beginRecentChangesList();
315 $counter = 1;
316 foreach ( $rows as $obj ) {
317 # Make RC entry
318 $rc = RecentChange::newFromRow( $obj );
319 $rc->counter = $counter++;
320
321 if ( $wgShowUpdatedMarker ) {
322 $updated = $obj->wl_notificationtimestamp;
323 } else {
324 $updated = false;
325 }
326
327 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
328 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
329 'COUNT(*)',
330 array(
331 'wl_namespace' => $obj->rc_namespace,
332 'wl_title' => $obj->rc_title,
333 ),
334 __METHOD__ );
335 } else {
336 $rc->numberofWatchingusers = 0;
337 }
338
339 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
340 if ( $changeLine !== false ) {
341 $s .= $changeLine;
342 }
343 }
344 $s .= $list->endRecentChangesList();
345
346 // Print things out
347
348 $output = $this->getOutput();
349
350 $output->addSubtitle(
351 $this->msg( 'watchlistfor2', $user->getName() )
352 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
353 );
354
355 // Output options box
356 $this->doHeader( $opts );
357
358 // Add feed links
359 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
360 if ( $wlToken ) {
361 $this->addFeedLinks( array(
362 'action' => 'feedwatchlist',
363 'allrev' => 1,
364 'wlowner' => $user->getName(),
365 'wltoken' => $wlToken,
366 ) );
367 }
368
369 # Show a message about slave lag, if applicable
370 $lag = wfGetLB()->safeGetLag( $dbr );
371 if ( $lag > 0 ) {
372 $output->showLagWarning( $lag );
373 }
374
375 if ( $rows->numRows() == 0 ) {
376 $output->wrapWikiMsg(
377 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
378 );
379 } else {
380 $output->addHTML( $s );
381 }
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 global $wgScript;
392
393 $user = $this->getUser();
394
395 $this->setTopText( $opts );
396
397 $lang = $this->getLanguage();
398 $wlInfo = '';
399 if ( $opts['days'] > 0 ) {
400 $timestamp = wfTimestampNow();
401 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
402 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
403 }
404
405 $nondefaults = $opts->getChangedValues();
406 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
407
408 # Spit out some control panel links
409 $filters = array(
410 'hideminor' => 'rcshowhideminor',
411 'hidebots' => 'rcshowhidebots',
412 'hideanons' => 'rcshowhideanons',
413 'hideliu' => 'rcshowhideliu',
414 'hidemyself' => 'rcshowhidemine',
415 'hidepatrolled' => 'rcshowhidepatr'
416 );
417 foreach ( $this->getCustomFilters() as $key => $params ) {
418 $filters[$key] = $params['msg'];
419 }
420 // Disable some if needed
421 if ( !$user->useNPPatrol() ) {
422 unset( $filters['hidepatrolled'] );
423 }
424
425 $links = array();
426 foreach ( $filters as $name => $msg ) {
427 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
428 }
429
430 $hiddenFields = $nondefaults;
431 unset( $hiddenFields['namespace'] );
432 unset( $hiddenFields['invert'] );
433 unset( $hiddenFields['associated'] );
434
435 # Create output
436 $form = '';
437
438 # Namespace filter and put the whole form together.
439 $form .= $wlInfo;
440 $form .= $cutofflinks;
441 $form .= $lang->pipeList( $links ) . "\n";
442 $form .= "<hr />\n<p>";
443 $form .= Html::namespaceSelector(
444 array(
445 'selected' => $opts['namespace'],
446 'all' => '',
447 'label' => $this->msg( 'namespace' )->text()
448 ), array(
449 'name' => 'namespace',
450 'id' => 'namespace',
451 'class' => 'namespaceselector',
452 )
453 ) . '&#160;';
454 $form .= Xml::checkLabel(
455 $this->msg( 'invert' )->text(),
456 'invert',
457 'nsinvert',
458 $opts['invert'],
459 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
460 ) . '&#160;';
461 $form .= Xml::checkLabel(
462 $this->msg( 'namespace_association' )->text(),
463 'associated',
464 'nsassociated',
465 $opts['associated'],
466 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
467 ) . '&#160;';
468 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
469 foreach ( $hiddenFields as $key => $value ) {
470 $form .= Html::hidden( $key, $value ) . "\n";
471 }
472 $form .= Xml::closeElement( 'fieldset' ) . "\n";
473 $form .= Xml::closeElement( 'form' ) . "\n";
474 $this->getOutput()->addHTML( $form );
475
476 $this->setBottomText( $opts );
477 }
478
479 function setTopText( FormOptions $opts ) {
480 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
481
482 $nondefaults = $opts->getChangedValues();
483 $form = "";
484 $user = $this->getUser();
485
486 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
487 $numItems = $this->countItems( $dbr );
488
489 // Show watchlist header
490 $form .= "<p>";
491 if ( $numItems == 0 ) {
492 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
493 } else {
494 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
495 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
496 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
497 }
498 if ( $wgShowUpdatedMarker ) {
499 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
500 }
501 }
502 $form .= "</p>";
503
504 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
505 $form .= Xml::openElement( 'form', array( 'method' => 'post',
506 'action' => $this->getPageTitle()->getLocalURL(),
507 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
508 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
509 Html::hidden( 'reset', 'all' ) . "\n";
510 foreach ( $nondefaults as $key => $value ) {
511 $form .= Html::hidden( $key, $value ) . "\n";
512 }
513 $form .= Xml::closeElement( 'form' ) . "\n";
514 }
515
516 $form .= Xml::openElement( 'form', array(
517 'method' => 'post',
518 'action' => $this->getPageTitle()->getLocalURL(),
519 'id' => 'mw-watchlist-form'
520 ) );
521 $form .= Xml::fieldset(
522 $this->msg( 'watchlist-options' )->text(),
523 false,
524 array( 'id' => 'mw-watchlist-options' )
525 );
526
527 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
528
529 $this->getOutput()->addHTML( $form );
530 }
531
532 protected function showHideLink( $options, $message, $name, $value ) {
533 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
534 $options[$name] = 1 - (int)$value;
535
536 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
537 }
538
539 protected function hoursLink( $h, $options = array() ) {
540 $options['days'] = ( $h / 24.0 );
541
542 return Linker::linkKnown(
543 $this->getPageTitle(),
544 $this->getLanguage()->formatNum( $h ),
545 array(),
546 $options
547 );
548 }
549
550 protected function daysLink( $d, $options = array() ) {
551 $options['days'] = $d;
552 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
553
554 return Linker::linkKnown(
555 $this->getPageTitle(),
556 $message,
557 array(),
558 $options
559 );
560 }
561
562 /**
563 * Returns html
564 *
565 * @param int $days This gets overwritten, so is not used
566 * @param array $options Query parameters for URL
567 * @return string
568 */
569 protected function cutoffLinks( $days, $options = array() ) {
570 $hours = array( 1, 2, 6, 12 );
571 $days = array( 1, 3, 7 );
572 $i = 0;
573 foreach ( $hours as $h ) {
574 $hours[$i++] = $this->hoursLink( $h, $options );
575 }
576 $i = 0;
577 foreach ( $days as $d ) {
578 $days[$i++] = $this->daysLink( $d, $options );
579 }
580 return $this->msg( 'wlshowlast' )->rawParams(
581 $this->getLanguage()->pipeList( $hours ),
582 $this->getLanguage()->pipeList( $days ),
583 $this->daysLink( 0, $options ) )->parse();
584 }
585
586 /**
587 * Count the number of items on a user's watchlist
588 *
589 * @param DatabaseBase $dbr A database connection
590 * @return Integer
591 */
592 protected function countItems( $dbr ) {
593 # Fetch the raw count
594 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
595 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
596 $row = $dbr->fetchObject( $rows );
597 $count = $row->count;
598
599 return floor( $count / 2 );
600 }
601 }