Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiParserMigration
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
90
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\ParserMigration;
4
5use ApiBase;
6use ApiMain;
7use MediaWiki\Page\RedirectLookup;
8use MediaWiki\Revision\RevisionLookup;
9use MediaWiki\Revision\SlotRecord;
10use MediaWiki\Title\Title;
11use ParserOptions;
12use RuntimeException;
13use Wikimedia\ParamValidator\ParamValidator;
14
15class ApiParserMigration extends ApiBase {
16    /** @var string[] */
17    private static $configNames = [
18        0 => 'old',
19        1 => 'new',
20    ];
21
22    private RedirectLookup $redirectLookup;
23    private RevisionLookup $revisionLookup;
24
25    public function __construct(
26        ApiMain $main,
27        string $action,
28        RedirectLookup $redirectLookup,
29        RevisionLookup $revisionLookup
30    ) {
31        parent::__construct( $main, $action );
32        $this->redirectLookup = $redirectLookup;
33        $this->revisionLookup = $revisionLookup;
34    }
35
36    public function execute() {
37        $params = $this->extractRequestParams();
38
39        $title = $this->getTitleOrPageId( $params )->getTitle();
40        // Follow redirects by default. Redirect link output is not
41        // interesting for rendering diff comparisons. Provide clients
42        // the option to choose the redirect page via '&redirect=no'.
43        if ( $title->isRedirect() && (
44            !isset( $params['redirect'] ) || $params['redirect'] !== 'no'
45        ) ) {
46            $redirect = $this->redirectLookup->getRedirectTarget( $title );
47            $title = Title::castFromLinkTarget( $redirect ) ?? $title;
48        }
49        $revisionRecord = $this->revisionLookup->getRevisionByTitle( $title );
50        if ( !$revisionRecord ) {
51            $this->dieWithError( 'apierror-missingtitle' );
52        }
53        $content = $revisionRecord->getContent( SlotRecord::MAIN );
54        if ( !$content ) {
55            $this->dieWithError( [ 'apierror-missingcontent-pageid', $revisionRecord->getPageId() ] );
56        }
57
58        $configIndexesByName = array_flip( self::$configNames );
59        $configIndexes = [];
60        foreach ( $params['config'] as $configName ) {
61            if ( !isset( $configIndexesByName[$configName] ) ) {
62                throw new RuntimeException( 'Invalid config name, should have already been validated' );
63            }
64            $configIndexes[] = $configIndexesByName[$configName];
65        }
66
67        $mechanism = new Mechanism();
68        $user = $this->getUser();
69        $options = ParserOptions::newFromContext( $this->getContext() );
70        $outputs = $mechanism->parse( $content, $title, $options, $user, $configIndexes );
71
72        $result = $this->getResult();
73        foreach ( $configIndexes as $index ) {
74            $result->addValue( null, self::$configNames[$index], $outputs[$index]->getText() );
75        }
76    }
77
78    /** @inheritDoc */
79    public function isInternal() {
80        return true;
81    }
82
83    /** @inheritDoc */
84    public function getAllowedParams() {
85        return [
86            'title' => [
87                ParamValidator::PARAM_TYPE => 'string',
88                ParamValidator::PARAM_REQUIRED => true,
89            ],
90            'config' => [
91                ParamValidator::PARAM_TYPE => self::$configNames,
92                ParamValidator::PARAM_DEFAULT => 'old|new',
93                ParamValidator::PARAM_ISMULTI => true,
94                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
95            ],
96            'redirect' => [
97                ParamValidator::PARAM_TYPE => 'string',
98                ParamValidator::PARAM_REQUIRED => false,
99            ],
100        ];
101    }
102}