Merge "Simplify watchlist edit mode handling"
[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 Watchlist
22 */
23 class SpecialWatchlist extends SpecialPage {
24 protected $customFilters;
25
26 /**
27 * Constructor
28 */
29 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
30 parent::__construct( $page, $restriction );
31 }
32
33 /**
34 * Execute
35 * @param $par Parameter passed to the page
36 */
37 function execute( $par ) {
38 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
39
40 $user = $this->getUser();
41 $output = $this->getOutput();
42
43 # Anons don't get a watchlist
44 if ( $user->isAnon() ) {
45 $output->setPageTitle( $this->msg( 'watchnologin' ) );
46 $output->setRobotPolicy( 'noindex,nofollow' );
47 $llink = Linker::linkKnown(
48 SpecialPage::getTitleFor( 'Userlogin' ),
49 $this->msg( 'loginreqlink' )->escaped(),
50 array(),
51 array( 'returnto' => $this->getTitle()->getPrefixedText() )
52 );
53 $output->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
54 return;
55 }
56
57 // Check permissions
58 $this->checkPermissions();
59
60 // Add feed links
61 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
62 if ( $wlToken ) {
63 $this->addFeedLinks( array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
64 'wlowner' => $user->getName(), 'wltoken' => $wlToken ) );
65 }
66
67 $this->setHeaders();
68 $this->outputHeader();
69
70 $output->addSubtitle( $this->msg( 'watchlistfor2', $user->getName()
71 )->rawParams( SpecialEditWatchlist::buildTools( null ) ) );
72
73 $request = $this->getRequest();
74
75 $mode = SpecialEditWatchlist::getMode( $request, $par );
76 if ( $mode !== false ) {
77 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
78 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
79 } else {
80 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
81 }
82
83 $output->redirect( $title->getLocalURL() );
84 return;
85 }
86
87 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
88
89 $nitems = $this->countItems( $dbr );
90 if ( $nitems == 0 ) {
91 $output->addWikiMsg( 'nowatchlist' );
92 return;
93 }
94
95 // @todo use FormOptions!
96 $defaults = array(
97 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ),
98 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
99 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
100 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
101 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
102 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
103 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
104 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
105 /* ? */ 'namespace' => '', //means all
106 /* ? */ 'invert' => false,
107 /* bool */ 'associated' => false,
108 );
109 $this->customFilters = array();
110 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ) );
111 foreach ( $this->customFilters as $key => $params ) {
112 $defaults[$key] = $params['default'];
113 }
114
115 # Extract variables from the request, falling back to user preferences or
116 # other default values if these don't exist
117 $values = array();
118 $values['days'] = floatval( $request->getVal( 'days', $defaults['days'] ) );
119 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
120 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
121 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
122 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
123 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
124 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
125 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
126 foreach ( $this->customFilters as $key => $params ) {
127 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
128 }
129
130 # Get namespace value, if supplied, and prepare a WHERE fragment
131 $nameSpace = $request->getIntOrNull( 'namespace' );
132 $invert = $request->getBool( 'invert' );
133 $associated = $request->getBool( 'associated' );
134 if ( !is_null( $nameSpace ) ) {
135 $eq_op = $invert ? '!=' : '=';
136 $bool_op = $invert ? 'AND' : 'OR';
137 $nameSpace = intval( $nameSpace ); // paranioa
138 if ( !$associated ) {
139 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
140 } else {
141 $associatedNS = MWNamespace::getAssociated( $nameSpace );
142 $nameSpaceClause =
143 "rc_namespace $eq_op $nameSpace " .
144 $bool_op .
145 " rc_namespace $eq_op $associatedNS";
146 }
147 } else {
148 $nameSpace = '';
149 $nameSpaceClause = '';
150 }
151 $values['namespace'] = $nameSpace;
152 $values['invert'] = $invert;
153 $values['associated'] = $associated;
154
155 // Dump everything here
156 $nondefaults = array();
157 foreach ( $defaults as $name => $defValue ) {
158 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
159 }
160
161 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
162 $request->wasPosted() )
163 {
164 $user->clearAllNotifications();
165 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
166 return;
167 }
168
169 # Possible where conditions
170 $conds = array();
171
172 if ( $values['days'] > 0 ) {
173 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
174 }
175
176 # Toggles
177 if ( $values['hideOwn'] ) {
178 $conds[] = 'rc_user != ' . $user->getId();
179 }
180 if ( $values['hideBots'] ) {
181 $conds[] = 'rc_bot = 0';
182 }
183 if ( $values['hideMinor'] ) {
184 $conds[] = 'rc_minor = 0';
185 }
186 if ( $values['hideLiu'] ) {
187 $conds[] = 'rc_user = 0';
188 }
189 if ( $values['hideAnons'] ) {
190 $conds[] = 'rc_user != 0';
191 }
192 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
193 $conds[] = 'rc_patrolled != 1';
194 }
195 if ( $nameSpaceClause ) {
196 $conds[] = $nameSpaceClause;
197 }
198
199 # Toggle watchlist content (all recent edits or just the latest)
200 if ( $values['extended'] ) {
201 $limitWatchlist = $user->getIntOption( 'wllimit' );
202 $usePage = false;
203 } else {
204 # Top log Ids for a page are not stored
205 $nonRevisionTypes = array( RC_LOG );
206 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
207 if ( $nonRevisionTypes ) {
208 if ( count( $nonRevisionTypes ) === 1 ) {
209 // if only one use an equality instead of IN condition
210 $nonRevisionTypes = reset( $nonRevisionTypes );
211 }
212 $conds[] = $dbr->makeList(
213 array(
214 'rc_this_oldid=page_latest',
215 'rc_type' => $nonRevisionTypes,
216 ),
217 LIST_OR
218 );
219 }
220 $limitWatchlist = 0;
221 $usePage = true;
222 }
223
224 # Show a message about slave lag, if applicable
225 $lag = wfGetLB()->safeGetLag( $dbr );
226 if ( $lag > 0 ) {
227 $output->showLagWarning( $lag );
228 }
229
230 # Create output
231 $form = '';
232
233 # Show watchlist header
234 $form .= "<p>";
235 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
236 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
237 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
238 }
239 if ( $wgShowUpdatedMarker ) {
240 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
241 }
242 $form .= "</p>";
243
244 if ( $wgShowUpdatedMarker ) {
245 $form .= Xml::openElement( 'form', array( 'method' => 'post',
246 'action' => $this->getTitle()->getLocalURL(),
247 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
248 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
249 Html::hidden( 'reset', 'all' ) . "\n";
250 foreach ( $nondefaults as $key => $value ) {
251 $form .= Html::hidden( $key, $value ) . "\n";
252 }
253 $form .= Xml::closeElement( 'form' ) . "\n";
254 }
255
256 $form .= Xml::openElement( 'form', array(
257 'method' => 'post',
258 'action' => $this->getTitle()->getLocalURL(),
259 'id' => 'mw-watchlist-form'
260 ) );
261 $form .= Xml::fieldset(
262 $this->msg( 'watchlist-options' )->text(),
263 false,
264 array( 'id' => 'mw-watchlist-options' )
265 );
266
267 $tables = array( 'recentchanges', 'watchlist' );
268 $fields = RecentChange::selectFields();
269 $join_conds = array(
270 'watchlist' => array(
271 'INNER JOIN',
272 array(
273 'wl_user' => $user->getId(),
274 'wl_namespace=rc_namespace',
275 'wl_title=rc_title'
276 ),
277 ),
278 );
279 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
280 if ( $wgShowUpdatedMarker ) {
281 $fields[] = 'wl_notificationtimestamp';
282 }
283 if ( $limitWatchlist ) {
284 $options['LIMIT'] = $limitWatchlist;
285 }
286
287 $rollbacker = $user->isAllowed( 'rollback' );
288 if ( $usePage || $rollbacker ) {
289 $tables[] = 'page';
290 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
291 if ( $rollbacker ) {
292 $fields[] = 'page_latest';
293 }
294 }
295
296 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
297 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
298
299 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
300 $numRows = $res->numRows();
301
302 /* Start bottom header */
303
304 $lang = $this->getLanguage();
305 $wlInfo = '';
306 if ( $values['days'] > 0 ) {
307 $timestamp = wfTimestampNow();
308 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
309 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
310 }
311
312 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
313
314 # Spit out some control panel links
315 $filters = array(
316 'hideMinor' => 'rcshowhideminor',
317 'hideBots' => 'rcshowhidebots',
318 'hideAnons' => 'rcshowhideanons',
319 'hideLiu' => 'rcshowhideliu',
320 'hideOwn' => 'rcshowhidemine',
321 'hidePatrolled' => 'rcshowhidepatr'
322 );
323 foreach ( $this->customFilters as $key => $params ) {
324 $filters[$key] = $params['msg'];
325 }
326 // Disable some if needed
327 if ( !$user->useNPPatrol() ) {
328 unset( $filters['hidePatrolled'] );
329 }
330
331 $links = array();
332 foreach ( $filters as $name => $msg ) {
333 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
334 }
335
336 $hiddenFields = $nondefaults;
337 unset( $hiddenFields['namespace'] );
338 unset( $hiddenFields['invert'] );
339 unset( $hiddenFields['associated'] );
340
341 # Namespace filter and put the whole form together.
342 $form .= $wlInfo;
343 $form .= $cutofflinks;
344 $form .= $lang->pipeList( $links ) . "\n";
345 $form .= "<hr />\n<p>";
346 $form .= Html::namespaceSelector(
347 array(
348 'selected' => $nameSpace,
349 'all' => '',
350 'label' => $this->msg( 'namespace' )->text()
351 ), array(
352 'name' => 'namespace',
353 'id' => 'namespace',
354 'class' => 'namespaceselector',
355 )
356 ) . '&#160;';
357 $form .= Xml::checkLabel(
358 $this->msg( 'invert' )->text(),
359 'invert',
360 'nsinvert',
361 $invert,
362 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
363 ) . '&#160;';
364 $form .= Xml::checkLabel(
365 $this->msg( 'namespace_association' )->text(),
366 'associated',
367 'associated',
368 $associated,
369 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
370 ) . '&#160;';
371 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
372 foreach ( $hiddenFields as $key => $value ) {
373 $form .= Html::hidden( $key, $value ) . "\n";
374 }
375 $form .= Xml::closeElement( 'fieldset' ) . "\n";
376 $form .= Xml::closeElement( 'form' ) . "\n";
377 $output->addHTML( $form );
378
379 # If there's nothing to show, stop here
380 if ( $numRows == 0 ) {
381 $output->wrapWikiMsg(
382 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
383 );
384 return;
385 }
386
387 /* End bottom header */
388
389 /* Do link batch query */
390 $linkBatch = new LinkBatch;
391 foreach ( $res as $row ) {
392 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
393 if ( $row->rc_user != 0 ) {
394 $linkBatch->add( NS_USER, $userNameUnderscored );
395 }
396 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
397
398 $linkBatch->add( $row->rc_namespace, $row->rc_title );
399 }
400 $linkBatch->execute();
401 $dbr->dataSeek( $res, 0 );
402
403 $list = ChangesList::newFromContext( $this->getContext() );
404 $list->setWatchlistDivs();
405
406 $s = $list->beginRecentChangesList();
407 $counter = 1;
408 foreach ( $res as $obj ) {
409 # Make RC entry
410 $rc = RecentChange::newFromRow( $obj );
411 $rc->counter = $counter++;
412
413 if ( $wgShowUpdatedMarker ) {
414 $updated = $obj->wl_notificationtimestamp;
415 } else {
416 $updated = false;
417 }
418
419 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
420 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
421 'COUNT(*)',
422 array(
423 'wl_namespace' => $obj->rc_namespace,
424 'wl_title' => $obj->rc_title,
425 ),
426 __METHOD__ );
427 } else {
428 $rc->numberofWatchingusers = 0;
429 }
430
431 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
432 if ( $changeLine !== false ) {
433 $s .= $changeLine;
434 }
435 }
436 $s .= $list->endRecentChangesList();
437
438 $output->addHTML( $s );
439 }
440
441 protected function showHideLink( $options, $message, $name, $value ) {
442 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
443 $options[$name] = 1 - (int)$value;
444
445 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
446 }
447
448 protected function hoursLink( $h, $options = array() ) {
449 $options['days'] = ( $h / 24.0 );
450
451 return Linker::linkKnown(
452 $this->getTitle(),
453 $this->getLanguage()->formatNum( $h ),
454 array(),
455 $options
456 );
457 }
458
459 protected function daysLink( $d, $options = array() ) {
460 $options['days'] = $d;
461 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
462
463 return Linker::linkKnown(
464 $this->getTitle(),
465 $message,
466 array(),
467 $options
468 );
469 }
470
471 /**
472 * Returns html
473 *
474 * @param int $days This gets overwritten, so is not used
475 * @param array $options Query parameters for URL
476 * @return string
477 */
478 protected function cutoffLinks( $days, $options = array() ) {
479 $hours = array( 1, 2, 6, 12 );
480 $days = array( 1, 3, 7 );
481 $i = 0;
482 foreach ( $hours as $h ) {
483 $hours[$i++] = $this->hoursLink( $h, $options );
484 }
485 $i = 0;
486 foreach ( $days as $d ) {
487 $days[$i++] = $this->daysLink( $d, $options );
488 }
489 return $this->msg( 'wlshowlast' )->rawParams(
490 $this->getLanguage()->pipeList( $hours ),
491 $this->getLanguage()->pipeList( $days ),
492 $this->daysLink( 0, $options ) )->parse();
493 }
494
495 /**
496 * Count the number of items on a user's watchlist
497 *
498 * @param DatabaseBase $dbr A database connection
499 * @return Integer
500 */
501 protected function countItems( $dbr ) {
502 # Fetch the raw count
503 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
504 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
505 $row = $dbr->fetchObject( $res );
506 $count = $row->count;
507
508 return floor( $count / 2 );
509 }
510
511 protected function getGroupName() {
512 return 'changes';
513 }
514 }