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
);
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() ),
216 array( 'ORDER BY' => 'wl_namespace, wl_title' ),
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
);
230 $cache->addBadLinkObj( $title );
233 if( !$title->isTalkPage() ) {
234 $titles[$row->wl_namespace
][$row->wl_title
] = $row->page_is_redirect
;
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
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 ) ) );
255 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
261 * Remove all titles from a user's watchlist
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
279 private function watchTitles( $titles, $user ) {
280 $dbw = wfGetDB( DB_MASTER
);
282 foreach( $titles as $title ) {
283 if( !$title instanceof Title
) {
284 $title = Title
::newFromText( $title );
286 if( $title instanceof Title
) {
288 'wl_user' => $user->getId(),
289 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
290 'wl_title' => $title->getDBkey(),
291 'wl_notificationtimestamp' => null,
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
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
) {
323 'wl_user' => $user->getId(),
324 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
325 'wl_title' => $title->getDBkey(),
332 'wl_user' => $user->getId(),
333 'wl_namespace' => ( $title->getNamespace() |
1 ),
334 'wl_title' => $title->getDBkey(),
338 $article = new Article($title);
339 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
345 * Show the standard watchlist editing form
347 * @param $output OutputPage
350 private function showNormalForm( $output, $user ) {
352 $count = $this->showItemCount( $output, $user );
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.
373 * @param $skin Skin (really, Linker)
375 private function buildRemoveList( $user, $skin ) {
377 $toc = $skin->tocIndent();
379 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
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();
388 foreach( $pages as $dbkey => $redirect ) {
389 $title = Title
::makeTitleSafe( $namespace, $dbkey );
390 $list .= $this->buildRemoveLine( $title, $redirect, $skin );
394 // ISSUE: omit the TOC if the total number of titles is low?
395 if( $tocLength > 1 ) {
396 $list = $skin->tocList( $toc ) . $list;
402 * Get the correct "heading" for a namespace
404 * @param $namespace int
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
422 private function buildRemoveLine( $title, $redirect, $skin ) {
425 $link = $skin->link( $title );
427 $link = '<span class="watchlistredir">' . $link . '</span>';
429 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
430 if( $title->exists() ) {
431 $tools[] = $skin->link(
433 wfMsgHtml( 'history_short' ),
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' ),
445 array( 'known', 'noclasses' )
449 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
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
462 public function showRawForm( $output, $user ) {
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' );
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
493 public static function getMode( $request, $par ) {
494 $mode = strtolower( $request->getVal( 'action', $par ) );
497 return self
::EDIT_CLEAR
;
499 return self
::EDIT_RAW
;
501 return self
::EDIT_NORMAL
;
508 * Build a set of links for convenient navigation
509 * between watchlist viewing and editing modes
511 * @param $skin Skin to use
514 public static function buildTools( $skin ) {
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 ) ) );