Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
CRAP
40.62% covered (danger)
40.62%
13 / 32
ValidatingUploadBase
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
42.14
40.62% covered (danger)
40.62%
13 / 32
 __construct
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 validateTitle
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 validateFile
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 validateUpload
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 19
 initializeFromRequest
n/a
0 / 0
1
n/a
0 / 0
 getSourceType
n/a
0 / 0
1
n/a
0 / 0
<?php
namespace FileImporter\Services\UploadBase;
use FileImporter\Data\TextRevision;
use Hooks;
use MediaWiki\Linker\LinkTarget;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Status;
use UploadBase;
use User;
use WebRequest;
/**
 * This class extends the MediaWiki UploadBase class in order to perform validation
 * that is normally carried out as part of the upload process.
 * Ideally MediaWiki core would be refactored so that this could more easily be accessed.
 *
 * @license GPL-2.0-or-later
 * @author Addshore
 */
class ValidatingUploadBase extends UploadBase {
    /**
     * @var LoggerInterface
     */
    private $logger;
    /**
     * @param LinkTarget $targetTitle
     * @param string $tempPath
     * @param LoggerInterface|null $logger
     */
    public function __construct(
        LinkTarget $targetTitle,
        $tempPath,
        LoggerInterface $logger = null
    ) {
        $this->initializePathInfo(
            $targetTitle->getText(),
            $tempPath,
            null,
            false
        );
        $this->logger = $logger ?: new NullLogger();
    }
    /**
     * @return int 0 if valid, a non-zero error code from UploadBase::getTitle() if not
     */
    public function validateTitle(): int {
        return $this->getTitle() ? UploadBase::OK : $this->mTitleError;
    }
    /**
     * @return Status
     */
    public function validateFile() {
        $fileVerification = $this->verifyFile();
        if ( $fileVerification !== true ) {
            $this->logger->info(
                __METHOD__ . ' checks failed', [ 'fileVerification' => $fileVerification ]
            );
            return Status::newFatal( ...$fileVerification );
        }
        return Status::newGood();
    }
    /**
     * @param User $user user performing the import
     * @param TextRevision|null $textRevision optional text revision to validate the upload with
     *
     * @return Status
     */
    public function validateUpload( User $user, TextRevision $textRevision = null ) {
        $error = null;
        if ( !$textRevision ) {
            Hooks::run( 'UploadStashFile', [
                $this,
                $user,
                $this->mFileProps,
                &$error
            ] );
        } else {
            Hooks::run( 'UploadVerifyUpload', [
                $this,
                $user,
                $this->mFileProps,
                $textRevision->getField( 'comment' ),
                $textRevision->getField( '*' ),
                &$error
            ] );
        }
        // @phan-suppress-next-line PhanImpossibleCondition May set by hook
        if ( $error ) {
            if ( !is_array( $error ) ) {
                $error = [ $error ];
            }
            return Status::newFatal( ...$error );
        }
        return Status::newGood();
    }
    /**
     * Should never be used but must be implemented for UploadBase
     *
     * @param WebRequest &$request
     * @codeCoverageIgnore
     */
    public function initializeFromRequest( &$request ) {
        // Should never be called
    }
    /**
     * @inheritDoc
     * @codeCoverageIgnore
     */
    public function getSourceType() {
        return 'file';
    }
}