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();
58 array( 'parseinline', 'replaceafter' ),
59 $this->getUser()->getName(),
60 SpecialEditWatchlist
::buildTools( null )
62 $out->setSubtitle( $sub );
64 # B/C: $mode used to be waaay down the parameter list, and the first parameter
66 if( $mode instanceof User
){
67 $args = func_get_args();
68 if( count( $args >= 4 ) ){
72 $mode = self
::getMode( $this->getRequest(), $mode );
75 case self
::EDIT_CLEAR
:
76 // The "Clear" link scared people too much.
77 // Pass on to the raw editor, from which it's very easy to clear.
80 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
81 $form = $this->getRawForm();
83 $out->addHTML( $this->successMessage
);
88 case self
::EDIT_NORMAL
:
90 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
91 $form = $this->getNormalForm();
93 $out->addHTML( $this->successMessage
);
95 } elseif ( $this->toc
!== false ) {
96 $out->prependHTML( $this->toc
);
103 * Extract a list of titles from a blob of text, returning
104 * (prefixed) strings; unwatchable titles are ignored
106 * @param $list String
109 private function extractTitles( $list ) {
111 $list = explode( "\n", trim( $list ) );
112 if( !is_array( $list ) ) {
115 foreach( $list as $text ) {
116 $text = trim( $text );
117 if( strlen( $text ) > 0 ) {
118 $title = Title
::newFromText( $text );
119 if( $title instanceof Title
&& $title->isWatchable() ) {
120 $titles[] = $title->getPrefixedText();
124 return array_unique( $titles );
127 public function submitRaw( $data ){
128 $wanted = $this->extractTitles( $data['Titles'] );
129 $current = $this->getWatchlist();
131 if( count( $wanted ) > 0 ) {
132 $toWatch = array_diff( $wanted, $current );
133 $toUnwatch = array_diff( $current, $wanted );
134 $this->watchTitles( $toWatch );
135 $this->unwatchTitles( $toUnwatch );
136 $this->getUser()->invalidateCache();
138 if( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ){
139 $this->successMessage
= wfMessage( 'watchlistedit-raw-done' )->parse();
144 if( count( $toWatch ) > 0 ) {
145 $this->successMessage
.= wfMessage(
146 'watchlistedit-raw-added',
147 $this->getLang()->formatNum( count( $toWatch ) )
149 $this->showTitles( $toWatch, $this->successMessage
);
152 if( count( $toUnwatch ) > 0 ) {
153 $this->successMessage
.= wfMessage(
154 'watchlistedit-raw-removed',
155 $this->getLang()->formatNum( count( $toUnwatch ) )
157 $this->showTitles( $toUnwatch, $this->successMessage
);
160 $this->clearWatchlist();
161 $this->getUser()->invalidateCache();
162 $this->successMessage
.= wfMessage(
163 'watchlistedit-raw-removed',
164 $this->getLang()->formatNum( count( $current ) )
166 $this->showTitles( $current, $this->successMessage
);
172 * Print out a list of linked titles
174 * $titles can be an array of strings or Title objects; the former
175 * is preferred, since Titles are very memory-heavy
177 * @param $titles array of strings, or Title objects
178 * @param $output String
180 private function showTitles( $titles, &$output ) {
181 $talk = wfMsgHtml( 'talkpagelinktext' );
182 // Do a batch existence check
183 $batch = new LinkBatch();
184 foreach( $titles as $title ) {
185 if( !$title instanceof Title
) {
186 $title = Title
::newFromText( $title );
188 if( $title instanceof Title
) {
189 $batch->addObj( $title );
190 $batch->addObj( $title->getTalkPage() );
194 // Print out the list
196 foreach( $titles as $title ) {
197 if( !$title instanceof Title
) {
198 $title = Title
::newFromText( $title );
200 if( $title instanceof Title
) {
202 . Linker
::link( $title )
203 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
207 $output .= "</ul>\n";
211 * Prepare a list of titles on a user's watchlist (excluding talk pages)
212 * and return an array of (prefixed) strings
216 private function getWatchlist() {
218 $dbr = wfGetDB( DB_MASTER
);
223 'wl_user' => $this->getUser()->getId(),
227 if( $res->numRows() > 0 ) {
228 foreach ( $res as $row ) {
229 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
230 if( $title instanceof Title
&& !$title->isTalkPage() )
231 $list[] = $title->getPrefixedText();
239 * Get a list of titles on a user's watchlist, excluding talk pages,
240 * and return as a two-dimensional array with namespace, title and
245 private function getWatchlistInfo() {
247 $dbr = wfGetDB( DB_MASTER
);
250 array( 'watchlist' ),
251 array( 'wl_namespace', 'wl_title' ),
252 array( 'wl_user' => $this->getUser()->getId() ),
254 array( 'ORDER BY' => 'wl_namespace, wl_title' )
257 $lb = new LinkBatch();
258 foreach ( $res as $row ) {
259 $lb->add( $row->wl_namespace
, $row->wl_title
);
260 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
261 $titles[$row->wl_namespace
][$row->wl_title
] = false;
270 * Remove all titles from a user's watchlist
272 private function clearWatchlist() {
273 $dbw = wfGetDB( DB_MASTER
);
276 array( 'wl_user' => $this->getUser()->getId() ),
282 * Add a list of titles to a user's watchlist
284 * $titles can be an array of strings or Title objects; the former
285 * is preferred, since Titles are very memory-heavy
287 * @param $titles Array of strings, or Title objects
289 private function watchTitles( $titles ) {
290 $dbw = wfGetDB( DB_MASTER
);
292 foreach( $titles as $title ) {
293 if( !$title instanceof Title
) {
294 $title = Title
::newFromText( $title );
296 if( $title instanceof Title
) {
298 'wl_user' => $this->getUser()->getId(),
299 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
300 'wl_title' => $title->getDBkey(),
301 'wl_notificationtimestamp' => null,
304 'wl_user' => $this->getUser()->getId(),
305 'wl_namespace' => ( $title->getNamespace() |
1 ),
306 'wl_title' => $title->getDBkey(),
307 'wl_notificationtimestamp' => null,
311 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
315 * Remove a list of titles from a user's watchlist
317 * $titles can be an array of strings or Title objects; the former
318 * is preferred, since Titles are very memory-heavy
320 * @param $titles Array of strings, or Title objects
322 private function unwatchTitles( $titles ) {
323 $dbw = wfGetDB( DB_MASTER
);
324 foreach( $titles as $title ) {
325 if( !$title instanceof Title
) {
326 $title = Title
::newFromText( $title );
328 if( $title instanceof Title
) {
332 'wl_user' => $this->getUser()->getId(),
333 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
334 'wl_title' => $title->getDBkey(),
341 'wl_user' => $this->getUser()->getId(),
342 'wl_namespace' => ( $title->getNamespace() |
1 ),
343 'wl_title' => $title->getDBkey(),
347 $article = new Article( $title, 0 );
348 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$article ) );
353 public function submitNormal( $data ) {
356 foreach( $data as $titles ) {
357 $this->unwatchTitles( $titles );
361 if( count( $removed ) > 0 ) {
362 $this->successMessage
= wfMessage(
363 'watchlistedit-normal-done',
364 $this->getLang()->formatNum( count( $removed ) )
366 $this->showTitles( $removed, $this->successMessage
);
374 * Get the standard watchlist editing form
378 protected function getNormalForm(){
384 $haveInvalidNamespaces = false;
385 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
386 if ( $namespace < 0 ) {
387 $haveInvalidNamespaces = true;
391 $fields['TitlesNs'.$namespace] = array(
392 'class' => 'EditWatchlistCheckboxSeriesField',
393 'options' => array(),
394 'section' => "ns$namespace",
397 foreach( array_keys( $pages ) as $dbkey ){
398 $title = Title
::makeTitleSafe( $namespace, $dbkey );
399 $text = $this->buildRemoveLine( $title );
400 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
404 if ( $haveInvalidNamespaces ) {
405 wfDebug( "User {$this->getContext()->getUser()->getId()} has invalid watchlist entries, cleaning up...\n" );
406 $this->getContext()->getUser()->cleanupWatchlist();
409 if ( count( $fields ) > 1 && $count > 30 ) {
410 $this->toc
= Linker
::tocIndent();
412 foreach( $fields as $key => $data ) {
413 $ns = substr( $data['section'], 2 );
414 $nsText = $ns == NS_MAIN
415 ?
wfMsgHtml( 'blanknamespace' )
416 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
417 $this->toc
.= Linker
::tocLine( "mw-htmlform-{$data['section']}", $nsText, ++
$tocLength, 1 ) . Linker
::tocLineEnd();
419 $this->toc
= Linker
::tocList( $this->toc
);
424 $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
425 $form->setTitle( $this->getTitle() );
426 $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
427 $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
428 $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
429 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
434 * Build the label for a checkbox, with a link to the title, and various additional bits
436 * @param $title Title
439 private function buildRemoveLine( $title ) {
440 $link = Linker
::link( $title );
441 if( $title->isRedirect() ) {
442 // Linker already makes class mw-redirect, so this is redundant
443 $link = '<span class="watchlistredir">' . $link . '</span>';
445 $tools[] = Linker
::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
446 if( $title->exists() ) {
447 $tools[] = Linker
::linkKnown(
449 wfMsgHtml( 'history_short' ),
451 array( 'action' => 'history' )
454 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
455 $tools[] = Linker
::linkKnown(
456 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
457 wfMsgHtml( 'contributions' )
461 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
463 return $link . " (" . $this->getLang()->pipeList( $tools ) . ")";
467 * Get a form for editing the watchlist in "raw" mode
471 protected function getRawForm(){
472 $titles = implode( $this->getWatchlist(), "\n" );
475 'type' => 'textarea',
476 'label-message' => 'watchlistedit-raw-titles',
477 'default' => $titles,
480 $form = new HTMLForm( $fields, $this->getContext() );
481 $form->setTitle( $this->getTitle( 'raw' ) );
482 $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
483 $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
484 $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
485 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
490 * Determine whether we are editing the watchlist, and if so, what
491 * kind of editing operation
493 * @param $request WebRequest
497 public static function getMode( $request, $par ) {
498 $mode = strtolower( $request->getVal( 'action', $par ) );
501 case self
::EDIT_CLEAR
:
502 return self
::EDIT_CLEAR
;
506 return self
::EDIT_RAW
;
509 case self
::EDIT_NORMAL
:
510 return self
::EDIT_NORMAL
;
518 * Build a set of links for convenient navigation
519 * between watchlist viewing and editing modes
521 * @param $unused Unused
524 public static function buildTools( $unused ) {
529 'view' => array( 'Watchlist', false ),
530 'edit' => array( 'EditWatchlist', false ),
531 'raw' => array( 'EditWatchlist', 'raw' ),
533 foreach( $modes as $mode => $arr ) {
534 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
535 $tools[] = Linker
::linkKnown(
536 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
537 wfMsgHtml( "watchlisttools-{$mode}" )
540 return Html
::rawElement( 'span',
541 array( 'class' => 'mw-watchlist-toollinks' ),
542 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
547 class WatchlistEditor
extends SpecialEditWatchlist
{}
550 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
552 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
553 public function getLegend( $namespace ){
554 $namespace = substr( $namespace, 2 );
555 return $namespace == NS_MAIN
556 ?
wfMsgHtml( 'blanknamespace' )
557 : htmlspecialchars( $this->getContext()->getLang()->getFormattedNsText( $namespace ) );
561 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
563 * HTMLMultiSelectField throws validation errors if we get input data
564 * that doesn't match the data set in the form setup. This causes
565 * problems if something gets removed from the watchlist while the
566 * form is open (bug 32126), but we know that invalid items will
567 * be harmless so we can override it here.
569 * @param $value String the value the field was submitted with
570 * @param $alldata Array the data collected from the form
571 * @return Mixed Bool true on success, or String error to display.
573 function validate( $value, $alldata ) {
574 // Need to call into grandparent to be a good citizen. :)
575 return HTMLFormField
::validate( $value, $alldata );