4 * Provides the UI through which users can perform editing
5 * operations on their watchlist
8 * @author Rob Church <robchur@gmail.com>
10 class WatchlistEditor
{
17 const EDIT_NORMAL
= 3;
20 * Main execution point
23 * @param $output OutputPage
24 * @param $request WebRequest
27 public function execute( $user, $output, $request, $mode ) {
30 $output->readOnlyPage();
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.
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() );
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 );
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 );
82 * Check the edit token from a form submission
84 * @param $request WebRequest
88 private function checkToken( $request, $user ) {
89 return $user->matchEditToken( $request->getVal( 'token' ), 'watchlistedit' );
93 * Extract a list of titles from a blob of text, returning
94 * (prefixed) strings; unwatchable titles are ignored
99 private function extractTitles( $list ) {
101 if( !is_array( $list ) ) {
102 $list = explode( "\n", trim( $list ) );
103 if( !is_array( $list ) ) {
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
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() );
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
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
177 private function getWatchlist( $user ) {
179 $dbr = wfGetDB( DB_MASTER
);
184 'wl_user' => $user->getId(),
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();
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
207 private function getWatchlistInfo( $user ) {
209 $dbr = wfGetDB( DB_MASTER
);
210 $uid = intval( $user->getId() );
211 list( $watchlist, $page ) = $dbr->tableNamesN( 'watchlist', 'page' );
212 $sql = "SELECT wl_namespace, wl_title, page_id, page_len, page_is_redirect, page_latest
213 FROM {$watchlist} LEFT JOIN {$page} ON ( wl_namespace = page_namespace
214 AND wl_title = page_title ) WHERE wl_user = {$uid}";
215 if ( ! $dbr->implicitOrderby() ) {
216 $sql .= " ORDER BY wl_title";
218 $res = $dbr->query( $sql, __METHOD__
);
219 if( $res && $dbr->numRows( $res ) > 0 ) {
220 $cache = LinkCache
::singleton();
221 foreach ( $res as $row ) {
222 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
223 if( $title instanceof Title
) {
224 // Update the link cache while we're at it
225 if( $row->page_id
) {
226 $cache->addGoodLinkObj( $row->page_id
, $title, $row->page_len
, $row->page_is_redirect
, $row->page_latest
);
228 $cache->addBadLinkObj( $title );
231 if( !$title->isTalkPage() ) {
232 $titles[$row->wl_namespace
][$row->wl_title
] = $row->page_is_redirect
;
241 * Show a message indicating the number of items on the user's watchlist,
242 * and return this count for additional checking
244 * @param $output OutputPage
248 private function showItemCount( $output, $user ) {
249 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
250 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
251 $GLOBALS['wgLang']->formatNum( $count ) ) );
253 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
259 * Remove all titles from a user's watchlist
263 private function clearWatchlist( $user ) {
264 $dbw = wfGetDB( DB_MASTER
);
265 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__
);
269 * Add a list of titles to a user's watchlist
271 * $titles can be an array of strings or Title objects; the former
272 * is preferred, since Titles are very memory-heavy
274 * @param $titles An array of strings, or Title objects
277 private function watchTitles( $titles, $user ) {
278 $dbw = wfGetDB( DB_MASTER
);
280 foreach( $titles as $title ) {
281 if( !$title instanceof Title
) {
282 $title = Title
::newFromText( $title );
284 if( $title instanceof Title
) {
286 'wl_user' => $user->getId(),
287 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
288 'wl_title' => $title->getDBkey(),
289 'wl_notificationtimestamp' => null,
292 'wl_user' => $user->getId(),
293 'wl_namespace' => ( $title->getNamespace() |
1 ),
294 'wl_title' => $title->getDBkey(),
295 'wl_notificationtimestamp' => null,
299 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
303 * Remove a list of titles from a user's watchlist
305 * $titles can be an array of strings or Title objects; the former
306 * is preferred, since Titles are very memory-heavy
308 * @param $titles An array of strings, or Title objects
311 private function unwatchTitles( $titles, $user ) {
312 $dbw = wfGetDB( DB_MASTER
);
313 foreach( $titles as $title ) {
314 if( !$title instanceof Title
) {
315 $title = Title
::newFromText( $title );
317 if( $title instanceof Title
) {
321 'wl_user' => $user->getId(),
322 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
323 'wl_title' => $title->getDBkey(),
330 'wl_user' => $user->getId(),
331 'wl_namespace' => ( $title->getNamespace() |
1 ),
332 'wl_title' => $title->getDBkey(),
336 $article = new Article($title);
337 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
343 * Show the standard watchlist editing form
345 * @param $output OutputPage
348 private function showNormalForm( $output, $user ) {
350 $count = $this->showItemCount( $output, $user );
352 $self = SpecialPage
::getTitleFor( 'Watchlist' );
353 $form = Xml
::openElement( 'form', array( 'method' => 'post',
354 'action' => $self->getLocalUrl( array( 'action' => 'edit' ) ) ) );
355 $form .= Html
::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
356 $form .= "<fieldset>\n<legend>" . wfMsgHtml( 'watchlistedit-normal-legend' ) . "</legend>";
357 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
358 $form .= $this->buildRemoveList( $user, $wgUser->getSkin() );
359 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
360 $form .= '</fieldset></form>';
361 $output->addHTML( $form );
366 * Build the part of the standard watchlist editing form with the actual
367 * title selection checkboxes and stuff. Also generates a table of
368 * contents if there's more than one heading.
371 * @param $skin Skin (really, Linker)
373 private function buildRemoveList( $user, $skin ) {
375 $toc = $skin->tocIndent();
377 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
379 $heading = htmlspecialchars( $this->getNamespaceHeading( $namespace ) );
380 $anchor = "editwatchlist-ns" . $namespace;
382 $list .= $skin->makeHeadLine( 2, ">", $anchor, $heading, "" );
383 $toc .= $skin->tocLine( $anchor, $heading, $tocLength, 1 ) . $skin->tocLineEnd();
386 foreach( $pages as $dbkey => $redirect ) {
387 $title = Title
::makeTitleSafe( $namespace, $dbkey );
388 $list .= $this->buildRemoveLine( $title, $redirect, $skin );
392 // ISSUE: omit the TOC if the total number of titles is low?
393 if( $tocLength > 1 ) {
394 $list = $skin->tocList( $toc ) . $list;
400 * Get the correct "heading" for a namespace
402 * @param $namespace int
405 private function getNamespaceHeading( $namespace ) {
406 return $namespace == NS_MAIN
407 ?
wfMsgHtml( 'blanknamespace' )
408 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
412 * Build a single list item containing a check box selecting a title
413 * and a link to that title, with various additional bits
415 * @param $title Title
416 * @param $redirect bool
420 private function buildRemoveLine( $title, $redirect, $skin ) {
423 $link = $skin->link( $title );
425 $link = '<span class="watchlistredir">' . $link . '</span>';
427 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
428 if( $title->exists() ) {
429 $tools[] = $skin->link(
431 wfMsgHtml( 'history_short' ),
433 array( 'action' => 'history' ),
434 array( 'known', 'noclasses' )
437 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
438 $tools[] = $skin->link(
439 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
440 wfMsgHtml( 'contributions' ),
443 array( 'known', 'noclasses' )
447 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
450 . Xml
::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
451 . $link . " (" . $wgLang->pipeList( $tools ) . ")" . "</li>\n";
455 * Show a form for editing the watchlist in "raw" mode
457 * @param $output OutputPage
460 public function showRawForm( $output, $user ) {
462 $this->showItemCount( $output, $user );
463 $self = SpecialPage
::getTitleFor( 'Watchlist' );
464 $form = Xml
::openElement( 'form', array( 'method' => 'post',
465 'action' => $self->getLocalUrl( array( 'action' => 'raw' ) ) ) );
466 $form .= Html
::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
467 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
468 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
469 $form .= Xml
::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
471 $form .= Xml
::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
472 'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
473 $titles = $this->getWatchlist( $user );
474 foreach( $titles as $title ) {
475 $form .= htmlspecialchars( $title ) . "\n";
477 $form .= '</textarea>';
478 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
479 $form .= '</fieldset></form>';
480 $output->addHTML( $form );
484 * Determine whether we are editing the watchlist, and if so, what
485 * kind of editing operation
487 * @param $request WebRequest
491 public static function getMode( $request, $par ) {
492 $mode = strtolower( $request->getVal( 'action', $par ) );
495 return self
::EDIT_CLEAR
;
497 return self
::EDIT_RAW
;
499 return self
::EDIT_NORMAL
;
506 * Build a set of links for convenient navigation
507 * between watchlist viewing and editing modes
509 * @param $skin Skin to use
512 public static function buildTools( $skin ) {
516 $modes = array( 'view' => false, 'edit' => 'edit', 'raw' => 'raw' );
517 foreach( $modes as $mode => $subpage ) {
518 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
519 $tools[] = $skin->linkKnown(
520 SpecialPage
::getTitleFor( 'Watchlist', $subpage ),
521 wfMsgHtml( "watchlisttools-{$mode}" )
524 return Html
::rawElement( 'span',
525 array( 'class' => 'mw-watchlist-toollinks' ),
526 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );