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' ),
224 array( 'wl_user' => $user->getId() ),
226 array( 'ORDER BY' => 'wl_namespace, wl_title' ),
227 array( 'page' => array(
229 'wl_namespace = page_namespace AND wl_title = page_title'
233 if( $res && $dbr->numRows( $res ) > 0 ) {
234 $cache = LinkCache
::singleton();
235 foreach ( $res as $row ) {
236 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
237 if( $title instanceof Title
) {
238 // Update the link cache while we're at it
239 if( $row->page_id
) {
240 $cache->addGoodLinkObj( $row->page_id
, $title, $row->page_len
, $row->page_is_redirect
, $row->page_latest
);
242 $cache->addBadLinkObj( $title );
245 if( !$title->isTalkPage() ) {
246 $titles[$row->wl_namespace
][$row->wl_title
] = $row->page_is_redirect
;
255 * Show a message indicating the number of items on the user's watchlist,
256 * and return this count for additional checking
258 * @param $output OutputPage
262 private function showItemCount( $output, $user ) {
263 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
264 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
265 $GLOBALS['wgLang']->formatNum( $count ) ) );
267 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
273 * Remove all titles from a user's watchlist
277 private function clearWatchlist( $user ) {
278 $dbw = wfGetDB( DB_MASTER
);
279 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__
);
283 * Add a list of titles to a user's watchlist
285 * $titles can be an array of strings or Title objects; the former
286 * is preferred, since Titles are very memory-heavy
288 * @param $titles An array of strings, or Title objects
291 private function watchTitles( $titles, $user ) {
292 $dbw = wfGetDB( DB_MASTER
);
294 foreach( $titles as $title ) {
295 if( !$title instanceof Title
) {
296 $title = Title
::newFromText( $title );
298 if( $title instanceof Title
) {
300 'wl_user' => $user->getId(),
301 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
302 'wl_title' => $title->getDBkey(),
303 'wl_notificationtimestamp' => null,
306 'wl_user' => $user->getId(),
307 'wl_namespace' => ( $title->getNamespace() |
1 ),
308 'wl_title' => $title->getDBkey(),
309 'wl_notificationtimestamp' => null,
313 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
317 * Remove a list of titles from a user's watchlist
319 * $titles can be an array of strings or Title objects; the former
320 * is preferred, since Titles are very memory-heavy
322 * @param $titles An array of strings, or Title objects
325 private function unwatchTitles( $titles, $user ) {
326 $dbw = wfGetDB( DB_MASTER
);
327 foreach( $titles as $title ) {
328 if( !$title instanceof Title
) {
329 $title = Title
::newFromText( $title );
331 if( $title instanceof Title
) {
335 'wl_user' => $user->getId(),
336 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
337 'wl_title' => $title->getDBkey(),
344 'wl_user' => $user->getId(),
345 'wl_namespace' => ( $title->getNamespace() |
1 ),
346 'wl_title' => $title->getDBkey(),
350 $article = new Article($title);
351 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
357 * Show the standard watchlist editing form
359 * @param $output OutputPage
362 private function showNormalForm( $output, $user ) {
364 $count = $this->showItemCount( $output, $user );
366 $self = SpecialPage
::getTitleFor( 'Watchlist' );
367 $form = Xml
::openElement( 'form', array( 'method' => 'post',
368 'action' => $self->getLocalUrl( array( 'action' => 'edit' ) ) ) );
369 $form .= Html
::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
370 $form .= "<fieldset>\n<legend>" . wfMsgHtml( 'watchlistedit-normal-legend' ) . "</legend>";
371 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
372 $form .= $this->buildRemoveList( $user, $wgUser->getSkin() );
373 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
374 $form .= '</fieldset></form>';
375 $output->addHTML( $form );
380 * Build the part of the standard watchlist editing form with the actual
381 * title selection checkboxes and stuff. Also generates a table of
382 * contents if there's more than one heading.
385 * @param $skin Skin (really, Linker)
387 private function buildRemoveList( $user, $skin ) {
389 $toc = $skin->tocIndent();
391 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
393 $heading = htmlspecialchars( $this->getNamespaceHeading( $namespace ) );
394 $anchor = "editwatchlist-ns" . $namespace;
396 $list .= $skin->makeHeadLine( 2, ">", $anchor, $heading, "" );
397 $toc .= $skin->tocLine( $anchor, $heading, $tocLength, 1 ) . $skin->tocLineEnd();
400 foreach( $pages as $dbkey => $redirect ) {
401 $title = Title
::makeTitleSafe( $namespace, $dbkey );
402 $list .= $this->buildRemoveLine( $title, $redirect, $skin );
406 // ISSUE: omit the TOC if the total number of titles is low?
407 if( $tocLength > 1 ) {
408 $list = $skin->tocList( $toc ) . $list;
414 * Get the correct "heading" for a namespace
416 * @param $namespace int
419 private function getNamespaceHeading( $namespace ) {
420 return $namespace == NS_MAIN
421 ?
wfMsgHtml( 'blanknamespace' )
422 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
426 * Build a single list item containing a check box selecting a title
427 * and a link to that title, with various additional bits
429 * @param $title Title
430 * @param $redirect bool
434 private function buildRemoveLine( $title, $redirect, $skin ) {
437 $link = $skin->link( $title );
439 $link = '<span class="watchlistredir">' . $link . '</span>';
441 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
442 if( $title->exists() ) {
443 $tools[] = $skin->link(
445 wfMsgHtml( 'history_short' ),
447 array( 'action' => 'history' ),
448 array( 'known', 'noclasses' )
451 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
452 $tools[] = $skin->link(
453 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
454 wfMsgHtml( 'contributions' ),
457 array( 'known', 'noclasses' )
461 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
464 . Xml
::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
465 . $link . " (" . $wgLang->pipeList( $tools ) . ")" . "</li>\n";
469 * Show a form for editing the watchlist in "raw" mode
471 * @param $output OutputPage
474 public function showRawForm( $output, $user ) {
476 $this->showItemCount( $output, $user );
477 $self = SpecialPage
::getTitleFor( 'Watchlist' );
478 $form = Xml
::openElement( 'form', array( 'method' => 'post',
479 'action' => $self->getLocalUrl( array( 'action' => 'raw' ) ) ) );
480 $form .= Html
::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
481 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
482 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
483 $form .= Xml
::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
485 $form .= Xml
::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
486 'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
487 $titles = $this->getWatchlist( $user );
488 foreach( $titles as $title ) {
489 $form .= htmlspecialchars( $title ) . "\n";
491 $form .= '</textarea>';
492 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
493 $form .= '</fieldset></form>';
494 $output->addHTML( $form );
498 * Determine whether we are editing the watchlist, and if so, what
499 * kind of editing operation
501 * @param $request WebRequest
505 public static function getMode( $request, $par ) {
506 $mode = strtolower( $request->getVal( 'action', $par ) );
509 return self
::EDIT_CLEAR
;
511 return self
::EDIT_RAW
;
513 return self
::EDIT_NORMAL
;
520 * Build a set of links for convenient navigation
521 * between watchlist viewing and editing modes
523 * @param $skin Skin to use
526 public static function buildTools( $skin ) {
530 $modes = array( 'view' => false, 'edit' => 'edit', 'raw' => 'raw' );
531 foreach( $modes as $mode => $subpage ) {
532 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
533 $tools[] = $skin->linkKnown(
534 SpecialPage
::getTitleFor( 'Watchlist', $subpage ),
535 wfMsgHtml( "watchlisttools-{$mode}" )
538 return Html
::rawElement( 'span',
539 array( 'class' => 'mw-watchlist-toollinks' ),
540 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );