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