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();
73 $this->outputSubtitle();
74 $out->addModuleStyles( 'mediawiki.special' );
76 # B/C: $mode used to be waaay down the parameter list, and the first parameter
78 if ( $mode instanceof User
) {
79 $args = func_get_args();
80 if ( count( $args ) >= 4 ) {
84 $mode = self
::getMode( $this->getRequest(), $mode );
88 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
89 $form = $this->getRawForm();
90 if ( $form->show() ) {
91 $out->addHTML( $this->successMessage
);
92 $out->addReturnTo( SpecialPage
::getTitleFor( 'Watchlist' ) );
95 case self
::EDIT_CLEAR
:
96 $out->setPageTitle( $this->msg( 'watchlistedit-clear-title' ) );
97 $form = $this->getClearForm();
98 if ( $form->show() ) {
99 $out->addHTML( $this->successMessage
);
100 $out->addReturnTo( SpecialPage
::getTitleFor( 'Watchlist' ) );
104 case self
::EDIT_NORMAL
:
106 $this->executeViewEditWatchlist();
112 * Renders a subheader on the watchlist page.
114 protected function outputSubtitle() {
115 $out = $this->getOutput();
116 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName() )
117 ->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
121 * Executes an edit mode for the watchlist view, from which you can manage your watchlist
124 protected function executeViewEditWatchlist() {
125 $out = $this->getOutput();
126 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
127 $form = $this->getNormalForm();
128 if ( $form->show() ) {
129 $out->addHTML( $this->successMessage
);
130 $out->addReturnTo( SpecialPage
::getTitleFor( 'Watchlist' ) );
131 } elseif ( $this->toc
!== false ) {
132 $out->prependHTML( $this->toc
);
133 $out->addModules( 'mediawiki.toc' );
138 * Return an array of subpages that this special page will accept.
140 * @see also SpecialWatchlist::getSubpagesForPrefixSearch
141 * @return string[] subpages
143 public function getSubpagesForPrefixSearch() {
144 // SpecialWatchlist uses SpecialEditWatchlist::getMode, so new types should be added
145 // here and there - no 'edit' here, because that the default for this page
153 * Extract a list of titles from a blob of text, returning
154 * (prefixed) strings; unwatchable titles are ignored
156 * @param string $list
159 private function extractTitles( $list ) {
160 $list = explode( "\n", trim( $list ) );
161 if ( !is_array( $list ) ) {
167 foreach ( $list as $text ) {
168 $text = trim( $text );
169 if ( strlen( $text ) > 0 ) {
170 $title = Title
::newFromText( $text );
171 if ( $title instanceof Title
&& $title->isWatchable() ) {
177 GenderCache
::singleton()->doTitlesArray( $titles );
180 /** @var Title $title */
181 foreach ( $titles as $title ) {
182 $list[] = $title->getPrefixedText();
185 return array_unique( $list );
188 public function submitRaw( $data ) {
189 $wanted = $this->extractTitles( $data['Titles'] );
190 $current = $this->getWatchlist();
192 if ( count( $wanted ) > 0 ) {
193 $toWatch = array_diff( $wanted, $current );
194 $toUnwatch = array_diff( $current, $wanted );
195 $this->watchTitles( $toWatch );
196 $this->unwatchTitles( $toUnwatch );
197 $this->getUser()->invalidateCache();
199 if ( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ) {
200 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
205 if ( count( $toWatch ) > 0 ) {
206 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-added' )
207 ->numParams( count( $toWatch ) )->parse();
208 $this->showTitles( $toWatch, $this->successMessage
);
211 if ( count( $toUnwatch ) > 0 ) {
212 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed' )
213 ->numParams( count( $toUnwatch ) )->parse();
214 $this->showTitles( $toUnwatch, $this->successMessage
);
217 $this->clearWatchlist();
218 $this->getUser()->invalidateCache();
220 if ( count( $current ) > 0 ) {
221 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
226 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed' )
227 ->numParams( count( $current ) )->parse();
228 $this->showTitles( $current, $this->successMessage
);
234 public function submitClear( $data ) {
235 $current = $this->getWatchlist();
236 $this->clearWatchlist();
237 $this->getUser()->invalidateCache();
238 $this->successMessage
= $this->msg( 'watchlistedit-clear-done' )->parse();
239 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-clear-removed' )
240 ->numParams( count( $current ) )->parse();
241 $this->showTitles( $current, $this->successMessage
);
247 * Print out a list of linked titles
249 * $titles can be an array of strings or Title objects; the former
250 * is preferred, since Titles are very memory-heavy
252 * @param array $titles Array of strings, or Title objects
253 * @param string $output
255 private function showTitles( $titles, &$output ) {
256 $talk = $this->msg( 'talkpagelinktext' )->escaped();
257 // Do a batch existence check
258 $batch = new LinkBatch();
259 if ( count( $titles ) >= 100 ) {
260 $output = $this->msg( 'watchlistedit-too-many' )->parse();
263 foreach ( $titles as $title ) {
264 if ( !$title instanceof Title
) {
265 $title = Title
::newFromText( $title );
268 if ( $title instanceof Title
) {
269 $batch->addObj( $title );
270 $batch->addObj( $title->getTalkPage() );
276 // Print out the list
279 foreach ( $titles as $title ) {
280 if ( !$title instanceof Title
) {
281 $title = Title
::newFromText( $title );
284 if ( $title instanceof Title
) {
286 . Linker
::link( $title )
287 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
292 $output .= "</ul>\n";
296 * Prepare a list of titles on a user's watchlist (excluding talk pages)
297 * and return an array of (prefixed) strings
301 private function getWatchlist() {
304 $index = $this->getRequest()->wasPosted() ? DB_MASTER
: DB_SLAVE
;
305 $dbr = wfGetDB( $index );
310 'wl_namespace', 'wl_title'
312 'wl_user' => $this->getUser()->getId(),
317 if ( $res->numRows() > 0 ) {
318 /** @var Title[] $titles */
320 foreach ( $res as $row ) {
321 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
323 if ( $this->checkTitle( $title, $row->wl_namespace
, $row->wl_title
)
324 && !$title->isTalkPage()
331 GenderCache
::singleton()->doTitlesArray( $titles );
333 foreach ( $titles as $title ) {
334 $list[] = $title->getPrefixedText();
338 $this->cleanupWatchlist();
344 * Get a list of titles on a user's watchlist, excluding talk pages,
345 * and return as a two-dimensional array with namespace and title.
349 protected function getWatchlistInfo() {
351 $dbr = wfGetDB( DB_SLAVE
);
354 array( 'watchlist' ),
355 array( 'wl_namespace', 'wl_title' ),
356 array( 'wl_user' => $this->getUser()->getId() ),
358 array( 'ORDER BY' => array( 'wl_namespace', 'wl_title' ) )
361 $lb = new LinkBatch();
363 foreach ( $res as $row ) {
364 $lb->add( $row->wl_namespace
, $row->wl_title
);
365 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
366 $titles[$row->wl_namespace
][$row->wl_title
] = 1;
376 * Validates watchlist entry
378 * @param Title $title
379 * @param int $namespace
380 * @param string $dbKey
381 * @return bool Whether this item is valid
383 private function checkTitle( $title, $namespace, $dbKey ) {
385 && ( $title->isExternal()
386 ||
$title->getNamespace() < 0
389 $title = false; // unrecoverable
393 ||
$title->getNamespace() != $namespace
394 ||
$title->getDBkey() != $dbKey
396 $this->badItems
[] = array( $title, $namespace, $dbKey );
403 * Attempts to clean up broken items
405 private function cleanupWatchlist() {
406 if ( !count( $this->badItems
) ) {
407 return; // nothing to do
410 $dbw = wfGetDB( DB_MASTER
);
411 $user = $this->getUser();
413 foreach ( $this->badItems
as $row ) {
414 list( $title, $namespace, $dbKey ) = $row;
415 $action = $title ?
'cleaning up' : 'deleting';
416 wfDebug( "User {$user->getName()} has broken watchlist item ns($namespace):$dbKey, $action.\n" );
418 $dbw->delete( 'watchlist',
420 'wl_user' => $user->getId(),
421 'wl_namespace' => $namespace,
422 'wl_title' => $dbKey,
427 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
429 $user->addWatch( $title );
435 * Remove all titles from a user's watchlist
437 private function clearWatchlist() {
438 $dbw = wfGetDB( DB_MASTER
);
441 array( 'wl_user' => $this->getUser()->getId() ),
447 * Add a list of titles to a user's watchlist
449 * $titles can be an array of strings or Title objects; the former
450 * is preferred, since Titles are very memory-heavy
452 * @param array $titles Array of strings, or Title objects
454 private function watchTitles( $titles ) {
455 $dbw = wfGetDB( DB_MASTER
);
458 foreach ( $titles as $title ) {
459 if ( !$title instanceof Title
) {
460 $title = Title
::newFromText( $title );
463 if ( $title instanceof Title
) {
465 'wl_user' => $this->getUser()->getId(),
466 'wl_namespace' => MWNamespace
::getSubject( $title->getNamespace() ),
467 'wl_title' => $title->getDBkey(),
468 'wl_notificationtimestamp' => null,
471 'wl_user' => $this->getUser()->getId(),
472 'wl_namespace' => MWNamespace
::getTalk( $title->getNamespace() ),
473 'wl_title' => $title->getDBkey(),
474 'wl_notificationtimestamp' => null,
479 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
483 * Remove a list of titles from a user's watchlist
485 * $titles can be an array of strings or Title objects; the former
486 * is preferred, since Titles are very memory-heavy
488 * @param array $titles Array of strings, or Title objects
490 private function unwatchTitles( $titles ) {
491 $dbw = wfGetDB( DB_MASTER
);
493 foreach ( $titles as $title ) {
494 if ( !$title instanceof Title
) {
495 $title = Title
::newFromText( $title );
498 if ( $title instanceof Title
) {
502 'wl_user' => $this->getUser()->getId(),
503 'wl_namespace' => MWNamespace
::getSubject( $title->getNamespace() ),
504 'wl_title' => $title->getDBkey(),
512 'wl_user' => $this->getUser()->getId(),
513 'wl_namespace' => MWNamespace
::getTalk( $title->getNamespace() ),
514 'wl_title' => $title->getDBkey(),
519 $page = WikiPage
::factory( $title );
520 Hooks
::run( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
525 public function submitNormal( $data ) {
528 foreach ( $data as $titles ) {
529 $this->unwatchTitles( $titles );
530 $removed = array_merge( $removed, $titles );
533 if ( count( $removed ) > 0 ) {
534 $this->successMessage
= $this->msg( 'watchlistedit-normal-done'
535 )->numParams( count( $removed ) )->parse();
536 $this->showTitles( $removed, $this->successMessage
);
545 * Get the standard watchlist editing form
549 protected function getNormalForm() {
555 // Allow subscribers to manipulate the list of watched pages (or use it
556 // to preload lots of details at once)
557 $watchlistInfo = $this->getWatchlistInfo();
559 'WatchlistEditorBeforeFormRender',
560 array( &$watchlistInfo )
563 foreach ( $watchlistInfo as $namespace => $pages ) {
566 foreach ( array_keys( $pages ) as $dbkey ) {
567 $title = Title
::makeTitleSafe( $namespace, $dbkey );
569 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
570 $text = $this->buildRemoveLine( $title );
571 $options[$text] = $title->getPrefixedText();
576 // checkTitle can filter some options out, avoid empty sections
577 if ( count( $options ) > 0 ) {
578 $fields['TitlesNs' . $namespace] = array(
579 'class' => 'EditWatchlistCheckboxSeriesField',
580 'options' => $options,
581 'section' => "ns$namespace",
585 $this->cleanupWatchlist();
587 if ( count( $fields ) > 1 && $count > 30 ) {
588 $this->toc
= Linker
::tocIndent();
591 foreach ( $fields as $data ) {
592 # strip out the 'ns' prefix from the section name:
593 $ns = substr( $data['section'], 2 );
595 $nsText = ( $ns == NS_MAIN
)
596 ?
$this->msg( 'blanknamespace' )->escaped()
597 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
598 $this->toc
.= Linker
::tocLine( "editwatchlist-{$data['section']}", $nsText,
599 $this->getLanguage()->formatNum( ++
$tocLength ), 1 ) . Linker
::tocLineEnd();
602 $this->toc
= Linker
::tocList( $this->toc
);
607 $context = new DerivativeContext( $this->getContext() );
608 $context->setTitle( $this->getPageTitle() ); // Remove subpage
609 $form = new EditWatchlistNormalHTMLForm( $fields, $context );
610 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
611 $form->setSubmitDestructive();
613 # 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
614 $form->setSubmitTooltip( 'watchlistedit-normal-submit' );
615 $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
616 $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() );
617 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
623 * Build the label for a checkbox, with a link to the title, and various additional bits
625 * @param Title $title
628 private function buildRemoveLine( $title ) {
629 $link = Linker
::link( $title );
631 $tools['talk'] = Linker
::link(
632 $title->getTalkPage(),
633 $this->msg( 'talkpagelinktext' )->escaped()
636 if ( $title->exists() ) {
637 $tools['history'] = Linker
::linkKnown(
639 $this->msg( 'history_short' )->escaped(),
641 array( 'action' => 'history' )
645 if ( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
646 $tools['contributions'] = Linker
::linkKnown(
647 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
648 $this->msg( 'contributions' )->escaped()
653 'WatchlistEditorBuildRemoveLine',
654 array( &$tools, $title, $title->isRedirect(), $this->getSkin(), &$link )
657 if ( $title->isRedirect() ) {
658 // Linker already makes class mw-redirect, so this is redundant
659 $link = '<span class="watchlistredir">' . $link . '</span>';
662 return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
666 * Get a form for editing the watchlist in "raw" mode
670 protected function getRawForm() {
671 $titles = implode( $this->getWatchlist(), "\n" );
674 'type' => 'textarea',
675 'label-message' => 'watchlistedit-raw-titles',
676 'default' => $titles,
679 $context = new DerivativeContext( $this->getContext() );
680 $context->setTitle( $this->getPageTitle( 'raw' ) ); // Reset subpage
681 $form = new HTMLForm( $fields, $context );
682 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
683 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
684 $form->setSubmitTooltip( 'watchlistedit-raw-submit' );
685 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
686 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
687 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
693 * Get a form for clearing the watchlist
697 protected function getClearForm() {
698 $context = new DerivativeContext( $this->getContext() );
699 $context->setTitle( $this->getPageTitle( 'clear' ) ); // Reset subpage
700 $form = new HTMLForm( array(), $context );
701 $form->setSubmitTextMsg( 'watchlistedit-clear-submit' );
702 # Used message keys: 'accesskey-watchlistedit-clear-submit', 'tooltip-watchlistedit-clear-submit'
703 $form->setSubmitTooltip( 'watchlistedit-clear-submit' );
704 $form->setWrapperLegendMsg( 'watchlistedit-clear-legend' );
705 $form->addHeaderText( $this->msg( 'watchlistedit-clear-explain' )->parse() );
706 $form->setSubmitCallback( array( $this, 'submitClear' ) );
707 $form->setSubmitDestructive();
713 * Determine whether we are editing the watchlist, and if so, what
714 * kind of editing operation
716 * @param WebRequest $request
720 public static function getMode( $request, $par ) {
721 $mode = strtolower( $request->getVal( 'action', $par ) );
725 case self
::EDIT_CLEAR
:
726 return self
::EDIT_CLEAR
;
729 return self
::EDIT_RAW
;
731 case self
::EDIT_NORMAL
:
732 return self
::EDIT_NORMAL
;
739 * Build a set of links for convenient navigation
740 * between watchlist viewing and editing modes
742 * @param null $unused
745 public static function buildTools( $unused ) {
750 'view' => array( 'Watchlist', false ),
751 'edit' => array( 'EditWatchlist', false ),
752 'raw' => array( 'EditWatchlist', 'raw' ),
753 'clear' => array( 'EditWatchlist', 'clear' ),
756 foreach ( $modes as $mode => $arr ) {
757 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
758 $tools[] = Linker
::linkKnown(
759 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
760 wfMessage( "watchlisttools-{$mode}" )->escaped()
764 return Html
::rawElement(
766 array( 'class' => 'mw-watchlist-toollinks' ),
767 wfMessage( 'parentheses' )->rawParams( $wgLang->pipeList( $tools ) )->escaped()
773 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
775 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
776 public function getLegend( $namespace ) {
777 $namespace = substr( $namespace, 2 );
779 return $namespace == NS_MAIN
780 ?
$this->msg( 'blanknamespace' )->escaped()
781 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
784 public function getBody() {
785 return $this->displaySection( $this->mFieldTree
, '', 'editwatchlist-' );
789 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
791 * HTMLMultiSelectField throws validation errors if we get input data
792 * that doesn't match the data set in the form setup. This causes
793 * problems if something gets removed from the watchlist while the
794 * form is open (bug 32126), but we know that invalid items will
795 * be harmless so we can override it here.
797 * @param string $value The value the field was submitted with
798 * @param array $alldata The data collected from the form
799 * @return bool|string Bool true on success, or String error to display.
801 function validate( $value, $alldata ) {
802 // Need to call into grandparent to be a good citizen. :)
803 return HTMLFormField
::validate( $value, $alldata );