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 // SpecialWatchlist uses SpecialEditWatchlist::getMode, so new types should be added
129 // here and there - no 'edit' here, because that the default for this page
130 $subpages = array( 'clear', 'raw' );
131 $escaped = preg_quote( $search );
132 return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit );
136 * Extract a list of titles from a blob of text, returning
137 * (prefixed) strings; unwatchable titles are ignored
139 * @param string $list
142 private function extractTitles( $list ) {
143 $list = explode( "\n", trim( $list ) );
144 if ( !is_array( $list ) ) {
150 foreach ( $list as $text ) {
151 $text = trim( $text );
152 if ( strlen( $text ) > 0 ) {
153 $title = Title
::newFromText( $text );
154 if ( $title instanceof Title
&& $title->isWatchable() ) {
160 GenderCache
::singleton()->doTitlesArray( $titles );
163 /** @var Title $title */
164 foreach ( $titles as $title ) {
165 $list[] = $title->getPrefixedText();
168 return array_unique( $list );
171 public function submitRaw( $data ) {
172 $wanted = $this->extractTitles( $data['Titles'] );
173 $current = $this->getWatchlist();
175 if ( count( $wanted ) > 0 ) {
176 $toWatch = array_diff( $wanted, $current );
177 $toUnwatch = array_diff( $current, $wanted );
178 $this->watchTitles( $toWatch );
179 $this->unwatchTitles( $toUnwatch );
180 $this->getUser()->invalidateCache();
182 if ( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ) {
183 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
188 if ( count( $toWatch ) > 0 ) {
189 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-added' )
190 ->numParams( count( $toWatch ) )->parse();
191 $this->showTitles( $toWatch, $this->successMessage
);
194 if ( count( $toUnwatch ) > 0 ) {
195 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed' )
196 ->numParams( count( $toUnwatch ) )->parse();
197 $this->showTitles( $toUnwatch, $this->successMessage
);
200 $this->clearWatchlist();
201 $this->getUser()->invalidateCache();
203 if ( count( $current ) > 0 ) {
204 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
209 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed' )
210 ->numParams( count( $current ) )->parse();
211 $this->showTitles( $current, $this->successMessage
);
217 public function submitClear( $data ) {
218 $current = $this->getWatchlist();
219 $this->clearWatchlist();
220 $this->getUser()->invalidateCache();
221 $this->successMessage
= $this->msg( 'watchlistedit-clear-done' )->parse();
222 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-clear-removed' )
223 ->numParams( count( $current ) )->parse();
224 $this->showTitles( $current, $this->successMessage
);
230 * Print out a list of linked titles
232 * $titles can be an array of strings or Title objects; the former
233 * is preferred, since Titles are very memory-heavy
235 * @param array $titles Array of strings, or Title objects
236 * @param string $output
238 private function showTitles( $titles, &$output ) {
239 $talk = $this->msg( 'talkpagelinktext' )->escaped();
240 // Do a batch existence check
241 $batch = new LinkBatch();
242 if (count($titles) >= 100) {
243 $output = wfMessage( 'watchlistedit-too-many' )->parse();
246 foreach ( $titles as $title ) {
247 if ( !$title instanceof Title
) {
248 $title = Title
::newFromText( $title );
251 if ( $title instanceof Title
) {
252 $batch->addObj( $title );
253 $batch->addObj( $title->getTalkPage() );
259 // Print out the list
262 foreach ( $titles as $title ) {
263 if ( !$title instanceof Title
) {
264 $title = Title
::newFromText( $title );
267 if ( $title instanceof Title
) {
269 . Linker
::link( $title )
270 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
275 $output .= "</ul>\n";
279 * Prepare a list of titles on a user's watchlist (excluding talk pages)
280 * and return an array of (prefixed) strings
284 private function getWatchlist() {
286 $dbr = wfGetDB( DB_MASTER
);
291 'wl_namespace', 'wl_title'
293 'wl_user' => $this->getUser()->getId(),
298 if ( $res->numRows() > 0 ) {
300 foreach ( $res as $row ) {
301 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
303 if ( $this->checkTitle( $title, $row->wl_namespace
, $row->wl_title
)
304 && !$title->isTalkPage()
311 GenderCache
::singleton()->doTitlesArray( $titles );
313 foreach ( $titles as $title ) {
314 $list[] = $title->getPrefixedText();
318 $this->cleanupWatchlist();
324 * Get a list of titles on a user's watchlist, excluding talk pages,
325 * and return as a two-dimensional array with namespace and title.
329 private function getWatchlistInfo() {
331 $dbr = wfGetDB( DB_MASTER
);
334 array( 'watchlist' ),
335 array( 'wl_namespace', 'wl_title' ),
336 array( 'wl_user' => $this->getUser()->getId() ),
338 array( 'ORDER BY' => array( 'wl_namespace', 'wl_title' ) )
341 $lb = new LinkBatch();
343 foreach ( $res as $row ) {
344 $lb->add( $row->wl_namespace
, $row->wl_title
);
345 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
346 $titles[$row->wl_namespace
][$row->wl_title
] = 1;
356 * Validates watchlist entry
358 * @param Title $title
359 * @param int $namespace
360 * @param string $dbKey
361 * @return bool Whether this item is valid
363 private function checkTitle( $title, $namespace, $dbKey ) {
365 && ( $title->isExternal()
366 ||
$title->getNamespace() < 0
369 $title = false; // unrecoverable
373 ||
$title->getNamespace() != $namespace
374 ||
$title->getDBkey() != $dbKey
376 $this->badItems
[] = array( $title, $namespace, $dbKey );
383 * Attempts to clean up broken items
385 private function cleanupWatchlist() {
386 if ( !count( $this->badItems
) ) {
387 return; //nothing to do
390 $dbw = wfGetDB( DB_MASTER
);
391 $user = $this->getUser();
393 foreach ( $this->badItems
as $row ) {
394 list( $title, $namespace, $dbKey ) = $row;
395 $action = $title ?
'cleaning up' : 'deleting';
396 wfDebug( "User {$user->getName()} has broken watchlist item ns($namespace):$dbKey, $action.\n" );
398 $dbw->delete( 'watchlist',
400 'wl_user' => $user->getId(),
401 'wl_namespace' => $namespace,
402 'wl_title' => $dbKey,
407 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
409 $user->addWatch( $title );
415 * Remove all titles from a user's watchlist
417 private function clearWatchlist() {
418 $dbw = wfGetDB( DB_MASTER
);
421 array( 'wl_user' => $this->getUser()->getId() ),
427 * Add a list of titles to a user's watchlist
429 * $titles can be an array of strings or Title objects; the former
430 * is preferred, since Titles are very memory-heavy
432 * @param array $titles Array of strings, or Title objects
434 private function watchTitles( $titles ) {
435 $dbw = wfGetDB( DB_MASTER
);
438 foreach ( $titles as $title ) {
439 if ( !$title instanceof Title
) {
440 $title = Title
::newFromText( $title );
443 if ( $title instanceof Title
) {
445 'wl_user' => $this->getUser()->getId(),
446 'wl_namespace' => MWNamespace
::getSubject( $title->getNamespace() ),
447 'wl_title' => $title->getDBkey(),
448 'wl_notificationtimestamp' => null,
451 'wl_user' => $this->getUser()->getId(),
452 'wl_namespace' => MWNamespace
::getTalk( $title->getNamespace() ),
453 'wl_title' => $title->getDBkey(),
454 'wl_notificationtimestamp' => null,
459 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
463 * Remove a list of titles from a user's watchlist
465 * $titles can be an array of strings or Title objects; the former
466 * is preferred, since Titles are very memory-heavy
468 * @param array $titles Array of strings, or Title objects
470 private function unwatchTitles( $titles ) {
471 $dbw = wfGetDB( DB_MASTER
);
473 foreach ( $titles as $title ) {
474 if ( !$title instanceof Title
) {
475 $title = Title
::newFromText( $title );
478 if ( $title instanceof Title
) {
482 'wl_user' => $this->getUser()->getId(),
483 'wl_namespace' => MWNamespace
::getSubject( $title->getNamespace() ),
484 'wl_title' => $title->getDBkey(),
492 'wl_user' => $this->getUser()->getId(),
493 'wl_namespace' => MWNamespace
::getTalk( $title->getNamespace() ),
494 'wl_title' => $title->getDBkey(),
499 $page = WikiPage
::factory( $title );
500 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
505 public function submitNormal( $data ) {
508 foreach ( $data as $titles ) {
509 $this->unwatchTitles( $titles );
510 $removed = array_merge( $removed, $titles );
513 if ( count( $removed ) > 0 ) {
514 $this->successMessage
= $this->msg( 'watchlistedit-normal-done'
515 )->numParams( count( $removed ) )->parse();
516 $this->showTitles( $removed, $this->successMessage
);
525 * Get the standard watchlist editing form
529 protected function getNormalForm() {
535 foreach ( $this->getWatchlistInfo() as $namespace => $pages ) {
536 if ( $namespace >= 0 ) {
537 $fields['TitlesNs' . $namespace] = array(
538 'class' => 'EditWatchlistCheckboxSeriesField',
539 'options' => array(),
540 'section' => "ns$namespace",
544 foreach ( array_keys( $pages ) as $dbkey ) {
545 $title = Title
::makeTitleSafe( $namespace, $dbkey );
547 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
548 $text = $this->buildRemoveLine( $title );
549 $fields['TitlesNs' . $namespace]['options'][$text] = $title->getPrefixedText();
554 $this->cleanupWatchlist();
556 if ( count( $fields ) > 1 && $count > 30 ) {
557 $this->toc
= Linker
::tocIndent();
560 foreach ( $fields as $data ) {
561 # strip out the 'ns' prefix from the section name:
562 $ns = substr( $data['section'], 2 );
564 $nsText = ( $ns == NS_MAIN
)
565 ?
$this->msg( 'blanknamespace' )->escaped()
566 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
567 $this->toc
.= Linker
::tocLine( "editwatchlist-{$data['section']}", $nsText,
568 $this->getLanguage()->formatNum( ++
$tocLength ), 1 ) . Linker
::tocLineEnd();
571 $this->toc
= Linker
::tocList( $this->toc
);
576 $context = new DerivativeContext( $this->getContext() );
577 $context->setTitle( $this->getPageTitle() ); // Remove subpage
578 $form = new EditWatchlistNormalHTMLForm( $fields, $context );
579 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
581 # 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
582 $form->setSubmitTooltip( 'watchlistedit-normal-submit' );
583 $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
584 $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() );
585 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
591 * Build the label for a checkbox, with a link to the title, and various additional bits
593 * @param Title $title
596 private function buildRemoveLine( $title ) {
597 $link = Linker
::link( $title );
599 if ( $title->isRedirect() ) {
600 // Linker already makes class mw-redirect, so this is redundant
601 $link = '<span class="watchlistredir">' . $link . '</span>';
604 $tools[] = Linker
::link( $title->getTalkPage(), $this->msg( 'talkpagelinktext' )->escaped() );
606 if ( $title->exists() ) {
607 $tools[] = Linker
::linkKnown(
609 $this->msg( 'history_short' )->escaped(),
611 array( 'action' => 'history' )
615 if ( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
616 $tools[] = Linker
::linkKnown(
617 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
618 $this->msg( 'contributions' )->escaped()
623 'WatchlistEditorBuildRemoveLine',
624 array( &$tools, $title, $title->isRedirect(), $this->getSkin() )
627 return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
631 * Get a form for editing the watchlist in "raw" mode
635 protected function getRawForm() {
636 $titles = implode( $this->getWatchlist(), "\n" );
639 'type' => 'textarea',
640 'label-message' => 'watchlistedit-raw-titles',
641 'default' => $titles,
644 $context = new DerivativeContext( $this->getContext() );
645 $context->setTitle( $this->getPageTitle( 'raw' ) ); // Reset subpage
646 $form = new HTMLForm( $fields, $context );
647 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
648 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
649 $form->setSubmitTooltip( 'watchlistedit-raw-submit' );
650 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
651 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
652 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
658 * Get a form for clearing the watchlist
662 protected function getClearForm() {
663 $context = new DerivativeContext( $this->getContext() );
664 $context->setTitle( $this->getPageTitle( 'clear' ) ); // Reset subpage
665 $form = new HTMLForm( array(), $context );
666 $form->setSubmitTextMsg( 'watchlistedit-clear-submit' );
667 # Used message keys: 'accesskey-watchlistedit-clear-submit', 'tooltip-watchlistedit-clear-submit'
668 $form->setSubmitTooltip( 'watchlistedit-clear-submit' );
669 $form->setWrapperLegendMsg( 'watchlistedit-clear-legend' );
670 $form->addHeaderText( $this->msg( 'watchlistedit-clear-explain' )->parse() );
671 $form->setSubmitCallback( array( $this, 'submitClear' ) );
677 * Determine whether we are editing the watchlist, and if so, what
678 * kind of editing operation
680 * @param WebRequest $request
684 public static function getMode( $request, $par ) {
685 $mode = strtolower( $request->getVal( 'action', $par ) );
689 case self
::EDIT_CLEAR
:
690 return self
::EDIT_CLEAR
;
693 return self
::EDIT_RAW
;
695 case self
::EDIT_NORMAL
:
696 return self
::EDIT_NORMAL
;
703 * Build a set of links for convenient navigation
704 * between watchlist viewing and editing modes
706 * @param null $unused
709 public static function buildTools( $unused ) {
714 'view' => array( 'Watchlist', false ),
715 'edit' => array( 'EditWatchlist', false ),
716 'raw' => array( 'EditWatchlist', 'raw' ),
717 'clear' => array( 'EditWatchlist', 'clear' ),
720 foreach ( $modes as $mode => $arr ) {
721 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
722 $tools[] = Linker
::linkKnown(
723 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
724 wfMessage( "watchlisttools-{$mode}" )->escaped()
728 return Html
::rawElement(
730 array( 'class' => 'mw-watchlist-toollinks' ),
731 wfMessage( 'parentheses', $wgLang->pipeList( $tools ) )->text()
737 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
739 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
740 public function getLegend( $namespace ) {
741 $namespace = substr( $namespace, 2 );
743 return $namespace == NS_MAIN
744 ?
$this->msg( 'blanknamespace' )->escaped()
745 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
748 public function getBody() {
749 return $this->displaySection( $this->mFieldTree
, '', 'editwatchlist-' );
753 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
755 * HTMLMultiSelectField throws validation errors if we get input data
756 * that doesn't match the data set in the form setup. This causes
757 * problems if something gets removed from the watchlist while the
758 * form is open (bug 32126), but we know that invalid items will
759 * be harmless so we can override it here.
761 * @param string $value the value the field was submitted with
762 * @param array $alldata the data collected from the form
763 * @return bool|string Bool true on success, or String error to display.
765 function validate( $value, $alldata ) {
766 // Need to call into grandparent to be a good citizen. :)
767 return HTMLFormField
::validate( $value, $alldata );