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