Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
24 / 30
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
TextRevisionFromTextRevision
80.00% covered (warning)
80.00%
24 / 30
60.00% covered (warning)
60.00%
3 / 5
7.39
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
 prepare
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 validate
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
2.01
 commit
37.50% covered (danger)
37.50%
3 / 8
0.00% covered (danger)
0.00%
0 / 1
2.98
 getWikiRevision
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace FileImporter\Operations;
4
5use FileImporter\Data\TextRevision;
6use FileImporter\Interfaces\ImportOperation;
7use FileImporter\Services\FileTextRevisionValidator;
8use FileImporter\Services\WikiRevisionFactory;
9use MediaWiki\Permissions\RestrictionStore;
10use MediaWiki\Title\Title;
11use MediaWiki\User\User;
12use OldRevisionImporter;
13use Psr\Log\LoggerInterface;
14use Psr\Log\NullLogger;
15use StatusValue;
16use WikiRevision;
17
18/**
19 * @license GPL-2.0-or-later
20 * @author Addshore
21 */
22class TextRevisionFromTextRevision implements ImportOperation {
23
24    private Title $plannedTitle;
25    /** @var User user performing the import */
26    private User $user;
27    private TextRevision $textRevision;
28    private WikiRevisionFactory $wikiRevisionFactory;
29    /** @var WikiRevision|null */
30    private $wikiRevision;
31    private OldRevisionImporter $importer;
32    private FileTextRevisionValidator $textRevisionValidator;
33    private RestrictionStore $restrictionStore;
34    private LoggerInterface $logger;
35
36    public function __construct(
37        Title $plannedTitle,
38        User $user,
39        TextRevision $textRevision,
40        WikiRevisionFactory $wikiRevisionFactory,
41        OldRevisionImporter $importer,
42        FileTextRevisionValidator $textRevisionValidator,
43        RestrictionStore $restrictionStore,
44        ?LoggerInterface $logger = null
45    ) {
46        $this->plannedTitle = $plannedTitle;
47        $this->user = $user;
48        $this->textRevision = $textRevision;
49        $this->wikiRevisionFactory = $wikiRevisionFactory;
50        $this->importer = $importer;
51        $this->textRevisionValidator = $textRevisionValidator;
52        $this->restrictionStore = $restrictionStore;
53        $this->logger = $logger ?? new NullLogger();
54    }
55
56    /**
57     * Method to prepare an operation. This will not commit anything to any persistent storage.
58     * @return StatusValue isOK on success
59     */
60    public function prepare(): StatusValue {
61        $wikiRevision = $this->wikiRevisionFactory->newFromTextRevision( $this->textRevision );
62        $wikiRevision->setTitle( $this->plannedTitle );
63
64        $this->wikiRevision = $wikiRevision;
65
66        return StatusValue::newGood();
67    }
68
69    /**
70     * Method to validate prepared data that should be committed.
71     * @return StatusValue isOK on success
72     */
73    public function validate(): StatusValue {
74        // Even administrators should not (accidentially) move a file to a protected file name
75        if ( $this->restrictionStore->isProtected( $this->plannedTitle ) ) {
76            return StatusValue::newFatal( 'fileimporter-filenameerror-protected' );
77        }
78
79        return $this->textRevisionValidator->validate(
80            $this->plannedTitle,
81            $this->user,
82            $this->wikiRevision->getContent(),
83            $this->wikiRevision->getComment(),
84            $this->wikiRevision->getMinor()
85        );
86    }
87
88    /**
89     * Commit this operation to persistent storage.
90     * @return StatusValue isOK on success
91     */
92    public function commit(): StatusValue {
93        $result = $this->importer->import( $this->wikiRevision );
94
95        if ( $result ) {
96            return StatusValue::newGood();
97        } else {
98            $this->logger->error(
99                __METHOD__ . ' failed to commit.',
100                [ 'textRevision-getFields' => $this->textRevision->getFields() ]
101            );
102            return StatusValue::newFatal( 'fileimporter-importfailed' );
103        }
104    }
105
106    /**
107     * @return WikiRevision|null
108     */
109    public function getWikiRevision() {
110        return $this->wikiRevision;
111    }
112
113}