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