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