Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
FileTextRevisionValidator
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace FileImporter\Services;
4
5use FileImporter\HookRunner;
6use MediaWiki\Content\Content;
7use MediaWiki\Context\DerivativeContext;
8use MediaWiki\Context\RequestContext;
9use MediaWiki\MediaWikiServices;
10use MediaWiki\Request\FauxRequest;
11use MediaWiki\Status\Status;
12use MediaWiki\Title\Title;
13use MediaWiki\User\User;
14use StatusValue;
15
16/**
17 * Class that can be used to validate the content of a text revision
18 *
19 * @license GPL-2.0-or-later
20 * @author Christoph Jauera <christoph.jauera@wikimedia.de>
21 */
22class FileTextRevisionValidator {
23
24    private DerivativeContext $context;
25
26    public function __construct() {
27        $this->context = new DerivativeContext( RequestContext::getMain() );
28        $this->context->setRequest( new FauxRequest() );
29    }
30
31    /**
32     * @param Title $title
33     * @param User $user
34     * @param Content $content
35     * @param string $summary
36     * @param bool $minor
37     *
38     * @return StatusValue isOK when validation succeeds
39     */
40    public function validate(
41        Title $title,
42        User $user,
43        Content $content,
44        string $summary,
45        $minor
46    ): StatusValue {
47        if ( !$title->inNamespace( NS_FILE ) ) {
48            return StatusValue::newFatal( 'fileimporter-badnamespace' );
49        }
50
51        $status = Status::newGood();
52        $this->context->setUser( $user );
53        $this->context->setTitle( $title );
54
55        ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onEditFilterMergedContent(
56            $this->context,
57            $content,
58            $status,
59            $summary,
60            $user,
61            $minor
62        );
63
64        return $status;
65    }
66
67}