4 * Provides the UI through which users can perform editing
5 * operations on their watchlist
8 * @author Rob Church <robchur@gmail.com>
10 class SpecialEditWatchlist
extends UnlistedSpecialPage
{
17 const EDIT_NORMAL
= 3;
19 protected $successMessage;
21 public function __construct(){
22 parent
::__construct( 'EditWatchlist' );
26 * Main execution point
29 * @param $output OutputPage
30 * @param $request WebRequest
33 public function execute( $mode ) {
34 global $wgUser, $wgOut, $wgRequest;
36 $wgOut->readOnlyPage();
40 # Anons don't get a watchlist
41 if( $wgUser->isAnon() ) {
42 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
43 $llink = $wgUser->getSkin()->linkKnown(
44 SpecialPage
::getTitleFor( 'Userlogin' ),
45 wfMsgHtml( 'loginreqlink' ),
47 array( 'returnto' => $this->getTitle()->getPrefixedText() )
49 $wgOut->addWikiMsgArray( 'watchlistanontext', array( $llink ), array( 'replaceafter' ) );
55 array( 'parseinline', 'replaceafter' ),
57 SpecialEditWatchlist
::buildTools( $wgUser->getSkin() )
59 $wgOut->setSubtitle( $sub );
61 # B/C: $mode used to be waaay down the parameter list, and the first parameter
63 if( $mode instanceof User
){
64 $args = func_get_args();
65 if( count( $args >= 4 ) ){
69 $mode = self
::getMode( $wgRequest, $mode );
72 case self
::EDIT_CLEAR
:
73 // The "Clear" link scared people too much.
74 // Pass on to the raw editor, from which it's very easy to clear.
77 $wgOut->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
78 $form = $this->getRawForm( $wgUser );
80 $wgOut->addHTML( $this->successMessage
);
81 $wgOut->returnToMain();
85 case self
::EDIT_NORMAL
:
87 $wgOut->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
88 $form = $this->getNormalForm( $wgUser );
90 $wgOut->addHTML( $this->successMessage
);
91 $wgOut->returnToMain();
98 * Extract a list of titles from a blob of text, returning
99 * (prefixed) strings; unwatchable titles are ignored
101 * @param $list String
104 private function extractTitles( $list ) {
106 $list = explode( "\n", trim( $list ) );
107 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 );
122 public function submitRaw( $data ){
123 global $wgUser, $wgLang;
124 $wanted = $this->extractTitles( $data['Titles'] );
125 $current = $this->getWatchlist( $wgUser );
127 if( count( $wanted ) > 0 ) {
128 $toWatch = array_diff( $wanted, $current );
129 $toUnwatch = array_diff( $current, $wanted );
130 $this->watchTitles( $toWatch, $wgUser );
131 $this->unwatchTitles( $toUnwatch, $wgUser );
132 $wgUser->invalidateCache();
134 if( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ){
135 $this->successMessage
= wfMessage( 'watchlistedit-raw-done' )->parse();
140 if( count( $toWatch ) > 0 ) {
141 $this->successMessage
.= wfMessage(
142 'watchlistedit-raw-added',
143 $wgLang->formatNum( count( $toWatch ) )
145 $this->showTitles( $toWatch, $this->successMessage
, $wgUser->getSkin() );
148 if( count( $toUnwatch ) > 0 ) {
149 $this->successMessage
.= wfMessage(
150 'watchlistedit-raw-removed',
151 $wgLang->formatNum( count( $toUnwatch ) )
153 $this->showTitles( $toUnwatch, $this->successMessage
, $wgUser->getSkin() );
156 $this->clearWatchlist( $wgUser );
157 $wgUser->invalidateCache();
158 $this->successMessage
.= wfMessage(
159 'watchlistedit-raw-removed',
160 $wgLang->formatNum( count( $current ) )
162 $this->showTitles( $current, $this->successMessage
, $wgUser->getSkin() );
168 * Print out a list of linked titles
170 * $titles can be an array of strings or Title objects; the former
171 * is preferred, since Titles are very memory-heavy
173 * @param $titles array of strings, or Title objects
174 * @param $output String
177 private function showTitles( $titles, &$output, $skin ) {
178 $talk = wfMsgHtml( 'talkpagelinktext' );
179 // Do a batch existence check
180 $batch = new LinkBatch();
181 foreach( $titles as $title ) {
182 if( !$title instanceof Title
) {
183 $title = Title
::newFromText( $title );
185 if( $title instanceof Title
) {
186 $batch->addObj( $title );
187 $batch->addObj( $title->getTalkPage() );
191 // Print out the list
193 foreach( $titles as $title ) {
194 if( !$title instanceof Title
) {
195 $title = Title
::newFromText( $title );
197 if( $title instanceof Title
) {
199 . $skin->link( $title )
200 . ' (' . $skin->link( $title->getTalkPage(), $talk )
204 $output .= "</ul>\n";
208 * Count the number of titles on a user's watchlist, excluding talk pages
213 private function countWatchlist( $user ) {
214 $dbr = wfGetDB( DB_MASTER
);
215 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__
);
216 $row = $dbr->fetchObject( $res );
217 return ceil( $row->count
/ 2 ); // Paranoia
221 * Prepare a list of titles on a user's watchlist (excluding talk pages)
222 * and return an array of (prefixed) strings
227 private function getWatchlist( $user ) {
229 $dbr = wfGetDB( DB_MASTER
);
234 'wl_user' => $user->getId(),
238 if( $res->numRows() > 0 ) {
239 foreach ( $res as $row ) {
240 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
241 if( $title instanceof Title
&& !$title->isTalkPage() )
242 $list[] = $title->getPrefixedText();
250 * Get a list of titles on a user's watchlist, excluding talk pages,
251 * and return as a two-dimensional array with namespace, title and
257 private function getWatchlistInfo( $user ) {
259 $dbr = wfGetDB( DB_MASTER
);
262 array( 'watchlist', 'page' ),
271 array( 'wl_user' => $user->getId() ),
273 array( 'ORDER BY' => 'wl_namespace, wl_title' ),
274 array( 'page' => array(
276 'wl_namespace = page_namespace AND wl_title = page_title'
280 if( $res && $dbr->numRows( $res ) > 0 ) {
281 $cache = LinkCache
::singleton();
282 foreach ( $res as $row ) {
283 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
284 if( $title instanceof Title
) {
285 // Update the link cache while we're at it
286 if( $row->page_id
) {
287 $cache->addGoodLinkObj( $row->page_id
, $title, $row->page_len
, $row->page_is_redirect
, $row->page_latest
);
289 $cache->addBadLinkObj( $title );
292 if( !$title->isTalkPage() ) {
293 $titles[$row->wl_namespace
][$row->wl_title
] = $row->page_is_redirect
;
302 * Show a message indicating the number of items on the user's watchlist,
303 * and return this count for additional checking
305 * @param $output OutputPage
309 private function showItemCount( $output, $user ) {
310 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
311 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
312 $GLOBALS['wgLang']->formatNum( $count ) ) );
314 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
320 * Remove all titles from a user's watchlist
324 private function clearWatchlist( $user ) {
325 $dbw = wfGetDB( DB_MASTER
);
328 array( 'wl_user' => $user->getId() ),
334 * Add a list of titles to a user's watchlist
336 * $titles can be an array of strings or Title objects; the former
337 * is preferred, since Titles are very memory-heavy
339 * @param $titles Array of strings, or Title objects
342 private function watchTitles( $titles, $user ) {
343 $dbw = wfGetDB( DB_MASTER
);
345 foreach( $titles as $title ) {
346 if( !$title instanceof Title
) {
347 $title = Title
::newFromText( $title );
349 if( $title instanceof Title
) {
351 'wl_user' => $user->getId(),
352 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
353 'wl_title' => $title->getDBkey(),
354 'wl_notificationtimestamp' => null,
357 'wl_user' => $user->getId(),
358 'wl_namespace' => ( $title->getNamespace() |
1 ),
359 'wl_title' => $title->getDBkey(),
360 'wl_notificationtimestamp' => null,
364 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
368 * Remove a list of titles from a user's watchlist
370 * $titles can be an array of strings or Title objects; the former
371 * is preferred, since Titles are very memory-heavy
373 * @param $titles Array of strings, or Title objects
376 private function unwatchTitles( $titles, $user ) {
377 $dbw = wfGetDB( DB_MASTER
);
378 foreach( $titles as $title ) {
379 if( !$title instanceof Title
) {
380 $title = Title
::newFromText( $title );
382 if( $title instanceof Title
) {
386 'wl_user' => $user->getId(),
387 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
388 'wl_title' => $title->getDBkey(),
395 'wl_user' => $user->getId(),
396 'wl_namespace' => ( $title->getNamespace() |
1 ),
397 'wl_title' => $title->getDBkey(),
401 $article = new Article($title);
402 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
407 public function submitNormal( $data ) {
411 foreach( $data as $titles ) {
412 $this->unwatchTitles( $titles, $wgUser );
416 if( count( $removed ) > 0 ) {
418 $this->successMessage
= wfMessage(
419 'watchlistedit-normal-done',
420 $wgLang->formatNum( count( $removed ) )
422 $this->showTitles( $removed, $this->successMessage
, $wgUser->getSkin() );
430 * Get the standard watchlist editing form
435 protected function getNormalForm( $user ){
437 $skin = $user->getSkin();
440 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ){
442 $namespace == NS_MAIN
443 ?
wfMsgHtml( 'blanknamespace' )
444 : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
446 $fields['TitlesNs'.$namespace] = array(
447 'type' => 'multiselect',
448 'options' => array(),
449 'section' => "ns$namespace",
452 foreach( $pages as $dbkey => $redirect ){
453 $title = Title
::makeTitleSafe( $namespace, $dbkey );
454 $text = $this->buildRemoveLine( $title, $redirect, $skin );
455 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
459 $form = new EditWatchlistNormalHTMLForm( $fields );
460 $form->setTitle( $this->getTitle() );
461 $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
462 $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
463 $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
464 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
469 * Build the label for a checkbox, with a link to the title, and various additional bits
471 * @param $title Title
472 * @param $redirect bool
476 private function buildRemoveLine( $title, $redirect, $skin ) {
479 $link = $skin->link( $title );
481 $link = '<span class="watchlistredir">' . $link . '</span>';
483 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
484 if( $title->exists() ) {
485 $tools[] = $skin->link(
487 wfMsgHtml( 'history_short' ),
489 array( 'action' => 'history' ),
490 array( 'known', 'noclasses' )
493 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
494 $tools[] = $skin->link(
495 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
496 wfMsgHtml( 'contributions' ),
499 array( 'known', 'noclasses' )
503 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
505 return $link . " (" . $wgLang->pipeList( $tools ) . ")";
509 * Get a form for editing the watchlist in "raw" mode
514 protected function getRawForm( $user ){
515 $titles = implode( array_map( 'htmlspecialchars', $this->getWatchlist( $user ) ), "\n" );
518 'type' => 'textarea',
519 'label-message' => 'watchlistedit-raw-titles',
520 'default' => $titles,
523 $form = new HTMLForm( $fields );
524 $form->setTitle( $this->getTitle( 'raw' ) );
525 $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
526 $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
527 $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
528 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
533 * Determine whether we are editing the watchlist, and if so, what
534 * kind of editing operation
536 * @param $request WebRequest
540 public static function getMode( $request, $par ) {
541 $mode = strtolower( $request->getVal( 'action', $par ) );
544 case self
::EDIT_CLEAR
:
545 return self
::EDIT_CLEAR
;
549 return self
::EDIT_RAW
;
552 case self
::EDIT_NORMAL
:
553 return self
::EDIT_NORMAL
;
561 * Build a set of links for convenient navigation
562 * between watchlist viewing and editing modes
564 * @param $skin Skin to use
567 public static function buildTools( $skin ) {
572 'view' => array( 'Watchlist', false ),
573 'edit' => array( 'EditWatchlist', false ),
574 'raw' => array( 'EditWatchlist', 'raw' ),
576 foreach( $modes as $mode => $arr ) {
577 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
578 $tools[] = $skin->linkKnown(
579 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
580 wfMsgHtml( "watchlisttools-{$mode}" )
583 return Html
::rawElement( 'span',
584 array( 'class' => 'mw-watchlist-toollinks' ),
585 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
590 class WatchlistEditor
extends SpecialEditWatchlist
{}
593 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
595 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
596 public function getLegend( $namespace ){
598 $namespace = substr( $namespace, 2 );
599 return $namespace == NS_MAIN
600 ?
wfMsgHtml( 'blanknamespace' )
601 : htmlspecialchars( $wgLang->getFormattedNsText( $namespace ) );