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();
75 # B/C: $mode used to be waaay down the parameter list, and the first parameter
77 if ( $mode instanceof User
) {
78 $args = func_get_args();
79 if ( count( $args ) >= 4 ) {
83 $mode = self
::getMode( $this->getRequest(), $mode );
87 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
88 $form = $this->getRawForm();
89 if ( $form->show() ) {
90 $out->addHTML( $this->successMessage
);
91 $out->addReturnTo( SpecialPage
::getTitleFor( 'Watchlist' ) );
94 case self
::EDIT_CLEAR
:
95 $out->setPageTitle( $this->msg( 'watchlistedit-clear-title' ) );
96 $form = $this->getClearForm();
97 if ( $form->show() ) {
98 $out->addHTML( $this->successMessage
);
99 $out->addReturnTo( SpecialPage
::getTitleFor( 'Watchlist' ) );
103 case self
::EDIT_NORMAL
:
105 $this->executeViewEditWatchlist();
111 * Renders a subheader on the watchlist page.
113 protected function outputSubtitle() {
114 $out = $this->getOutput();
115 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName() )
116 ->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
120 * Executes an edit mode for the watchlist view, from which you can manage your watchlist
123 protected function executeViewEditWatchlist() {
124 $out = $this->getOutput();
125 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
126 $form = $this->getNormalForm();
127 if ( $form->show() ) {
128 $out->addHTML( $this->successMessage
);
129 $out->addReturnTo( SpecialPage
::getTitleFor( 'Watchlist' ) );
130 } elseif ( $this->toc
!== false ) {
131 $out->prependHTML( $this->toc
);
132 $out->addModules( 'mediawiki.toc' );
137 * Return an array of subpages beginning with $search that this special page will accept.
139 * @param string $search Prefix to search for
140 * @param int $limit Maximum number of results to return
141 * @return string[] Matching subpages
143 public function prefixSearchSubpages( $search, $limit = 10 ) {
144 return self
::prefixSearchArray(
147 // SpecialWatchlist uses SpecialEditWatchlist::getMode, so new types should be added
148 // here and there - no 'edit' here, because that the default for this page
157 * Extract a list of titles from a blob of text, returning
158 * (prefixed) strings; unwatchable titles are ignored
160 * @param string $list
163 private function extractTitles( $list ) {
164 $list = explode( "\n", trim( $list ) );
165 if ( !is_array( $list ) ) {
171 foreach ( $list as $text ) {
172 $text = trim( $text );
173 if ( strlen( $text ) > 0 ) {
174 $title = Title
::newFromText( $text );
175 if ( $title instanceof Title
&& $title->isWatchable() ) {
181 GenderCache
::singleton()->doTitlesArray( $titles );
184 /** @var Title $title */
185 foreach ( $titles as $title ) {
186 $list[] = $title->getPrefixedText();
189 return array_unique( $list );
192 public function submitRaw( $data ) {
193 $wanted = $this->extractTitles( $data['Titles'] );
194 $current = $this->getWatchlist();
196 if ( count( $wanted ) > 0 ) {
197 $toWatch = array_diff( $wanted, $current );
198 $toUnwatch = array_diff( $current, $wanted );
199 $this->watchTitles( $toWatch );
200 $this->unwatchTitles( $toUnwatch );
201 $this->getUser()->invalidateCache();
203 if ( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ) {
204 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
209 if ( count( $toWatch ) > 0 ) {
210 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-added' )
211 ->numParams( count( $toWatch ) )->parse();
212 $this->showTitles( $toWatch, $this->successMessage
);
215 if ( count( $toUnwatch ) > 0 ) {
216 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed' )
217 ->numParams( count( $toUnwatch ) )->parse();
218 $this->showTitles( $toUnwatch, $this->successMessage
);
221 $this->clearWatchlist();
222 $this->getUser()->invalidateCache();
224 if ( count( $current ) > 0 ) {
225 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
230 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed' )
231 ->numParams( count( $current ) )->parse();
232 $this->showTitles( $current, $this->successMessage
);
238 public function submitClear( $data ) {
239 $current = $this->getWatchlist();
240 $this->clearWatchlist();
241 $this->getUser()->invalidateCache();
242 $this->successMessage
= $this->msg( 'watchlistedit-clear-done' )->parse();
243 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-clear-removed' )
244 ->numParams( count( $current ) )->parse();
245 $this->showTitles( $current, $this->successMessage
);
251 * Print out a list of linked titles
253 * $titles can be an array of strings or Title objects; the former
254 * is preferred, since Titles are very memory-heavy
256 * @param array $titles Array of strings, or Title objects
257 * @param string $output
259 private function showTitles( $titles, &$output ) {
260 $talk = $this->msg( 'talkpagelinktext' )->escaped();
261 // Do a batch existence check
262 $batch = new LinkBatch();
263 if ( count( $titles ) >= 100 ) {
264 $output = wfMessage( 'watchlistedit-too-many' )->parse();
267 foreach ( $titles as $title ) {
268 if ( !$title instanceof Title
) {
269 $title = Title
::newFromText( $title );
272 if ( $title instanceof Title
) {
273 $batch->addObj( $title );
274 $batch->addObj( $title->getTalkPage() );
280 // Print out the list
283 foreach ( $titles as $title ) {
284 if ( !$title instanceof Title
) {
285 $title = Title
::newFromText( $title );
288 if ( $title instanceof Title
) {
290 . Linker
::link( $title )
291 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
296 $output .= "</ul>\n";
300 * Prepare a list of titles on a user's watchlist (excluding talk pages)
301 * and return an array of (prefixed) strings
305 private function getWatchlist() {
307 $dbr = wfGetDB( DB_MASTER
);
312 'wl_namespace', 'wl_title'
314 'wl_user' => $this->getUser()->getId(),
319 if ( $res->numRows() > 0 ) {
321 foreach ( $res as $row ) {
322 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
324 if ( $this->checkTitle( $title, $row->wl_namespace
, $row->wl_title
)
325 && !$title->isTalkPage()
332 GenderCache
::singleton()->doTitlesArray( $titles );
334 foreach ( $titles as $title ) {
335 $list[] = $title->getPrefixedText();
339 $this->cleanupWatchlist();
345 * Get a list of titles on a user's watchlist, excluding talk pages,
346 * and return as a two-dimensional array with namespace and title.
350 protected function getWatchlistInfo() {
352 $dbr = wfGetDB( DB_MASTER
);
355 array( 'watchlist' ),
356 array( 'wl_namespace', 'wl_title' ),
357 array( 'wl_user' => $this->getUser()->getId() ),
359 array( 'ORDER BY' => array( 'wl_namespace', 'wl_title' ) )
362 $lb = new LinkBatch();
364 foreach ( $res as $row ) {
365 $lb->add( $row->wl_namespace
, $row->wl_title
);
366 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
367 $titles[$row->wl_namespace
][$row->wl_title
] = 1;
377 * Validates watchlist entry
379 * @param Title $title
380 * @param int $namespace
381 * @param string $dbKey
382 * @return bool Whether this item is valid
384 private function checkTitle( $title, $namespace, $dbKey ) {
386 && ( $title->isExternal()
387 ||
$title->getNamespace() < 0
390 $title = false; // unrecoverable
394 ||
$title->getNamespace() != $namespace
395 ||
$title->getDBkey() != $dbKey
397 $this->badItems
[] = array( $title, $namespace, $dbKey );
404 * Attempts to clean up broken items
406 private function cleanupWatchlist() {
407 if ( !count( $this->badItems
) ) {
408 return; //nothing to do
411 $dbw = wfGetDB( DB_MASTER
);
412 $user = $this->getUser();
414 foreach ( $this->badItems
as $row ) {
415 list( $title, $namespace, $dbKey ) = $row;
416 $action = $title ?
'cleaning up' : 'deleting';
417 wfDebug( "User {$user->getName()} has broken watchlist item ns($namespace):$dbKey, $action.\n" );
419 $dbw->delete( 'watchlist',
421 'wl_user' => $user->getId(),
422 'wl_namespace' => $namespace,
423 'wl_title' => $dbKey,
428 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
430 $user->addWatch( $title );
436 * Remove all titles from a user's watchlist
438 private function clearWatchlist() {
439 $dbw = wfGetDB( DB_MASTER
);
442 array( 'wl_user' => $this->getUser()->getId() ),
448 * Add a list of titles to a user's watchlist
450 * $titles can be an array of strings or Title objects; the former
451 * is preferred, since Titles are very memory-heavy
453 * @param array $titles Array of strings, or Title objects
455 private function watchTitles( $titles ) {
456 $dbw = wfGetDB( DB_MASTER
);
459 foreach ( $titles as $title ) {
460 if ( !$title instanceof Title
) {
461 $title = Title
::newFromText( $title );
464 if ( $title instanceof Title
) {
466 'wl_user' => $this->getUser()->getId(),
467 'wl_namespace' => MWNamespace
::getSubject( $title->getNamespace() ),
468 'wl_title' => $title->getDBkey(),
469 'wl_notificationtimestamp' => null,
472 'wl_user' => $this->getUser()->getId(),
473 'wl_namespace' => MWNamespace
::getTalk( $title->getNamespace() ),
474 'wl_title' => $title->getDBkey(),
475 'wl_notificationtimestamp' => null,
480 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
484 * Remove a list of titles from a user's watchlist
486 * $titles can be an array of strings or Title objects; the former
487 * is preferred, since Titles are very memory-heavy
489 * @param array $titles Array of strings, or Title objects
491 private function unwatchTitles( $titles ) {
492 $dbw = wfGetDB( DB_MASTER
);
494 foreach ( $titles as $title ) {
495 if ( !$title instanceof Title
) {
496 $title = Title
::newFromText( $title );
499 if ( $title instanceof Title
) {
503 'wl_user' => $this->getUser()->getId(),
504 'wl_namespace' => MWNamespace
::getSubject( $title->getNamespace() ),
505 'wl_title' => $title->getDBkey(),
513 'wl_user' => $this->getUser()->getId(),
514 'wl_namespace' => MWNamespace
::getTalk( $title->getNamespace() ),
515 'wl_title' => $title->getDBkey(),
520 $page = WikiPage
::factory( $title );
521 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
526 public function submitNormal( $data ) {
529 foreach ( $data as $titles ) {
530 $this->unwatchTitles( $titles );
531 $removed = array_merge( $removed, $titles );
534 if ( count( $removed ) > 0 ) {
535 $this->successMessage
= $this->msg( 'watchlistedit-normal-done'
536 )->numParams( count( $removed ) )->parse();
537 $this->showTitles( $removed, $this->successMessage
);
546 * Get the standard watchlist editing form
550 protected function getNormalForm() {
556 // Allow subscribers to manipulate the list of watched pages (or use it
557 // to preload lots of details at once)
558 $watchlistInfo = $this->getWatchlistInfo();
560 'WatchlistEditorBeforeFormRender',
561 array( &$watchlistInfo )
564 foreach ( $watchlistInfo as $namespace => $pages ) {
567 foreach ( array_keys( $pages ) as $dbkey ) {
568 $title = Title
::makeTitleSafe( $namespace, $dbkey );
570 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
571 $text = $this->buildRemoveLine( $title );
572 $options[$text] = $title->getPrefixedText();
577 // checkTitle can filter some options out, avoid empty sections
578 if ( count( $options ) > 0 ) {
579 $fields['TitlesNs' . $namespace] = array(
580 'class' => 'EditWatchlistCheckboxSeriesField',
581 'options' => $options,
582 'section' => "ns$namespace",
586 $this->cleanupWatchlist();
588 if ( count( $fields ) > 1 && $count > 30 ) {
589 $this->toc
= Linker
::tocIndent();
592 foreach ( $fields as $data ) {
593 # strip out the 'ns' prefix from the section name:
594 $ns = substr( $data['section'], 2 );
596 $nsText = ( $ns == NS_MAIN
)
597 ?
$this->msg( 'blanknamespace' )->escaped()
598 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
599 $this->toc
.= Linker
::tocLine( "editwatchlist-{$data['section']}", $nsText,
600 $this->getLanguage()->formatNum( ++
$tocLength ), 1 ) . Linker
::tocLineEnd();
603 $this->toc
= Linker
::tocList( $this->toc
);
608 $context = new DerivativeContext( $this->getContext() );
609 $context->setTitle( $this->getPageTitle() ); // Remove subpage
610 $form = new EditWatchlistNormalHTMLForm( $fields, $context );
611 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
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( $title->getTalkPage(), $this->msg( 'talkpagelinktext' )->escaped() );
633 if ( $title->exists() ) {
634 $tools['history'] = Linker
::linkKnown(
636 $this->msg( 'history_short' )->escaped(),
638 array( 'action' => 'history' )
642 if ( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
643 $tools['contributions'] = Linker
::linkKnown(
644 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
645 $this->msg( 'contributions' )->escaped()
650 'WatchlistEditorBuildRemoveLine',
651 array( &$tools, $title, $title->isRedirect(), $this->getSkin(), &$link )
654 if ( $title->isRedirect() ) {
655 // Linker already makes class mw-redirect, so this is redundant
656 $link = '<span class="watchlistredir">' . $link . '</span>';
659 return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
663 * Get a form for editing the watchlist in "raw" mode
667 protected function getRawForm() {
668 $titles = implode( $this->getWatchlist(), "\n" );
671 'type' => 'textarea',
672 'label-message' => 'watchlistedit-raw-titles',
673 'default' => $titles,
676 $context = new DerivativeContext( $this->getContext() );
677 $context->setTitle( $this->getPageTitle( 'raw' ) ); // Reset subpage
678 $form = new HTMLForm( $fields, $context );
679 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
680 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
681 $form->setSubmitTooltip( 'watchlistedit-raw-submit' );
682 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
683 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
684 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
690 * Get a form for clearing the watchlist
694 protected function getClearForm() {
695 $context = new DerivativeContext( $this->getContext() );
696 $context->setTitle( $this->getPageTitle( 'clear' ) ); // Reset subpage
697 $form = new HTMLForm( array(), $context );
698 $form->setSubmitTextMsg( 'watchlistedit-clear-submit' );
699 # Used message keys: 'accesskey-watchlistedit-clear-submit', 'tooltip-watchlistedit-clear-submit'
700 $form->setSubmitTooltip( 'watchlistedit-clear-submit' );
701 $form->setWrapperLegendMsg( 'watchlistedit-clear-legend' );
702 $form->addHeaderText( $this->msg( 'watchlistedit-clear-explain' )->parse() );
703 $form->setSubmitCallback( array( $this, 'submitClear' ) );
709 * Determine whether we are editing the watchlist, and if so, what
710 * kind of editing operation
712 * @param WebRequest $request
716 public static function getMode( $request, $par ) {
717 $mode = strtolower( $request->getVal( 'action', $par ) );
721 case self
::EDIT_CLEAR
:
722 return self
::EDIT_CLEAR
;
725 return self
::EDIT_RAW
;
727 case self
::EDIT_NORMAL
:
728 return self
::EDIT_NORMAL
;
735 * Build a set of links for convenient navigation
736 * between watchlist viewing and editing modes
738 * @param null $unused
741 public static function buildTools( $unused ) {
746 'view' => array( 'Watchlist', false ),
747 'edit' => array( 'EditWatchlist', false ),
748 'raw' => array( 'EditWatchlist', 'raw' ),
749 'clear' => array( 'EditWatchlist', 'clear' ),
752 foreach ( $modes as $mode => $arr ) {
753 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
754 $tools[] = Linker
::linkKnown(
755 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
756 wfMessage( "watchlisttools-{$mode}" )->escaped()
760 return Html
::rawElement(
762 array( 'class' => 'mw-watchlist-toollinks' ),
763 wfMessage( 'parentheses', $wgLang->pipeList( $tools ) )->text()
769 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
771 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
772 public function getLegend( $namespace ) {
773 $namespace = substr( $namespace, 2 );
775 return $namespace == NS_MAIN
776 ?
$this->msg( 'blanknamespace' )->escaped()
777 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
780 public function getBody() {
781 return $this->displaySection( $this->mFieldTree
, '', 'editwatchlist-' );
785 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
787 * HTMLMultiSelectField throws validation errors if we get input data
788 * that doesn't match the data set in the form setup. This causes
789 * problems if something gets removed from the watchlist while the
790 * form is open (bug 32126), but we know that invalid items will
791 * be harmless so we can override it here.
793 * @param string $value The value the field was submitted with
794 * @param array $alldata The data collected from the form
795 * @return bool|string Bool true on success, or String error to display.
797 function validate( $value, $alldata ) {
798 // Need to call into grandparent to be a good citizen. :)
799 return HTMLFormField
::validate( $value, $alldata );