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
30 public function execute( $mode ) {
31 global $wgUser, $wgOut, $wgRequest;
33 $wgOut->readOnlyPage();
37 # Anons don't get a watchlist
38 if( $wgUser->isAnon() ) {
39 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
40 $llink = $this->getSkin()->linkKnown(
41 SpecialPage
::getTitleFor( 'Userlogin' ),
42 wfMsgHtml( 'loginreqlink' ),
44 array( 'returnto' => $this->getTitle()->getPrefixedText() )
46 $wgOut->addWikiMsgArray( 'watchlistanontext', array( $llink ), array( 'replaceafter' ) );
52 array( 'parseinline', 'replaceafter' ),
54 SpecialEditWatchlist
::buildTools( $this->getSkin() )
56 $wgOut->setSubtitle( $sub );
58 # B/C: $mode used to be waaay down the parameter list, and the first parameter
60 if( $mode instanceof User
){
61 $args = func_get_args();
62 if( count( $args >= 4 ) ){
66 $mode = self
::getMode( $wgRequest, $mode );
69 case self
::EDIT_CLEAR
:
70 // The "Clear" link scared people too much.
71 // Pass on to the raw editor, from which it's very easy to clear.
74 $wgOut->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
75 $form = $this->getRawForm( $wgUser );
77 $wgOut->addHTML( $this->successMessage
);
78 $wgOut->returnToMain();
82 case self
::EDIT_NORMAL
:
84 $wgOut->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
85 $form = $this->getNormalForm( $wgUser );
87 $wgOut->addHTML( $this->successMessage
);
88 $wgOut->returnToMain();
95 * Extract a list of titles from a blob of text, returning
96 * (prefixed) strings; unwatchable titles are ignored
101 private function extractTitles( $list ) {
103 $list = explode( "\n", trim( $list ) );
104 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 );
119 public function submitRaw( $data ){
120 global $wgUser, $wgLang;
121 $wanted = $this->extractTitles( $data['Titles'] );
122 $current = $this->getWatchlist( $wgUser );
124 if( count( $wanted ) > 0 ) {
125 $toWatch = array_diff( $wanted, $current );
126 $toUnwatch = array_diff( $current, $wanted );
127 $this->watchTitles( $toWatch, $wgUser );
128 $this->unwatchTitles( $toUnwatch, $wgUser );
129 $wgUser->invalidateCache();
131 if( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ){
132 $this->successMessage
= wfMessage( 'watchlistedit-raw-done' )->parse();
137 if( count( $toWatch ) > 0 ) {
138 $this->successMessage
.= wfMessage(
139 'watchlistedit-raw-added',
140 $wgLang->formatNum( count( $toWatch ) )
142 $this->showTitles( $toWatch, $this->successMessage
, $this->getSkin() );
145 if( count( $toUnwatch ) > 0 ) {
146 $this->successMessage
.= wfMessage(
147 'watchlistedit-raw-removed',
148 $wgLang->formatNum( count( $toUnwatch ) )
150 $this->showTitles( $toUnwatch, $this->successMessage
, $this->getSkin() );
153 $this->clearWatchlist( $wgUser );
154 $wgUser->invalidateCache();
155 $this->successMessage
.= wfMessage(
156 'watchlistedit-raw-removed',
157 $wgLang->formatNum( count( $current ) )
159 $this->showTitles( $current, $this->successMessage
, $this->getSkin() );
165 * Print out a list of linked titles
167 * $titles can be an array of strings or Title objects; the former
168 * is preferred, since Titles are very memory-heavy
170 * @param $titles array of strings, or Title objects
171 * @param $output String
174 private function showTitles( $titles, &$output, $skin ) {
175 $talk = wfMsgHtml( 'talkpagelinktext' );
176 // Do a batch existence check
177 $batch = new LinkBatch();
178 foreach( $titles as $title ) {
179 if( !$title instanceof Title
) {
180 $title = Title
::newFromText( $title );
182 if( $title instanceof Title
) {
183 $batch->addObj( $title );
184 $batch->addObj( $title->getTalkPage() );
188 // Print out the list
190 foreach( $titles as $title ) {
191 if( !$title instanceof Title
) {
192 $title = Title
::newFromText( $title );
194 if( $title instanceof Title
) {
196 . $skin->link( $title )
197 . ' (' . $skin->link( $title->getTalkPage(), $talk )
201 $output .= "</ul>\n";
205 * Count the number of titles on a user's watchlist, excluding talk pages
210 private function countWatchlist( $user ) {
211 $dbr = wfGetDB( DB_MASTER
);
212 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__
);
213 $row = $dbr->fetchObject( $res );
214 return ceil( $row->count
/ 2 ); // Paranoia
218 * Prepare a list of titles on a user's watchlist (excluding talk pages)
219 * and return an array of (prefixed) strings
224 private function getWatchlist( $user ) {
226 $dbr = wfGetDB( DB_MASTER
);
231 'wl_user' => $user->getId(),
235 if( $res->numRows() > 0 ) {
236 foreach ( $res as $row ) {
237 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
238 if( $title instanceof Title
&& !$title->isTalkPage() )
239 $list[] = $title->getPrefixedText();
247 * Get a list of titles on a user's watchlist, excluding talk pages,
248 * and return as a two-dimensional array with namespace, title and
254 private function getWatchlistInfo( $user ) {
256 $dbr = wfGetDB( DB_MASTER
);
259 array( 'watchlist', 'page' ),
268 array( 'wl_user' => $user->getId() ),
270 array( 'ORDER BY' => 'wl_namespace, wl_title' ),
271 array( 'page' => array(
273 'wl_namespace = page_namespace AND wl_title = page_title'
277 if( $res && $dbr->numRows( $res ) > 0 ) {
278 $cache = LinkCache
::singleton();
279 foreach ( $res as $row ) {
280 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
281 if( $title instanceof Title
) {
282 // Update the link cache while we're at it
283 if( $row->page_id
) {
284 $cache->addGoodLinkObj( $row->page_id
, $title, $row->page_len
, $row->page_is_redirect
, $row->page_latest
);
286 $cache->addBadLinkObj( $title );
289 if( !$title->isTalkPage() ) {
290 $titles[$row->wl_namespace
][$row->wl_title
] = $row->page_is_redirect
;
299 * Show a message indicating the number of items on the user's watchlist,
300 * and return this count for additional checking
302 * @param $output OutputPage
306 private function showItemCount( $output, $user ) {
307 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
308 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
309 $GLOBALS['wgLang']->formatNum( $count ) ) );
311 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
317 * Remove all titles from a user's watchlist
321 private function clearWatchlist( $user ) {
322 $dbw = wfGetDB( DB_MASTER
);
325 array( 'wl_user' => $user->getId() ),
331 * Add a list of titles to a user's watchlist
333 * $titles can be an array of strings or Title objects; the former
334 * is preferred, since Titles are very memory-heavy
336 * @param $titles Array of strings, or Title objects
339 private function watchTitles( $titles, $user ) {
340 $dbw = wfGetDB( DB_MASTER
);
342 foreach( $titles as $title ) {
343 if( !$title instanceof Title
) {
344 $title = Title
::newFromText( $title );
346 if( $title instanceof Title
) {
348 'wl_user' => $user->getId(),
349 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
350 'wl_title' => $title->getDBkey(),
351 'wl_notificationtimestamp' => null,
354 'wl_user' => $user->getId(),
355 'wl_namespace' => ( $title->getNamespace() |
1 ),
356 'wl_title' => $title->getDBkey(),
357 'wl_notificationtimestamp' => null,
361 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
365 * Remove a list of titles from a user's watchlist
367 * $titles can be an array of strings or Title objects; the former
368 * is preferred, since Titles are very memory-heavy
370 * @param $titles Array of strings, or Title objects
373 private function unwatchTitles( $titles, $user ) {
374 $dbw = wfGetDB( DB_MASTER
);
375 foreach( $titles as $title ) {
376 if( !$title instanceof Title
) {
377 $title = Title
::newFromText( $title );
379 if( $title instanceof Title
) {
383 'wl_user' => $user->getId(),
384 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
385 'wl_title' => $title->getDBkey(),
392 'wl_user' => $user->getId(),
393 'wl_namespace' => ( $title->getNamespace() |
1 ),
394 'wl_title' => $title->getDBkey(),
398 $article = new Article($title);
399 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
404 public function submitNormal( $data ) {
408 foreach( $data as $titles ) {
409 $this->unwatchTitles( $titles, $wgUser );
413 if( count( $removed ) > 0 ) {
415 $this->successMessage
= wfMessage(
416 'watchlistedit-normal-done',
417 $wgLang->formatNum( count( $removed ) )
419 $this->showTitles( $removed, $this->successMessage
, $this->getSkin() );
427 * Get the standard watchlist editing form
432 protected function getNormalForm( $user ){
434 $skin = $user->getSkin();
437 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ){
439 $namespace == NS_MAIN
440 ?
wfMsgHtml( 'blanknamespace' )
441 : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
443 $fields['TitlesNs'.$namespace] = array(
444 'type' => 'multiselect',
445 'options' => array(),
446 'section' => "ns$namespace",
449 foreach( $pages as $dbkey => $redirect ){
450 $title = Title
::makeTitleSafe( $namespace, $dbkey );
451 $text = $this->buildRemoveLine( $title, $redirect, $skin );
452 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
456 $form = new EditWatchlistNormalHTMLForm( $fields );
457 $form->setTitle( $this->getTitle() );
458 $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
459 $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
460 $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
461 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
466 * Build the label for a checkbox, with a link to the title, and various additional bits
468 * @param $title Title
469 * @param $redirect bool
473 private function buildRemoveLine( $title, $redirect, $skin ) {
476 $link = $skin->link( $title );
478 $link = '<span class="watchlistredir">' . $link . '</span>';
480 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
481 if( $title->exists() ) {
482 $tools[] = $skin->link(
484 wfMsgHtml( 'history_short' ),
486 array( 'action' => 'history' ),
487 array( 'known', 'noclasses' )
490 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
491 $tools[] = $skin->link(
492 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
493 wfMsgHtml( 'contributions' ),
496 array( 'known', 'noclasses' )
500 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
502 return $link . " (" . $wgLang->pipeList( $tools ) . ")";
506 * Get a form for editing the watchlist in "raw" mode
511 protected function getRawForm( $user ){
512 $titles = implode( array_map( 'htmlspecialchars', $this->getWatchlist( $user ) ), "\n" );
515 'type' => 'textarea',
516 'label-message' => 'watchlistedit-raw-titles',
517 'default' => $titles,
520 $form = new HTMLForm( $fields );
521 $form->setTitle( $this->getTitle( 'raw' ) );
522 $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
523 $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
524 $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
525 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
530 * Determine whether we are editing the watchlist, and if so, what
531 * kind of editing operation
533 * @param $request WebRequest
537 public static function getMode( $request, $par ) {
538 $act = $request->getVal( 'action' );
539 $mode = ( $act == 'view' ) ?
$par : $act;
540 $mode = strtolower( $mode );
543 case self
::EDIT_CLEAR
:
544 return self
::EDIT_CLEAR
;
548 return self
::EDIT_RAW
;
551 case self
::EDIT_NORMAL
:
552 return self
::EDIT_NORMAL
;
560 * Build a set of links for convenient navigation
561 * between watchlist viewing and editing modes
563 * @param $skin Skin to use
566 public static function buildTools( $skin ) {
571 'view' => array( 'Watchlist', false ),
572 'edit' => array( 'EditWatchlist', false ),
573 'raw' => array( 'EditWatchlist', 'raw' ),
575 foreach( $modes as $mode => $arr ) {
576 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
577 $tools[] = $skin->linkKnown(
578 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
579 wfMsgHtml( "watchlisttools-{$mode}" )
582 return Html
::rawElement( 'span',
583 array( 'class' => 'mw-watchlist-toollinks' ),
584 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
589 class WatchlistEditor
extends SpecialEditWatchlist
{}
592 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
594 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
595 public function getLegend( $namespace ){
597 $namespace = substr( $namespace, 2 );
598 return $namespace == NS_MAIN
599 ?
wfMsgHtml( 'blanknamespace' )
600 : htmlspecialchars( $wgLang->getFormattedNsText( $namespace ) );