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 and title.
239 private function getWatchlistInfo() {
241 $dbr = wfGetDB( DB_MASTER
);
244 array( 'watchlist' ),
245 array( 'wl_namespace', 'wl_title' ),
246 array( 'wl_user' => $this->getUser()->getId() ),
248 array( 'ORDER BY' => 'wl_namespace, wl_title' )
251 $lb = new LinkBatch();
252 foreach ( $res as $row ) {
253 $lb->add( $row->wl_namespace
, $row->wl_title
);
254 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
255 $titles[$row->wl_namespace
][$row->wl_title
] = 1;
264 * Remove all titles from a user's watchlist
266 private function clearWatchlist() {
267 $dbw = wfGetDB( DB_MASTER
);
270 array( 'wl_user' => $this->getUser()->getId() ),
276 * Add a list of titles to a user's watchlist
278 * $titles can be an array of strings or Title objects; the former
279 * is preferred, since Titles are very memory-heavy
281 * @param $titles Array of strings, or Title objects
283 private function watchTitles( $titles ) {
284 $dbw = wfGetDB( DB_MASTER
);
286 foreach( $titles as $title ) {
287 if( !$title instanceof Title
) {
288 $title = Title
::newFromText( $title );
290 if( $title instanceof Title
) {
292 'wl_user' => $this->getUser()->getId(),
293 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
294 'wl_title' => $title->getDBkey(),
295 'wl_notificationtimestamp' => null,
298 'wl_user' => $this->getUser()->getId(),
299 'wl_namespace' => ( $title->getNamespace() |
1 ),
300 'wl_title' => $title->getDBkey(),
301 'wl_notificationtimestamp' => null,
305 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
309 * Remove a list of titles from a user's watchlist
311 * $titles can be an array of strings or Title objects; the former
312 * is preferred, since Titles are very memory-heavy
314 * @param $titles Array of strings, or Title objects
316 private function unwatchTitles( $titles ) {
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' => $this->getUser()->getId(),
327 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
328 'wl_title' => $title->getDBkey(),
335 'wl_user' => $this->getUser()->getId(),
336 'wl_namespace' => ( $title->getNamespace() |
1 ),
337 'wl_title' => $title->getDBkey(),
341 $article = new Article( $title, 0 );
342 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$article ) );
347 public function submitNormal( $data ) {
350 foreach( $data as $titles ) {
351 $this->unwatchTitles( $titles );
355 if( count( $removed ) > 0 ) {
356 $this->successMessage
= wfMessage(
357 'watchlistedit-normal-done',
358 $this->getLang()->formatNum( count( $removed ) )
360 $this->showTitles( $removed, $this->successMessage
);
368 * Get the standard watchlist editing form
372 protected function getNormalForm(){
378 $haveInvalidNamespaces = false;
379 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
380 if ( $namespace < 0 ) {
381 $haveInvalidNamespaces = true;
385 $fields['TitlesNs'.$namespace] = array(
386 'class' => 'EditWatchlistCheckboxSeriesField',
387 'options' => array(),
388 'section' => "ns$namespace",
391 foreach( array_keys( $pages ) as $dbkey ){
392 $title = Title
::makeTitleSafe( $namespace, $dbkey );
393 $text = $this->buildRemoveLine( $title );
394 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
398 if ( $haveInvalidNamespaces ) {
399 wfDebug( "User {$this->getContext()->getUser()->getId()} has invalid watchlist entries, cleaning up...\n" );
400 $this->getContext()->getUser()->cleanupWatchlist();
403 if ( count( $fields ) > 1 && $count > 30 ) {
404 $this->toc
= Linker
::tocIndent();
406 foreach( $fields as $key => $data ) {
408 # strip out the 'ns' prefix from the section name:
409 $ns = substr( $data['section'], 2 );
411 $nsText = ($ns == NS_MAIN
)
412 ?
wfMsgHtml( 'blanknamespace' )
413 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
414 $this->toc
.= Linker
::tocLine( "mw-htmlform-{$data['section']}", $nsText, ++
$tocLength, 1 ) . Linker
::tocLineEnd();
416 $this->toc
= Linker
::tocList( $this->toc
);
421 $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
422 $form->setTitle( $this->getTitle() );
423 $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
424 $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
425 $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
426 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
431 * Build the label for a checkbox, with a link to the title, and various additional bits
433 * @param $title Title
436 private function buildRemoveLine( $title ) {
437 $link = Linker
::link( $title );
438 if( $title->isRedirect() ) {
439 // Linker already makes class mw-redirect, so this is redundant
440 $link = '<span class="watchlistredir">' . $link . '</span>';
442 $tools[] = Linker
::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
443 if( $title->exists() ) {
444 $tools[] = Linker
::linkKnown(
446 wfMsgHtml( 'history_short' ),
448 array( 'action' => 'history' )
451 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
452 $tools[] = Linker
::linkKnown(
453 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
454 wfMsgHtml( 'contributions' )
458 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
460 return $link . " (" . $this->getLang()->pipeList( $tools ) . ")";
464 * Get a form for editing the watchlist in "raw" mode
468 protected function getRawForm(){
469 $titles = implode( $this->getWatchlist(), "\n" );
472 'type' => 'textarea',
473 'label-message' => 'watchlistedit-raw-titles',
474 'default' => $titles,
477 $form = new HTMLForm( $fields, $this->getContext() );
478 $form->setTitle( $this->getTitle( 'raw' ) );
479 $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
480 $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
481 $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
482 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
487 * Determine whether we are editing the watchlist, and if so, what
488 * kind of editing operation
490 * @param $request WebRequest
494 public static function getMode( $request, $par ) {
495 $mode = strtolower( $request->getVal( 'action', $par ) );
498 case self
::EDIT_CLEAR
:
499 return self
::EDIT_CLEAR
;
503 return self
::EDIT_RAW
;
506 case self
::EDIT_NORMAL
:
507 return self
::EDIT_NORMAL
;
515 * Build a set of links for convenient navigation
516 * between watchlist viewing and editing modes
518 * @param $unused Unused
521 public static function buildTools( $unused ) {
526 'view' => array( 'Watchlist', false ),
527 'edit' => array( 'EditWatchlist', false ),
528 'raw' => array( 'EditWatchlist', 'raw' ),
530 foreach( $modes as $mode => $arr ) {
531 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
532 $tools[] = Linker
::linkKnown(
533 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
534 wfMsgHtml( "watchlisttools-{$mode}" )
537 return Html
::rawElement( 'span',
538 array( 'class' => 'mw-watchlist-toollinks' ),
539 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
544 class WatchlistEditor
extends SpecialEditWatchlist
{}
547 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
549 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
550 public function getLegend( $namespace ){
551 $namespace = substr( $namespace, 2 );
552 return $namespace == NS_MAIN
553 ?
wfMsgHtml( 'blanknamespace' )
554 : htmlspecialchars( $this->getContext()->getLang()->getFormattedNsText( $namespace ) );
558 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
560 * HTMLMultiSelectField throws validation errors if we get input data
561 * that doesn't match the data set in the form setup. This causes
562 * problems if something gets removed from the watchlist while the
563 * form is open (bug 32126), but we know that invalid items will
564 * be harmless so we can override it here.
566 * @param $value String the value the field was submitted with
567 * @param $alldata Array the data collected from the form
568 * @return Mixed Bool true on success, or String error to display.
570 function validate( $value, $alldata ) {
571 // Need to call into grandparent to be a good citizen. :)
572 return HTMLFormField
::validate( $value, $alldata );