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 * Extract a list of titles from a blob of text, returning
122 * (prefixed) strings; unwatchable titles are ignored
124 * @param string $list
127 private function extractTitles( $list ) {
128 $list = explode( "\n", trim( $list ) );
129 if ( !is_array( $list ) ) {
135 foreach ( $list as $text ) {
136 $text = trim( $text );
137 if ( strlen( $text ) > 0 ) {
138 $title = Title
::newFromText( $text );
139 if ( $title instanceof Title
&& $title->isWatchable() ) {
145 GenderCache
::singleton()->doTitlesArray( $titles );
148 /** @var Title $title */
149 foreach ( $titles as $title ) {
150 $list[] = $title->getPrefixedText();
153 return array_unique( $list );
156 public function submitRaw( $data ) {
157 $wanted = $this->extractTitles( $data['Titles'] );
158 $current = $this->getWatchlist();
160 if ( count( $wanted ) > 0 ) {
161 $toWatch = array_diff( $wanted, $current );
162 $toUnwatch = array_diff( $current, $wanted );
163 $this->watchTitles( $toWatch );
164 $this->unwatchTitles( $toUnwatch );
165 $this->getUser()->invalidateCache();
167 if ( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ) {
168 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
173 if ( count( $toWatch ) > 0 ) {
174 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-added' )
175 ->numParams( count( $toWatch ) )->parse();
176 $this->showTitles( $toWatch, $this->successMessage
);
179 if ( count( $toUnwatch ) > 0 ) {
180 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed' )
181 ->numParams( count( $toUnwatch ) )->parse();
182 $this->showTitles( $toUnwatch, $this->successMessage
);
185 $this->clearWatchlist();
186 $this->getUser()->invalidateCache();
188 if ( count( $current ) > 0 ) {
189 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
194 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed' )
195 ->numParams( count( $current ) )->parse();
196 $this->showTitles( $current, $this->successMessage
);
202 public function submitClear( $data ) {
203 $current = $this->getWatchlist();
204 $this->clearWatchlist();
205 $this->getUser()->invalidateCache();
206 $this->successMessage
= $this->msg( 'watchlistedit-clear-done' )->parse();
207 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-clear-removed' )
208 ->numParams( count( $current ) )->parse();
209 $this->showTitles( $current, $this->successMessage
);
215 * Print out a list of linked titles
217 * $titles can be an array of strings or Title objects; the former
218 * is preferred, since Titles are very memory-heavy
220 * @param array $titles Array of strings, or Title objects
221 * @param string $output
223 private function showTitles( $titles, &$output ) {
224 $talk = $this->msg( 'talkpagelinktext' )->escaped();
225 // Do a batch existence check
226 $batch = new LinkBatch();
227 if (count($titles) >= 100) {
228 $output = wfMessage( 'watchlistedit-too-many' )->parse();
231 foreach ( $titles as $title ) {
232 if ( !$title instanceof Title
) {
233 $title = Title
::newFromText( $title );
236 if ( $title instanceof Title
) {
237 $batch->addObj( $title );
238 $batch->addObj( $title->getTalkPage() );
244 // Print out the list
247 foreach ( $titles as $title ) {
248 if ( !$title instanceof Title
) {
249 $title = Title
::newFromText( $title );
252 if ( $title instanceof Title
) {
254 . Linker
::link( $title )
255 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
260 $output .= "</ul>\n";
264 * Prepare a list of titles on a user's watchlist (excluding talk pages)
265 * and return an array of (prefixed) strings
269 private function getWatchlist() {
271 $dbr = wfGetDB( DB_MASTER
);
276 'wl_namespace', 'wl_title'
278 'wl_user' => $this->getUser()->getId(),
283 if ( $res->numRows() > 0 ) {
285 foreach ( $res as $row ) {
286 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
288 if ( $this->checkTitle( $title, $row->wl_namespace
, $row->wl_title
)
289 && !$title->isTalkPage()
296 GenderCache
::singleton()->doTitlesArray( $titles );
298 foreach ( $titles as $title ) {
299 $list[] = $title->getPrefixedText();
303 $this->cleanupWatchlist();
309 * Get a list of titles on a user's watchlist, excluding talk pages,
310 * and return as a two-dimensional array with namespace and title.
314 private function getWatchlistInfo() {
316 $dbr = wfGetDB( DB_MASTER
);
319 array( 'watchlist' ),
320 array( 'wl_namespace', 'wl_title' ),
321 array( 'wl_user' => $this->getUser()->getId() ),
323 array( 'ORDER BY' => array( 'wl_namespace', 'wl_title' ) )
326 $lb = new LinkBatch();
328 foreach ( $res as $row ) {
329 $lb->add( $row->wl_namespace
, $row->wl_title
);
330 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
331 $titles[$row->wl_namespace
][$row->wl_title
] = 1;
341 * Validates watchlist entry
343 * @param Title $title
344 * @param int $namespace
345 * @param string $dbKey
346 * @return bool Whether this item is valid
348 private function checkTitle( $title, $namespace, $dbKey ) {
350 && ( $title->isExternal()
351 ||
$title->getNamespace() < 0
354 $title = false; // unrecoverable
358 ||
$title->getNamespace() != $namespace
359 ||
$title->getDBkey() != $dbKey
361 $this->badItems
[] = array( $title, $namespace, $dbKey );
368 * Attempts to clean up broken items
370 private function cleanupWatchlist() {
371 if ( !count( $this->badItems
) ) {
372 return; //nothing to do
375 $dbw = wfGetDB( DB_MASTER
);
376 $user = $this->getUser();
378 foreach ( $this->badItems
as $row ) {
379 list( $title, $namespace, $dbKey ) = $row;
380 $action = $title ?
'cleaning up' : 'deleting';
381 wfDebug( "User {$user->getName()} has broken watchlist item ns($namespace):$dbKey, $action.\n" );
383 $dbw->delete( 'watchlist',
385 'wl_user' => $user->getId(),
386 'wl_namespace' => $namespace,
387 'wl_title' => $dbKey,
392 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
394 $user->addWatch( $title );
400 * Remove all titles from a user's watchlist
402 private function clearWatchlist() {
403 $dbw = wfGetDB( DB_MASTER
);
406 array( 'wl_user' => $this->getUser()->getId() ),
412 * Add a list of titles to a user's watchlist
414 * $titles can be an array of strings or Title objects; the former
415 * is preferred, since Titles are very memory-heavy
417 * @param array $titles Array of strings, or Title objects
419 private function watchTitles( $titles ) {
420 $dbw = wfGetDB( DB_MASTER
);
423 foreach ( $titles as $title ) {
424 if ( !$title instanceof Title
) {
425 $title = Title
::newFromText( $title );
428 if ( $title instanceof Title
) {
430 'wl_user' => $this->getUser()->getId(),
431 'wl_namespace' => MWNamespace
::getSubject( $title->getNamespace() ),
432 'wl_title' => $title->getDBkey(),
433 'wl_notificationtimestamp' => null,
436 'wl_user' => $this->getUser()->getId(),
437 'wl_namespace' => MWNamespace
::getTalk( $title->getNamespace() ),
438 'wl_title' => $title->getDBkey(),
439 'wl_notificationtimestamp' => null,
444 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
448 * Remove a list of titles from 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 unwatchTitles( $titles ) {
456 $dbw = wfGetDB( DB_MASTER
);
458 foreach ( $titles as $title ) {
459 if ( !$title instanceof Title
) {
460 $title = Title
::newFromText( $title );
463 if ( $title instanceof Title
) {
467 'wl_user' => $this->getUser()->getId(),
468 'wl_namespace' => MWNamespace
::getSubject( $title->getNamespace() ),
469 'wl_title' => $title->getDBkey(),
477 'wl_user' => $this->getUser()->getId(),
478 'wl_namespace' => MWNamespace
::getTalk( $title->getNamespace() ),
479 'wl_title' => $title->getDBkey(),
484 $page = WikiPage
::factory( $title );
485 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
490 public function submitNormal( $data ) {
493 foreach ( $data as $titles ) {
494 $this->unwatchTitles( $titles );
495 $removed = array_merge( $removed, $titles );
498 if ( count( $removed ) > 0 ) {
499 $this->successMessage
= $this->msg( 'watchlistedit-normal-done'
500 )->numParams( count( $removed ) )->parse();
501 $this->showTitles( $removed, $this->successMessage
);
510 * Get the standard watchlist editing form
514 protected function getNormalForm() {
520 foreach ( $this->getWatchlistInfo() as $namespace => $pages ) {
521 if ( $namespace >= 0 ) {
522 $fields['TitlesNs' . $namespace] = array(
523 'class' => 'EditWatchlistCheckboxSeriesField',
524 'options' => array(),
525 'section' => "ns$namespace",
529 foreach ( array_keys( $pages ) as $dbkey ) {
530 $title = Title
::makeTitleSafe( $namespace, $dbkey );
532 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
533 $text = $this->buildRemoveLine( $title );
534 $fields['TitlesNs' . $namespace]['options'][$text] = $title->getPrefixedText();
539 $this->cleanupWatchlist();
541 if ( count( $fields ) > 1 && $count > 30 ) {
542 $this->toc
= Linker
::tocIndent();
545 foreach ( $fields as $data ) {
546 # strip out the 'ns' prefix from the section name:
547 $ns = substr( $data['section'], 2 );
549 $nsText = ( $ns == NS_MAIN
)
550 ?
$this->msg( 'blanknamespace' )->escaped()
551 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
552 $this->toc
.= Linker
::tocLine( "editwatchlist-{$data['section']}", $nsText,
553 $this->getLanguage()->formatNum( ++
$tocLength ), 1 ) . Linker
::tocLineEnd();
556 $this->toc
= Linker
::tocList( $this->toc
);
561 $context = new DerivativeContext( $this->getContext() );
562 $context->setTitle( $this->getPageTitle() ); // Remove subpage
563 $form = new EditWatchlistNormalHTMLForm( $fields, $context );
564 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
566 # 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
567 $form->setSubmitTooltip( 'watchlistedit-normal-submit' );
568 $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
569 $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() );
570 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
576 * Build the label for a checkbox, with a link to the title, and various additional bits
578 * @param Title $title
581 private function buildRemoveLine( $title ) {
582 $link = Linker
::link( $title );
584 if ( $title->isRedirect() ) {
585 // Linker already makes class mw-redirect, so this is redundant
586 $link = '<span class="watchlistredir">' . $link . '</span>';
589 $tools[] = Linker
::link( $title->getTalkPage(), $this->msg( 'talkpagelinktext' )->escaped() );
591 if ( $title->exists() ) {
592 $tools[] = Linker
::linkKnown(
594 $this->msg( 'history_short' )->escaped(),
596 array( 'action' => 'history' )
600 if ( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
601 $tools[] = Linker
::linkKnown(
602 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
603 $this->msg( 'contributions' )->escaped()
608 'WatchlistEditorBuildRemoveLine',
609 array( &$tools, $title, $title->isRedirect(), $this->getSkin() )
612 return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
616 * Get a form for editing the watchlist in "raw" mode
620 protected function getRawForm() {
621 $titles = implode( $this->getWatchlist(), "\n" );
624 'type' => 'textarea',
625 'label-message' => 'watchlistedit-raw-titles',
626 'default' => $titles,
629 $context = new DerivativeContext( $this->getContext() );
630 $context->setTitle( $this->getPageTitle( 'raw' ) ); // Reset subpage
631 $form = new HTMLForm( $fields, $context );
632 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
633 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
634 $form->setSubmitTooltip( 'watchlistedit-raw-submit' );
635 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
636 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
637 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
643 * Get a form for clearing the watchlist
647 protected function getClearForm() {
648 $context = new DerivativeContext( $this->getContext() );
649 $context->setTitle( $this->getPageTitle( 'clear' ) ); // Reset subpage
650 $form = new HTMLForm( array(), $context );
651 $form->setSubmitTextMsg( 'watchlistedit-clear-submit' );
652 # Used message keys: 'accesskey-watchlistedit-clear-submit', 'tooltip-watchlistedit-clear-submit'
653 $form->setSubmitTooltip( 'watchlistedit-clear-submit' );
654 $form->setWrapperLegendMsg( 'watchlistedit-clear-legend' );
655 $form->addHeaderText( $this->msg( 'watchlistedit-clear-explain' )->parse() );
656 $form->setSubmitCallback( array( $this, 'submitClear' ) );
662 * Determine whether we are editing the watchlist, and if so, what
663 * kind of editing operation
665 * @param WebRequest $request
669 public static function getMode( $request, $par ) {
670 $mode = strtolower( $request->getVal( 'action', $par ) );
674 case self
::EDIT_CLEAR
:
675 return self
::EDIT_CLEAR
;
678 return self
::EDIT_RAW
;
680 case self
::EDIT_NORMAL
:
681 return self
::EDIT_NORMAL
;
688 * Build a set of links for convenient navigation
689 * between watchlist viewing and editing modes
691 * @param null $unused
694 public static function buildTools( $unused ) {
699 'view' => array( 'Watchlist', false ),
700 'edit' => array( 'EditWatchlist', false ),
701 'raw' => array( 'EditWatchlist', 'raw' ),
702 'clear' => array( 'EditWatchlist', 'clear' ),
705 foreach ( $modes as $mode => $arr ) {
706 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
707 $tools[] = Linker
::linkKnown(
708 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
709 wfMessage( "watchlisttools-{$mode}" )->escaped()
713 return Html
::rawElement(
715 array( 'class' => 'mw-watchlist-toollinks' ),
716 wfMessage( 'parentheses', $wgLang->pipeList( $tools ) )->text()
722 class WatchlistEditor
extends SpecialEditWatchlist
{
726 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
728 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
729 public function getLegend( $namespace ) {
730 $namespace = substr( $namespace, 2 );
732 return $namespace == NS_MAIN
733 ?
$this->msg( 'blanknamespace' )->escaped()
734 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
737 public function getBody() {
738 return $this->displaySection( $this->mFieldTree
, '', 'editwatchlist-' );
742 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
744 * HTMLMultiSelectField throws validation errors if we get input data
745 * that doesn't match the data set in the form setup. This causes
746 * problems if something gets removed from the watchlist while the
747 * form is open (bug 32126), but we know that invalid items will
748 * be harmless so we can override it here.
750 * @param string $value the value the field was submitted with
751 * @param array $alldata the data collected from the form
752 * @return bool|string Bool true on success, or String error to display.
754 function validate( $value, $alldata ) {
755 // Need to call into grandparent to be a good citizen. :)
756 return HTMLFormField
::validate( $value, $alldata );