Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ImportIdentityFormSnippet
100.00% covered (success)
100.00%
24 / 24
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%
12 / 12
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 array $identityParts;
17
18    private const IDENTITY_KEYS = [
19        'clientUrl',
20        'intendedFileName',
21        'intendedRevisionSummary',
22        'intendedWikitext',
23        'actionStats',
24        'validationWarnings',
25        'importDetailsHash',
26        'automateSourceWikiCleanup',
27        'automateSourceWikiDelete'
28    ];
29
30    /**
31     * @param ImportPlan $importPlan
32     * @param string[] $exclude Field names to exclude from the identity
33     * @return self
34     */
35    public static function newFromImportPlan( ImportPlan $importPlan, array $exclude = [] ): self {
36        return new self( array_diff_key( [
37            'clientUrl' => $importPlan->getRequest()->getUrl(),
38            'intendedFileName' => $importPlan->getFileName(),
39            'intendedRevisionSummary' => $importPlan->getRequest()->getIntendedSummary(),
40            'intendedWikitext' => $importPlan->getFileInfoText(),
41            'actionStats' => json_encode( $importPlan->getActionStats() ),
42            'validationWarnings' => json_encode( $importPlan->getValidationWarnings() ),
43            'importDetailsHash' => $importPlan->getRequest()->getImportDetailsHash(),
44            'automateSourceWikiCleanup' => $importPlan->getAutomateSourceWikiCleanUp(),
45            'automateSourceWikiDelete' => $importPlan->getAutomateSourceWikiDelete(),
46        ], array_flip( $exclude ) ) );
47    }
48
49    /**
50     * @param array $identityParts Keys:
51     *     - clientUrl, as initial input by the user
52     *     - intendedFileName, either generated from the client URL or passed by the user
53     *     - importDetailsHash, generated from the first import request, to ensure we know what
54     *                          we are importing
55     */
56    public function __construct( array $identityParts ) {
57        $this->identityParts = $identityParts;
58    }
59
60    public function getHtml(): string {
61        $html = '';
62
63        foreach ( self::IDENTITY_KEYS as $identityKey ) {
64            if ( array_key_exists( $identityKey, $this->identityParts ) ) {
65                $html .= Html::element(
66                    'input',
67                    [
68                        'type' => 'hidden',
69                        'name' => $identityKey,
70                        'value' => $this->identityParts[$identityKey],
71                    ]
72                );
73            }
74        }
75
76        return $html;
77    }
78
79}