3 * @defgroup Watchlist Users watchlist handling
7 * Implements Special:EditWatchlist
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 * @ingroup SpecialPage
30 * Provides the UI through which users can perform editing
31 * operations on their watchlist
33 * @ingroup SpecialPage
35 * @author Rob Church <robchur@gmail.com>
37 class SpecialEditWatchlist
extends UnlistedSpecialPage
{
39 * Editing modes. EDIT_CLEAR is no longer used; the "Clear" link scared people
40 * too much. Now it's passed on to the raw editor, from which it's very easy to clear.
44 const EDIT_NORMAL
= 3;
46 protected $successMessage;
50 private $badItems = array();
52 public function __construct() {
53 parent
::__construct( 'EditWatchlist', 'editmywatchlist' );
57 * Main execution point
61 public function execute( $mode ) {
64 # Anons don't get a watchlist
65 $this->requireLogin( 'watchlistanontext' );
67 $out = $this->getOutput();
69 $this->checkPermissions();
70 $this->checkReadOnly();
72 $this->outputHeader();
74 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName() )
75 ->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
77 # B/C: $mode used to be waaay down the parameter list, and the first parameter
79 if ( $mode instanceof User
) {
80 $args = func_get_args();
81 if ( count( $args ) >= 4 ) {
85 $mode = self
::getMode( $this->getRequest(), $mode );
89 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
90 $form = $this->getRawForm();
91 if ( $form->show() ) {
92 $out->addHTML( $this->successMessage
);
93 $out->addReturnTo( SpecialPage
::getTitleFor( 'Watchlist' ) );
96 case self
::EDIT_CLEAR
:
97 $out->setPageTitle( $this->msg( 'watchlistedit-clear-title' ) );
98 $form = $this->getClearForm();
99 if ( $form->show() ) {
100 $out->addHTML( $this->successMessage
);
101 $out->addReturnTo( SpecialPage
::getTitleFor( 'Watchlist' ) );
105 case self
::EDIT_NORMAL
:
107 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
108 $form = $this->getNormalForm();
109 if ( $form->show() ) {
110 $out->addHTML( $this->successMessage
);
111 $out->addReturnTo( SpecialPage
::getTitleFor( 'Watchlist' ) );
112 } elseif ( $this->toc
!== false ) {
113 $out->prependHTML( $this->toc
);
114 $out->addModules( 'mediawiki.toc' );
121 * Return an array of subpages beginning with $search that this special page will accept.
123 * @param string $search Prefix to search for
124 * @param int $limit Maximum number of results to return
125 * @return string[] Matching subpages
127 public function prefixSearchSubpages( $search, $limit = 10 ) {
128 return self
::prefixSearchArray(
131 // SpecialWatchlist uses SpecialEditWatchlist::getMode, so new types should be added
132 // here and there - no 'edit' here, because that the default for this page
141 * Extract a list of titles from a blob of text, returning
142 * (prefixed) strings; unwatchable titles are ignored
144 * @param string $list
147 private function extractTitles( $list ) {
148 $list = explode( "\n", trim( $list ) );
149 if ( !is_array( $list ) ) {
155 foreach ( $list as $text ) {
156 $text = trim( $text );
157 if ( strlen( $text ) > 0 ) {
158 $title = Title
::newFromText( $text );
159 if ( $title instanceof Title
&& $title->isWatchable() ) {
165 GenderCache
::singleton()->doTitlesArray( $titles );
168 /** @var Title $title */
169 foreach ( $titles as $title ) {
170 $list[] = $title->getPrefixedText();
173 return array_unique( $list );
176 public function submitRaw( $data ) {
177 $wanted = $this->extractTitles( $data['Titles'] );
178 $current = $this->getWatchlist();
180 if ( count( $wanted ) > 0 ) {
181 $toWatch = array_diff( $wanted, $current );
182 $toUnwatch = array_diff( $current, $wanted );
183 $this->watchTitles( $toWatch );
184 $this->unwatchTitles( $toUnwatch );
185 $this->getUser()->invalidateCache();
187 if ( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ) {
188 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
193 if ( count( $toWatch ) > 0 ) {
194 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-added' )
195 ->numParams( count( $toWatch ) )->parse();
196 $this->showTitles( $toWatch, $this->successMessage
);
199 if ( count( $toUnwatch ) > 0 ) {
200 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed' )
201 ->numParams( count( $toUnwatch ) )->parse();
202 $this->showTitles( $toUnwatch, $this->successMessage
);
205 $this->clearWatchlist();
206 $this->getUser()->invalidateCache();
208 if ( count( $current ) > 0 ) {
209 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
214 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed' )
215 ->numParams( count( $current ) )->parse();
216 $this->showTitles( $current, $this->successMessage
);
222 public function submitClear( $data ) {
223 $current = $this->getWatchlist();
224 $this->clearWatchlist();
225 $this->getUser()->invalidateCache();
226 $this->successMessage
= $this->msg( 'watchlistedit-clear-done' )->parse();
227 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-clear-removed' )
228 ->numParams( count( $current ) )->parse();
229 $this->showTitles( $current, $this->successMessage
);
235 * Print out a list of linked titles
237 * $titles can be an array of strings or Title objects; the former
238 * is preferred, since Titles are very memory-heavy
240 * @param array $titles Array of strings, or Title objects
241 * @param string $output
243 private function showTitles( $titles, &$output ) {
244 $talk = $this->msg( 'talkpagelinktext' )->escaped();
245 // Do a batch existence check
246 $batch = new LinkBatch();
247 if ( count( $titles ) >= 100 ) {
248 $output = wfMessage( 'watchlistedit-too-many' )->parse();
251 foreach ( $titles as $title ) {
252 if ( !$title instanceof Title
) {
253 $title = Title
::newFromText( $title );
256 if ( $title instanceof Title
) {
257 $batch->addObj( $title );
258 $batch->addObj( $title->getTalkPage() );
264 // Print out the list
267 foreach ( $titles as $title ) {
268 if ( !$title instanceof Title
) {
269 $title = Title
::newFromText( $title );
272 if ( $title instanceof Title
) {
274 . Linker
::link( $title )
275 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
280 $output .= "</ul>\n";
284 * Prepare a list of titles on a user's watchlist (excluding talk pages)
285 * and return an array of (prefixed) strings
289 private function getWatchlist() {
291 $dbr = wfGetDB( DB_MASTER
);
296 'wl_namespace', 'wl_title'
298 'wl_user' => $this->getUser()->getId(),
303 if ( $res->numRows() > 0 ) {
305 foreach ( $res as $row ) {
306 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
308 if ( $this->checkTitle( $title, $row->wl_namespace
, $row->wl_title
)
309 && !$title->isTalkPage()
316 GenderCache
::singleton()->doTitlesArray( $titles );
318 foreach ( $titles as $title ) {
319 $list[] = $title->getPrefixedText();
323 $this->cleanupWatchlist();
329 * Get a list of titles on a user's watchlist, excluding talk pages,
330 * and return as a two-dimensional array with namespace and title.
334 private function getWatchlistInfo() {
336 $dbr = wfGetDB( DB_MASTER
);
339 array( 'watchlist' ),
340 array( 'wl_namespace', 'wl_title' ),
341 array( 'wl_user' => $this->getUser()->getId() ),
343 array( 'ORDER BY' => array( 'wl_namespace', 'wl_title' ) )
346 $lb = new LinkBatch();
348 foreach ( $res as $row ) {
349 $lb->add( $row->wl_namespace
, $row->wl_title
);
350 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
351 $titles[$row->wl_namespace
][$row->wl_title
] = 1;
361 * Validates watchlist entry
363 * @param Title $title
364 * @param int $namespace
365 * @param string $dbKey
366 * @return bool Whether this item is valid
368 private function checkTitle( $title, $namespace, $dbKey ) {
370 && ( $title->isExternal()
371 ||
$title->getNamespace() < 0
374 $title = false; // unrecoverable
378 ||
$title->getNamespace() != $namespace
379 ||
$title->getDBkey() != $dbKey
381 $this->badItems
[] = array( $title, $namespace, $dbKey );
388 * Attempts to clean up broken items
390 private function cleanupWatchlist() {
391 if ( !count( $this->badItems
) ) {
392 return; //nothing to do
395 $dbw = wfGetDB( DB_MASTER
);
396 $user = $this->getUser();
398 foreach ( $this->badItems
as $row ) {
399 list( $title, $namespace, $dbKey ) = $row;
400 $action = $title ?
'cleaning up' : 'deleting';
401 wfDebug( "User {$user->getName()} has broken watchlist item ns($namespace):$dbKey, $action.\n" );
403 $dbw->delete( 'watchlist',
405 'wl_user' => $user->getId(),
406 'wl_namespace' => $namespace,
407 'wl_title' => $dbKey,
412 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
414 $user->addWatch( $title );
420 * Remove all titles from a user's watchlist
422 private function clearWatchlist() {
423 $dbw = wfGetDB( DB_MASTER
);
426 array( 'wl_user' => $this->getUser()->getId() ),
432 * Add a list of titles to a user's watchlist
434 * $titles can be an array of strings or Title objects; the former
435 * is preferred, since Titles are very memory-heavy
437 * @param array $titles Array of strings, or Title objects
439 private function watchTitles( $titles ) {
440 $dbw = wfGetDB( DB_MASTER
);
443 foreach ( $titles as $title ) {
444 if ( !$title instanceof Title
) {
445 $title = Title
::newFromText( $title );
448 if ( $title instanceof Title
) {
450 'wl_user' => $this->getUser()->getId(),
451 'wl_namespace' => MWNamespace
::getSubject( $title->getNamespace() ),
452 'wl_title' => $title->getDBkey(),
453 'wl_notificationtimestamp' => null,
456 'wl_user' => $this->getUser()->getId(),
457 'wl_namespace' => MWNamespace
::getTalk( $title->getNamespace() ),
458 'wl_title' => $title->getDBkey(),
459 'wl_notificationtimestamp' => null,
464 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
468 * Remove a list of titles from a user's watchlist
470 * $titles can be an array of strings or Title objects; the former
471 * is preferred, since Titles are very memory-heavy
473 * @param array $titles Array of strings, or Title objects
475 private function unwatchTitles( $titles ) {
476 $dbw = wfGetDB( DB_MASTER
);
478 foreach ( $titles as $title ) {
479 if ( !$title instanceof Title
) {
480 $title = Title
::newFromText( $title );
483 if ( $title instanceof Title
) {
487 'wl_user' => $this->getUser()->getId(),
488 'wl_namespace' => MWNamespace
::getSubject( $title->getNamespace() ),
489 'wl_title' => $title->getDBkey(),
497 'wl_user' => $this->getUser()->getId(),
498 'wl_namespace' => MWNamespace
::getTalk( $title->getNamespace() ),
499 'wl_title' => $title->getDBkey(),
504 $page = WikiPage
::factory( $title );
505 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
510 public function submitNormal( $data ) {
513 foreach ( $data as $titles ) {
514 $this->unwatchTitles( $titles );
515 $removed = array_merge( $removed, $titles );
518 if ( count( $removed ) > 0 ) {
519 $this->successMessage
= $this->msg( 'watchlistedit-normal-done'
520 )->numParams( count( $removed ) )->parse();
521 $this->showTitles( $removed, $this->successMessage
);
530 * Get the standard watchlist editing form
534 protected function getNormalForm() {
540 foreach ( $this->getWatchlistInfo() as $namespace => $pages ) {
543 foreach ( array_keys( $pages ) as $dbkey ) {
544 $title = Title
::makeTitleSafe( $namespace, $dbkey );
546 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
547 $text = $this->buildRemoveLine( $title );
548 $options[$text] = $title->getPrefixedText();
553 // checkTitle can filter some options out, avoid empty sections
554 if ( count( $options ) > 0 ) {
555 $fields['TitlesNs' . $namespace] = array(
556 'class' => 'EditWatchlistCheckboxSeriesField',
557 'options' => $options,
558 'section' => "ns$namespace",
562 $this->cleanupWatchlist();
564 if ( count( $fields ) > 1 && $count > 30 ) {
565 $this->toc
= Linker
::tocIndent();
568 foreach ( $fields as $data ) {
569 # strip out the 'ns' prefix from the section name:
570 $ns = substr( $data['section'], 2 );
572 $nsText = ( $ns == NS_MAIN
)
573 ?
$this->msg( 'blanknamespace' )->escaped()
574 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
575 $this->toc
.= Linker
::tocLine( "editwatchlist-{$data['section']}", $nsText,
576 $this->getLanguage()->formatNum( ++
$tocLength ), 1 ) . Linker
::tocLineEnd();
579 $this->toc
= Linker
::tocList( $this->toc
);
584 $context = new DerivativeContext( $this->getContext() );
585 $context->setTitle( $this->getPageTitle() ); // Remove subpage
586 $form = new EditWatchlistNormalHTMLForm( $fields, $context );
587 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
589 # 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
590 $form->setSubmitTooltip( 'watchlistedit-normal-submit' );
591 $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
592 $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() );
593 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
599 * Build the label for a checkbox, with a link to the title, and various additional bits
601 * @param Title $title
604 private function buildRemoveLine( $title ) {
605 $link = Linker
::link( $title );
607 if ( $title->isRedirect() ) {
608 // Linker already makes class mw-redirect, so this is redundant
609 $link = '<span class="watchlistredir">' . $link . '</span>';
612 $tools[] = Linker
::link( $title->getTalkPage(), $this->msg( 'talkpagelinktext' )->escaped() );
614 if ( $title->exists() ) {
615 $tools[] = Linker
::linkKnown(
617 $this->msg( 'history_short' )->escaped(),
619 array( 'action' => 'history' )
623 if ( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
624 $tools[] = Linker
::linkKnown(
625 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
626 $this->msg( 'contributions' )->escaped()
631 'WatchlistEditorBuildRemoveLine',
632 array( &$tools, $title, $title->isRedirect(), $this->getSkin() )
635 return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
639 * Get a form for editing the watchlist in "raw" mode
643 protected function getRawForm() {
644 $titles = implode( $this->getWatchlist(), "\n" );
647 'type' => 'textarea',
648 'label-message' => 'watchlistedit-raw-titles',
649 'default' => $titles,
652 $context = new DerivativeContext( $this->getContext() );
653 $context->setTitle( $this->getPageTitle( 'raw' ) ); // Reset subpage
654 $form = new HTMLForm( $fields, $context );
655 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
656 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
657 $form->setSubmitTooltip( 'watchlistedit-raw-submit' );
658 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
659 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
660 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
666 * Get a form for clearing the watchlist
670 protected function getClearForm() {
671 $context = new DerivativeContext( $this->getContext() );
672 $context->setTitle( $this->getPageTitle( 'clear' ) ); // Reset subpage
673 $form = new HTMLForm( array(), $context );
674 $form->setSubmitTextMsg( 'watchlistedit-clear-submit' );
675 # Used message keys: 'accesskey-watchlistedit-clear-submit', 'tooltip-watchlistedit-clear-submit'
676 $form->setSubmitTooltip( 'watchlistedit-clear-submit' );
677 $form->setWrapperLegendMsg( 'watchlistedit-clear-legend' );
678 $form->addHeaderText( $this->msg( 'watchlistedit-clear-explain' )->parse() );
679 $form->setSubmitCallback( array( $this, 'submitClear' ) );
685 * Determine whether we are editing the watchlist, and if so, what
686 * kind of editing operation
688 * @param WebRequest $request
692 public static function getMode( $request, $par ) {
693 $mode = strtolower( $request->getVal( 'action', $par ) );
697 case self
::EDIT_CLEAR
:
698 return self
::EDIT_CLEAR
;
701 return self
::EDIT_RAW
;
703 case self
::EDIT_NORMAL
:
704 return self
::EDIT_NORMAL
;
711 * Build a set of links for convenient navigation
712 * between watchlist viewing and editing modes
714 * @param null $unused
717 public static function buildTools( $unused ) {
722 'view' => array( 'Watchlist', false ),
723 'edit' => array( 'EditWatchlist', false ),
724 'raw' => array( 'EditWatchlist', 'raw' ),
725 'clear' => array( 'EditWatchlist', 'clear' ),
728 foreach ( $modes as $mode => $arr ) {
729 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
730 $tools[] = Linker
::linkKnown(
731 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
732 wfMessage( "watchlisttools-{$mode}" )->escaped()
736 return Html
::rawElement(
738 array( 'class' => 'mw-watchlist-toollinks' ),
739 wfMessage( 'parentheses', $wgLang->pipeList( $tools ) )->text()
745 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
747 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
748 public function getLegend( $namespace ) {
749 $namespace = substr( $namespace, 2 );
751 return $namespace == NS_MAIN
752 ?
$this->msg( 'blanknamespace' )->escaped()
753 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
756 public function getBody() {
757 return $this->displaySection( $this->mFieldTree
, '', 'editwatchlist-' );
761 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
763 * HTMLMultiSelectField throws validation errors if we get input data
764 * that doesn't match the data set in the form setup. This causes
765 * problems if something gets removed from the watchlist while the
766 * form is open (bug 32126), but we know that invalid items will
767 * be harmless so we can override it here.
769 * @param string $value The value the field was submitted with
770 * @param array $alldata The data collected from the form
771 * @return bool|string Bool true on success, or String error to display.
773 function validate( $value, $alldata ) {
774 // Need to call into grandparent to be a good citizen. :)
775 return HTMLFormField
::validate( $value, $alldata );