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