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() );
50 $this->checkPermissions();
52 $this->outputHeader();
54 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName()
55 )->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
57 # B/C: $mode used to be waaay down the parameter list, and the first parameter
59 if( $mode instanceof User
){
60 $args = func_get_args();
61 if( count( $args >= 4 ) ){
65 $mode = self
::getMode( $this->getRequest(), $mode );
68 case self
::EDIT_CLEAR
:
69 // The "Clear" link scared people too much.
70 // Pass on to the raw editor, from which it's very easy to clear.
73 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
74 $form = $this->getRawForm();
76 $out->addHTML( $this->successMessage
);
81 case self
::EDIT_NORMAL
:
83 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
84 $form = $this->getNormalForm();
86 $out->addHTML( $this->successMessage
);
88 } elseif ( $this->toc
!== false ) {
89 $out->prependHTML( $this->toc
);
96 * Extract a list of titles from a blob of text, returning
97 * (prefixed) strings; unwatchable titles are ignored
102 private function extractTitles( $list ) {
104 $list = explode( "\n", trim( $list ) );
105 if( !is_array( $list ) ) {
108 foreach( $list as $text ) {
109 $text = trim( $text );
110 if( strlen( $text ) > 0 ) {
111 $title = Title
::newFromText( $text );
112 if( $title instanceof Title
&& $title->isWatchable() ) {
113 $titles[] = $title->getPrefixedText();
117 return array_unique( $titles );
120 public function submitRaw( $data ){
121 $wanted = $this->extractTitles( $data['Titles'] );
122 $current = $this->getWatchlist();
124 if( count( $wanted ) > 0 ) {
125 $toWatch = array_diff( $wanted, $current );
126 $toUnwatch = array_diff( $current, $wanted );
127 $this->watchTitles( $toWatch );
128 $this->unwatchTitles( $toUnwatch );
129 $this->getUser()->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 $this->getLang()->formatNum( count( $toWatch ) )
142 $this->showTitles( $toWatch, $this->successMessage
);
145 if( count( $toUnwatch ) > 0 ) {
146 $this->successMessage
.= ' ' . wfMessage(
147 'watchlistedit-raw-removed',
148 $this->getLang()->formatNum( count( $toUnwatch ) )
150 $this->showTitles( $toUnwatch, $this->successMessage
);
153 $this->clearWatchlist();
154 $this->getUser()->invalidateCache();
156 if( count( $current ) > 0 ){
157 $this->successMessage
= wfMessage( 'watchlistedit-raw-done' )->parse();
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 and title.
244 private function getWatchlistInfo() {
246 $dbr = wfGetDB( DB_MASTER
);
249 array( 'watchlist' ),
250 array( 'wl_namespace', 'wl_title' ),
251 array( 'wl_user' => $this->getUser()->getId() ),
253 array( 'ORDER BY' => 'wl_namespace, wl_title' )
256 $lb = new LinkBatch();
257 foreach ( $res as $row ) {
258 $lb->add( $row->wl_namespace
, $row->wl_title
);
259 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
260 $titles[$row->wl_namespace
][$row->wl_title
] = 1;
269 * Remove all titles from a user's watchlist
271 private function clearWatchlist() {
272 $dbw = wfGetDB( DB_MASTER
);
275 array( 'wl_user' => $this->getUser()->getId() ),
281 * Add a list of titles to a user's watchlist
283 * $titles can be an array of strings or Title objects; the former
284 * is preferred, since Titles are very memory-heavy
286 * @param $titles Array of strings, or Title objects
288 private function watchTitles( $titles ) {
289 $dbw = wfGetDB( DB_MASTER
);
291 foreach( $titles as $title ) {
292 if( !$title instanceof Title
) {
293 $title = Title
::newFromText( $title );
295 if( $title instanceof Title
) {
297 'wl_user' => $this->getUser()->getId(),
298 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
299 'wl_title' => $title->getDBkey(),
300 'wl_notificationtimestamp' => null,
303 'wl_user' => $this->getUser()->getId(),
304 'wl_namespace' => ( $title->getNamespace() |
1 ),
305 'wl_title' => $title->getDBkey(),
306 'wl_notificationtimestamp' => null,
310 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
314 * Remove a list of titles from a user's watchlist
316 * $titles can be an array of strings or Title objects; the former
317 * is preferred, since Titles are very memory-heavy
319 * @param $titles Array of strings, or Title objects
321 private function unwatchTitles( $titles ) {
322 $dbw = wfGetDB( DB_MASTER
);
323 foreach( $titles as $title ) {
324 if( !$title instanceof Title
) {
325 $title = Title
::newFromText( $title );
327 if( $title instanceof Title
) {
331 'wl_user' => $this->getUser()->getId(),
332 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
333 'wl_title' => $title->getDBkey(),
340 'wl_user' => $this->getUser()->getId(),
341 'wl_namespace' => ( $title->getNamespace() |
1 ),
342 'wl_title' => $title->getDBkey(),
346 $article = new Article( $title, 0 );
347 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$article ) );
352 public function submitNormal( $data ) {
355 foreach( $data as $titles ) {
356 $this->unwatchTitles( $titles );
360 if( count( $removed ) > 0 ) {
361 $this->successMessage
= wfMessage(
362 'watchlistedit-normal-done',
363 $this->getLang()->formatNum( count( $removed ) )
365 $this->showTitles( $removed, $this->successMessage
);
373 * Get the standard watchlist editing form
377 protected function getNormalForm(){
383 $haveInvalidNamespaces = false;
384 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
385 if ( $namespace < 0 ) {
386 $haveInvalidNamespaces = true;
390 $fields['TitlesNs'.$namespace] = array(
391 'class' => 'EditWatchlistCheckboxSeriesField',
392 'options' => array(),
393 'section' => "ns$namespace",
396 foreach( array_keys( $pages ) as $dbkey ){
397 $title = Title
::makeTitleSafe( $namespace, $dbkey );
398 $text = $this->buildRemoveLine( $title );
399 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
403 if ( $haveInvalidNamespaces ) {
404 wfDebug( "User {$this->getContext()->getUser()->getId()} has invalid watchlist entries, cleaning up...\n" );
405 $this->getContext()->getUser()->cleanupWatchlist();
408 if ( count( $fields ) > 1 && $count > 30 ) {
409 $this->toc
= Linker
::tocIndent();
411 foreach( $fields as $key => $data ) {
413 # strip out the 'ns' prefix from the section name:
414 $ns = substr( $data['section'], 2 );
416 $nsText = ($ns == NS_MAIN
)
417 ?
wfMsgHtml( 'blanknamespace' )
418 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
419 $this->toc
.= Linker
::tocLine( "mw-htmlform-{$data['section']}", $nsText,
420 $this->getLang()->formatNum( ++
$tocLength ), 1 ) . Linker
::tocLineEnd();
422 $this->toc
= Linker
::tocList( $this->toc
);
427 $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
428 $form->setTitle( $this->getTitle() );
429 $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
430 $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
431 $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
432 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
437 * Build the label for a checkbox, with a link to the title, and various additional bits
439 * @param $title Title
442 private function buildRemoveLine( $title ) {
443 $link = Linker
::link( $title );
444 if( $title->isRedirect() ) {
445 // Linker already makes class mw-redirect, so this is redundant
446 $link = '<span class="watchlistredir">' . $link . '</span>';
448 $tools[] = Linker
::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
449 if( $title->exists() ) {
450 $tools[] = Linker
::linkKnown(
452 wfMsgHtml( 'history_short' ),
454 array( 'action' => 'history' )
457 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
458 $tools[] = Linker
::linkKnown(
459 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
460 wfMsgHtml( 'contributions' )
464 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
466 return $link . " (" . $this->getLang()->pipeList( $tools ) . ")";
470 * Get a form for editing the watchlist in "raw" mode
474 protected function getRawForm(){
475 $titles = implode( $this->getWatchlist(), "\n" );
478 'type' => 'textarea',
479 'label-message' => 'watchlistedit-raw-titles',
480 'default' => $titles,
483 $form = new HTMLForm( $fields, $this->getContext() );
484 $form->setTitle( $this->getTitle( 'raw' ) );
485 $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
486 $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
487 $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
488 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
493 * Determine whether we are editing the watchlist, and if so, what
494 * kind of editing operation
496 * @param $request WebRequest
500 public static function getMode( $request, $par ) {
501 $mode = strtolower( $request->getVal( 'action', $par ) );
504 case self
::EDIT_CLEAR
:
505 return self
::EDIT_CLEAR
;
509 return self
::EDIT_RAW
;
512 case self
::EDIT_NORMAL
:
513 return self
::EDIT_NORMAL
;
521 * Build a set of links for convenient navigation
522 * between watchlist viewing and editing modes
524 * @param $unused Unused
527 public static function buildTools( $unused ) {
532 'view' => array( 'Watchlist', false ),
533 'edit' => array( 'EditWatchlist', false ),
534 'raw' => array( 'EditWatchlist', 'raw' ),
536 foreach( $modes as $mode => $arr ) {
537 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
538 $tools[] = Linker
::linkKnown(
539 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
540 wfMsgHtml( "watchlisttools-{$mode}" )
543 return Html
::rawElement( 'span',
544 array( 'class' => 'mw-watchlist-toollinks' ),
545 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
550 class WatchlistEditor
extends SpecialEditWatchlist
{}
553 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
555 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
556 public function getLegend( $namespace ){
557 $namespace = substr( $namespace, 2 );
558 return $namespace == NS_MAIN
559 ?
wfMsgHtml( 'blanknamespace' )
560 : htmlspecialchars( $this->getContext()->getLang()->getFormattedNsText( $namespace ) );
564 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
566 * HTMLMultiSelectField throws validation errors if we get input data
567 * that doesn't match the data set in the form setup. This causes
568 * problems if something gets removed from the watchlist while the
569 * form is open (bug 32126), but we know that invalid items will
570 * be harmless so we can override it here.
572 * @param $value String the value the field was submitted with
573 * @param $alldata Array the data collected from the form
574 * @return Mixed Bool true on success, or String error to display.
576 function validate( $value, $alldata ) {
577 // Need to call into grandparent to be a good citizen. :)
578 return HTMLFormField
::validate( $value, $alldata );