Merge "Register a default value for the timecorrection preference"
[lhc/web/wiklou.git] / includes / specials / SpecialEditWatchlist.php
1 <?php
2 /**
3 * @defgroup Watchlist Users watchlist handling
4 */
5
6 /**
7 * Implements Special:EditWatchlist
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 * @ingroup SpecialPage
26 * @ingroup Watchlist
27 */
28
29 use MediaWiki\Linker\LinkRenderer;
30 use MediaWiki\Linker\LinkTarget;
31 use MediaWiki\MediaWikiServices;
32
33 /**
34 * Provides the UI through which users can perform editing
35 * operations on their watchlist
36 *
37 * @ingroup SpecialPage
38 * @ingroup Watchlist
39 * @author Rob Church <robchur@gmail.com>
40 */
41 class SpecialEditWatchlist extends UnlistedSpecialPage {
42 /**
43 * Editing modes. EDIT_CLEAR is no longer used; the "Clear" link scared people
44 * too much. Now it's passed on to the raw editor, from which it's very easy to clear.
45 */
46 const EDIT_CLEAR = 1;
47 const EDIT_RAW = 2;
48 const EDIT_NORMAL = 3;
49
50 protected $successMessage;
51
52 protected $toc;
53
54 private $badItems = [];
55
56 /**
57 * @var TitleParser
58 */
59 private $titleParser;
60
61 public function __construct() {
62 parent::__construct( 'EditWatchlist', 'editmywatchlist' );
63 }
64
65 /**
66 * Initialize any services we'll need (unless it has already been provided via a setter).
67 * This allows for dependency injection even though we don't control object creation.
68 */
69 private function initServices() {
70 if ( !$this->titleParser ) {
71 $lang = $this->getContext()->getLanguage();
72 $this->titleParser = new MediaWikiTitleCodec( $lang, GenderCache::singleton() );
73 }
74 }
75
76 public function doesWrites() {
77 return true;
78 }
79
80 /**
81 * Main execution point
82 *
83 * @param int $mode
84 */
85 public function execute( $mode ) {
86 $this->initServices();
87 $this->setHeaders();
88
89 # Anons don't get a watchlist
90 $this->requireLogin( 'watchlistanontext' );
91
92 $out = $this->getOutput();
93
94 $this->checkPermissions();
95 $this->checkReadOnly();
96
97 $this->outputHeader();
98 $this->outputSubtitle();
99 $out->addModuleStyles( 'mediawiki.special' );
100
101 # B/C: $mode used to be waaay down the parameter list, and the first parameter
102 # was $wgUser
103 if ( $mode instanceof User ) {
104 $args = func_get_args();
105 if ( count( $args ) >= 4 ) {
106 $mode = $args[3];
107 }
108 }
109 $mode = self::getMode( $this->getRequest(), $mode );
110
111 switch ( $mode ) {
112 case self::EDIT_RAW:
113 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
114 $form = $this->getRawForm();
115 if ( $form->show() ) {
116 $out->addHTML( $this->successMessage );
117 $out->addReturnTo( SpecialPage::getTitleFor( 'Watchlist' ) );
118 }
119 break;
120 case self::EDIT_CLEAR:
121 $out->setPageTitle( $this->msg( 'watchlistedit-clear-title' ) );
122 $form = $this->getClearForm();
123 if ( $form->show() ) {
124 $out->addHTML( $this->successMessage );
125 $out->addReturnTo( SpecialPage::getTitleFor( 'Watchlist' ) );
126 }
127 break;
128
129 case self::EDIT_NORMAL:
130 default:
131 $this->executeViewEditWatchlist();
132 break;
133 }
134 }
135
136 /**
137 * Renders a subheader on the watchlist page.
138 */
139 protected function outputSubtitle() {
140 $out = $this->getOutput();
141 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName() )
142 ->rawParams(
143 self::buildTools(
144 $this->getLanguage(),
145 $this->getLinkRenderer()
146 )
147 )
148 );
149 }
150
151 /**
152 * Executes an edit mode for the watchlist view, from which you can manage your watchlist
153 *
154 */
155 protected function executeViewEditWatchlist() {
156 $out = $this->getOutput();
157 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
158 $form = $this->getNormalForm();
159 if ( $form->show() ) {
160 $out->addHTML( $this->successMessage );
161 $out->addReturnTo( SpecialPage::getTitleFor( 'Watchlist' ) );
162 } elseif ( $this->toc !== false ) {
163 $out->prependHTML( $this->toc );
164 $out->addModules( 'mediawiki.toc' );
165 }
166 }
167
168 /**
169 * Return an array of subpages that this special page will accept.
170 *
171 * @see also SpecialWatchlist::getSubpagesForPrefixSearch
172 * @return string[] subpages
173 */
174 public function getSubpagesForPrefixSearch() {
175 // SpecialWatchlist uses SpecialEditWatchlist::getMode, so new types should be added
176 // here and there - no 'edit' here, because that the default for this page
177 return [
178 'clear',
179 'raw',
180 ];
181 }
182
183 /**
184 * Extract a list of titles from a blob of text, returning
185 * (prefixed) strings; unwatchable titles are ignored
186 *
187 * @param string $list
188 * @return array
189 */
190 private function extractTitles( $list ) {
191 $list = explode( "\n", trim( $list ) );
192 if ( !is_array( $list ) ) {
193 return [];
194 }
195
196 $titles = [];
197
198 foreach ( $list as $text ) {
199 $text = trim( $text );
200 if ( strlen( $text ) > 0 ) {
201 $title = Title::newFromText( $text );
202 if ( $title instanceof Title && $title->isWatchable() ) {
203 $titles[] = $title;
204 }
205 }
206 }
207
208 GenderCache::singleton()->doTitlesArray( $titles );
209
210 $list = [];
211 /** @var Title $title */
212 foreach ( $titles as $title ) {
213 $list[] = $title->getPrefixedText();
214 }
215
216 return array_unique( $list );
217 }
218
219 public function submitRaw( $data ) {
220 $wanted = $this->extractTitles( $data['Titles'] );
221 $current = $this->getWatchlist();
222
223 if ( count( $wanted ) > 0 ) {
224 $toWatch = array_diff( $wanted, $current );
225 $toUnwatch = array_diff( $current, $wanted );
226 $this->watchTitles( $toWatch );
227 $this->unwatchTitles( $toUnwatch );
228 $this->getUser()->invalidateCache();
229
230 if ( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 ) {
231 $this->successMessage = $this->msg( 'watchlistedit-raw-done' )->parse();
232 } else {
233 return false;
234 }
235
236 if ( count( $toWatch ) > 0 ) {
237 $this->successMessage .= ' ' . $this->msg( 'watchlistedit-raw-added' )
238 ->numParams( count( $toWatch ) )->parse();
239 $this->showTitles( $toWatch, $this->successMessage );
240 }
241
242 if ( count( $toUnwatch ) > 0 ) {
243 $this->successMessage .= ' ' . $this->msg( 'watchlistedit-raw-removed' )
244 ->numParams( count( $toUnwatch ) )->parse();
245 $this->showTitles( $toUnwatch, $this->successMessage );
246 }
247 } else {
248 $this->clearWatchlist();
249 $this->getUser()->invalidateCache();
250
251 if ( count( $current ) > 0 ) {
252 $this->successMessage = $this->msg( 'watchlistedit-raw-done' )->parse();
253 } else {
254 return false;
255 }
256
257 $this->successMessage .= ' ' . $this->msg( 'watchlistedit-raw-removed' )
258 ->numParams( count( $current ) )->parse();
259 $this->showTitles( $current, $this->successMessage );
260 }
261
262 return true;
263 }
264
265 public function submitClear( $data ) {
266 $current = $this->getWatchlist();
267 $this->clearWatchlist();
268 $this->getUser()->invalidateCache();
269 $this->successMessage = $this->msg( 'watchlistedit-clear-done' )->parse();
270 $this->successMessage .= ' ' . $this->msg( 'watchlistedit-clear-removed' )
271 ->numParams( count( $current ) )->parse();
272 $this->showTitles( $current, $this->successMessage );
273
274 return true;
275 }
276
277 /**
278 * Print out a list of linked titles
279 *
280 * $titles can be an array of strings or Title objects; the former
281 * is preferred, since Titles are very memory-heavy
282 *
283 * @param array $titles Array of strings, or Title objects
284 * @param string $output
285 */
286 private function showTitles( $titles, &$output ) {
287 $talk = $this->msg( 'talkpagelinktext' )->text();
288 // Do a batch existence check
289 $batch = new LinkBatch();
290 if ( count( $titles ) >= 100 ) {
291 $output = $this->msg( 'watchlistedit-too-many' )->parse();
292 return;
293 }
294 foreach ( $titles as $title ) {
295 if ( !$title instanceof Title ) {
296 $title = Title::newFromText( $title );
297 }
298
299 if ( $title instanceof Title ) {
300 $batch->addObj( $title );
301 $batch->addObj( $title->getTalkPage() );
302 }
303 }
304
305 $batch->execute();
306
307 // Print out the list
308 $output .= "<ul>\n";
309
310 $linkRenderer = $this->getLinkRenderer();
311 foreach ( $titles as $title ) {
312 if ( !$title instanceof Title ) {
313 $title = Title::newFromText( $title );
314 }
315
316 if ( $title instanceof Title ) {
317 $output .= '<li>' .
318 $linkRenderer->makeLink( $title ) . ' ' .
319 $this->msg( 'parentheses' )->rawParams(
320 $linkRenderer->makeLink( $title->getTalkPage(), $talk )
321 )->escaped() .
322 "</li>\n";
323 }
324 }
325
326 $output .= "</ul>\n";
327 }
328
329 /**
330 * Prepare a list of titles on a user's watchlist (excluding talk pages)
331 * and return an array of (prefixed) strings
332 *
333 * @return array
334 */
335 private function getWatchlist() {
336 $list = [];
337
338 $watchedItems = MediaWikiServices::getInstance()->getWatchedItemStore()->getWatchedItemsForUser(
339 $this->getUser(),
340 [ 'forWrite' => $this->getRequest()->wasPosted() ]
341 );
342
343 if ( $watchedItems ) {
344 /** @var Title[] $titles */
345 $titles = [];
346 foreach ( $watchedItems as $watchedItem ) {
347 $namespace = $watchedItem->getLinkTarget()->getNamespace();
348 $dbKey = $watchedItem->getLinkTarget()->getDBkey();
349 $title = Title::makeTitleSafe( $namespace, $dbKey );
350
351 if ( $this->checkTitle( $title, $namespace, $dbKey )
352 && !$title->isTalkPage()
353 ) {
354 $titles[] = $title;
355 }
356 }
357
358 GenderCache::singleton()->doTitlesArray( $titles );
359
360 foreach ( $titles as $title ) {
361 $list[] = $title->getPrefixedText();
362 }
363 }
364
365 $this->cleanupWatchlist();
366
367 return $list;
368 }
369
370 /**
371 * Get a list of titles on a user's watchlist, excluding talk pages,
372 * and return as a two-dimensional array with namespace and title.
373 *
374 * @return array
375 */
376 protected function getWatchlistInfo() {
377 $titles = [];
378
379 $watchedItems = MediaWikiServices::getInstance()->getWatchedItemStore()
380 ->getWatchedItemsForUser( $this->getUser(), [ 'sort' => WatchedItemStore::SORT_ASC ] );
381
382 $lb = new LinkBatch();
383
384 foreach ( $watchedItems as $watchedItem ) {
385 $namespace = $watchedItem->getLinkTarget()->getNamespace();
386 $dbKey = $watchedItem->getLinkTarget()->getDBkey();
387 $lb->add( $namespace, $dbKey );
388 if ( !MWNamespace::isTalk( $namespace ) ) {
389 $titles[$namespace][$dbKey] = 1;
390 }
391 }
392
393 $lb->execute();
394
395 return $titles;
396 }
397
398 /**
399 * Validates watchlist entry
400 *
401 * @param Title $title
402 * @param int $namespace
403 * @param string $dbKey
404 * @return bool Whether this item is valid
405 */
406 private function checkTitle( $title, $namespace, $dbKey ) {
407 if ( $title
408 && ( $title->isExternal()
409 || $title->getNamespace() < 0
410 )
411 ) {
412 $title = false; // unrecoverable
413 }
414
415 if ( !$title
416 || $title->getNamespace() != $namespace
417 || $title->getDBkey() != $dbKey
418 ) {
419 $this->badItems[] = [ $title, $namespace, $dbKey ];
420 }
421
422 return (bool)$title;
423 }
424
425 /**
426 * Attempts to clean up broken items
427 */
428 private function cleanupWatchlist() {
429 if ( !count( $this->badItems ) ) {
430 return; // nothing to do
431 }
432
433 $user = $this->getUser();
434 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
435
436 foreach ( $this->badItems as $row ) {
437 list( $title, $namespace, $dbKey ) = $row;
438 $action = $title ? 'cleaning up' : 'deleting';
439 wfDebug( "User {$user->getName()} has broken watchlist item ns($namespace):$dbKey, $action.\n" );
440
441 $store->removeWatch( $user, new TitleValue( (int)$namespace, $dbKey ) );
442
443 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
444 if ( $title ) {
445 $user->addWatch( $title );
446 }
447 }
448 }
449
450 /**
451 * Remove all titles from a user's watchlist
452 */
453 private function clearWatchlist() {
454 $dbw = wfGetDB( DB_MASTER );
455 $dbw->delete(
456 'watchlist',
457 [ 'wl_user' => $this->getUser()->getId() ],
458 __METHOD__
459 );
460 }
461
462 /**
463 * Add a list of targets to a user's watchlist
464 *
465 * @param string[]|LinkTarget[] $targets
466 */
467 private function watchTitles( $targets ) {
468 $expandedTargets = [];
469 foreach ( $targets as $target ) {
470 if ( !$target instanceof LinkTarget ) {
471 try {
472 $target = $this->titleParser->parseTitle( $target, NS_MAIN );
473 }
474 catch ( MalformedTitleException $e ) {
475 continue;
476 }
477 }
478
479 $ns = $target->getNamespace();
480 $dbKey = $target->getDBkey();
481 $expandedTargets[] = new TitleValue( MWNamespace::getSubject( $ns ), $dbKey );
482 $expandedTargets[] = new TitleValue( MWNamespace::getTalk( $ns ), $dbKey );
483 }
484
485 MediaWikiServices::getInstance()->getWatchedItemStore()->addWatchBatchForUser(
486 $this->getUser(),
487 $expandedTargets
488 );
489 }
490
491 /**
492 * Remove a list of titles from a user's watchlist
493 *
494 * $titles can be an array of strings or Title objects; the former
495 * is preferred, since Titles are very memory-heavy
496 *
497 * @param array $titles Array of strings, or Title objects
498 */
499 private function unwatchTitles( $titles ) {
500 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
501
502 foreach ( $titles as $title ) {
503 if ( !$title instanceof Title ) {
504 $title = Title::newFromText( $title );
505 }
506
507 if ( $title instanceof Title ) {
508 $store->removeWatch( $this->getUser(), $title->getSubjectPage() );
509 $store->removeWatch( $this->getUser(), $title->getTalkPage() );
510
511 $page = WikiPage::factory( $title );
512 Hooks::run( 'UnwatchArticleComplete', [ $this->getUser(), &$page ] );
513 }
514 }
515 }
516
517 public function submitNormal( $data ) {
518 $removed = [];
519
520 foreach ( $data as $titles ) {
521 $this->unwatchTitles( $titles );
522 $removed = array_merge( $removed, $titles );
523 }
524
525 if ( count( $removed ) > 0 ) {
526 $this->successMessage = $this->msg( 'watchlistedit-normal-done'
527 )->numParams( count( $removed ) )->parse();
528 $this->showTitles( $removed, $this->successMessage );
529
530 return true;
531 } else {
532 return false;
533 }
534 }
535
536 /**
537 * Get the standard watchlist editing form
538 *
539 * @return HTMLForm
540 */
541 protected function getNormalForm() {
542 global $wgContLang;
543
544 $fields = [];
545 $count = 0;
546
547 // Allow subscribers to manipulate the list of watched pages (or use it
548 // to preload lots of details at once)
549 $watchlistInfo = $this->getWatchlistInfo();
550 Hooks::run(
551 'WatchlistEditorBeforeFormRender',
552 [ &$watchlistInfo ]
553 );
554
555 foreach ( $watchlistInfo as $namespace => $pages ) {
556 $options = [];
557
558 foreach ( array_keys( $pages ) as $dbkey ) {
559 $title = Title::makeTitleSafe( $namespace, $dbkey );
560
561 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
562 $text = $this->buildRemoveLine( $title );
563 $options[$text] = $title->getPrefixedText();
564 $count++;
565 }
566 }
567
568 // checkTitle can filter some options out, avoid empty sections
569 if ( count( $options ) > 0 ) {
570 $fields['TitlesNs' . $namespace] = [
571 'class' => 'EditWatchlistCheckboxSeriesField',
572 'options' => $options,
573 'section' => "ns$namespace",
574 ];
575 }
576 }
577 $this->cleanupWatchlist();
578
579 if ( count( $fields ) > 1 && $count > 30 ) {
580 $this->toc = Linker::tocIndent();
581 $tocLength = 0;
582
583 foreach ( $fields as $data ) {
584 # strip out the 'ns' prefix from the section name:
585 $ns = substr( $data['section'], 2 );
586
587 $nsText = ( $ns == NS_MAIN )
588 ? $this->msg( 'blanknamespace' )->escaped()
589 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
590 $this->toc .= Linker::tocLine( "editwatchlist-{$data['section']}", $nsText,
591 $this->getLanguage()->formatNum( ++$tocLength ), 1 ) . Linker::tocLineEnd();
592 }
593
594 $this->toc = Linker::tocList( $this->toc );
595 } else {
596 $this->toc = false;
597 }
598
599 $context = new DerivativeContext( $this->getContext() );
600 $context->setTitle( $this->getPageTitle() ); // Remove subpage
601 $form = new EditWatchlistNormalHTMLForm( $fields, $context );
602 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
603 $form->setSubmitDestructive();
604 # Used message keys:
605 # 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
606 $form->setSubmitTooltip( 'watchlistedit-normal-submit' );
607 $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
608 $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() );
609 $form->setSubmitCallback( [ $this, 'submitNormal' ] );
610
611 return $form;
612 }
613
614 /**
615 * Build the label for a checkbox, with a link to the title, and various additional bits
616 *
617 * @param Title $title
618 * @return string
619 */
620 private function buildRemoveLine( $title ) {
621 $linkRenderer = $this->getLinkRenderer();
622 $link = $linkRenderer->makeLink( $title );
623
624 $tools['talk'] = $linkRenderer->makeLink(
625 $title->getTalkPage(),
626 $this->msg( 'talkpagelinktext' )->text()
627 );
628
629 if ( $title->exists() ) {
630 $tools['history'] = $linkRenderer->makeKnownLink(
631 $title,
632 $this->msg( 'history_short' )->text(),
633 [],
634 [ 'action' => 'history' ]
635 );
636 }
637
638 if ( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
639 $tools['contributions'] = $linkRenderer->makeKnownLink(
640 SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
641 $this->msg( 'contributions' )->text()
642 );
643 }
644
645 Hooks::run(
646 'WatchlistEditorBuildRemoveLine',
647 [ &$tools, $title, $title->isRedirect(), $this->getSkin(), &$link ]
648 );
649
650 if ( $title->isRedirect() ) {
651 // Linker already makes class mw-redirect, so this is redundant
652 $link = '<span class="watchlistredir">' . $link . '</span>';
653 }
654
655 return $link . ' ' .
656 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList( $tools ) )->escaped();
657 }
658
659 /**
660 * Get a form for editing the watchlist in "raw" mode
661 *
662 * @return HTMLForm
663 */
664 protected function getRawForm() {
665 $titles = implode( $this->getWatchlist(), "\n" );
666 $fields = [
667 'Titles' => [
668 'type' => 'textarea',
669 'label-message' => 'watchlistedit-raw-titles',
670 'default' => $titles,
671 ],
672 ];
673 $context = new DerivativeContext( $this->getContext() );
674 $context->setTitle( $this->getPageTitle( 'raw' ) ); // Reset subpage
675 $form = new HTMLForm( $fields, $context );
676 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
677 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
678 $form->setSubmitTooltip( 'watchlistedit-raw-submit' );
679 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
680 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
681 $form->setSubmitCallback( [ $this, 'submitRaw' ] );
682
683 return $form;
684 }
685
686 /**
687 * Get a form for clearing the watchlist
688 *
689 * @return HTMLForm
690 */
691 protected function getClearForm() {
692 $context = new DerivativeContext( $this->getContext() );
693 $context->setTitle( $this->getPageTitle( 'clear' ) ); // Reset subpage
694 $form = new HTMLForm( [], $context );
695 $form->setSubmitTextMsg( 'watchlistedit-clear-submit' );
696 # Used message keys: 'accesskey-watchlistedit-clear-submit', 'tooltip-watchlistedit-clear-submit'
697 $form->setSubmitTooltip( 'watchlistedit-clear-submit' );
698 $form->setWrapperLegendMsg( 'watchlistedit-clear-legend' );
699 $form->addHeaderText( $this->msg( 'watchlistedit-clear-explain' )->parse() );
700 $form->setSubmitCallback( [ $this, 'submitClear' ] );
701 $form->setSubmitDestructive();
702
703 return $form;
704 }
705
706 /**
707 * Determine whether we are editing the watchlist, and if so, what
708 * kind of editing operation
709 *
710 * @param WebRequest $request
711 * @param string $par
712 * @return int
713 */
714 public static function getMode( $request, $par ) {
715 $mode = strtolower( $request->getVal( 'action', $par ) );
716
717 switch ( $mode ) {
718 case 'clear':
719 case self::EDIT_CLEAR:
720 return self::EDIT_CLEAR;
721 case 'raw':
722 case self::EDIT_RAW:
723 return self::EDIT_RAW;
724 case 'edit':
725 case self::EDIT_NORMAL:
726 return self::EDIT_NORMAL;
727 default:
728 return false;
729 }
730 }
731
732 /**
733 * Build a set of links for convenient navigation
734 * between watchlist viewing and editing modes
735 *
736 * @param Language $lang
737 * @param LinkRenderer|null $linkRenderer
738 * @return string
739 */
740 public static function buildTools( $lang, LinkRenderer $linkRenderer = null ) {
741 if ( !$lang instanceof Language ) {
742 // back-compat where the first parameter was $unused
743 global $wgLang;
744 $lang = $wgLang;
745 }
746 if ( !$linkRenderer ) {
747 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
748 }
749
750 $tools = [];
751 $modes = [
752 'view' => [ 'Watchlist', false ],
753 'edit' => [ 'EditWatchlist', false ],
754 'raw' => [ 'EditWatchlist', 'raw' ],
755 'clear' => [ 'EditWatchlist', 'clear' ],
756 ];
757
758 foreach ( $modes as $mode => $arr ) {
759 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
760 $tools[] = $linkRenderer->makeKnownLink(
761 SpecialPage::getTitleFor( $arr[0], $arr[1] ),
762 wfMessage( "watchlisttools-{$mode}" )->text()
763 );
764 }
765
766 return Html::rawElement(
767 'span',
768 [ 'class' => 'mw-watchlist-toollinks' ],
769 wfMessage( 'parentheses' )->rawParams( $lang->pipeList( $tools ) )->escaped()
770 );
771 }
772 }
773
774 /**
775 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
776 */
777 class EditWatchlistNormalHTMLForm extends HTMLForm {
778 public function getLegend( $namespace ) {
779 $namespace = substr( $namespace, 2 );
780
781 return $namespace == NS_MAIN
782 ? $this->msg( 'blanknamespace' )->escaped()
783 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
784 }
785
786 public function getBody() {
787 return $this->displaySection( $this->mFieldTree, '', 'editwatchlist-' );
788 }
789 }
790
791 class EditWatchlistCheckboxSeriesField extends HTMLMultiSelectField {
792 /**
793 * HTMLMultiSelectField throws validation errors if we get input data
794 * that doesn't match the data set in the form setup. This causes
795 * problems if something gets removed from the watchlist while the
796 * form is open (bug 32126), but we know that invalid items will
797 * be harmless so we can override it here.
798 *
799 * @param string $value The value the field was submitted with
800 * @param array $alldata The data collected from the form
801 * @return bool|string Bool true on success, or String error to display.
802 */
803 function validate( $value, $alldata ) {
804 // Need to call into grandparent to be a good citizen. :)
805 return HTMLFormField::validate( $value, $alldata );
806 }
807 }