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