Merge "(bug 47070) check content model namespace on import."
[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 = 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 $user = $this->getUser();
392
393 $this->setTopText( $opts );
394
395 $lang = $this->getLanguage();
396 $wlInfo = '';
397 if ( $opts['days'] > 0 ) {
398 $timestamp = wfTimestampNow();
399 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
400 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
401 }
402
403 $nondefaults = $opts->getChangedValues();
404 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
405
406 # Spit out some control panel links
407 $filters = array(
408 'hideminor' => 'rcshowhideminor',
409 'hidebots' => 'rcshowhidebots',
410 'hideanons' => 'rcshowhideanons',
411 'hideliu' => 'rcshowhideliu',
412 'hidemyself' => 'rcshowhidemine',
413 'hidepatrolled' => 'rcshowhidepatr'
414 );
415 foreach ( $this->getCustomFilters() as $key => $params ) {
416 $filters[$key] = $params['msg'];
417 }
418 // Disable some if needed
419 if ( !$user->useNPPatrol() ) {
420 unset( $filters['hidepatrolled'] );
421 }
422
423 $links = array();
424 foreach ( $filters as $name => $msg ) {
425 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
426 }
427
428 $hiddenFields = $nondefaults;
429 unset( $hiddenFields['namespace'] );
430 unset( $hiddenFields['invert'] );
431 unset( $hiddenFields['associated'] );
432
433 # Create output
434 $form = '';
435
436 # Namespace filter and put the whole form together.
437 $form .= $wlInfo;
438 $form .= $cutofflinks;
439 $form .= $lang->pipeList( $links ) . "\n";
440 $form .= "<hr />\n<p>";
441 $form .= Html::namespaceSelector(
442 array(
443 'selected' => $opts['namespace'],
444 'all' => '',
445 'label' => $this->msg( 'namespace' )->text()
446 ), array(
447 'name' => 'namespace',
448 'id' => 'namespace',
449 'class' => 'namespaceselector',
450 )
451 ) . '&#160;';
452 $form .= Xml::checkLabel(
453 $this->msg( 'invert' )->text(),
454 'invert',
455 'nsinvert',
456 $opts['invert'],
457 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
458 ) . '&#160;';
459 $form .= Xml::checkLabel(
460 $this->msg( 'namespace_association' )->text(),
461 'associated',
462 'nsassociated',
463 $opts['associated'],
464 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
465 ) . '&#160;';
466 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
467 foreach ( $hiddenFields as $key => $value ) {
468 $form .= Html::hidden( $key, $value ) . "\n";
469 }
470 $form .= Xml::closeElement( 'fieldset' ) . "\n";
471 $form .= Xml::closeElement( 'form' ) . "\n";
472 $this->getOutput()->addHTML( $form );
473
474 $this->setBottomText( $opts );
475 }
476
477 function setTopText( FormOptions $opts ) {
478 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
479
480 $nondefaults = $opts->getChangedValues();
481 $form = "";
482 $user = $this->getUser();
483
484 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
485 $numItems = $this->countItems( $dbr );
486
487 // Show watchlist header
488 $form .= "<p>";
489 if ( $numItems == 0 ) {
490 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
491 } else {
492 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
493 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
494 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
495 }
496 if ( $wgShowUpdatedMarker ) {
497 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
498 }
499 }
500 $form .= "</p>";
501
502 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
503 $form .= Xml::openElement( 'form', array( 'method' => 'post',
504 'action' => $this->getPageTitle()->getLocalURL(),
505 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
506 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
507 Html::hidden( 'reset', 'all' ) . "\n";
508 foreach ( $nondefaults as $key => $value ) {
509 $form .= Html::hidden( $key, $value ) . "\n";
510 }
511 $form .= Xml::closeElement( 'form' ) . "\n";
512 }
513
514 $form .= Xml::openElement( 'form', array(
515 'method' => 'post',
516 'action' => $this->getPageTitle()->getLocalURL(),
517 'id' => 'mw-watchlist-form'
518 ) );
519 $form .= Xml::fieldset(
520 $this->msg( 'watchlist-options' )->text(),
521 false,
522 array( 'id' => 'mw-watchlist-options' )
523 );
524
525 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
526
527 $this->getOutput()->addHTML( $form );
528 }
529
530 protected function showHideLink( $options, $message, $name, $value ) {
531 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
532 $options[$name] = 1 - (int)$value;
533
534 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
535 }
536
537 protected function hoursLink( $h, $options = array() ) {
538 $options['days'] = ( $h / 24.0 );
539
540 return Linker::linkKnown(
541 $this->getPageTitle(),
542 $this->getLanguage()->formatNum( $h ),
543 array(),
544 $options
545 );
546 }
547
548 protected function daysLink( $d, $options = array() ) {
549 $options['days'] = $d;
550 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
551
552 return Linker::linkKnown(
553 $this->getPageTitle(),
554 $message,
555 array(),
556 $options
557 );
558 }
559
560 /**
561 * Returns html
562 *
563 * @param int $days This gets overwritten, so is not used
564 * @param array $options Query parameters for URL
565 * @return string
566 */
567 protected function cutoffLinks( $days, $options = array() ) {
568 $hours = array( 1, 2, 6, 12 );
569 $days = array( 1, 3, 7 );
570 $i = 0;
571 foreach ( $hours as $h ) {
572 $hours[$i++] = $this->hoursLink( $h, $options );
573 }
574 $i = 0;
575 foreach ( $days as $d ) {
576 $days[$i++] = $this->daysLink( $d, $options );
577 }
578 return $this->msg( 'wlshowlast' )->rawParams(
579 $this->getLanguage()->pipeList( $hours ),
580 $this->getLanguage()->pipeList( $days ),
581 $this->daysLink( 0, $options ) )->parse();
582 }
583
584 /**
585 * Count the number of items on a user's watchlist
586 *
587 * @param DatabaseBase $dbr A database connection
588 * @return Integer
589 */
590 protected function countItems( $dbr ) {
591 # Fetch the raw count
592 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
593 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
594 $row = $dbr->fetchObject( $rows );
595 $count = $row->count;
596
597 return floor( $count / 2 );
598 }
599 }