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