11632a38cffd2fc3f81b388a8098da9946ef5c0b
[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 # TODO: localise?
78 switch ( $mode ) {
79 case SpecialEditWatchlist::EDIT_CLEAR:
80 $mode = 'clear';
81 break;
82 case SpecialEditWatchlist::EDIT_RAW:
83 $mode = 'raw';
84 break;
85 default:
86 $mode = null;
87 }
88 $title = SpecialPage::getTitleFor( 'EditWatchlist', $mode );
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 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
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 $tables = array( 'recentchanges', 'watchlist' );
260 $fields = RecentChange::selectFields();
261 $join_conds = array(
262 'watchlist' => array(
263 'INNER JOIN',
264 array(
265 'wl_user' => $user->getId(),
266 'wl_namespace=rc_namespace',
267 'wl_title=rc_title'
268 ),
269 ),
270 );
271 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
272 if ( $wgShowUpdatedMarker ) {
273 $fields[] = 'wl_notificationtimestamp';
274 }
275 if ( $limitWatchlist ) {
276 $options['LIMIT'] = $limitWatchlist;
277 }
278
279 $rollbacker = $user->isAllowed( 'rollback' );
280 if ( $usePage || $rollbacker ) {
281 $tables[] = 'page';
282 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
283 if ( $rollbacker ) {
284 $fields[] = 'page_latest';
285 }
286 }
287
288 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
289 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
290
291 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
292 $numRows = $res->numRows();
293
294 /* Start bottom header */
295
296 $lang = $this->getLanguage();
297 $wlInfo = '';
298 if ( $values['days'] > 0 ) {
299 $timestamp = wfTimestampNow();
300 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
301 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
302 }
303
304 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
305
306 # Spit out some control panel links
307 $filters = array(
308 'hideMinor' => 'rcshowhideminor',
309 'hideBots' => 'rcshowhidebots',
310 'hideAnons' => 'rcshowhideanons',
311 'hideLiu' => 'rcshowhideliu',
312 'hideOwn' => 'rcshowhidemine',
313 'hidePatrolled' => 'rcshowhidepatr'
314 );
315 foreach ( $this->customFilters as $key => $params ) {
316 $filters[$key] = $params['msg'];
317 }
318 // Disable some if needed
319 if ( !$user->useNPPatrol() ) {
320 unset( $filters['hidePatrolled'] );
321 }
322
323 $links = array();
324 foreach ( $filters as $name => $msg ) {
325 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
326 }
327
328 $hiddenFields = $nondefaults;
329 unset( $hiddenFields['namespace'] );
330 unset( $hiddenFields['invert'] );
331 unset( $hiddenFields['associated'] );
332
333 # Namespace filter and put the whole form together.
334 $form .= $wlInfo;
335 $form .= $cutofflinks;
336 $form .= $lang->pipeList( $links ) . "\n";
337 $form .= "<hr />\n<p>";
338 $form .= Html::namespaceSelector(
339 array(
340 'selected' => $nameSpace,
341 'all' => '',
342 'label' => $this->msg( 'namespace' )->text()
343 ), array(
344 'name' => 'namespace',
345 'id' => 'namespace',
346 'class' => 'namespaceselector',
347 )
348 ) . '&#160;';
349 $form .= Xml::checkLabel(
350 $this->msg( 'invert' )->text(),
351 'invert',
352 'nsinvert',
353 $invert,
354 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
355 ) . '&#160;';
356 $form .= Xml::checkLabel(
357 $this->msg( 'namespace_association' )->text(),
358 'associated',
359 'associated',
360 $associated,
361 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
362 ) . '&#160;';
363 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
364 foreach ( $hiddenFields as $key => $value ) {
365 $form .= Html::hidden( $key, $value ) . "\n";
366 }
367 $form .= Xml::closeElement( 'fieldset' ) . "\n";
368 $form .= Xml::closeElement( 'form' ) . "\n";
369 $output->addHTML( $form );
370
371 # If there's nothing to show, stop here
372 if ( $numRows == 0 ) {
373 $output->wrapWikiMsg(
374 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
375 );
376 return;
377 }
378
379 /* End bottom header */
380
381 /* Do link batch query */
382 $linkBatch = new LinkBatch;
383 foreach ( $res as $row ) {
384 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
385 if ( $row->rc_user != 0 ) {
386 $linkBatch->add( NS_USER, $userNameUnderscored );
387 }
388 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
389
390 $linkBatch->add( $row->rc_namespace, $row->rc_title );
391 }
392 $linkBatch->execute();
393 $dbr->dataSeek( $res, 0 );
394
395 $list = ChangesList::newFromContext( $this->getContext() );
396 $list->setWatchlistDivs();
397
398 $s = $list->beginRecentChangesList();
399 $counter = 1;
400 foreach ( $res as $obj ) {
401 # Make RC entry
402 $rc = RecentChange::newFromRow( $obj );
403 $rc->counter = $counter++;
404
405 if ( $wgShowUpdatedMarker ) {
406 $updated = $obj->wl_notificationtimestamp;
407 } else {
408 $updated = false;
409 }
410
411 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
412 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
413 'COUNT(*)',
414 array(
415 'wl_namespace' => $obj->rc_namespace,
416 'wl_title' => $obj->rc_title,
417 ),
418 __METHOD__ );
419 } else {
420 $rc->numberofWatchingusers = 0;
421 }
422
423 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
424 if ( $changeLine !== false ) {
425 $s .= $changeLine;
426 }
427 }
428 $s .= $list->endRecentChangesList();
429
430 $output->addHTML( $s );
431 }
432
433 protected function showHideLink( $options, $message, $name, $value ) {
434 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
435 $options[$name] = 1 - (int)$value;
436
437 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
438 }
439
440 protected function hoursLink( $h, $options = array() ) {
441 $options['days'] = ( $h / 24.0 );
442
443 return Linker::linkKnown(
444 $this->getTitle(),
445 $this->getLanguage()->formatNum( $h ),
446 array(),
447 $options
448 );
449 }
450
451 protected function daysLink( $d, $options = array() ) {
452 $options['days'] = $d;
453 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
454
455 return Linker::linkKnown(
456 $this->getTitle(),
457 $message,
458 array(),
459 $options
460 );
461 }
462
463 /**
464 * Returns html
465 *
466 * @param int $days This gets overwritten, so is not used
467 * @param array $options Query parameters for URL
468 * @return string
469 */
470 protected function cutoffLinks( $days, $options = array() ) {
471 $hours = array( 1, 2, 6, 12 );
472 $days = array( 1, 3, 7 );
473 $i = 0;
474 foreach ( $hours as $h ) {
475 $hours[$i++] = $this->hoursLink( $h, $options );
476 }
477 $i = 0;
478 foreach ( $days as $d ) {
479 $days[$i++] = $this->daysLink( $d, $options );
480 }
481 return $this->msg( 'wlshowlast' )->rawParams(
482 $this->getLanguage()->pipeList( $hours ),
483 $this->getLanguage()->pipeList( $days ),
484 $this->daysLink( 0, $options ) )->parse();
485 }
486
487 /**
488 * Count the number of items on a user's watchlist
489 *
490 * @param DatabaseBase $dbr A database connection
491 * @return Integer
492 */
493 protected function countItems( $dbr ) {
494 # Fetch the raw count
495 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
496 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
497 $row = $dbr->fetchObject( $res );
498 $count = $row->count;
499
500 return floor( $count / 2 );
501 }
502
503 protected function getGroupName() {
504 return 'changes';
505 }
506 }