Followup r79707, nicer whitespace. Beyond this, I'm beyond caring...
[mediawiki.git] / includes / WatchlistEditor.php
bloba1a33640dacab2ed5735723455f00352e61f8b50
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 WatchlistEditor {
12 /**
13 * Editing modes
15 const EDIT_CLEAR = 1;
16 const EDIT_RAW = 2;
17 const EDIT_NORMAL = 3;
19 /**
20 * Main execution point
22 * @param $user User
23 * @param $output OutputPage
24 * @param $request WebRequest
25 * @param $mode int
27 public function execute( $user, $output, $request, $mode ) {
28 global $wgUser;
29 if( wfReadOnly() ) {
30 $output->readOnlyPage();
31 return;
33 switch( $mode ) {
34 case self::EDIT_CLEAR:
35 // The "Clear" link scared people too much.
36 // Pass on to the raw editor, from which it's very easy to clear.
37 case self::EDIT_RAW:
38 $output->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
39 if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
40 $wanted = $this->extractTitles( $request->getText( 'titles' ) );
41 $current = $this->getWatchlist( $user );
42 if( count( $wanted ) > 0 ) {
43 $toWatch = array_diff( $wanted, $current );
44 $toUnwatch = array_diff( $current, $wanted );
45 $this->watchTitles( $toWatch, $user );
46 $this->unwatchTitles( $toUnwatch, $user );
47 $user->invalidateCache();
48 if( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 )
49 $output->addHTML( wfMsgExt( 'watchlistedit-raw-done', 'parse' ) );
50 if( ( $count = count( $toWatch ) ) > 0 ) {
51 $output->addHTML( wfMsgExt( 'watchlistedit-raw-added', 'parse', $count ) );
52 $this->showTitles( $toWatch, $output, $wgUser->getSkin() );
54 if( ( $count = count( $toUnwatch ) ) > 0 ) {
55 $output->addHTML( wfMsgExt( 'watchlistedit-raw-removed', 'parse', $count ) );
56 $this->showTitles( $toUnwatch, $output, $wgUser->getSkin() );
58 } else {
59 $this->clearWatchlist( $user );
60 $user->invalidateCache();
61 $output->addHTML( wfMsgExt( 'watchlistedit-raw-removed', 'parse', count( $current ) ) );
62 $this->showTitles( $current, $output, $wgUser->getSkin() );
65 $this->showRawForm( $output, $user );
66 break;
67 case self::EDIT_NORMAL:
68 $output->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
69 if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
70 $titles = $this->extractTitles( $request->getArray( 'titles' ) );
71 $this->unwatchTitles( $titles, $user );
72 $user->invalidateCache();
73 $output->addHTML( wfMsgExt( 'watchlistedit-normal-done', 'parse',
74 $GLOBALS['wgLang']->formatNum( count( $titles ) ) ) );
75 $this->showTitles( $titles, $output, $wgUser->getSkin() );
77 $this->showNormalForm( $output, $user );
81 /**
82 * Check the edit token from a form submission
84 * @param $request WebRequest
85 * @param $user User
86 * @return bool
88 private function checkToken( $request, $user ) {
89 return $user->matchEditToken( $request->getVal( 'token' ), 'watchlistedit' );
92 /**
93 * Extract a list of titles from a blob of text, returning
94 * (prefixed) strings; unwatchable titles are ignored
96 * @param $list mixed
97 * @return array
99 private function extractTitles( $list ) {
100 $titles = array();
101 if( !is_array( $list ) ) {
102 $list = explode( "\n", trim( $list ) );
103 if( !is_array( $list ) ) {
104 return array();
107 foreach( $list as $text ) {
108 $text = trim( $text );
109 if( strlen( $text ) > 0 ) {
110 $title = Title::newFromText( $text );
111 if( $title instanceof Title && $title->isWatchable() ) {
112 $titles[] = $title->getPrefixedText();
116 return array_unique( $titles );
120 * Print out a list of linked titles
122 * $titles can be an array of strings or Title objects; the former
123 * is preferred, since Titles are very memory-heavy
125 * @param $titles An array of strings, or Title objects
126 * @param $output OutputPage
127 * @param $skin Skin
129 private function showTitles( $titles, $output, $skin ) {
130 $talk = wfMsgHtml( 'talkpagelinktext' );
131 // Do a batch existence check
132 $batch = new LinkBatch();
133 foreach( $titles as $title ) {
134 if( !$title instanceof Title ) {
135 $title = Title::newFromText( $title );
137 if( $title instanceof Title ) {
138 $batch->addObj( $title );
139 $batch->addObj( $title->getTalkPage() );
142 $batch->execute();
143 // Print out the list
144 $output->addHTML( "<ul>\n" );
145 foreach( $titles as $title ) {
146 if( !$title instanceof Title ) {
147 $title = Title::newFromText( $title );
149 if( $title instanceof Title ) {
150 $output->addHTML( "<li>" . $skin->link( $title )
151 . ' (' . $skin->link( $title->getTalkPage(), $talk ) . ")</li>\n" );
154 $output->addHTML( "</ul>\n" );
158 * Count the number of titles on a user's watchlist, excluding talk pages
160 * @param $user User
161 * @return int
163 private function countWatchlist( $user ) {
164 $dbr = wfGetDB( DB_MASTER );
165 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__ );
166 $row = $dbr->fetchObject( $res );
167 return ceil( $row->count / 2 ); // Paranoia
171 * Prepare a list of titles on a user's watchlist (excluding talk pages)
172 * and return an array of (prefixed) strings
174 * @param $user User
175 * @return array
177 private function getWatchlist( $user ) {
178 $list = array();
179 $dbr = wfGetDB( DB_MASTER );
180 $res = $dbr->select(
181 'watchlist',
182 '*',
183 array(
184 'wl_user' => $user->getId(),
186 __METHOD__
188 if( $res->numRows() > 0 ) {
189 foreach ( $res as $row ) {
190 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
191 if( $title instanceof Title && !$title->isTalkPage() )
192 $list[] = $title->getPrefixedText();
194 $res->free();
196 return $list;
200 * Get a list of titles on a user's watchlist, excluding talk pages,
201 * and return as a two-dimensional array with namespace, title and
202 * redirect status
204 * @param $user User
205 * @return array
207 private function getWatchlistInfo( $user ) {
208 $titles = array();
209 $dbr = wfGetDB( DB_MASTER );
211 $res = $dbr->select(
212 array( 'watchlist', 'page' ),
213 array( 'wl_namespace', 'wl_title', 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
214 array( 'wl_user' => $user->getId() ),
215 __METHOD__,
216 $dbr->implicitOrderby() ? array( ) : array( 'ORDER BY' => 'wl_title' ),
217 array( 'page' =>
218 array( 'LEFT JOIN', 'wl_namespace = page_namespace AND wl_title = page_title' ) )
221 if( $res && $dbr->numRows( $res ) > 0 ) {
222 $cache = LinkCache::singleton();
223 foreach ( $res as $row ) {
224 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
225 if( $title instanceof Title ) {
226 // Update the link cache while we're at it
227 if( $row->page_id ) {
228 $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect, $row->page_latest );
229 } else {
230 $cache->addBadLinkObj( $title );
232 // Ignore non-talk
233 if( !$title->isTalkPage() ) {
234 $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
239 return $titles;
243 * Show a message indicating the number of items on the user's watchlist,
244 * and return this count for additional checking
246 * @param $output OutputPage
247 * @param $user User
248 * @return int
250 private function showItemCount( $output, $user ) {
251 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
252 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
253 $GLOBALS['wgLang']->formatNum( $count ) ) );
254 } else {
255 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
257 return $count;
261 * Remove all titles from a user's watchlist
263 * @param $user User
265 private function clearWatchlist( $user ) {
266 $dbw = wfGetDB( DB_MASTER );
267 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__ );
271 * Add a list of titles to a user's watchlist
273 * $titles can be an array of strings or Title objects; the former
274 * is preferred, since Titles are very memory-heavy
276 * @param $titles An array of strings, or Title objects
277 * @param $user User
279 private function watchTitles( $titles, $user ) {
280 $dbw = wfGetDB( DB_MASTER );
281 $rows = array();
282 foreach( $titles as $title ) {
283 if( !$title instanceof Title ) {
284 $title = Title::newFromText( $title );
286 if( $title instanceof Title ) {
287 $rows[] = array(
288 'wl_user' => $user->getId(),
289 'wl_namespace' => ( $title->getNamespace() & ~1 ),
290 'wl_title' => $title->getDBkey(),
291 'wl_notificationtimestamp' => null,
293 $rows[] = array(
294 'wl_user' => $user->getId(),
295 'wl_namespace' => ( $title->getNamespace() | 1 ),
296 'wl_title' => $title->getDBkey(),
297 'wl_notificationtimestamp' => null,
301 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
305 * Remove a list of titles from a user's watchlist
307 * $titles can be an array of strings or Title objects; the former
308 * is preferred, since Titles are very memory-heavy
310 * @param $titles An array of strings, or Title objects
311 * @param $user User
313 private function unwatchTitles( $titles, $user ) {
314 $dbw = wfGetDB( DB_MASTER );
315 foreach( $titles as $title ) {
316 if( !$title instanceof Title ) {
317 $title = Title::newFromText( $title );
319 if( $title instanceof Title ) {
320 $dbw->delete(
321 'watchlist',
322 array(
323 'wl_user' => $user->getId(),
324 'wl_namespace' => ( $title->getNamespace() & ~1 ),
325 'wl_title' => $title->getDBkey(),
327 __METHOD__
329 $dbw->delete(
330 'watchlist',
331 array(
332 'wl_user' => $user->getId(),
333 'wl_namespace' => ( $title->getNamespace() | 1 ),
334 'wl_title' => $title->getDBkey(),
336 __METHOD__
338 $article = new Article($title);
339 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
345 * Show the standard watchlist editing form
347 * @param $output OutputPage
348 * @param $user User
350 private function showNormalForm( $output, $user ) {
351 global $wgUser;
352 $count = $this->showItemCount( $output, $user );
353 if( $count > 0 ) {
354 $self = SpecialPage::getTitleFor( 'Watchlist' );
355 $form = Xml::openElement( 'form', array( 'method' => 'post',
356 'action' => $self->getLocalUrl( array( 'action' => 'edit' ) ) ) );
357 $form .= Html::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
358 $form .= "<fieldset>\n<legend>" . wfMsgHtml( 'watchlistedit-normal-legend' ) . "</legend>";
359 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
360 $form .= $this->buildRemoveList( $user, $wgUser->getSkin() );
361 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
362 $form .= '</fieldset></form>';
363 $output->addHTML( $form );
368 * Build the part of the standard watchlist editing form with the actual
369 * title selection checkboxes and stuff. Also generates a table of
370 * contents if there's more than one heading.
372 * @param $user User
373 * @param $skin Skin (really, Linker)
375 private function buildRemoveList( $user, $skin ) {
376 $list = "";
377 $toc = $skin->tocIndent();
378 $tocLength = 0;
379 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
380 $tocLength++;
381 $heading = htmlspecialchars( $this->getNamespaceHeading( $namespace ) );
382 $anchor = "editwatchlist-ns" . $namespace;
384 $list .= $skin->makeHeadLine( 2, ">", $anchor, $heading, "" );
385 $toc .= $skin->tocLine( $anchor, $heading, $tocLength, 1 ) . $skin->tocLineEnd();
387 $list .= "<ul>\n";
388 foreach( $pages as $dbkey => $redirect ) {
389 $title = Title::makeTitleSafe( $namespace, $dbkey );
390 $list .= $this->buildRemoveLine( $title, $redirect, $skin );
392 $list .= "</ul>\n";
394 // ISSUE: omit the TOC if the total number of titles is low?
395 if( $tocLength > 1 ) {
396 $list = $skin->tocList( $toc ) . $list;
398 return $list;
402 * Get the correct "heading" for a namespace
404 * @param $namespace int
405 * @return string
407 private function getNamespaceHeading( $namespace ) {
408 return $namespace == NS_MAIN
409 ? wfMsgHtml( 'blanknamespace' )
410 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
414 * Build a single list item containing a check box selecting a title
415 * and a link to that title, with various additional bits
417 * @param $title Title
418 * @param $redirect bool
419 * @param $skin Skin
420 * @return string
422 private function buildRemoveLine( $title, $redirect, $skin ) {
423 global $wgLang;
425 $link = $skin->link( $title );
426 if( $redirect ) {
427 $link = '<span class="watchlistredir">' . $link . '</span>';
429 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
430 if( $title->exists() ) {
431 $tools[] = $skin->link(
432 $title,
433 wfMsgHtml( 'history_short' ),
434 array(),
435 array( 'action' => 'history' ),
436 array( 'known', 'noclasses' )
439 if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
440 $tools[] = $skin->link(
441 SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
442 wfMsgHtml( 'contributions' ),
443 array(),
444 array(),
445 array( 'known', 'noclasses' )
449 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
451 return "<li>"
452 . Xml::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
453 . $link . " (" . $wgLang->pipeList( $tools ) . ")" . "</li>\n";
457 * Show a form for editing the watchlist in "raw" mode
459 * @param $output OutputPage
460 * @param $user User
462 public function showRawForm( $output, $user ) {
463 global $wgUser;
464 $this->showItemCount( $output, $user );
465 $self = SpecialPage::getTitleFor( 'Watchlist' );
466 $form = Xml::openElement( 'form', array( 'method' => 'post',
467 'action' => $self->getLocalUrl( array( 'action' => 'raw' ) ) ) );
468 $form .= Html::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
469 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
470 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
471 $form .= Xml::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
472 $form .= "<br />\n";
473 $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
474 'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
475 $titles = $this->getWatchlist( $user );
476 foreach( $titles as $title ) {
477 $form .= htmlspecialchars( $title ) . "\n";
479 $form .= '</textarea>';
480 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
481 $form .= '</fieldset></form>';
482 $output->addHTML( $form );
486 * Determine whether we are editing the watchlist, and if so, what
487 * kind of editing operation
489 * @param $request WebRequest
490 * @param $par mixed
491 * @return int
493 public static function getMode( $request, $par ) {
494 $mode = strtolower( $request->getVal( 'action', $par ) );
495 switch( $mode ) {
496 case 'clear':
497 return self::EDIT_CLEAR;
498 case 'raw':
499 return self::EDIT_RAW;
500 case 'edit':
501 return self::EDIT_NORMAL;
502 default:
503 return false;
508 * Build a set of links for convenient navigation
509 * between watchlist viewing and editing modes
511 * @param $skin Skin to use
512 * @return string
514 public static function buildTools( $skin ) {
515 global $wgLang;
517 $tools = array();
518 $modes = array( 'view' => false, 'edit' => 'edit', 'raw' => 'raw' );
519 foreach( $modes as $mode => $subpage ) {
520 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
521 $tools[] = $skin->linkKnown(
522 SpecialPage::getTitleFor( 'Watchlist', $subpage ),
523 wfMsgHtml( "watchlisttools-{$mode}" )
526 return Html::rawElement( 'span',
527 array( 'class' => 'mw-watchlist-toollinks' ),
528 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );