Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.54% covered (danger)
40.54%
15 / 37
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ValidatingUploadBase
40.54% covered (danger)
40.54%
15 / 37
75.00% covered (warning)
75.00%
3 / 4
36.44
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
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    }
46
47    /**
48     * @return int 0 if valid, a non-zero error code from UploadBase::getTitle() if not
49     */
50    public function validateTitle(): int {
51        return $this->getTitle() ? UploadBase::OK : $this->mTitleError;
52    }
53
54    /**
55     * @return StatusValue
56     */
57    public function validateFile() {
58        $fileVerification = $this->verifyFile();
59
60        if ( $fileVerification !== true ) {
61            $this->logger->info(
62                __METHOD__ . ' checks failed', [ 'fileVerification' => $fileVerification ]
63            );
64            // @phan-suppress-next-line PhanParamTooFewUnpack
65            return StatusValue::newFatal( ...$fileVerification );
66        }
67
68        return StatusValue::newGood();
69    }
70
71    /**
72     * @param User $user user performing the import
73     * @param TextRevision|null $textRevision optional text revision to validate the upload with
74     *
75     * @return StatusValue
76     */
77    public function validateUpload( User $user, TextRevision $textRevision = null ) {
78        $error = null;
79        $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
80
81        if ( !$textRevision ) {
82            $hookRunner->onUploadStashFile(
83                $this,
84                $user,
85                $this->mFileProps,
86                $error
87            );
88        } else {
89            $hookRunner->onUploadVerifyUpload(
90                $this,
91                $user,
92                $this->mFileProps,
93                $textRevision->getField( 'comment' ),
94                $textRevision->getContent(),
95                $error
96            );
97        }
98
99        if ( $error ) {
100            if ( !is_array( $error ) ) {
101                $error = [ $error ];
102            }
103            return StatusValue::newFatal( ...$error );
104        }
105
106        return StatusValue::newGood();
107    }
108
109    /**
110     * Should never be used but must be implemented for UploadBase
111     *
112     * @param WebRequest &$request
113     * @codeCoverageIgnore
114     */
115    public function initializeFromRequest( &$request ) {
116        // Should never be called
117    }
118
119    /**
120     * @inheritDoc
121     * @codeCoverageIgnore
122     */
123    public function getSourceType() {
124        return 'file';
125    }
126
127}