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