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;
23 public function __construct(){
24 parent
::__construct( 'EditWatchlist' );
28 * Main execution point
32 public function execute( $mode ) {
35 $out = $this->getOutput();
37 # Anons don't get a watchlist
38 if( $this->getUser()->isAnon() ) {
39 $out->setPageTitle( $this->msg( 'watchnologin' ) );
40 $llink = Linker
::linkKnown(
41 SpecialPage
::getTitleFor( 'Userlogin' ),
42 wfMsgHtml( 'loginreqlink' ),
44 array( 'returnto' => $this->getTitle()->getPrefixedText() )
46 $out->addHTML( wfMessage( 'watchlistanontext' )->rawParams( $llink )->parse() );
51 throw new ReadOnlyError
;
54 $this->outputHeader();
56 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName()
57 )->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
59 # B/C: $mode used to be waaay down the parameter list, and the first parameter
61 if( $mode instanceof User
){
62 $args = func_get_args();
63 if( count( $args >= 4 ) ){
67 $mode = self
::getMode( $this->getRequest(), $mode );
70 case self
::EDIT_CLEAR
:
71 // The "Clear" link scared people too much.
72 // Pass on to the raw editor, from which it's very easy to clear.
75 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
76 $form = $this->getRawForm();
78 $out->addHTML( $this->successMessage
);
83 case self
::EDIT_NORMAL
:
85 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
86 $form = $this->getNormalForm();
88 $out->addHTML( $this->successMessage
);
90 } elseif ( $this->toc
!== false ) {
91 $out->prependHTML( $this->toc
);
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 $wanted = $this->extractTitles( $data['Titles'] );
124 $current = $this->getWatchlist();
126 if( count( $wanted ) > 0 ) {
127 $toWatch = array_diff( $wanted, $current );
128 $toUnwatch = array_diff( $current, $wanted );
129 $this->watchTitles( $toWatch );
130 $this->unwatchTitles( $toUnwatch );
131 $this->getUser()->invalidateCache();
133 if( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ){
134 $this->successMessage
= wfMessage( 'watchlistedit-raw-done' )->parse();
139 if( count( $toWatch ) > 0 ) {
140 $this->successMessage
.= wfMessage(
141 'watchlistedit-raw-added',
142 $this->getLang()->formatNum( count( $toWatch ) )
144 $this->showTitles( $toWatch, $this->successMessage
);
147 if( count( $toUnwatch ) > 0 ) {
148 $this->successMessage
.= wfMessage(
149 'watchlistedit-raw-removed',
150 $this->getLang()->formatNum( count( $toUnwatch ) )
152 $this->showTitles( $toUnwatch, $this->successMessage
);
155 $this->clearWatchlist();
156 $this->getUser()->invalidateCache();
157 $this->successMessage
.= wfMessage(
158 'watchlistedit-raw-removed',
159 $this->getLang()->formatNum( count( $current ) )
161 $this->showTitles( $current, $this->successMessage
);
167 * Print out a list of linked titles
169 * $titles can be an array of strings or Title objects; the former
170 * is preferred, since Titles are very memory-heavy
172 * @param $titles array of strings, or Title objects
173 * @param $output String
175 private function showTitles( $titles, &$output ) {
176 $talk = wfMsgHtml( 'talkpagelinktext' );
177 // Do a batch existence check
178 $batch = new LinkBatch();
179 foreach( $titles as $title ) {
180 if( !$title instanceof Title
) {
181 $title = Title
::newFromText( $title );
183 if( $title instanceof Title
) {
184 $batch->addObj( $title );
185 $batch->addObj( $title->getTalkPage() );
189 // Print out the list
191 foreach( $titles as $title ) {
192 if( !$title instanceof Title
) {
193 $title = Title
::newFromText( $title );
195 if( $title instanceof Title
) {
197 . Linker
::link( $title )
198 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
202 $output .= "</ul>\n";
206 * Prepare a list of titles on a user's watchlist (excluding talk pages)
207 * and return an array of (prefixed) strings
211 private function getWatchlist() {
213 $dbr = wfGetDB( DB_MASTER
);
218 'wl_user' => $this->getUser()->getId(),
222 if( $res->numRows() > 0 ) {
223 foreach ( $res as $row ) {
224 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
225 if( $title instanceof Title
&& !$title->isTalkPage() )
226 $list[] = $title->getPrefixedText();
234 * Get a list of titles on a user's watchlist, excluding talk pages,
235 * and return as a two-dimensional array with namespace, title and
240 private function getWatchlistInfo() {
242 $dbr = wfGetDB( DB_MASTER
);
245 array( 'watchlist' ),
246 array( 'wl_namespace', 'wl_title' ),
247 array( 'wl_user' => $this->getUser()->getId() ),
249 array( 'ORDER BY' => 'wl_namespace, wl_title' )
252 $lb = new LinkBatch();
253 foreach ( $res as $row ) {
254 $lb->add( $row->wl_namespace
, $row->wl_title
);
255 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
256 $titles[$row->wl_namespace
][$row->wl_title
] = false;
265 * Remove all titles from a user's watchlist
267 private function clearWatchlist() {
268 $dbw = wfGetDB( DB_MASTER
);
271 array( 'wl_user' => $this->getUser()->getId() ),
277 * Add a list of titles to a user's watchlist
279 * $titles can be an array of strings or Title objects; the former
280 * is preferred, since Titles are very memory-heavy
282 * @param $titles Array of strings, or Title objects
284 private function watchTitles( $titles ) {
285 $dbw = wfGetDB( DB_MASTER
);
287 foreach( $titles as $title ) {
288 if( !$title instanceof Title
) {
289 $title = Title
::newFromText( $title );
291 if( $title instanceof Title
) {
293 'wl_user' => $this->getUser()->getId(),
294 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
295 'wl_title' => $title->getDBkey(),
296 'wl_notificationtimestamp' => null,
299 'wl_user' => $this->getUser()->getId(),
300 'wl_namespace' => ( $title->getNamespace() |
1 ),
301 'wl_title' => $title->getDBkey(),
302 'wl_notificationtimestamp' => null,
306 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
310 * Remove a list of titles from a user's watchlist
312 * $titles can be an array of strings or Title objects; the former
313 * is preferred, since Titles are very memory-heavy
315 * @param $titles Array of strings, or Title objects
317 private function unwatchTitles( $titles ) {
318 $dbw = wfGetDB( DB_MASTER
);
319 foreach( $titles as $title ) {
320 if( !$title instanceof Title
) {
321 $title = Title
::newFromText( $title );
323 if( $title instanceof Title
) {
327 'wl_user' => $this->getUser()->getId(),
328 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
329 'wl_title' => $title->getDBkey(),
336 'wl_user' => $this->getUser()->getId(),
337 'wl_namespace' => ( $title->getNamespace() |
1 ),
338 'wl_title' => $title->getDBkey(),
342 $article = new Article( $title, 0 );
343 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$article ) );
348 public function submitNormal( $data ) {
351 foreach( $data as $titles ) {
352 $this->unwatchTitles( $titles );
356 if( count( $removed ) > 0 ) {
357 $this->successMessage
= wfMessage(
358 'watchlistedit-normal-done',
359 $this->getLang()->formatNum( count( $removed ) )
361 $this->showTitles( $removed, $this->successMessage
);
369 * Get the standard watchlist editing form
373 protected function getNormalForm(){
379 $haveInvalidNamespaces = false;
380 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
381 if ( $namespace < 0 ) {
382 $haveInvalidNamespaces = true;
386 $fields['TitlesNs'.$namespace] = array(
387 'class' => 'EditWatchlistCheckboxSeriesField',
388 'options' => array(),
389 'section' => "ns$namespace",
392 foreach( array_keys( $pages ) as $dbkey ){
393 $title = Title
::makeTitleSafe( $namespace, $dbkey );
394 $text = $this->buildRemoveLine( $title );
395 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
399 if ( $haveInvalidNamespaces ) {
400 wfDebug( "User {$this->getContext()->getUser()->getId()} has invalid watchlist entries, cleaning up...\n" );
401 $this->getContext()->getUser()->cleanupWatchlist();
404 if ( count( $fields ) > 1 && $count > 30 ) {
405 $this->toc
= Linker
::tocIndent();
407 foreach( $fields as $key => $data ) {
409 # strip out the 'ns' prefix from the section name:
410 $ns = substr( $data['section'], 2 );
412 $nsText = ($ns == NS_MAIN
)
413 ?
wfMsgHtml( 'blanknamespace' )
414 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
415 $this->toc
.= Linker
::tocLine( "mw-htmlform-{$data['section']}", $nsText, ++
$tocLength, 1 ) . Linker
::tocLineEnd();
417 $this->toc
= Linker
::tocList( $this->toc
);
422 $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
423 $form->setTitle( $this->getTitle() );
424 $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
425 $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
426 $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
427 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
432 * Build the label for a checkbox, with a link to the title, and various additional bits
434 * @param $title Title
437 private function buildRemoveLine( $title ) {
438 $link = Linker
::link( $title );
439 if( $title->isRedirect() ) {
440 // Linker already makes class mw-redirect, so this is redundant
441 $link = '<span class="watchlistredir">' . $link . '</span>';
443 $tools[] = Linker
::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
444 if( $title->exists() ) {
445 $tools[] = Linker
::linkKnown(
447 wfMsgHtml( 'history_short' ),
449 array( 'action' => 'history' )
452 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
453 $tools[] = Linker
::linkKnown(
454 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
455 wfMsgHtml( 'contributions' )
459 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
461 return $link . " (" . $this->getLang()->pipeList( $tools ) . ")";
465 * Get a form for editing the watchlist in "raw" mode
469 protected function getRawForm(){
470 $titles = implode( $this->getWatchlist(), "\n" );
473 'type' => 'textarea',
474 'label-message' => 'watchlistedit-raw-titles',
475 'default' => $titles,
478 $form = new HTMLForm( $fields, $this->getContext() );
479 $form->setTitle( $this->getTitle( 'raw' ) );
480 $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
481 $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
482 $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
483 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
488 * Determine whether we are editing the watchlist, and if so, what
489 * kind of editing operation
491 * @param $request WebRequest
495 public static function getMode( $request, $par ) {
496 $mode = strtolower( $request->getVal( 'action', $par ) );
499 case self
::EDIT_CLEAR
:
500 return self
::EDIT_CLEAR
;
504 return self
::EDIT_RAW
;
507 case self
::EDIT_NORMAL
:
508 return self
::EDIT_NORMAL
;
516 * Build a set of links for convenient navigation
517 * between watchlist viewing and editing modes
519 * @param $unused Unused
522 public static function buildTools( $unused ) {
527 'view' => array( 'Watchlist', false ),
528 'edit' => array( 'EditWatchlist', false ),
529 'raw' => array( 'EditWatchlist', 'raw' ),
531 foreach( $modes as $mode => $arr ) {
532 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
533 $tools[] = Linker
::linkKnown(
534 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
535 wfMsgHtml( "watchlisttools-{$mode}" )
538 return Html
::rawElement( 'span',
539 array( 'class' => 'mw-watchlist-toollinks' ),
540 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
545 class WatchlistEditor
extends SpecialEditWatchlist
{}
548 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
550 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
551 public function getLegend( $namespace ){
552 $namespace = substr( $namespace, 2 );
553 return $namespace == NS_MAIN
554 ?
wfMsgHtml( 'blanknamespace' )
555 : htmlspecialchars( $this->getContext()->getLang()->getFormattedNsText( $namespace ) );
559 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
561 * HTMLMultiSelectField throws validation errors if we get input data
562 * that doesn't match the data set in the form setup. This causes
563 * problems if something gets removed from the watchlist while the
564 * form is open (bug 32126), but we know that invalid items will
565 * be harmless so we can override it here.
567 * @param $value String the value the field was submitted with
568 * @param $alldata Array the data collected from the form
569 * @return Mixed Bool true on success, or String error to display.
571 function validate( $value, $alldata ) {
572 // Need to call into grandparent to be a good citizen. :)
573 return HTMLFormField
::validate( $value, $alldata );