Remove "related" searches
[mediawiki.git] / includes / specials / SpecialEditWatchlist.php
blob3656b9cc73a955dc833d829e04592357b5356d85
1 <?php
2 /**
3 * @defgroup Watchlist Users watchlist handling
4 */
6 /**
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
24 * @file
25 * @ingroup SpecialPage
26 * @ingroup Watchlist
29 /**
30 * Provides the UI through which users can perform editing
31 * operations on their watchlist
33 * @ingroup SpecialPage
34 * @ingroup Watchlist
35 * @author Rob Church <robchur@gmail.com>
37 class SpecialEditWatchlist extends UnlistedSpecialPage {
38 /**
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.
42 const EDIT_CLEAR = 1;
43 const EDIT_RAW = 2;
44 const EDIT_NORMAL = 3;
46 protected $successMessage;
48 protected $toc;
50 private $badItems = array();
52 public function __construct() {
53 parent::__construct( 'EditWatchlist', 'editmywatchlist' );
56 /**
57 * Main execution point
59 * @param int $mode
61 public function execute( $mode ) {
62 $this->setHeaders();
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
76 # was $wgUser
77 if ( $mode instanceof User ) {
78 $args = func_get_args();
79 if ( count( $args ) >= 4 ) {
80 $mode = $args[3];
83 $mode = self::getMode( $this->getRequest(), $mode );
85 switch ( $mode ) {
86 case self::EDIT_RAW:
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' ) );
93 break;
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' ) );
101 break;
103 case self::EDIT_NORMAL:
104 default:
105 $this->executeViewEditWatchlist();
106 break;
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(
145 $search,
146 $limit,
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
149 array(
150 'clear',
151 'raw',
157 * Extract a list of titles from a blob of text, returning
158 * (prefixed) strings; unwatchable titles are ignored
160 * @param string $list
161 * @return array
163 private function extractTitles( $list ) {
164 $list = explode( "\n", trim( $list ) );
165 if ( !is_array( $list ) ) {
166 return array();
169 $titles = array();
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() ) {
176 $titles[] = $title;
181 GenderCache::singleton()->doTitlesArray( $titles );
183 $list = array();
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();
205 } else {
206 return false;
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 );
220 } else {
221 $this->clearWatchlist();
222 $this->getUser()->invalidateCache();
224 if ( count( $current ) > 0 ) {
225 $this->successMessage = $this->msg( 'watchlistedit-raw-done' )->parse();
226 } else {
227 return false;
230 $this->successMessage .= ' ' . $this->msg( 'watchlistedit-raw-removed' )
231 ->numParams( count( $current ) )->parse();
232 $this->showTitles( $current, $this->successMessage );
235 return true;
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 );
247 return true;
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();
265 return;
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() );
278 $batch->execute();
280 // Print out the list
281 $output .= "<ul>\n";
283 foreach ( $titles as $title ) {
284 if ( !$title instanceof Title ) {
285 $title = Title::newFromText( $title );
288 if ( $title instanceof Title ) {
289 $output .= "<li>"
290 . Linker::link( $title )
291 . ' (' . Linker::link( $title->getTalkPage(), $talk )
292 . ")</li>\n";
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
303 * @return array
305 private function getWatchlist() {
306 $list = array();
307 $dbr = wfGetDB( DB_MASTER );
309 $res = $dbr->select(
310 'watchlist',
311 array(
312 'wl_namespace', 'wl_title'
313 ), array(
314 'wl_user' => $this->getUser()->getId(),
316 __METHOD__
319 if ( $res->numRows() > 0 ) {
320 $titles = array();
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()
327 $titles[] = $title;
330 $res->free();
332 GenderCache::singleton()->doTitlesArray( $titles );
334 foreach ( $titles as $title ) {
335 $list[] = $title->getPrefixedText();
339 $this->cleanupWatchlist();
341 return $list;
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.
348 * @return array
350 protected function getWatchlistInfo() {
351 $titles = array();
352 $dbr = wfGetDB( DB_MASTER );
354 $res = $dbr->select(
355 array( 'watchlist' ),
356 array( 'wl_namespace', 'wl_title' ),
357 array( 'wl_user' => $this->getUser()->getId() ),
358 __METHOD__,
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;
371 $lb->execute();
373 return $titles;
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 ) {
385 if ( $title
386 && ( $title->isExternal()
387 || $title->getNamespace() < 0
390 $title = false; // unrecoverable
393 if ( !$title
394 || $title->getNamespace() != $namespace
395 || $title->getDBkey() != $dbKey
397 $this->badItems[] = array( $title, $namespace, $dbKey );
400 return (bool)$title;
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',
420 array(
421 'wl_user' => $user->getId(),
422 'wl_namespace' => $namespace,
423 'wl_title' => $dbKey,
425 __METHOD__
428 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
429 if ( $title ) {
430 $user->addWatch( $title );
436 * Remove all titles from a user's watchlist
438 private function clearWatchlist() {
439 $dbw = wfGetDB( DB_MASTER );
440 $dbw->delete(
441 'watchlist',
442 array( 'wl_user' => $this->getUser()->getId() ),
443 __METHOD__
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 );
457 $rows = array();
459 foreach ( $titles as $title ) {
460 if ( !$title instanceof Title ) {
461 $title = Title::newFromText( $title );
464 if ( $title instanceof Title ) {
465 $rows[] = array(
466 'wl_user' => $this->getUser()->getId(),
467 'wl_namespace' => MWNamespace::getSubject( $title->getNamespace() ),
468 'wl_title' => $title->getDBkey(),
469 'wl_notificationtimestamp' => null,
471 $rows[] = array(
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 ) {
500 $dbw->delete(
501 'watchlist',
502 array(
503 'wl_user' => $this->getUser()->getId(),
504 'wl_namespace' => MWNamespace::getSubject( $title->getNamespace() ),
505 'wl_title' => $title->getDBkey(),
507 __METHOD__
510 $dbw->delete(
511 'watchlist',
512 array(
513 'wl_user' => $this->getUser()->getId(),
514 'wl_namespace' => MWNamespace::getTalk( $title->getNamespace() ),
515 'wl_title' => $title->getDBkey(),
517 __METHOD__
520 $page = WikiPage::factory( $title );
521 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
526 public function submitNormal( $data ) {
527 $removed = array();
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 );
539 return true;
540 } else {
541 return false;
546 * Get the standard watchlist editing form
548 * @return HTMLForm
550 protected function getNormalForm() {
551 global $wgContLang;
553 $fields = array();
554 $count = 0;
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();
559 wfRunHooks(
560 'WatchlistEditorBeforeFormRender',
561 array( &$watchlistInfo )
564 foreach ( $watchlistInfo as $namespace => $pages ) {
565 $options = array();
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();
573 $count++;
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();
590 $tocLength = 0;
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 );
604 } else {
605 $this->toc = false;
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' );
612 # Used message keys:
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' ) );
619 return $form;
623 * Build the label for a checkbox, with a link to the title, and various additional bits
625 * @param Title $title
626 * @return string
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(
635 $title,
636 $this->msg( 'history_short' )->escaped(),
637 array(),
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()
649 wfRunHooks(
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
665 * @return HTMLForm
667 protected function getRawForm() {
668 $titles = implode( $this->getWatchlist(), "\n" );
669 $fields = array(
670 'Titles' => array(
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' ) );
686 return $form;
690 * Get a form for clearing the watchlist
692 * @return HTMLForm
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' ) );
705 return $form;
709 * Determine whether we are editing the watchlist, and if so, what
710 * kind of editing operation
712 * @param WebRequest $request
713 * @param string $par
714 * @return int
716 public static function getMode( $request, $par ) {
717 $mode = strtolower( $request->getVal( 'action', $par ) );
719 switch ( $mode ) {
720 case 'clear':
721 case self::EDIT_CLEAR:
722 return self::EDIT_CLEAR;
723 case 'raw':
724 case self::EDIT_RAW:
725 return self::EDIT_RAW;
726 case 'edit':
727 case self::EDIT_NORMAL:
728 return self::EDIT_NORMAL;
729 default:
730 return false;
735 * Build a set of links for convenient navigation
736 * between watchlist viewing and editing modes
738 * @param null $unused
739 * @return string
741 public static function buildTools( $unused ) {
742 global $wgLang;
744 $tools = array();
745 $modes = array(
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(
761 'span',
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 );