Switch some HTMLForms in special pages to OOUI
[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 // Anons don't get a watchlist
42 $this->requireLogin( 'watchlistanontext' );
43
44 $output = $this->getOutput();
45 $request = $this->getRequest();
46 $this->addHelpLink( 'Help:Watching pages' );
47
48 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
49 if ( $mode !== false ) {
50 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
51 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
52 } elseif ( $mode === SpecialEditWatchlist::EDIT_CLEAR ) {
53 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'clear' );
54 } else {
55 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
56 }
57
58 $output->redirect( $title->getLocalURL() );
59
60 return;
61 }
62
63 $this->checkPermissions();
64
65 $user = $this->getUser();
66 $opts = $this->getOptions();
67
68 $config = $this->getConfig();
69 if ( ( $config->get( 'EnotifWatchlist' ) || $config->get( 'ShowUpdatedMarker' ) )
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 that this special page will accept.
84 *
85 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
86 * @return string[] subpages
87 */
88 public function getSubpagesForPrefixSearch() {
89 return array(
90 'clear',
91 'edit',
92 'raw',
93 );
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 Hooks::run( '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 $dbr = $this->getDB();
196 $user = $this->getUser();
197
198 # Toggle watchlist content (all recent edits or just the latest)
199 if ( $opts['extended'] ) {
200 $limitWatchlist = $user->getIntOption( 'wllimit' );
201 $usePage = false;
202 } else {
203 # Top log Ids for a page are not stored
204 $nonRevisionTypes = array( RC_LOG );
205 Hooks::run( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
206 if ( $nonRevisionTypes ) {
207 $conds[] = $dbr->makeList(
208 array(
209 'rc_this_oldid=page_latest',
210 'rc_type' => $nonRevisionTypes,
211 ),
212 LIST_OR
213 );
214 }
215 $limitWatchlist = 0;
216 $usePage = true;
217 }
218
219 $tables = array( 'recentchanges', 'watchlist' );
220 $fields = RecentChange::selectFields();
221 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
222 $join_conds = array(
223 'watchlist' => array(
224 'INNER JOIN',
225 array(
226 'wl_user' => $user->getId(),
227 'wl_namespace=rc_namespace',
228 'wl_title=rc_title'
229 ),
230 ),
231 );
232
233 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
234 $fields[] = 'wl_notificationtimestamp';
235 }
236 if ( $limitWatchlist ) {
237 $query_options['LIMIT'] = $limitWatchlist;
238 }
239
240 $rollbacker = $user->isAllowed( 'rollback' );
241 if ( $usePage || $rollbacker ) {
242 $tables[] = 'page';
243 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
244 if ( $rollbacker ) {
245 $fields[] = 'page_latest';
246 }
247 }
248
249 // Log entries with DELETED_ACTION must not show up unless the user has
250 // the necessary rights.
251 if ( !$user->isAllowed( 'deletedhistory' ) ) {
252 $bitmask = LogPage::DELETED_ACTION;
253 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
254 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
255 } else {
256 $bitmask = 0;
257 }
258 if ( $bitmask ) {
259 $conds[] = $dbr->makeList( array(
260 'rc_type != ' . RC_LOG,
261 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
262 ), LIST_OR );
263 }
264
265 ChangeTags::modifyDisplayQuery(
266 $tables,
267 $fields,
268 $conds,
269 $join_conds,
270 $query_options,
271 ''
272 );
273
274 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
275
276 return $dbr->select(
277 $tables,
278 $fields,
279 $conds,
280 __METHOD__,
281 $query_options,
282 $join_conds
283 );
284 }
285
286 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
287 &$join_conds, $opts
288 ) {
289 return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
290 && Hooks::run(
291 'SpecialWatchlistQuery',
292 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
293 '1.23'
294 );
295 }
296
297 /**
298 * Return a IDatabase object for reading
299 *
300 * @return IDatabase
301 */
302 protected function getDB() {
303 return wfGetDB( DB_SLAVE, 'watchlist' );
304 }
305
306 /**
307 * Output feed links.
308 */
309 public function outputFeedLinks() {
310 $user = $this->getUser();
311 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
312 if ( $wlToken ) {
313 $this->addFeedLinks( array(
314 'action' => 'feedwatchlist',
315 'allrev' => 1,
316 'wlowner' => $user->getName(),
317 'wltoken' => $wlToken,
318 ) );
319 }
320 }
321
322 /**
323 * Build and output the actual changes list.
324 *
325 * @param ResultWrapper $rows Database rows
326 * @param FormOptions $opts
327 */
328 public function outputChangesList( $rows, $opts ) {
329 $dbr = $this->getDB();
330 $user = $this->getUser();
331 $output = $this->getOutput();
332
333 # Show a message about slave lag, if applicable
334 $lag = wfGetLB()->safeGetLag( $dbr );
335 if ( $lag > 0 ) {
336 $output->showLagWarning( $lag );
337 }
338
339 # If no rows to display, show message before try to render the list
340 if ( $rows->numRows() == 0 ) {
341 $output->wrapWikiMsg(
342 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
343 );
344 return;
345 }
346
347 $dbr->dataSeek( $rows, 0 );
348
349 $list = ChangesList::newFromContext( $this->getContext() );
350 $list->setWatchlistDivs();
351 $list->initChangesListRows( $rows );
352 $dbr->dataSeek( $rows, 0 );
353
354 $s = $list->beginRecentChangesList();
355 $counter = 1;
356 foreach ( $rows as $obj ) {
357 # Make RC entry
358 $rc = RecentChange::newFromRow( $obj );
359 $rc->counter = $counter++;
360
361 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
362 $updated = $obj->wl_notificationtimestamp;
363 } else {
364 $updated = false;
365 }
366
367 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
368 && $user->getOption( 'shownumberswatching' )
369 ) {
370 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
371 'COUNT(*)',
372 array(
373 'wl_namespace' => $obj->rc_namespace,
374 'wl_title' => $obj->rc_title,
375 ),
376 __METHOD__ );
377 } else {
378 $rc->numberofWatchingusers = 0;
379 }
380
381 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
382 if ( $changeLine !== false ) {
383 $s .= $changeLine;
384 }
385 }
386 $s .= $list->endRecentChangesList();
387
388 $output->addHTML( $s );
389 }
390
391 /**
392 * Set the text to be displayed above the changes
393 *
394 * @param FormOptions $opts
395 * @param int $numRows Number of rows in the result to show after this header
396 */
397 public function doHeader( $opts, $numRows ) {
398 $user = $this->getUser();
399
400 $this->getOutput()->addSubtitle(
401 $this->msg( 'watchlistfor2', $user->getName() )
402 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
403 );
404
405 $this->setTopText( $opts );
406
407 $lang = $this->getLanguage();
408 $wlInfo = '';
409 if ( $opts['days'] > 0 ) {
410 $timestamp = wfTimestampNow();
411 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
412 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
413 )->parse() . "<br />\n";
414 }
415
416 $nondefaults = $opts->getChangedValues();
417 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
418
419 # Spit out some control panel links
420 $filters = array(
421 'hideminor' => 'rcshowhideminor',
422 'hidebots' => 'rcshowhidebots',
423 'hideanons' => 'rcshowhideanons',
424 'hideliu' => 'rcshowhideliu',
425 'hidemyself' => 'rcshowhidemine',
426 'hidepatrolled' => 'rcshowhidepatr'
427 );
428 foreach ( $this->getCustomFilters() as $key => $params ) {
429 $filters[$key] = $params['msg'];
430 }
431 // Disable some if needed
432 if ( !$user->useRCPatrol() ) {
433 unset( $filters['hidepatrolled'] );
434 }
435
436 $links = array();
437 foreach ( $filters as $name => $msg ) {
438 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
439 }
440
441 $hiddenFields = $nondefaults;
442 unset( $hiddenFields['namespace'] );
443 unset( $hiddenFields['invert'] );
444 unset( $hiddenFields['associated'] );
445
446 # Create output
447 $form = '';
448
449 # Namespace filter and put the whole form together.
450 $form .= $wlInfo;
451 $form .= $cutofflinks;
452 $form .= $lang->pipeList( $links ) . "\n";
453 $form .= "<hr />\n<p>";
454 $form .= Html::namespaceSelector(
455 array(
456 'selected' => $opts['namespace'],
457 'all' => '',
458 'label' => $this->msg( 'namespace' )->text()
459 ), array(
460 'name' => 'namespace',
461 'id' => 'namespace',
462 'class' => 'namespaceselector',
463 )
464 ) . '&#160;';
465 $form .= Xml::checkLabel(
466 $this->msg( 'invert' )->text(),
467 'invert',
468 'nsinvert',
469 $opts['invert'],
470 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
471 ) . '&#160;';
472 $form .= Xml::checkLabel(
473 $this->msg( 'namespace_association' )->text(),
474 'associated',
475 'nsassociated',
476 $opts['associated'],
477 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
478 ) . '&#160;';
479 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
480 foreach ( $hiddenFields as $key => $value ) {
481 $form .= Html::hidden( $key, $value ) . "\n";
482 }
483 $form .= Xml::closeElement( 'fieldset' ) . "\n";
484 $form .= Xml::closeElement( 'form' ) . "\n";
485 $this->getOutput()->addHTML( $form );
486
487 $this->setBottomText( $opts );
488 }
489
490 function setTopText( FormOptions $opts ) {
491 $nondefaults = $opts->getChangedValues();
492 $form = "";
493 $user = $this->getUser();
494
495 $dbr = $this->getDB();
496 $numItems = $this->countItems( $dbr );
497 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
498
499 // Show watchlist header
500 $form .= "<p>";
501 if ( $numItems == 0 ) {
502 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
503 } else {
504 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
505 if ( $this->getConfig()->get( 'EnotifWatchlist' )
506 && $user->getOption( 'enotifwatchlistpages' )
507 ) {
508 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
509 }
510 if ( $showUpdatedMarker ) {
511 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
512 }
513 }
514 $form .= "</p>";
515
516 if ( $numItems > 0 && $showUpdatedMarker ) {
517 $form .= Xml::openElement( 'form', array( 'method' => 'post',
518 'action' => $this->getPageTitle()->getLocalURL(),
519 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
520 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
521 Html::hidden( 'reset', 'all' ) . "\n";
522 foreach ( $nondefaults as $key => $value ) {
523 $form .= Html::hidden( $key, $value ) . "\n";
524 }
525 $form .= Xml::closeElement( 'form' ) . "\n";
526 }
527
528 $form .= Xml::openElement( 'form', array(
529 'method' => 'post',
530 'action' => $this->getPageTitle()->getLocalURL(),
531 'id' => 'mw-watchlist-form'
532 ) );
533 $form .= Xml::fieldset(
534 $this->msg( 'watchlist-options' )->text(),
535 false,
536 array( 'id' => 'mw-watchlist-options' )
537 );
538
539 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
540
541 $this->getOutput()->addHTML( $form );
542 }
543
544 protected function showHideLink( $options, $message, $name, $value ) {
545 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
546 $options[$name] = 1 - (int)$value;
547
548 return $this->msg( $message )
549 ->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
550 ->escaped();
551 }
552
553 protected function hoursLink( $h, $options = array() ) {
554 $options['days'] = ( $h / 24.0 );
555
556 return Linker::linkKnown(
557 $this->getPageTitle(),
558 $this->getLanguage()->formatNum( $h ),
559 array(),
560 $options
561 );
562 }
563
564 protected function daysLink( $d, $options = array() ) {
565 $options['days'] = $d;
566
567 return Linker::linkKnown(
568 $this->getPageTitle(),
569 $this->getLanguage()->formatNum( $d ),
570 array(),
571 $options
572 );
573 }
574
575 /**
576 * Returns html
577 *
578 * @param int $days This gets overwritten, so is not used
579 * @param array $options Query parameters for URL
580 * @return string
581 */
582 protected function cutoffLinks( $days, $options = array() ) {
583 global $wgRCMaxAge;
584 $watchlistMaxDays = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
585
586 $hours = array( 1, 2, 6, 12 );
587 $days = array( 1, 3, 7, $watchlistMaxDays );
588 $i = 0;
589 foreach ( $hours as $h ) {
590 $hours[$i++] = $this->hoursLink( $h, $options );
591 }
592 $i = 0;
593 foreach ( $days as $d ) {
594 $days[$i++] = $this->daysLink( $d, $options );
595 }
596
597 return $this->msg( 'wlshowlast' )->rawParams(
598 $this->getLanguage()->pipeList( $hours ),
599 $this->getLanguage()->pipeList( $days ) )->parse();
600 }
601
602 /**
603 * Count the number of items on a user's watchlist
604 *
605 * @param IDatabase $dbr A database connection
606 * @return int
607 */
608 protected function countItems( $dbr ) {
609 # Fetch the raw count
610 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
611 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
612 $row = $dbr->fetchObject( $rows );
613 $count = $row->count;
614
615 return floor( $count / 2 );
616 }
617 }