Merge "SpecialUnusedimages: Change message when CountCategorizedImagesAsUsed"
[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 $this->titleParser = MediaWikiServices::getInstance()->getTitleParser();
72 }
73 }
74
75 public function doesWrites() {
76 return true;
77 }
78
79 /**
80 * Main execution point
81 *
82 * @param int $mode
83 */
84 public function execute( $mode ) {
85 $this->initServices();
86 $this->setHeaders();
87
88 # Anons don't get a watchlist
89 $this->requireLogin( 'watchlistanontext' );
90
91 $out = $this->getOutput();
92
93 $this->checkPermissions();
94 $this->checkReadOnly();
95
96 $this->outputHeader();
97 $this->outputSubtitle();
98 $out->addModuleStyles( 'mediawiki.special' );
99
100 # B/C: $mode used to be waaay down the parameter list, and the first parameter
101 # was $wgUser
102 if ( $mode instanceof User ) {
103 $args = func_get_args();
104 if ( count( $args ) >= 4 ) {
105 $mode = $args[3];
106 }
107 }
108 $mode = self::getMode( $this->getRequest(), $mode );
109
110 switch ( $mode ) {
111 case self::EDIT_RAW:
112 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
113 $form = $this->getRawForm();
114 if ( $form->show() ) {
115 $out->addHTML( $this->successMessage );
116 $out->addReturnTo( SpecialPage::getTitleFor( 'Watchlist' ) );
117 }
118 break;
119 case self::EDIT_CLEAR:
120 $out->setPageTitle( $this->msg( 'watchlistedit-clear-title' ) );
121 $form = $this->getClearForm();
122 if ( $form->show() ) {
123 $out->addHTML( $this->successMessage );
124 $out->addReturnTo( SpecialPage::getTitleFor( 'Watchlist' ) );
125 }
126 break;
127
128 case self::EDIT_NORMAL:
129 default:
130 $this->executeViewEditWatchlist();
131 break;
132 }
133 }
134
135 /**
136 * Renders a subheader on the watchlist page.
137 */
138 protected function outputSubtitle() {
139 $out = $this->getOutput();
140 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName() )
141 ->rawParams(
142 self::buildTools(
143 $this->getLanguage(),
144 $this->getLinkRenderer()
145 )
146 )
147 );
148 }
149
150 /**
151 * Executes an edit mode for the watchlist view, from which you can manage your watchlist
152 */
153 protected function executeViewEditWatchlist() {
154 $out = $this->getOutput();
155 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
156 $form = $this->getNormalForm();
157 if ( $form->show() ) {
158 $out->addHTML( $this->successMessage );
159 $out->addReturnTo( SpecialPage::getTitleFor( 'Watchlist' ) );
160 } elseif ( $this->toc !== false ) {
161 $out->prependHTML( $this->toc );
162 $out->addModules( 'mediawiki.toc' );
163 $out->addModuleStyles( 'mediawiki.toc.styles' );
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
248 if ( count( $current ) === 0 ) {
249 return false;
250 }
251
252 $this->clearUserWatchedItems( $current, 'raw' );
253 $this->showTitles( $current, $this->successMessage );
254 }
255
256 return true;
257 }
258
259 public function submitClear( $data ) {
260 $current = $this->getWatchlist();
261 $this->clearUserWatchedItems( $current, 'clear' );
262 $this->showTitles( $current, $this->successMessage );
263 return true;
264 }
265
266 /**
267 * @param array $current
268 * @param string $messageFor 'raw' or 'clear'
269 */
270 private function clearUserWatchedItems( $current, $messageFor ) {
271 $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
272 if ( $watchedItemStore->clearUserWatchedItems( $this->getUser() ) ) {
273 $this->successMessage = $this->msg( 'watchlistedit-' . $messageFor . '-done' )->parse();
274 $this->successMessage .= ' ' . $this->msg( 'watchlistedit-' . $messageFor . '-removed' )
275 ->numParams( count( $current ) )->parse();
276 $this->getUser()->invalidateCache();
277 } else {
278 $watchedItemStore->clearUserWatchedItemsUsingJobQueue( $this->getUser() );
279 $this->successMessage = $this->msg( 'watchlistedit-clear-jobqueue' )->parse();
280 }
281 }
282
283 /**
284 * Print out a list of linked titles
285 *
286 * $titles can be an array of strings or Title objects; the former
287 * is preferred, since Titles are very memory-heavy
288 *
289 * @param array $titles Array of strings, or Title objects
290 * @param string $output
291 */
292 private function showTitles( $titles, &$output ) {
293 $talk = $this->msg( 'talkpagelinktext' )->text();
294 // Do a batch existence check
295 $batch = new LinkBatch();
296 if ( count( $titles ) >= 100 ) {
297 $output = $this->msg( 'watchlistedit-too-many' )->parse();
298 return;
299 }
300 foreach ( $titles as $title ) {
301 if ( !$title instanceof Title ) {
302 $title = Title::newFromText( $title );
303 }
304
305 if ( $title instanceof Title ) {
306 $batch->addObj( $title );
307 $batch->addObj( $title->getTalkPage() );
308 }
309 }
310
311 $batch->execute();
312
313 // Print out the list
314 $output .= "<ul>\n";
315
316 $linkRenderer = $this->getLinkRenderer();
317 foreach ( $titles as $title ) {
318 if ( !$title instanceof Title ) {
319 $title = Title::newFromText( $title );
320 }
321
322 if ( $title instanceof Title ) {
323 $output .= '<li>' .
324 $linkRenderer->makeLink( $title ) . ' ' .
325 $this->msg( 'parentheses' )->rawParams(
326 $linkRenderer->makeLink( $title->getTalkPage(), $talk )
327 )->escaped() .
328 "</li>\n";
329 }
330 }
331
332 $output .= "</ul>\n";
333 }
334
335 /**
336 * Prepare a list of titles on a user's watchlist (excluding talk pages)
337 * and return an array of (prefixed) strings
338 *
339 * @return array
340 */
341 private function getWatchlist() {
342 $list = [];
343
344 $watchedItems = MediaWikiServices::getInstance()->getWatchedItemStore()->getWatchedItemsForUser(
345 $this->getUser(),
346 [ 'forWrite' => $this->getRequest()->wasPosted() ]
347 );
348
349 if ( $watchedItems ) {
350 /** @var Title[] $titles */
351 $titles = [];
352 foreach ( $watchedItems as $watchedItem ) {
353 $namespace = $watchedItem->getLinkTarget()->getNamespace();
354 $dbKey = $watchedItem->getLinkTarget()->getDBkey();
355 $title = Title::makeTitleSafe( $namespace, $dbKey );
356
357 if ( $this->checkTitle( $title, $namespace, $dbKey )
358 && !$title->isTalkPage()
359 ) {
360 $titles[] = $title;
361 }
362 }
363
364 MediaWikiServices::getInstance()->getGenderCache()->doTitlesArray( $titles );
365
366 foreach ( $titles as $title ) {
367 $list[] = $title->getPrefixedText();
368 }
369 }
370
371 $this->cleanupWatchlist();
372
373 return $list;
374 }
375
376 /**
377 * Get a list of titles on a user's watchlist, excluding talk pages,
378 * and return as a two-dimensional array with namespace and title.
379 *
380 * @return array
381 */
382 protected function getWatchlistInfo() {
383 $titles = [];
384
385 $watchedItems = MediaWikiServices::getInstance()->getWatchedItemStore()
386 ->getWatchedItemsForUser( $this->getUser(), [ 'sort' => WatchedItemStore::SORT_ASC ] );
387
388 $lb = new LinkBatch();
389
390 foreach ( $watchedItems as $watchedItem ) {
391 $namespace = $watchedItem->getLinkTarget()->getNamespace();
392 $dbKey = $watchedItem->getLinkTarget()->getDBkey();
393 $lb->add( $namespace, $dbKey );
394 if ( !MWNamespace::isTalk( $namespace ) ) {
395 $titles[$namespace][$dbKey] = 1;
396 }
397 }
398
399 $lb->execute();
400
401 return $titles;
402 }
403
404 /**
405 * Validates watchlist entry
406 *
407 * @param Title $title
408 * @param int $namespace
409 * @param string $dbKey
410 * @return bool Whether this item is valid
411 */
412 private function checkTitle( $title, $namespace, $dbKey ) {
413 if ( $title
414 && ( $title->isExternal()
415 || $title->getNamespace() < 0
416 )
417 ) {
418 $title = false; // unrecoverable
419 }
420
421 if ( !$title
422 || $title->getNamespace() != $namespace
423 || $title->getDBkey() != $dbKey
424 ) {
425 $this->badItems[] = [ $title, $namespace, $dbKey ];
426 }
427
428 return (bool)$title;
429 }
430
431 /**
432 * Attempts to clean up broken items
433 */
434 private function cleanupWatchlist() {
435 if ( !count( $this->badItems ) ) {
436 return; // nothing to do
437 }
438
439 $user = $this->getUser();
440 $badItems = $this->badItems;
441 DeferredUpdates::addCallableUpdate( function () use ( $user, $badItems ) {
442 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
443 foreach ( $badItems as $row ) {
444 list( $title, $namespace, $dbKey ) = $row;
445 $action = $title ? 'cleaning up' : 'deleting';
446 wfDebug( "User {$user->getName()} has broken watchlist item " .
447 "ns($namespace):$dbKey, $action.\n" );
448
449 $store->removeWatch( $user, new TitleValue( (int)$namespace, $dbKey ) );
450 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
451 if ( $title ) {
452 $user->addWatch( $title );
453 }
454 }
455 } );
456 }
457
458 /**
459 * Add a list of targets to a user's watchlist
460 *
461 * @param string[]|LinkTarget[] $targets
462 * @return bool
463 * @throws FatalError
464 * @throws MWException
465 */
466 private function watchTitles( array $targets ) {
467 return MediaWikiServices::getInstance()->getWatchedItemStore()
468 ->addWatchBatchForUser( $this->getUser(), $this->getExpandedTargets( $targets ) )
469 && $this->runWatchUnwatchCompleteHook( 'Watch', $targets );
470 }
471
472 /**
473 * Remove a list of titles from a user's watchlist
474 *
475 * $titles can be an array of strings or Title objects; the former
476 * is preferred, since Titles are very memory-heavy
477 *
478 * @param string[]|LinkTarget[] $targets
479 *
480 * @return bool
481 * @throws FatalError
482 * @throws MWException
483 */
484 private function unwatchTitles( array $targets ) {
485 return MediaWikiServices::getInstance()->getWatchedItemStore()
486 ->removeWatchBatchForUser( $this->getUser(), $this->getExpandedTargets( $targets ) )
487 && $this->runWatchUnwatchCompleteHook( 'Unwatch', $targets );
488 }
489
490 /**
491 * @param string $action
492 * Can be "Watch" or "Unwatch"
493 * @param string[]|LinkTarget[] $targets
494 * @return bool
495 * @throws FatalError
496 * @throws MWException
497 */
498 private function runWatchUnwatchCompleteHook( $action, $targets ) {
499 foreach ( $targets as $target ) {
500 $title = $target instanceof TitleValue ?
501 Title::newFromTitleValue( $target ) :
502 Title::newFromText( $target );
503 $page = WikiPage::factory( $title );
504 Hooks::run( $action . 'ArticleComplete', [ $this->getUser(), &$page ] );
505 }
506 return true;
507 }
508
509 /**
510 * @param string[]|LinkTarget[] $targets
511 * @return TitleValue[]
512 */
513 private function getExpandedTargets( array $targets ) {
514 $expandedTargets = [];
515 foreach ( $targets as $target ) {
516 if ( !$target instanceof LinkTarget ) {
517 try {
518 $target = $this->titleParser->parseTitle( $target, NS_MAIN );
519 }
520 catch ( MalformedTitleException $e ) {
521 continue;
522 }
523 }
524
525 $ns = $target->getNamespace();
526 $dbKey = $target->getDBkey();
527 $expandedTargets[] = new TitleValue( MWNamespace::getSubject( $ns ), $dbKey );
528 $expandedTargets[] = new TitleValue( MWNamespace::getTalk( $ns ), $dbKey );
529 }
530 return $expandedTargets;
531 }
532
533 public function submitNormal( $data ) {
534 $removed = [];
535
536 foreach ( $data as $titles ) {
537 $this->unwatchTitles( $titles );
538 $removed = array_merge( $removed, $titles );
539 }
540
541 if ( count( $removed ) > 0 ) {
542 $this->successMessage = $this->msg( 'watchlistedit-normal-done'
543 )->numParams( count( $removed ) )->parse();
544 $this->showTitles( $removed, $this->successMessage );
545
546 return true;
547 } else {
548 return false;
549 }
550 }
551
552 /**
553 * Get the standard watchlist editing form
554 *
555 * @return HTMLForm
556 */
557 protected function getNormalForm() {
558 $fields = [];
559 $count = 0;
560
561 // Allow subscribers to manipulate the list of watched pages (or use it
562 // to preload lots of details at once)
563 $watchlistInfo = $this->getWatchlistInfo();
564 Hooks::run(
565 'WatchlistEditorBeforeFormRender',
566 [ &$watchlistInfo ]
567 );
568
569 foreach ( $watchlistInfo as $namespace => $pages ) {
570 $options = [];
571
572 foreach ( array_keys( $pages ) as $dbkey ) {
573 $title = Title::makeTitleSafe( $namespace, $dbkey );
574
575 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
576 $text = $this->buildRemoveLine( $title );
577 $options[$text] = $title->getPrefixedText();
578 $count++;
579 }
580 }
581
582 // checkTitle can filter some options out, avoid empty sections
583 if ( count( $options ) > 0 ) {
584 $fields['TitlesNs' . $namespace] = [
585 'class' => EditWatchlistCheckboxSeriesField::class,
586 'options' => $options,
587 'section' => "ns$namespace",
588 ];
589 }
590 }
591 $this->cleanupWatchlist();
592
593 if ( count( $fields ) > 1 && $count > 30 ) {
594 $this->toc = Linker::tocIndent();
595 $tocLength = 0;
596 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
597
598 foreach ( $fields as $data ) {
599 # strip out the 'ns' prefix from the section name:
600 $ns = substr( $data['section'], 2 );
601
602 $nsText = ( $ns == NS_MAIN )
603 ? $this->msg( 'blanknamespace' )->escaped()
604 : htmlspecialchars( $contLang->getFormattedNsText( $ns ) );
605 $this->toc .= Linker::tocLine( "editwatchlist-{$data['section']}", $nsText,
606 $this->getLanguage()->formatNum( ++$tocLength ), 1 ) . Linker::tocLineEnd();
607 }
608
609 $this->toc = Linker::tocList( $this->toc );
610 } else {
611 $this->toc = false;
612 }
613
614 $context = new DerivativeContext( $this->getContext() );
615 $context->setTitle( $this->getPageTitle() ); // Remove subpage
616 $form = new EditWatchlistNormalHTMLForm( $fields, $context );
617 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
618 $form->setSubmitDestructive();
619 # Used message keys:
620 # 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
621 $form->setSubmitTooltip( 'watchlistedit-normal-submit' );
622 $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
623 $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() );
624 $form->setSubmitCallback( [ $this, 'submitNormal' ] );
625
626 return $form;
627 }
628
629 /**
630 * Build the label for a checkbox, with a link to the title, and various additional bits
631 *
632 * @param Title $title
633 * @return string
634 */
635 private function buildRemoveLine( $title ) {
636 $linkRenderer = $this->getLinkRenderer();
637 $link = $linkRenderer->makeLink( $title );
638
639 $tools['talk'] = $linkRenderer->makeLink(
640 $title->getTalkPage(),
641 $this->msg( 'talkpagelinktext' )->text()
642 );
643
644 if ( $title->exists() ) {
645 $tools['history'] = $linkRenderer->makeKnownLink(
646 $title,
647 $this->msg( 'history_small' )->text(),
648 [],
649 [ 'action' => 'history' ]
650 );
651 }
652
653 if ( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
654 $tools['contributions'] = $linkRenderer->makeKnownLink(
655 SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
656 $this->msg( 'contribslink' )->text()
657 );
658 }
659
660 Hooks::run(
661 'WatchlistEditorBuildRemoveLine',
662 [ &$tools, $title, $title->isRedirect(), $this->getSkin(), &$link ]
663 );
664
665 if ( $title->isRedirect() ) {
666 // Linker already makes class mw-redirect, so this is redundant
667 $link = '<span class="watchlistredir">' . $link . '</span>';
668 }
669
670 return $link . ' ' .
671 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList( $tools ) )->escaped();
672 }
673
674 /**
675 * Get a form for editing the watchlist in "raw" mode
676 *
677 * @return HTMLForm
678 */
679 protected function getRawForm() {
680 $titles = implode( "\n", $this->getWatchlist() );
681 $fields = [
682 'Titles' => [
683 'type' => 'textarea',
684 'label-message' => 'watchlistedit-raw-titles',
685 'default' => $titles,
686 ],
687 ];
688 $context = new DerivativeContext( $this->getContext() );
689 $context->setTitle( $this->getPageTitle( 'raw' ) ); // Reset subpage
690 $form = new OOUIHTMLForm( $fields, $context );
691 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
692 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
693 $form->setSubmitTooltip( 'watchlistedit-raw-submit' );
694 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
695 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
696 $form->setSubmitCallback( [ $this, 'submitRaw' ] );
697
698 return $form;
699 }
700
701 /**
702 * Get a form for clearing the watchlist
703 *
704 * @return HTMLForm
705 */
706 protected function getClearForm() {
707 $context = new DerivativeContext( $this->getContext() );
708 $context->setTitle( $this->getPageTitle( 'clear' ) ); // Reset subpage
709 $form = new OOUIHTMLForm( [], $context );
710 $form->setSubmitTextMsg( 'watchlistedit-clear-submit' );
711 # Used message keys: 'accesskey-watchlistedit-clear-submit', 'tooltip-watchlistedit-clear-submit'
712 $form->setSubmitTooltip( 'watchlistedit-clear-submit' );
713 $form->setWrapperLegendMsg( 'watchlistedit-clear-legend' );
714 $form->addHeaderText( $this->msg( 'watchlistedit-clear-explain' )->parse() );
715 $form->setSubmitCallback( [ $this, 'submitClear' ] );
716 $form->setSubmitDestructive();
717
718 return $form;
719 }
720
721 /**
722 * Determine whether we are editing the watchlist, and if so, what
723 * kind of editing operation
724 *
725 * @param WebRequest $request
726 * @param string $par
727 * @return int
728 */
729 public static function getMode( $request, $par ) {
730 $mode = strtolower( $request->getVal( 'action', $par ) );
731
732 switch ( $mode ) {
733 case 'clear':
734 case self::EDIT_CLEAR:
735 return self::EDIT_CLEAR;
736 case 'raw':
737 case self::EDIT_RAW:
738 return self::EDIT_RAW;
739 case 'edit':
740 case self::EDIT_NORMAL:
741 return self::EDIT_NORMAL;
742 default:
743 return false;
744 }
745 }
746
747 /**
748 * Build a set of links for convenient navigation
749 * between watchlist viewing and editing modes
750 *
751 * @param Language $lang
752 * @param LinkRenderer|null $linkRenderer
753 * @return string
754 */
755 public static function buildTools( $lang, LinkRenderer $linkRenderer = null ) {
756 if ( !$lang instanceof Language ) {
757 // back-compat where the first parameter was $unused
758 global $wgLang;
759 $lang = $wgLang;
760 }
761 if ( !$linkRenderer ) {
762 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
763 }
764
765 $tools = [];
766 $modes = [
767 'view' => [ 'Watchlist', false ],
768 'edit' => [ 'EditWatchlist', false ],
769 'raw' => [ 'EditWatchlist', 'raw' ],
770 'clear' => [ 'EditWatchlist', 'clear' ],
771 ];
772
773 foreach ( $modes as $mode => $arr ) {
774 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
775 $tools[] = $linkRenderer->makeKnownLink(
776 SpecialPage::getTitleFor( $arr[0], $arr[1] ),
777 wfMessage( "watchlisttools-{$mode}" )->text()
778 );
779 }
780
781 return Html::rawElement(
782 'span',
783 [ 'class' => 'mw-watchlist-toollinks' ],
784 wfMessage( 'parentheses' )->rawParams( $lang->pipeList( $tools ) )->escaped()
785 );
786 }
787 }