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 integer $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 ) {
541 if ( $namespace >= 0 ) {
542 $fields['TitlesNs' . $namespace] = array(
543 'class' => 'EditWatchlistCheckboxSeriesField',
544 'options' => array(),
545 'section' => "ns$namespace",
549 foreach ( array_keys( $pages ) as $dbkey ) {
550 $title = Title
::makeTitleSafe( $namespace, $dbkey );
552 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
553 $text = $this->buildRemoveLine( $title );
554 $fields['TitlesNs' . $namespace]['options'][$text] = $title->getPrefixedText();
559 $this->cleanupWatchlist();
561 if ( count( $fields ) > 1 && $count > 30 ) {
562 $this->toc
= Linker
::tocIndent();
565 foreach ( $fields as $data ) {
566 # strip out the 'ns' prefix from the section name:
567 $ns = substr( $data['section'], 2 );
569 $nsText = ( $ns == NS_MAIN
)
570 ?
$this->msg( 'blanknamespace' )->escaped()
571 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
572 $this->toc
.= Linker
::tocLine( "editwatchlist-{$data['section']}", $nsText,
573 $this->getLanguage()->formatNum( ++
$tocLength ), 1 ) . Linker
::tocLineEnd();
576 $this->toc
= Linker
::tocList( $this->toc
);
581 $context = new DerivativeContext( $this->getContext() );
582 $context->setTitle( $this->getPageTitle() ); // Remove subpage
583 $form = new EditWatchlistNormalHTMLForm( $fields, $context );
584 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
586 # 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
587 $form->setSubmitTooltip( 'watchlistedit-normal-submit' );
588 $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
589 $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() );
590 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
596 * Build the label for a checkbox, with a link to the title, and various additional bits
598 * @param Title $title
601 private function buildRemoveLine( $title ) {
602 $link = Linker
::link( $title );
604 if ( $title->isRedirect() ) {
605 // Linker already makes class mw-redirect, so this is redundant
606 $link = '<span class="watchlistredir">' . $link . '</span>';
609 $tools[] = Linker
::link( $title->getTalkPage(), $this->msg( 'talkpagelinktext' )->escaped() );
611 if ( $title->exists() ) {
612 $tools[] = Linker
::linkKnown(
614 $this->msg( 'history_short' )->escaped(),
616 array( 'action' => 'history' )
620 if ( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
621 $tools[] = Linker
::linkKnown(
622 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
623 $this->msg( 'contributions' )->escaped()
628 'WatchlistEditorBuildRemoveLine',
629 array( &$tools, $title, $title->isRedirect(), $this->getSkin() )
632 return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
636 * Get a form for editing the watchlist in "raw" mode
640 protected function getRawForm() {
641 $titles = implode( $this->getWatchlist(), "\n" );
644 'type' => 'textarea',
645 'label-message' => 'watchlistedit-raw-titles',
646 'default' => $titles,
649 $context = new DerivativeContext( $this->getContext() );
650 $context->setTitle( $this->getPageTitle( 'raw' ) ); // Reset subpage
651 $form = new HTMLForm( $fields, $context );
652 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
653 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
654 $form->setSubmitTooltip( 'watchlistedit-raw-submit' );
655 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
656 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
657 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
663 * Get a form for clearing the watchlist
667 protected function getClearForm() {
668 $context = new DerivativeContext( $this->getContext() );
669 $context->setTitle( $this->getPageTitle( 'clear' ) ); // Reset subpage
670 $form = new HTMLForm( array(), $context );
671 $form->setSubmitTextMsg( 'watchlistedit-clear-submit' );
672 # Used message keys: 'accesskey-watchlistedit-clear-submit', 'tooltip-watchlistedit-clear-submit'
673 $form->setSubmitTooltip( 'watchlistedit-clear-submit' );
674 $form->setWrapperLegendMsg( 'watchlistedit-clear-legend' );
675 $form->addHeaderText( $this->msg( 'watchlistedit-clear-explain' )->parse() );
676 $form->setSubmitCallback( array( $this, 'submitClear' ) );
682 * Determine whether we are editing the watchlist, and if so, what
683 * kind of editing operation
685 * @param WebRequest $request
689 public static function getMode( $request, $par ) {
690 $mode = strtolower( $request->getVal( 'action', $par ) );
694 case self
::EDIT_CLEAR
:
695 return self
::EDIT_CLEAR
;
698 return self
::EDIT_RAW
;
700 case self
::EDIT_NORMAL
:
701 return self
::EDIT_NORMAL
;
708 * Build a set of links for convenient navigation
709 * between watchlist viewing and editing modes
711 * @param null $unused
714 public static function buildTools( $unused ) {
719 'view' => array( 'Watchlist', false ),
720 'edit' => array( 'EditWatchlist', false ),
721 'raw' => array( 'EditWatchlist', 'raw' ),
722 'clear' => array( 'EditWatchlist', 'clear' ),
725 foreach ( $modes as $mode => $arr ) {
726 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
727 $tools[] = Linker
::linkKnown(
728 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
729 wfMessage( "watchlisttools-{$mode}" )->escaped()
733 return Html
::rawElement(
735 array( 'class' => 'mw-watchlist-toollinks' ),
736 wfMessage( 'parentheses', $wgLang->pipeList( $tools ) )->text()
742 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
744 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
745 public function getLegend( $namespace ) {
746 $namespace = substr( $namespace, 2 );
748 return $namespace == NS_MAIN
749 ?
$this->msg( 'blanknamespace' )->escaped()
750 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
753 public function getBody() {
754 return $this->displaySection( $this->mFieldTree
, '', 'editwatchlist-' );
758 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
760 * HTMLMultiSelectField throws validation errors if we get input data
761 * that doesn't match the data set in the form setup. This causes
762 * problems if something gets removed from the watchlist while the
763 * form is open (bug 32126), but we know that invalid items will
764 * be harmless so we can override it here.
766 * @param string $value the value the field was submitted with
767 * @param array $alldata the data collected from the form
768 * @return bool|string Bool true on success, or String error to display.
770 function validate( $value, $alldata ) {
771 // Need to call into grandparent to be a good citizen. :)
772 return HTMLFormField
::validate( $value, $alldata );