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