Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ImportIdentityFormSnippet
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 newFromImportPlan
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHtml
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace FileImporter\Html;
4
5use FileImporter\Data\ImportPlan;
6use MediaWiki\Html\Html;
7
8/**
9 * Collection of input elements that are used to persist the request from page load to page load.
10 *
11 * @license GPL-2.0-or-later
12 * @author Addshore
13 */
14class ImportIdentityFormSnippet {
15
16    private const IDENTITY_KEYS = [
17        'clientUrl',
18        'intendedFileName',
19        'intendedRevisionSummary',
20        'intendedWikitext',
21        'actionStats',
22        'validationWarnings',
23        'importDetailsHash',
24        'automateSourceWikiCleanup',
25        'automateSourceWikiDelete'
26    ];
27
28    /**
29     * @param ImportPlan $importPlan
30     * @param string[] $exclude Field names to exclude from the identity
31     * @return self
32     */
33    public static function newFromImportPlan( ImportPlan $importPlan, array $exclude = [] ): self {
34        return new self( array_diff_key( [
35            'clientUrl' => $importPlan->getRequest()->getUrl(),
36            'intendedFileName' => $importPlan->getFileName(),
37            'intendedRevisionSummary' => $importPlan->getRequest()->getIntendedSummary(),
38            'intendedWikitext' => $importPlan->getFileInfoText(),
39            'actionStats' => json_encode( $importPlan->getActionStats() ),
40            'validationWarnings' => json_encode( $importPlan->getValidationWarnings() ),
41            'importDetailsHash' => $importPlan->getRequest()->getImportDetailsHash(),
42            'automateSourceWikiCleanup' => $importPlan->getAutomateSourceWikiCleanUp(),
43            'automateSourceWikiDelete' => $importPlan->getAutomateSourceWikiDelete(),
44        ], array_flip( $exclude ) ) );
45    }
46
47    /**
48     * @param array $identityParts Keys:
49     *     - clientUrl, as initial input by the user
50     *     - intendedFileName, either generated from the client URL or passed by the user
51     *     - importDetailsHash, generated from the first import request, to ensure we know what
52     *                          we are importing
53     */
54    public function __construct(
55        private readonly array $identityParts,
56    ) {
57    }
58
59    public function getHtml(): string {
60        $html = '';
61
62        foreach ( self::IDENTITY_KEYS as $identityKey ) {
63            if ( array_key_exists( $identityKey, $this->identityParts ) ) {
64                $html .= Html::hidden( $identityKey, $this->identityParts[$identityKey] );
65            }
66        }
67
68        return $html;
69    }
70
71}