3 * @defgroup Watchlist Users watchlist handling
7 * Provides the UI through which users can perform editing
8 * operations on their watchlist
11 * @author Rob Church <robchur@gmail.com>
13 class SpecialEditWatchlist
extends UnlistedSpecialPage
{
20 const EDIT_NORMAL
= 3;
22 protected $successMessage;
26 private $badItems = array();
28 public function __construct(){
29 parent
::__construct( 'EditWatchlist' );
33 * Main execution point
37 public function execute( $mode ) {
40 $out = $this->getOutput();
42 # Anons don't get a watchlist
43 if( $this->getUser()->isAnon() ) {
44 $out->setPageTitle( $this->msg( 'watchnologin' ) );
45 $llink = Linker
::linkKnown(
46 SpecialPage
::getTitleFor( 'Userlogin' ),
47 $this->msg( 'loginreqlink' )->escaped(),
49 array( 'returnto' => $this->getTitle()->getPrefixedText() )
51 $out->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
55 $this->checkPermissions();
57 $this->outputHeader();
59 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName()
60 )->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
62 # B/C: $mode used to be waaay down the parameter list, and the first parameter
64 if( $mode instanceof User
){
65 $args = func_get_args();
66 if( count( $args >= 4 ) ){
70 $mode = self
::getMode( $this->getRequest(), $mode );
73 case self
::EDIT_CLEAR
:
74 // The "Clear" link scared people too much.
75 // Pass on to the raw editor, from which it's very easy to clear.
78 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
79 $form = $this->getRawForm();
81 $out->addHTML( $this->successMessage
);
86 case self
::EDIT_NORMAL
:
88 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
89 $form = $this->getNormalForm();
91 $out->addHTML( $this->successMessage
);
93 } elseif ( $this->toc
!== false ) {
94 $out->prependHTML( $this->toc
);
101 * Extract a list of titles from a blob of text, returning
102 * (prefixed) strings; unwatchable titles are ignored
104 * @param $list String
107 private function extractTitles( $list ) {
109 $list = explode( "\n", trim( $list ) );
110 if( !is_array( $list ) ) {
113 foreach( $list as $text ) {
114 $text = trim( $text );
115 if( strlen( $text ) > 0 ) {
116 $title = Title
::newFromText( $text );
117 if( $title instanceof Title
&& $title->isWatchable() ) {
118 $titles[] = $title->getPrefixedText();
122 return array_unique( $titles );
125 public function submitRaw( $data ){
126 $wanted = $this->extractTitles( $data['Titles'] );
127 $current = $this->getWatchlist();
129 if( count( $wanted ) > 0 ) {
130 $toWatch = array_diff( $wanted, $current );
131 $toUnwatch = array_diff( $current, $wanted );
132 $this->watchTitles( $toWatch );
133 $this->unwatchTitles( $toUnwatch );
134 $this->getUser()->invalidateCache();
136 if( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ){
137 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
142 if( count( $toWatch ) > 0 ) {
143 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-added'
144 )->numParams( count( $toWatch ) )->parse();
145 $this->showTitles( $toWatch, $this->successMessage
);
148 if( count( $toUnwatch ) > 0 ) {
149 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed'
150 )->numParams( count( $toUnwatch ) )->parse();
151 $this->showTitles( $toUnwatch, $this->successMessage
);
154 $this->clearWatchlist();
155 $this->getUser()->invalidateCache();
157 if( count( $current ) > 0 ){
158 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
163 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed'
164 )->numParams( count( $current ) )->parse();
165 $this->showTitles( $current, $this->successMessage
);
171 * Print out a list of linked titles
173 * $titles can be an array of strings or Title objects; the former
174 * is preferred, since Titles are very memory-heavy
176 * @param $titles array of strings, or Title objects
177 * @param $output String
179 private function showTitles( $titles, &$output ) {
180 $talk = $this->msg( 'talkpagelinktext' )->escaped();
181 // Do a batch existence check
182 $batch = new LinkBatch();
183 foreach( $titles as $title ) {
184 if( !$title instanceof Title
) {
185 $title = Title
::newFromText( $title );
187 if( $title instanceof Title
) {
188 $batch->addObj( $title );
189 $batch->addObj( $title->getTalkPage() );
193 // Print out the list
195 foreach( $titles as $title ) {
196 if( !$title instanceof Title
) {
197 $title = Title
::newFromText( $title );
199 if( $title instanceof Title
) {
201 . Linker
::link( $title )
202 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
206 $output .= "</ul>\n";
210 * Prepare a list of titles on a user's watchlist (excluding talk pages)
211 * and return an array of (prefixed) strings
215 private function getWatchlist() {
217 $dbr = wfGetDB( DB_MASTER
);
222 'wl_user' => $this->getUser()->getId(),
226 if( $res->numRows() > 0 ) {
227 foreach ( $res as $row ) {
228 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
229 if ( $this->checkTitle( $title, $row->wl_namespace
, $row->wl_title
)
230 && !$title->isTalkPage()
232 $list[] = $title->getPrefixedText();
237 $this->cleanupWatchlist();
242 * Get a list of titles on a user's watchlist, excluding talk pages,
243 * and return as a two-dimensional array with namespace and title.
247 private function getWatchlistInfo() {
249 $dbr = wfGetDB( DB_MASTER
);
252 array( 'watchlist' ),
253 array( 'wl_namespace', 'wl_title' ),
254 array( 'wl_user' => $this->getUser()->getId() ),
256 array( 'ORDER BY' => 'wl_namespace, wl_title' )
259 $lb = new LinkBatch();
260 foreach ( $res as $row ) {
261 $lb->add( $row->wl_namespace
, $row->wl_title
);
262 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
263 $titles[$row->wl_namespace
][$row->wl_title
] = 1;
272 * Validates watchlist entry
274 * @param Title $title
275 * @param int $namespace
276 * @param String $dbKey
277 * @return bool: Whether this item is valid
279 private function checkTitle( $title, $namespace, $dbKey ) {
281 && ( $title->isExternal()
282 ||
$title->getNamespace() < 0
285 $title = false; // unrecoverable
288 ||
$title->getNamespace() != $namespace
289 ||
$title->getDBkey() != $dbKey
291 $this->badItems
[] = array( $title, $namespace, $dbKey );
297 * Attempts to clean up broken items
299 private function cleanupWatchlist() {
300 $dbw = wfGetDB( DB_MASTER
);
301 foreach ( $this->badItems
as $row ) {
302 list( $title, $namespace, $dbKey ) = $row;
303 wfDebug( "User {$this->getUser()} has broken watchlist item ns($namespace):$dbKey, "
304 . ( $title ?
'cleaning up' : 'deleting' ) . ".\n"
307 $dbw->delete( 'watchlist',
309 'wl_user' => $this->getUser()->getId(),
310 'wl_namespace' => $namespace,
311 'wl_title' => $dbKey,
316 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
318 $this->getUser()->addWatch( $title );
324 * Remove all titles from a user's watchlist
326 private function clearWatchlist() {
327 $dbw = wfGetDB( DB_MASTER
);
330 array( 'wl_user' => $this->getUser()->getId() ),
336 * Add a list of titles to a user's watchlist
338 * $titles can be an array of strings or Title objects; the former
339 * is preferred, since Titles are very memory-heavy
341 * @param $titles Array of strings, or Title objects
343 private function watchTitles( $titles ) {
344 $dbw = wfGetDB( DB_MASTER
);
346 foreach( $titles as $title ) {
347 if( !$title instanceof Title
) {
348 $title = Title
::newFromText( $title );
350 if( $title instanceof Title
) {
352 'wl_user' => $this->getUser()->getId(),
353 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
354 'wl_title' => $title->getDBkey(),
355 'wl_notificationtimestamp' => null,
358 'wl_user' => $this->getUser()->getId(),
359 'wl_namespace' => ( $title->getNamespace() |
1 ),
360 'wl_title' => $title->getDBkey(),
361 'wl_notificationtimestamp' => null,
365 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
369 * Remove a list of titles from a user's watchlist
371 * $titles can be an array of strings or Title objects; the former
372 * is preferred, since Titles are very memory-heavy
374 * @param $titles Array of strings, or Title objects
376 private function unwatchTitles( $titles ) {
377 $dbw = wfGetDB( DB_MASTER
);
378 foreach( $titles as $title ) {
379 if( !$title instanceof Title
) {
380 $title = Title
::newFromText( $title );
382 if( $title instanceof Title
) {
386 'wl_user' => $this->getUser()->getId(),
387 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
388 'wl_title' => $title->getDBkey(),
395 'wl_user' => $this->getUser()->getId(),
396 'wl_namespace' => ( $title->getNamespace() |
1 ),
397 'wl_title' => $title->getDBkey(),
401 $page = WikiPage
::factory( $title );
402 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
407 public function submitNormal( $data ) {
410 foreach( $data as $titles ) {
411 $this->unwatchTitles( $titles );
415 if( count( $removed ) > 0 ) {
416 $this->successMessage
= $this->msg( 'watchlistedit-normal-done'
417 )->numParams( count( $removed ) )->parse();
418 $this->showTitles( $removed, $this->successMessage
);
426 * Get the standard watchlist editing form
430 protected function getNormalForm(){
436 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
437 if ( $namespace >= 0 ) {
438 $fields['TitlesNs'.$namespace] = array(
439 'class' => 'EditWatchlistCheckboxSeriesField',
440 'options' => array(),
441 'section' => "ns$namespace",
445 foreach( array_keys( $pages ) as $dbkey ){
446 $title = Title
::makeTitleSafe( $namespace, $dbkey );
447 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
448 $text = $this->buildRemoveLine( $title );
449 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
454 $this->cleanupWatchlist();
456 if ( count( $fields ) > 1 && $count > 30 ) {
457 $this->toc
= Linker
::tocIndent();
459 foreach( $fields as $key => $data ) {
461 # strip out the 'ns' prefix from the section name:
462 $ns = substr( $data['section'], 2 );
464 $nsText = ($ns == NS_MAIN
)
465 ?
$this->msg( 'blanknamespace' )->escaped()
466 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
467 $this->toc
.= Linker
::tocLine( "editwatchlist-{$data['section']}", $nsText,
468 $this->getLanguage()->formatNum( ++
$tocLength ), 1 ) . Linker
::tocLineEnd();
470 $this->toc
= Linker
::tocList( $this->toc
);
475 $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
476 $form->setTitle( $this->getTitle() );
477 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
478 # Used message keys: 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
479 $form->setSubmitTooltip('watchlistedit-normal-submit');
480 $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
481 $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() );
482 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
487 * Build the label for a checkbox, with a link to the title, and various additional bits
489 * @param $title Title
492 private function buildRemoveLine( $title ) {
493 $link = Linker
::link( $title );
494 if( $title->isRedirect() ) {
495 // Linker already makes class mw-redirect, so this is redundant
496 $link = '<span class="watchlistredir">' . $link . '</span>';
498 $tools[] = Linker
::link( $title->getTalkPage(), $this->msg( 'talkpagelinktext' )->escaped() );
499 if( $title->exists() ) {
500 $tools[] = Linker
::linkKnown(
502 $this->msg( 'history_short' )->escaped(),
504 array( 'action' => 'history' )
507 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
508 $tools[] = Linker
::linkKnown(
509 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
510 $this->msg( 'contributions' )->escaped()
514 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
516 return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
520 * Get a form for editing the watchlist in "raw" mode
524 protected function getRawForm(){
525 $titles = implode( $this->getWatchlist(), "\n" );
528 'type' => 'textarea',
529 'label-message' => 'watchlistedit-raw-titles',
530 'default' => $titles,
533 $form = new HTMLForm( $fields, $this->getContext() );
534 $form->setTitle( $this->getTitle( 'raw' ) );
535 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
536 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
537 $form->setSubmitTooltip('watchlistedit-raw-submit');
538 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
539 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
540 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
545 * Determine whether we are editing the watchlist, and if so, what
546 * kind of editing operation
548 * @param $request WebRequest
552 public static function getMode( $request, $par ) {
553 $mode = strtolower( $request->getVal( 'action', $par ) );
556 case self
::EDIT_CLEAR
:
557 return self
::EDIT_CLEAR
;
561 return self
::EDIT_RAW
;
564 case self
::EDIT_NORMAL
:
565 return self
::EDIT_NORMAL
;
573 * Build a set of links for convenient navigation
574 * between watchlist viewing and editing modes
579 public static function buildTools( $unused ) {
584 'view' => array( 'Watchlist', false ),
585 'edit' => array( 'EditWatchlist', false ),
586 'raw' => array( 'EditWatchlist', 'raw' ),
588 foreach( $modes as $mode => $arr ) {
589 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
590 $tools[] = Linker
::linkKnown(
591 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
592 wfMsgHtml( "watchlisttools-{$mode}" )
595 return Html
::rawElement( 'span',
596 array( 'class' => 'mw-watchlist-toollinks' ),
597 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
602 class WatchlistEditor
extends SpecialEditWatchlist
{}
605 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
607 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
608 public function getLegend( $namespace ){
609 $namespace = substr( $namespace, 2 );
610 return $namespace == NS_MAIN
611 ?
$this->msg( 'blanknamespace' )->escaped()
612 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
614 public function getBody() {
615 return $this->displaySection( $this->mFieldTree
, '', 'editwatchlist-' );
619 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
621 * HTMLMultiSelectField throws validation errors if we get input data
622 * that doesn't match the data set in the form setup. This causes
623 * problems if something gets removed from the watchlist while the
624 * form is open (bug 32126), but we know that invalid items will
625 * be harmless so we can override it here.
627 * @param $value String the value the field was submitted with
628 * @param $alldata Array the data collected from the form
629 * @return Mixed Bool true on success, or String error to display.
631 function validate( $value, $alldata ) {
632 // Need to call into grandparent to be a good citizen. :)
633 return HTMLFormField
::validate( $value, $alldata );