Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.00% |
22 / 25 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ImportSuccessSnippet | |
88.00% |
22 / 25 |
|
33.33% |
1 / 3 |
8.11 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRedirectWithNotice | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getHtml | |
95.45% |
21 / 22 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace FileImporter\Html; |
| 4 | |
| 5 | use FileImporter\Services\SuccessCache; |
| 6 | use MediaWiki\Html\Html; |
| 7 | use MediaWiki\MediaWikiServices; |
| 8 | use MediaWiki\Title\Title; |
| 9 | use MediaWiki\User\UserIdentity; |
| 10 | use MessageLocalizer; |
| 11 | use OOUI\HtmlSnippet; |
| 12 | use OOUI\MessageWidget; |
| 13 | use StatusValue; |
| 14 | use Wikimedia\Message\MessageSpecifier; |
| 15 | |
| 16 | /** |
| 17 | * Informational block embedded at the top of page after a successful import. |
| 18 | * |
| 19 | * @license GPL-2.0-or-later |
| 20 | * @author Addshore |
| 21 | */ |
| 22 | class ImportSuccessSnippet { |
| 23 | |
| 24 | public const NOTICE_URL_KEY = 'fileImporterSuccess'; |
| 25 | |
| 26 | private SuccessCache $cache; |
| 27 | |
| 28 | public function __construct() { |
| 29 | $this->cache = MediaWikiServices::getInstance()->getService( 'FileImporterSuccessCache' ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Prepares an URL for redirect and stashes additional information for retrieval from that page. |
| 34 | * |
| 35 | * @return string Target file URL for redirect, including special parameter to show our notice. |
| 36 | */ |
| 37 | public function getRedirectWithNotice( Title $targetTitle, UserIdentity $user, StatusValue $importResult ) { |
| 38 | $this->cache->stashImportResult( $targetTitle, $user, $importResult ); |
| 39 | return $targetTitle->getInternalURL( [ self::NOTICE_URL_KEY => 1 ] ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param MessageLocalizer $messageLocalizer |
| 44 | * @param Title $targetTitle Final local title of imported file |
| 45 | * @param UserIdentity $user |
| 46 | */ |
| 47 | public function getHtml( MessageLocalizer $messageLocalizer, Title $targetTitle, UserIdentity $user ): string { |
| 48 | $importResult = $this->cache->fetchImportResult( $targetTitle, $user ); |
| 49 | // This can happen when the user reloads a URL that still contains fileImporterSuccess=1 |
| 50 | if ( !$importResult ) { |
| 51 | return ''; |
| 52 | } |
| 53 | |
| 54 | $html = ''; |
| 55 | |
| 56 | /** @var string|array|MessageSpecifier|null $spec */ |
| 57 | $spec = $importResult->getValue(); |
| 58 | if ( $spec ) { |
| 59 | // This reimplements Message::newFromSpecifier, but that wouldn't allow us to reuse the |
| 60 | // Language from the provided MessageLocalizer. |
| 61 | $msg = $messageLocalizer->msg( ...is_array( $spec ) ? $spec : [ $spec ] ); |
| 62 | $html .= new MessageWidget( [ |
| 63 | 'label' => new HtmlSnippet( $msg->parse() ), |
| 64 | 'type' => 'success', |
| 65 | ] ); |
| 66 | } |
| 67 | foreach ( $importResult->getMessages( 'error' ) as $msg ) { |
| 68 | $html .= new MessageWidget( [ |
| 69 | 'label' => new HtmlSnippet( $messageLocalizer->msg( $msg )->parse() ), |
| 70 | 'type' => 'error' |
| 71 | ] ); |
| 72 | } |
| 73 | foreach ( $importResult->getMessages( 'warning' ) as $msg ) { |
| 74 | $html .= new MessageWidget( [ |
| 75 | 'label' => new HtmlSnippet( $messageLocalizer->msg( $msg )->parse() ), |
| 76 | 'type' => 'warning', |
| 77 | ] ); |
| 78 | } |
| 79 | |
| 80 | return Html::rawElement( 'div', [ 'class' => 'mw-ext-fileimporter-noticebox' ], $html ); |
| 81 | } |
| 82 | |
| 83 | } |