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 ) {
28 global $wgUser, $wgLang;
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',
52 $wgLang->formatNum( $count ) ) );
53 $this->showTitles( $toWatch, $output, $wgUser->getSkin() );
55 if( ( $count = count( $toUnwatch ) ) > 0 ) {
56 $output->addHTML( wfMsgExt( 'watchlistedit-raw-removed', 'parse',
57 $wgLang->formatNum( $count ) ) );
58 $this->showTitles( $toUnwatch, $output, $wgUser->getSkin() );
61 $this->clearWatchlist( $user );
62 $user->invalidateCache();
63 $output->addHTML( wfMsgExt( 'watchlistedit-raw-removed', 'parse',
64 $wgLang->formatNum( count( $current ) ) ) );
65 $this->showTitles( $current, $output, $wgUser->getSkin() );
68 $this->showRawForm( $output, $user );
70 case self
::EDIT_NORMAL
:
71 $output->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
72 if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
73 $titles = $this->extractTitles( $request->getArray( 'titles' ) );
74 $this->unwatchTitles( $titles, $user );
75 $user->invalidateCache();
76 $output->addHTML( wfMsgExt( 'watchlistedit-normal-done', 'parse',
77 $wgLang->formatNum( count( $titles ) ) ) );
78 $this->showTitles( $titles, $output, $wgUser->getSkin() );
80 $this->showNormalForm( $output, $user );
85 * Check the edit token from a form submission
87 * @param $request WebRequest
91 private function checkToken( $request, $user ) {
92 return $user->matchEditToken( $request->getVal( 'token' ), 'watchlistedit' );
96 * Extract a list of titles from a blob of text, returning
97 * (prefixed) strings; unwatchable titles are ignored
102 private function extractTitles( $list ) {
104 if( !is_array( $list ) ) {
105 $list = explode( "\n", trim( $list ) );
106 if( !is_array( $list ) ) {
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 );
123 * Print out a list of linked titles
125 * $titles can be an array of strings or Title objects; the former
126 * is preferred, since Titles are very memory-heavy
128 * @param $titles An array of strings, or Title objects
129 * @param $output OutputPage
132 private function showTitles( $titles, $output, $skin ) {
133 $talk = wfMsgHtml( 'talkpagelinktext' );
134 // Do a batch existence check
135 $batch = new LinkBatch();
136 foreach( $titles as $title ) {
137 if( !$title instanceof Title
) {
138 $title = Title
::newFromText( $title );
140 if( $title instanceof Title
) {
141 $batch->addObj( $title );
142 $batch->addObj( $title->getTalkPage() );
146 // Print out the list
147 $output->addHTML( "<ul>\n" );
148 foreach( $titles as $title ) {
149 if( !$title instanceof Title
) {
150 $title = Title
::newFromText( $title );
152 if( $title instanceof Title
) {
153 $output->addHTML( "<li>" . $skin->link( $title )
154 . ' (' . $skin->link( $title->getTalkPage(), $talk ) . ")</li>\n" );
157 $output->addHTML( "</ul>\n" );
161 * Count the number of titles on a user's watchlist, excluding talk pages
166 private function countWatchlist( $user ) {
167 $dbr = wfGetDB( DB_MASTER
);
168 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__
);
169 $row = $dbr->fetchObject( $res );
170 return ceil( $row->count
/ 2 ); // Paranoia
174 * Prepare a list of titles on a user's watchlist (excluding talk pages)
175 * and return an array of (prefixed) strings
180 private function getWatchlist( $user ) {
182 $dbr = wfGetDB( DB_MASTER
);
187 'wl_user' => $user->getId(),
191 if( $res->numRows() > 0 ) {
192 foreach ( $res as $row ) {
193 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
194 if( $title instanceof Title
&& !$title->isTalkPage() )
195 $list[] = $title->getPrefixedText();
203 * Get a list of titles on a user's watchlist, excluding talk pages,
204 * and return as a two-dimensional array with namespace, title and
210 private function getWatchlistInfo( $user ) {
212 $dbr = wfGetDB( DB_MASTER
);
215 array( 'watchlist', 'page' ),
216 array( 'wl_namespace', 'wl_title', 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
217 array( 'wl_user' => $user->getId() ),
219 array( 'ORDER BY' => 'wl_namespace, wl_title' ),
221 array( 'LEFT JOIN', 'wl_namespace = page_namespace AND wl_title = page_title' ) )
224 if( $res && $dbr->numRows( $res ) > 0 ) {
225 $cache = LinkCache
::singleton();
226 foreach ( $res as $row ) {
227 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
228 if( $title instanceof Title
) {
229 // Update the link cache while we're at it
230 if( $row->page_id
) {
231 $cache->addGoodLinkObj( $row->page_id
, $title, $row->page_len
, $row->page_is_redirect
, $row->page_latest
);
233 $cache->addBadLinkObj( $title );
236 if( !$title->isTalkPage() ) {
237 $titles[$row->wl_namespace
][$row->wl_title
] = $row->page_is_redirect
;
246 * Show a message indicating the number of items on the user's watchlist,
247 * and return this count for additional checking
249 * @param $output OutputPage
253 private function showItemCount( $output, $user ) {
254 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
255 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
256 $GLOBALS['wgLang']->formatNum( $count ) ) );
258 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
264 * Remove all titles from a user's watchlist
268 private function clearWatchlist( $user ) {
269 $dbw = wfGetDB( DB_MASTER
);
270 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__
);
274 * Add a list of titles to a user's watchlist
276 * $titles can be an array of strings or Title objects; the former
277 * is preferred, since Titles are very memory-heavy
279 * @param $titles An array of strings, or Title objects
282 private function watchTitles( $titles, $user ) {
283 $dbw = wfGetDB( DB_MASTER
);
285 foreach( $titles as $title ) {
286 if( !$title instanceof Title
) {
287 $title = Title
::newFromText( $title );
289 if( $title instanceof Title
) {
291 'wl_user' => $user->getId(),
292 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
293 'wl_title' => $title->getDBkey(),
294 'wl_notificationtimestamp' => null,
297 'wl_user' => $user->getId(),
298 'wl_namespace' => ( $title->getNamespace() |
1 ),
299 'wl_title' => $title->getDBkey(),
300 'wl_notificationtimestamp' => null,
304 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
308 * Remove a list of titles from a user's watchlist
310 * $titles can be an array of strings or Title objects; the former
311 * is preferred, since Titles are very memory-heavy
313 * @param $titles An array of strings, or Title objects
316 private function unwatchTitles( $titles, $user ) {
317 $dbw = wfGetDB( DB_MASTER
);
318 foreach( $titles as $title ) {
319 if( !$title instanceof Title
) {
320 $title = Title
::newFromText( $title );
322 if( $title instanceof Title
) {
326 'wl_user' => $user->getId(),
327 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
328 'wl_title' => $title->getDBkey(),
335 'wl_user' => $user->getId(),
336 'wl_namespace' => ( $title->getNamespace() |
1 ),
337 'wl_title' => $title->getDBkey(),
341 $article = new Article($title);
342 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
348 * Show the standard watchlist editing form
350 * @param $output OutputPage
353 private function showNormalForm( $output, $user ) {
355 $count = $this->showItemCount( $output, $user );
357 $self = SpecialPage
::getTitleFor( 'Watchlist' );
358 $form = Xml
::openElement( 'form', array( 'method' => 'post',
359 'action' => $self->getLocalUrl( array( 'action' => 'edit' ) ) ) );
360 $form .= Html
::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
361 $form .= "<fieldset>\n<legend>" . wfMsgHtml( 'watchlistedit-normal-legend' ) . "</legend>";
362 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
363 $form .= $this->buildRemoveList( $user, $wgUser->getSkin() );
364 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
365 $form .= '</fieldset></form>';
366 $output->addHTML( $form );
371 * Build the part of the standard watchlist editing form with the actual
372 * title selection checkboxes and stuff. Also generates a table of
373 * contents if there's more than one heading.
376 * @param $skin Skin (really, Linker)
378 private function buildRemoveList( $user, $skin ) {
380 $toc = $skin->tocIndent();
382 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
384 $heading = htmlspecialchars( $this->getNamespaceHeading( $namespace ) );
385 $anchor = "editwatchlist-ns" . $namespace;
387 $list .= $skin->makeHeadLine( 2, ">", $anchor, $heading, "" );
388 $toc .= $skin->tocLine( $anchor, $heading, $tocLength, 1 ) . $skin->tocLineEnd();
391 foreach( $pages as $dbkey => $redirect ) {
392 $title = Title
::makeTitleSafe( $namespace, $dbkey );
393 $list .= $this->buildRemoveLine( $title, $redirect, $skin );
397 // ISSUE: omit the TOC if the total number of titles is low?
398 if( $tocLength > 1 ) {
399 $list = $skin->tocList( $toc ) . $list;
405 * Get the correct "heading" for a namespace
407 * @param $namespace int
410 private function getNamespaceHeading( $namespace ) {
411 return $namespace == NS_MAIN
412 ?
wfMsgHtml( 'blanknamespace' )
413 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
417 * Build a single list item containing a check box selecting a title
418 * and a link to that title, with various additional bits
420 * @param $title Title
421 * @param $redirect bool
425 private function buildRemoveLine( $title, $redirect, $skin ) {
428 $link = $skin->link( $title );
430 $link = '<span class="watchlistredir">' . $link . '</span>';
432 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
433 if( $title->exists() ) {
434 $tools[] = $skin->link(
436 wfMsgHtml( 'history_short' ),
438 array( 'action' => 'history' ),
439 array( 'known', 'noclasses' )
442 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
443 $tools[] = $skin->link(
444 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
445 wfMsgHtml( 'contributions' ),
448 array( 'known', 'noclasses' )
452 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
455 . Xml
::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
456 . $link . " (" . $wgLang->pipeList( $tools ) . ")" . "</li>\n";
460 * Show a form for editing the watchlist in "raw" mode
462 * @param $output OutputPage
465 public function showRawForm( $output, $user ) {
467 $this->showItemCount( $output, $user );
468 $self = SpecialPage
::getTitleFor( 'Watchlist' );
469 $form = Xml
::openElement( 'form', array( 'method' => 'post',
470 'action' => $self->getLocalUrl( array( 'action' => 'raw' ) ) ) );
471 $form .= Html
::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
472 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
473 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
474 $form .= Xml
::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
476 $form .= Xml
::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
477 'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
478 $titles = $this->getWatchlist( $user );
479 foreach( $titles as $title ) {
480 $form .= htmlspecialchars( $title ) . "\n";
482 $form .= '</textarea>';
483 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
484 $form .= '</fieldset></form>';
485 $output->addHTML( $form );
489 * Determine whether we are editing the watchlist, and if so, what
490 * kind of editing operation
492 * @param $request WebRequest
496 public static function getMode( $request, $par ) {
497 $mode = strtolower( $request->getVal( 'action', $par ) );
500 return self
::EDIT_CLEAR
;
502 return self
::EDIT_RAW
;
504 return self
::EDIT_NORMAL
;
511 * Build a set of links for convenient navigation
512 * between watchlist viewing and editing modes
514 * @param $skin Skin to use
517 public static function buildTools( $skin ) {
521 $modes = array( 'view' => false, 'edit' => 'edit', 'raw' => 'raw' );
522 foreach( $modes as $mode => $subpage ) {
523 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
524 $tools[] = $skin->linkKnown(
525 SpecialPage
::getTitleFor( 'Watchlist', $subpage ),
526 wfMsgHtml( "watchlisttools-{$mode}" )
529 return Html
::rawElement( 'span',
530 array( 'class' => 'mw-watchlist-toollinks' ),
531 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );