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