Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
42.11% covered (danger)
42.11%
16 / 38
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ValidatingUploadBase
42.11% covered (danger)
42.11%
16 / 38
75.00% covered (warning)
75.00%
3 / 4
34.48
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 validateTitle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 validateFile
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 validateUpload
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
20
 initializeFromRequest
n/a
0 / 0
n/a
0 / 0
1
 getSourceType
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3namespace FileImporter\Services\UploadBase;
4
5use FileImporter\Data\TextRevision;
6use FileImporter\HookRunner;
7use MediaWiki\Linker\LinkTarget;
8use MediaWiki\MediaWikiServices;
9use MediaWiki\Request\WebRequest;
10use MediaWiki\User\User;
11use Psr\Log\LoggerInterface;
12use Psr\Log\NullLogger;
13use StatusValue;
14use UploadBase;
15
16/**
17 * This class extends the MediaWiki UploadBase class in order to perform validation
18 * that is normally carried out as part of the upload process.
19 * Ideally MediaWiki core would be refactored so that this could more easily be accessed.
20 *
21 * @license GPL-2.0-or-later
22 * @author Addshore
23 */
24class ValidatingUploadBase extends UploadBase {
25
26    private LoggerInterface $logger;
27
28    /**
29     * @param LinkTarget $targetTitle
30     * @param string $tempPath
31     * @param LoggerInterface|null $logger
32     */
33    public function __construct(
34        LinkTarget $targetTitle,
35        $tempPath,
36        ?LoggerInterface $logger = null
37    ) {
38        $this->initializePathInfo(
39            $targetTitle->getText(),
40            $tempPath,
41            null,
42            false
43        );
44        $this->logger = $logger ?? new NullLogger();
45        parent::__construct();
46    }
47
48    /**
49     * @return int 0 if valid, a non-zero error code from UploadBase::getTitle() if not
50     */
51    public function validateTitle(): int {
52        return $this->getTitle() ? UploadBase::OK : $this->mTitleError;
53    }
54
55    /**
56     * @return StatusValue
57     */
58    public function validateFile() {
59        $fileVerification = $this->verifyFile();
60
61        if ( $fileVerification !== true ) {
62            $this->logger->info(
63                __METHOD__ . ' checks failed', [ 'fileVerification' => $fileVerification ]
64            );
65            // @phan-suppress-next-line PhanParamTooFewUnpack
66            return StatusValue::newFatal( ...$fileVerification );
67        }
68
69        return StatusValue::newGood();
70    }
71
72    /**
73     * @param User $user user performing the import
74     * @param TextRevision|null $textRevision optional text revision to validate the upload with
75     *
76     * @return StatusValue
77     */
78    public function validateUpload( User $user, ?TextRevision $textRevision = null ) {
79        $error = null;
80        $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
81
82        if ( !$textRevision ) {
83            $hookRunner->onUploadStashFile(
84                $this,
85                $user,
86                $this->mFileProps,
87                $error
88            );
89        } else {
90            $hookRunner->onUploadVerifyUpload(
91                $this,
92                $user,
93                $this->mFileProps,
94                $textRevision->getField( 'comment' ),
95                $textRevision->getContent(),
96                $error
97            );
98        }
99
100        if ( $error ) {
101            if ( !is_array( $error ) ) {
102                $error = [ $error ];
103            }
104            return StatusValue::newFatal( ...$error );
105        }
106
107        return StatusValue::newGood();
108    }
109
110    /**
111     * Should never be used but must be implemented for UploadBase
112     *
113     * @param WebRequest &$request
114     * @codeCoverageIgnore
115     */
116    public function initializeFromRequest( &$request ) {
117        // Should never be called
118    }
119
120    /**
121     * @inheritDoc
122     * @codeCoverageIgnore
123     */
124    public function getSourceType() {
125        return 'file';
126    }
127
128}