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