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