* changed display function for length to Linker::formatRevisionSize
[mediawiki.git] / includes / specials / SpecialEditWatchlist.php
bloba53b581052fc868a10ed537460e3b069c8cfcd06
1 <?php
3 /**
4 * Provides the UI through which users can perform editing
5 * operations on their watchlist
7 * @ingroup Watchlist
8 * @author Rob Church <robchur@gmail.com>
9 */
10 class SpecialEditWatchlist extends UnlistedSpecialPage {
12 /**
13 * Editing modes
15 const EDIT_CLEAR = 1;
16 const EDIT_RAW = 2;
17 const EDIT_NORMAL = 3;
19 protected $successMessage;
21 public function __construct(){
22 parent::__construct( 'EditWatchlist' );
25 /**
26 * Main execution point
28 * @param $user User
29 * @param $output OutputPage
30 * @param $request WebRequest
31 * @param $mode int
33 public function execute( $mode ) {
34 global $wgUser, $wgOut, $wgRequest;
35 if( wfReadOnly() ) {
36 $wgOut->readOnlyPage();
37 return;
40 # Anons don't get a watchlist
41 if( $wgUser->isAnon() ) {
42 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
43 $llink = $wgUser->getSkin()->linkKnown(
44 SpecialPage::getTitleFor( 'Userlogin' ),
45 wfMsgHtml( 'loginreqlink' ),
46 array(),
47 array( 'returnto' => $this->getTitle()->getPrefixedText() )
49 $wgOut->addWikiMsgArray( 'watchlistanontext', array( $llink ), array( 'replaceafter' ) );
50 return;
53 $sub = wfMsgExt(
54 'watchlistfor2',
55 array( 'parseinline', 'replaceafter' ),
56 $wgUser->getName(),
57 SpecialEditWatchlist::buildTools( $wgUser->getSkin() )
59 $wgOut->setSubtitle( $sub );
61 # B/C: $mode used to be waaay down the parameter list, and the first parameter
62 # was $wgUser
63 if( $mode instanceof User ){
64 $args = func_get_args();
65 if( count( $args >= 4 ) ){
66 $mode = $args[3];
69 $mode = self::getMode( $wgRequest, $mode );
71 switch( $mode ) {
72 case self::EDIT_CLEAR:
73 // The "Clear" link scared people too much.
74 // Pass on to the raw editor, from which it's very easy to clear.
76 case self::EDIT_RAW:
77 $wgOut->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
78 $form = $this->getRawForm( $wgUser );
79 if( $form->show() ){
80 $wgOut->addHTML( $this->successMessage );
81 $wgOut->returnToMain();
83 break;
85 case self::EDIT_NORMAL:
86 default:
87 $wgOut->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
88 $form = $this->getNormalForm( $wgUser );
89 if( $form->show() ){
90 $wgOut->addHTML( $this->successMessage );
91 $wgOut->returnToMain();
93 break;
97 /**
98 * Extract a list of titles from a blob of text, returning
99 * (prefixed) strings; unwatchable titles are ignored
101 * @param $list String
102 * @return array
104 private function extractTitles( $list ) {
105 $titles = array();
106 $list = explode( "\n", trim( $list ) );
107 if( !is_array( $list ) ) {
108 return array();
110 foreach( $list as $text ) {
111 $text = trim( $text );
112 if( strlen( $text ) > 0 ) {
113 $title = Title::newFromText( $text );
114 if( $title instanceof Title && $title->isWatchable() ) {
115 $titles[] = $title->getPrefixedText();
119 return array_unique( $titles );
122 public function submitRaw( $data ){
123 global $wgUser, $wgLang;
124 $wanted = $this->extractTitles( $data['Titles'] );
125 $current = $this->getWatchlist( $wgUser );
127 if( count( $wanted ) > 0 ) {
128 $toWatch = array_diff( $wanted, $current );
129 $toUnwatch = array_diff( $current, $wanted );
130 $this->watchTitles( $toWatch, $wgUser );
131 $this->unwatchTitles( $toUnwatch, $wgUser );
132 $wgUser->invalidateCache();
134 if( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 ){
135 $this->successMessage = wfMessage( 'watchlistedit-raw-done' )->parse();
136 } else {
137 return false;
140 if( count( $toWatch ) > 0 ) {
141 $this->successMessage .= wfMessage(
142 'watchlistedit-raw-added',
143 $wgLang->formatNum( count( $toWatch ) )
145 $this->showTitles( $toWatch, $this->successMessage, $wgUser->getSkin() );
148 if( count( $toUnwatch ) > 0 ) {
149 $this->successMessage .= wfMessage(
150 'watchlistedit-raw-removed',
151 $wgLang->formatNum( count( $toUnwatch ) )
153 $this->showTitles( $toUnwatch, $this->successMessage, $wgUser->getSkin() );
155 } else {
156 $this->clearWatchlist( $wgUser );
157 $wgUser->invalidateCache();
158 $this->successMessage .= wfMessage(
159 'watchlistedit-raw-removed',
160 $wgLang->formatNum( count( $current ) )
162 $this->showTitles( $current, $this->successMessage, $wgUser->getSkin() );
164 return true;
168 * Print out a list of linked titles
170 * $titles can be an array of strings or Title objects; the former
171 * is preferred, since Titles are very memory-heavy
173 * @param $titles array of strings, or Title objects
174 * @param $output String
175 * @param $skin Skin
177 private function showTitles( $titles, &$output, $skin ) {
178 $talk = wfMsgHtml( 'talkpagelinktext' );
179 // Do a batch existence check
180 $batch = new LinkBatch();
181 foreach( $titles as $title ) {
182 if( !$title instanceof Title ) {
183 $title = Title::newFromText( $title );
185 if( $title instanceof Title ) {
186 $batch->addObj( $title );
187 $batch->addObj( $title->getTalkPage() );
190 $batch->execute();
191 // Print out the list
192 $output .= "<ul>\n";
193 foreach( $titles as $title ) {
194 if( !$title instanceof Title ) {
195 $title = Title::newFromText( $title );
197 if( $title instanceof Title ) {
198 $output .= "<li>"
199 . $skin->link( $title )
200 . ' (' . $skin->link( $title->getTalkPage(), $talk )
201 . ")</li>\n";
204 $output .= "</ul>\n";
208 * Count the number of titles on a user's watchlist, excluding talk pages
210 * @param $user User
211 * @return int
213 private function countWatchlist( $user ) {
214 $dbr = wfGetDB( DB_MASTER );
215 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__ );
216 $row = $dbr->fetchObject( $res );
217 return ceil( $row->count / 2 ); // Paranoia
221 * Prepare a list of titles on a user's watchlist (excluding talk pages)
222 * and return an array of (prefixed) strings
224 * @param $user User
225 * @return array
227 private function getWatchlist( $user ) {
228 $list = array();
229 $dbr = wfGetDB( DB_MASTER );
230 $res = $dbr->select(
231 'watchlist',
232 '*',
233 array(
234 'wl_user' => $user->getId(),
236 __METHOD__
238 if( $res->numRows() > 0 ) {
239 foreach ( $res as $row ) {
240 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
241 if( $title instanceof Title && !$title->isTalkPage() )
242 $list[] = $title->getPrefixedText();
244 $res->free();
246 return $list;
250 * Get a list of titles on a user's watchlist, excluding talk pages,
251 * and return as a two-dimensional array with namespace, title and
252 * redirect status
254 * @param $user User
255 * @return array
257 private function getWatchlistInfo( $user ) {
258 $titles = array();
259 $dbr = wfGetDB( DB_MASTER );
261 $res = $dbr->select(
262 array( 'watchlist', 'page' ),
263 array(
264 'wl_namespace',
265 'wl_title',
266 'page_id',
267 'page_len',
268 'page_is_redirect',
269 'page_latest'
271 array( 'wl_user' => $user->getId() ),
272 __METHOD__,
273 array( 'ORDER BY' => 'wl_namespace, wl_title' ),
274 array( 'page' => array(
275 'LEFT JOIN',
276 'wl_namespace = page_namespace AND wl_title = page_title'
280 if( $res && $dbr->numRows( $res ) > 0 ) {
281 $cache = LinkCache::singleton();
282 foreach ( $res as $row ) {
283 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
284 if( $title instanceof Title ) {
285 // Update the link cache while we're at it
286 if( $row->page_id ) {
287 $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect, $row->page_latest );
288 } else {
289 $cache->addBadLinkObj( $title );
291 // Ignore non-talk
292 if( !$title->isTalkPage() ) {
293 $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
298 return $titles;
302 * Show a message indicating the number of items on the user's watchlist,
303 * and return this count for additional checking
305 * @param $output OutputPage
306 * @param $user User
307 * @return int
309 private function showItemCount( $output, $user ) {
310 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
311 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
312 $GLOBALS['wgLang']->formatNum( $count ) ) );
313 } else {
314 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
316 return $count;
320 * Remove all titles from a user's watchlist
322 * @param $user User
324 private function clearWatchlist( $user ) {
325 $dbw = wfGetDB( DB_MASTER );
326 $dbw->delete(
327 'watchlist',
328 array( 'wl_user' => $user->getId() ),
329 __METHOD__
334 * Add a list of titles to a user's watchlist
336 * $titles can be an array of strings or Title objects; the former
337 * is preferred, since Titles are very memory-heavy
339 * @param $titles Array of strings, or Title objects
340 * @param $user User
342 private function watchTitles( $titles, $user ) {
343 $dbw = wfGetDB( DB_MASTER );
344 $rows = array();
345 foreach( $titles as $title ) {
346 if( !$title instanceof Title ) {
347 $title = Title::newFromText( $title );
349 if( $title instanceof Title ) {
350 $rows[] = array(
351 'wl_user' => $user->getId(),
352 'wl_namespace' => ( $title->getNamespace() & ~1 ),
353 'wl_title' => $title->getDBkey(),
354 'wl_notificationtimestamp' => null,
356 $rows[] = array(
357 'wl_user' => $user->getId(),
358 'wl_namespace' => ( $title->getNamespace() | 1 ),
359 'wl_title' => $title->getDBkey(),
360 'wl_notificationtimestamp' => null,
364 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
368 * Remove a list of titles from a user's watchlist
370 * $titles can be an array of strings or Title objects; the former
371 * is preferred, since Titles are very memory-heavy
373 * @param $titles Array of strings, or Title objects
374 * @param $user User
376 private function unwatchTitles( $titles, $user ) {
377 $dbw = wfGetDB( DB_MASTER );
378 foreach( $titles as $title ) {
379 if( !$title instanceof Title ) {
380 $title = Title::newFromText( $title );
382 if( $title instanceof Title ) {
383 $dbw->delete(
384 'watchlist',
385 array(
386 'wl_user' => $user->getId(),
387 'wl_namespace' => ( $title->getNamespace() & ~1 ),
388 'wl_title' => $title->getDBkey(),
390 __METHOD__
392 $dbw->delete(
393 'watchlist',
394 array(
395 'wl_user' => $user->getId(),
396 'wl_namespace' => ( $title->getNamespace() | 1 ),
397 'wl_title' => $title->getDBkey(),
399 __METHOD__
401 $article = new Article($title);
402 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
407 public function submitNormal( $data ) {
408 global $wgUser;
409 $removed = array();
411 foreach( $data as $titles ) {
412 $this->unwatchTitles( $titles, $wgUser );
413 $removed += $titles;
416 if( count( $removed ) > 0 ) {
417 global $wgLang;
418 $this->successMessage = wfMessage(
419 'watchlistedit-normal-done',
420 $wgLang->formatNum( count( $removed ) )
422 $this->showTitles( $removed, $this->successMessage, $wgUser->getSkin() );
423 return true;
424 } else {
425 return false;
430 * Get the standard watchlist editing form
432 * @param $user User
433 * @return HTMLForm
435 protected function getNormalForm( $user ){
436 global $wgContLang;
437 $skin = $user->getSkin();
438 $fields = array();
440 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ){
442 $namespace == NS_MAIN
443 ? wfMsgHtml( 'blanknamespace' )
444 : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
446 $fields['TitlesNs'.$namespace] = array(
447 'type' => 'multiselect',
448 'options' => array(),
449 'section' => "ns$namespace",
452 foreach( $pages as $dbkey => $redirect ){
453 $title = Title::makeTitleSafe( $namespace, $dbkey );
454 $text = $this->buildRemoveLine( $title, $redirect, $skin );
455 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
459 $form = new EditWatchlistNormalHTMLForm( $fields );
460 $form->setTitle( $this->getTitle() );
461 $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
462 $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
463 $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
464 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
465 return $form;
469 * Build the label for a checkbox, with a link to the title, and various additional bits
471 * @param $title Title
472 * @param $redirect bool
473 * @param $skin Skin
474 * @return string
476 private function buildRemoveLine( $title, $redirect, $skin ) {
477 global $wgLang;
479 $link = $skin->link( $title );
480 if( $redirect ) {
481 $link = '<span class="watchlistredir">' . $link . '</span>';
483 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
484 if( $title->exists() ) {
485 $tools[] = $skin->link(
486 $title,
487 wfMsgHtml( 'history_short' ),
488 array(),
489 array( 'action' => 'history' ),
490 array( 'known', 'noclasses' )
493 if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
494 $tools[] = $skin->link(
495 SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
496 wfMsgHtml( 'contributions' ),
497 array(),
498 array(),
499 array( 'known', 'noclasses' )
503 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
505 return $link . " (" . $wgLang->pipeList( $tools ) . ")";
509 * Get a form for editing the watchlist in "raw" mode
511 * @param $user User
512 * @return HTMLForm
514 protected function getRawForm( $user ){
515 $titles = implode( array_map( 'htmlspecialchars', $this->getWatchlist( $user ) ), "\n" );
516 $fields = array(
517 'Titles' => array(
518 'type' => 'textarea',
519 'label-message' => 'watchlistedit-raw-titles',
520 'default' => $titles,
523 $form = new HTMLForm( $fields );
524 $form->setTitle( $this->getTitle( 'raw' ) );
525 $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
526 $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
527 $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
528 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
529 return $form;
533 * Determine whether we are editing the watchlist, and if so, what
534 * kind of editing operation
536 * @param $request WebRequest
537 * @param $par mixed
538 * @return int
540 public static function getMode( $request, $par ) {
541 $mode = strtolower( $request->getVal( 'action', $par ) );
542 switch( $mode ) {
543 case 'clear':
544 case self::EDIT_CLEAR:
545 return self::EDIT_CLEAR;
547 case 'raw':
548 case self::EDIT_RAW:
549 return self::EDIT_RAW;
551 case 'edit':
552 case self::EDIT_NORMAL:
553 return self::EDIT_NORMAL;
555 default:
556 return false;
561 * Build a set of links for convenient navigation
562 * between watchlist viewing and editing modes
564 * @param $skin Skin to use
565 * @return string
567 public static function buildTools( $skin ) {
568 global $wgLang;
570 $tools = array();
571 $modes = array(
572 'view' => array( 'Watchlist', false ),
573 'edit' => array( 'EditWatchlist', false ),
574 'raw' => array( 'EditWatchlist', 'raw' ),
576 foreach( $modes as $mode => $arr ) {
577 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
578 $tools[] = $skin->linkKnown(
579 SpecialPage::getTitleFor( $arr[0], $arr[1] ),
580 wfMsgHtml( "watchlisttools-{$mode}" )
583 return Html::rawElement( 'span',
584 array( 'class' => 'mw-watchlist-toollinks' ),
585 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
589 # B/C since 1.18
590 class WatchlistEditor extends SpecialEditWatchlist {}
593 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
595 class EditWatchlistNormalHTMLForm extends HTMLForm {
596 public function getLegend( $namespace ){
597 global $wgLang;
598 $namespace = substr( $namespace, 2 );
599 return $namespace == NS_MAIN
600 ? wfMsgHtml( 'blanknamespace' )
601 : htmlspecialchars( $wgLang->getFormattedNsText( $namespace ) );