mediawiki.page.gallery.resize: Remove weird mw.hook call
[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
22 */
23
24 /**
25 * A special page that lists last changes made to the wiki,
26 * limited to user-defined list of titles.
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialWatchlist extends ChangesListSpecialPage {
31 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
32 parent::__construct( $page, $restriction );
33 }
34
35 /**
36 * Main execution point
37 *
38 * @param string $subpage
39 */
40 function execute( $subpage ) {
41 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
42
43 // Anons don't get a watchlist
44 $this->requireLogin( 'watchlistanontext' );
45
46 $output = $this->getOutput();
47 $request = $this->getRequest();
48
49 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
50 if ( $mode !== false ) {
51 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
52 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
53 } elseif ( $mode === SpecialEditWatchlist::EDIT_CLEAR ) {
54 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'clear' );
55 } else {
56 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
57 }
58
59 $output->redirect( $title->getLocalURL() );
60
61 return;
62 }
63
64 $this->checkPermissions();
65
66 $user = $this->getUser();
67 $opts = $this->getOptions();
68
69 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker )
70 && $request->getVal( 'reset' )
71 && $request->wasPosted()
72 ) {
73 $user->clearAllNotifications();
74 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
75
76 return;
77 }
78
79 parent::execute( $subpage );
80 }
81
82 /**
83 * Return an array of subpages beginning with $search that this special page will accept.
84 *
85 * @param string $search Prefix to search for
86 * @param integer $limit Maximum number of results to return
87 * @return string[] Matching subpages
88 */
89 public function prefixSearchSubpages( $search, $limit = 10 ) {
90 // See also SpecialEditWatchlist::prefixSearchSubpages
91 $subpages = array( 'clear', 'edit', 'raw' );
92 $escaped = preg_quote( $search );
93 return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit );
94 }
95
96 /**
97 * Get a FormOptions object containing the default options
98 *
99 * @return FormOptions
100 */
101 public function getDefaultOptions() {
102 $opts = parent::getDefaultOptions();
103 $user = $this->getUser();
104
105 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
106
107 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
108 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
109 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
110 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
111 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
112 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
113
114 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
115
116 return $opts;
117 }
118
119 /**
120 * Get custom show/hide filters
121 *
122 * @return array Map of filter URL param names to properties (msg/default)
123 */
124 protected function getCustomFilters() {
125 if ( $this->customFilters === null ) {
126 $this->customFilters = parent::getCustomFilters();
127 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ), '1.23' );
128 }
129
130 return $this->customFilters;
131 }
132
133 /**
134 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
135 *
136 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
137 * to the current ones.
138 *
139 * @param FormOptions $opts
140 * @return FormOptions
141 */
142 protected function fetchOptionsFromRequest( $opts ) {
143 static $compatibilityMap = array(
144 'hideMinor' => 'hideminor',
145 'hideBots' => 'hidebots',
146 'hideAnons' => 'hideanons',
147 'hideLiu' => 'hideliu',
148 'hidePatrolled' => 'hidepatrolled',
149 'hideOwn' => 'hidemyself',
150 );
151
152 $params = $this->getRequest()->getValues();
153 foreach ( $compatibilityMap as $from => $to ) {
154 if ( isset( $params[$from] ) ) {
155 $params[$to] = $params[$from];
156 unset( $params[$from] );
157 }
158 }
159
160 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
161 // methods defined on WebRequest and removing this dependency would cause some code duplication.
162 $request = new DerivativeRequest( $this->getRequest(), $params );
163 $opts->fetchValuesFromRequest( $request );
164
165 return $opts;
166 }
167
168 /**
169 * Return an array of conditions depending of options set in $opts
170 *
171 * @param FormOptions $opts
172 * @return array
173 */
174 public function buildMainQueryConds( FormOptions $opts ) {
175 $dbr = $this->getDB();
176 $conds = parent::buildMainQueryConds( $opts );
177
178 // Calculate cutoff
179 if ( $opts['days'] > 0 ) {
180 $conds[] = 'rc_timestamp > ' .
181 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
182 }
183
184 return $conds;
185 }
186
187 /**
188 * Process the query
189 *
190 * @param array $conds
191 * @param FormOptions $opts
192 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
193 */
194 public function doMainQuery( $conds, $opts ) {
195 global $wgShowUpdatedMarker;
196
197 $dbr = $this->getDB();
198 $user = $this->getUser();
199
200 # Toggle watchlist content (all recent edits or just the latest)
201 if ( $opts['extended'] ) {
202 $limitWatchlist = $user->getIntOption( 'wllimit' );
203 $usePage = false;
204 } else {
205 # Top log Ids for a page are not stored
206 $nonRevisionTypes = array( RC_LOG );
207 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
208 if ( $nonRevisionTypes ) {
209 $conds[] = $dbr->makeList(
210 array(
211 'rc_this_oldid=page_latest',
212 'rc_type' => $nonRevisionTypes,
213 ),
214 LIST_OR
215 );
216 }
217 $limitWatchlist = 0;
218 $usePage = true;
219 }
220
221 $tables = array( 'recentchanges', 'watchlist' );
222 $fields = RecentChange::selectFields();
223 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
224 $join_conds = array(
225 'watchlist' => array(
226 'INNER JOIN',
227 array(
228 'wl_user' => $user->getId(),
229 'wl_namespace=rc_namespace',
230 'wl_title=rc_title'
231 ),
232 ),
233 );
234
235 if ( $wgShowUpdatedMarker ) {
236 $fields[] = 'wl_notificationtimestamp';
237 }
238 if ( $limitWatchlist ) {
239 $query_options['LIMIT'] = $limitWatchlist;
240 }
241
242 $rollbacker = $user->isAllowed( 'rollback' );
243 if ( $usePage || $rollbacker ) {
244 $tables[] = 'page';
245 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
246 if ( $rollbacker ) {
247 $fields[] = 'page_latest';
248 }
249 }
250
251 // Log entries with DELETED_ACTION must not show up unless the user has
252 // the necessary rights.
253 if ( !$user->isAllowed( 'deletedhistory' ) ) {
254 $bitmask = LogPage::DELETED_ACTION;
255 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
256 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
257 } else {
258 $bitmask = 0;
259 }
260 if ( $bitmask ) {
261 $conds[] = $dbr->makeList( array(
262 'rc_type != ' . RC_LOG,
263 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
264 ), LIST_OR );
265 }
266
267 ChangeTags::modifyDisplayQuery(
268 $tables,
269 $fields,
270 $conds,
271 $join_conds,
272 $query_options,
273 ''
274 );
275
276 wfRunHooks( 'SpecialWatchlistQuery',
277 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
278 '1.23' );
279
280 return $dbr->select(
281 $tables,
282 $fields,
283 $conds,
284 __METHOD__,
285 $query_options,
286 $join_conds
287 );
288 }
289
290 /**
291 * Return a DatabaseBase object for reading
292 *
293 * @return DatabaseBase
294 */
295 protected function getDB() {
296 return wfGetDB( DB_SLAVE, 'watchlist' );
297 }
298
299 /**
300 * Output feed links.
301 */
302 public function outputFeedLinks() {
303 $user = $this->getUser();
304 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
305 if ( $wlToken ) {
306 $this->addFeedLinks( array(
307 'action' => 'feedwatchlist',
308 'allrev' => 1,
309 'wlowner' => $user->getName(),
310 'wltoken' => $wlToken,
311 ) );
312 }
313 }
314
315 /**
316 * Build and output the actual changes list.
317 *
318 * @param ResultWrapper $rows Database rows
319 * @param FormOptions $opts
320 */
321 public function outputChangesList( $rows, $opts ) {
322 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
323
324 $dbr = $this->getDB();
325 $user = $this->getUser();
326 $output = $this->getOutput();
327
328 # Show a message about slave lag, if applicable
329 $lag = wfGetLB()->safeGetLag( $dbr );
330 if ( $lag > 0 ) {
331 $output->showLagWarning( $lag );
332 }
333
334 $dbr->dataSeek( $rows, 0 );
335
336 $list = ChangesList::newFromContext( $this->getContext() );
337 $list->setWatchlistDivs();
338 $list->initChangesListRows( $rows );
339 $dbr->dataSeek( $rows, 0 );
340
341 $s = $list->beginRecentChangesList();
342 $counter = 1;
343 foreach ( $rows as $obj ) {
344 # Make RC entry
345 $rc = RecentChange::newFromRow( $obj );
346 $rc->counter = $counter++;
347
348 if ( $wgShowUpdatedMarker ) {
349 $updated = $obj->wl_notificationtimestamp;
350 } else {
351 $updated = false;
352 }
353
354 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
355 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
356 'COUNT(*)',
357 array(
358 'wl_namespace' => $obj->rc_namespace,
359 'wl_title' => $obj->rc_title,
360 ),
361 __METHOD__ );
362 } else {
363 $rc->numberofWatchingusers = 0;
364 }
365
366 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
367 if ( $changeLine !== false ) {
368 $s .= $changeLine;
369 }
370 }
371 $s .= $list->endRecentChangesList();
372
373 if ( $rows->numRows() == 0 ) {
374 $output->wrapWikiMsg(
375 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
376 );
377 } else {
378 $output->addHTML( $s );
379 }
380 }
381
382 /**
383 * Return the text to be displayed above the changes
384 *
385 * @param FormOptions $opts
386 * @return string XHTML
387 */
388 public function doHeader( $opts ) {
389 $user = $this->getUser();
390
391 $this->getOutput()->addSubtitle(
392 $this->msg( 'watchlistfor2', $user->getName() )
393 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
394 );
395
396 $this->setTopText( $opts );
397
398 $lang = $this->getLanguage();
399 $wlInfo = '';
400 if ( $opts['days'] > 0 ) {
401 $timestamp = wfTimestampNow();
402 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
403 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
404 )->parse() . "<br />\n";
405 }
406
407 $nondefaults = $opts->getChangedValues();
408 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
409
410 # Spit out some control panel links
411 $filters = array(
412 'hideminor' => 'rcshowhideminor',
413 'hidebots' => 'rcshowhidebots',
414 'hideanons' => 'rcshowhideanons',
415 'hideliu' => 'rcshowhideliu',
416 'hidemyself' => 'rcshowhidemine',
417 'hidepatrolled' => 'rcshowhidepatr'
418 );
419 foreach ( $this->getCustomFilters() as $key => $params ) {
420 $filters[$key] = $params['msg'];
421 }
422 // Disable some if needed
423 if ( !$user->useNPPatrol() ) {
424 unset( $filters['hidepatrolled'] );
425 }
426
427 $links = array();
428 foreach ( $filters as $name => $msg ) {
429 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
430 }
431
432 $hiddenFields = $nondefaults;
433 unset( $hiddenFields['namespace'] );
434 unset( $hiddenFields['invert'] );
435 unset( $hiddenFields['associated'] );
436
437 # Create output
438 $form = '';
439
440 # Namespace filter and put the whole form together.
441 $form .= $wlInfo;
442 $form .= $cutofflinks;
443 $form .= $lang->pipeList( $links ) . "\n";
444 $form .= "<hr />\n<p>";
445 $form .= Html::namespaceSelector(
446 array(
447 'selected' => $opts['namespace'],
448 'all' => '',
449 'label' => $this->msg( 'namespace' )->text()
450 ), array(
451 'name' => 'namespace',
452 'id' => 'namespace',
453 'class' => 'namespaceselector',
454 )
455 ) . '&#160;';
456 $form .= Xml::checkLabel(
457 $this->msg( 'invert' )->text(),
458 'invert',
459 'nsinvert',
460 $opts['invert'],
461 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
462 ) . '&#160;';
463 $form .= Xml::checkLabel(
464 $this->msg( 'namespace_association' )->text(),
465 'associated',
466 'nsassociated',
467 $opts['associated'],
468 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
469 ) . '&#160;';
470 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
471 foreach ( $hiddenFields as $key => $value ) {
472 $form .= Html::hidden( $key, $value ) . "\n";
473 }
474 $form .= Xml::closeElement( 'fieldset' ) . "\n";
475 $form .= Xml::closeElement( 'form' ) . "\n";
476 $this->getOutput()->addHTML( $form );
477
478 $this->setBottomText( $opts );
479 }
480
481 function setTopText( FormOptions $opts ) {
482 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
483
484 $nondefaults = $opts->getChangedValues();
485 $form = "";
486 $user = $this->getUser();
487
488 $dbr = $this->getDB();
489 $numItems = $this->countItems( $dbr );
490
491 // Show watchlist header
492 $form .= "<p>";
493 if ( $numItems == 0 ) {
494 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
495 } else {
496 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
497 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
498 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
499 }
500 if ( $wgShowUpdatedMarker ) {
501 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
502 }
503 }
504 $form .= "</p>";
505
506 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
507 $form .= Xml::openElement( 'form', array( 'method' => 'post',
508 'action' => $this->getPageTitle()->getLocalURL(),
509 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
510 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
511 Html::hidden( 'reset', 'all' ) . "\n";
512 foreach ( $nondefaults as $key => $value ) {
513 $form .= Html::hidden( $key, $value ) . "\n";
514 }
515 $form .= Xml::closeElement( 'form' ) . "\n";
516 }
517
518 $form .= Xml::openElement( 'form', array(
519 'method' => 'post',
520 'action' => $this->getPageTitle()->getLocalURL(),
521 'id' => 'mw-watchlist-form'
522 ) );
523 $form .= Xml::fieldset(
524 $this->msg( 'watchlist-options' )->text(),
525 false,
526 array( 'id' => 'mw-watchlist-options' )
527 );
528
529 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
530
531 $this->getOutput()->addHTML( $form );
532 }
533
534 protected function showHideLink( $options, $message, $name, $value ) {
535 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
536 $options[$name] = 1 - (int)$value;
537
538 return $this->msg( $message )
539 ->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
540 ->escaped();
541 }
542
543 protected function hoursLink( $h, $options = array() ) {
544 $options['days'] = ( $h / 24.0 );
545
546 return Linker::linkKnown(
547 $this->getPageTitle(),
548 $this->getLanguage()->formatNum( $h ),
549 array(),
550 $options
551 );
552 }
553
554 protected function daysLink( $d, $options = array() ) {
555 $options['days'] = $d;
556 $message = $d ? $this->getLanguage()->formatNum( $d )
557 : $this->msg( 'watchlistall2' )->escaped();
558
559 return Linker::linkKnown(
560 $this->getPageTitle(),
561 $message,
562 array(),
563 $options
564 );
565 }
566
567 /**
568 * Returns html
569 *
570 * @param int $days This gets overwritten, so is not used
571 * @param array $options Query parameters for URL
572 * @return string
573 */
574 protected function cutoffLinks( $days, $options = array() ) {
575 $hours = array( 1, 2, 6, 12 );
576 $days = array( 1, 3, 7 );
577 $i = 0;
578 foreach ( $hours as $h ) {
579 $hours[$i++] = $this->hoursLink( $h, $options );
580 }
581 $i = 0;
582 foreach ( $days as $d ) {
583 $days[$i++] = $this->daysLink( $d, $options );
584 }
585
586 return $this->msg( 'wlshowlast' )->rawParams(
587 $this->getLanguage()->pipeList( $hours ),
588 $this->getLanguage()->pipeList( $days ),
589 $this->daysLink( 0, $options ) )->parse();
590 }
591
592 /**
593 * Count the number of items on a user's watchlist
594 *
595 * @param DatabaseBase $dbr A database connection
596 * @return int
597 */
598 protected function countItems( $dbr ) {
599 # Fetch the raw count
600 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
601 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
602 $row = $dbr->fetchObject( $rows );
603 $count = $row->count;
604
605 return floor( $count / 2 );
606 }
607 }