Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
18 / 21
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImportSuccessSnippet
85.71% covered (warning)
85.71%
18 / 21
33.33% covered (danger)
33.33%
1 / 3
8.19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRedirectWithNotice
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getHtml
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
6.01
1<?php
2
3namespace FileImporter\Html;
4
5use FileImporter\Services\SuccessCache;
6use MediaWiki\Html\Html;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\Title\Title;
9use MediaWiki\User\UserIdentity;
10use MessageLocalizer;
11use MessageSpecifier;
12use OOUI\HtmlSnippet;
13use OOUI\MessageWidget;
14use StatusValue;
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 */
22class 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     * @return string
48     */
49    public function getHtml( MessageLocalizer $messageLocalizer, Title $targetTitle, UserIdentity $user ) {
50        $importResult = $this->cache->fetchImportResult( $targetTitle, $user );
51        // This can happen when the user reloads a URL that still contains fileImporterSuccess=1
52        if ( !$importResult ) {
53            return '';
54        }
55
56        $html = '';
57
58        /** @var string|array|MessageSpecifier|null $spec */
59        $spec = $importResult->getValue();
60        if ( $spec ) {
61            // This reimplements Message::newFromSpecifier, but that wouldn't allow us to reuse the
62            // Language from the provided MessageLocalizer.
63            $msg = $messageLocalizer->msg( ...is_array( $spec ) ? $spec : [ $spec ] );
64            $html .= new MessageWidget( [
65                'label' => new HtmlSnippet( $msg->parse() ),
66                'type' => 'success',
67            ] );
68        }
69
70        foreach ( $importResult->getErrors() as $error ) {
71            $msg = $messageLocalizer->msg( $error['message'], $error['params'] ?? [] );
72            $html .= new MessageWidget( [
73                'label' => new HtmlSnippet( $msg->parse() ),
74                'type' => $error['type'] === 'error' ? 'error' : 'warning',
75            ] );
76        }
77
78        return Html::rawElement( 'div', [ 'class' => 'mw-ext-fileimporter-noticebox' ], $html );
79    }
80
81}