Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.10% covered (warning)
88.10%
37 / 42
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiSanitizeMapData
88.10% covered (warning)
88.10%
37 / 42
57.14% covered (warning)
57.14%
4 / 7
9.14
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 sanitizeJson
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
2
 getAllowedParams
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license MIT
4 * @file
5 *
6 * @author Yuri Astrakhan
7 * @author Max Semenik
8 */
9
10namespace Kartographer\Api;
11
12use Kartographer\MediaWikiWikitextParser;
13use Kartographer\SimpleStyleParser;
14use MediaWiki\Api\ApiBase;
15use MediaWiki\Api\ApiMain;
16use MediaWiki\Json\FormatJson;
17use MediaWiki\Parser\Parser;
18use MediaWiki\Parser\ParserFactory;
19use MediaWiki\Parser\ParserOptions;
20use MediaWiki\Status\Status;
21use MediaWiki\Title\Title;
22use Wikimedia\ParamValidator\ParamValidator;
23
24/**
25 * This class implements action=sanitize-mapdata API, validating and sanitizing user-entered
26 * GeoJSON.
27 *
28 * @license MIT
29 */
30class ApiSanitizeMapData extends ApiBase {
31
32    public function __construct(
33        ApiMain $main,
34        string $action,
35        private readonly ParserFactory $parserFactory,
36    ) {
37        parent::__construct( $main, $action );
38    }
39
40    /** @inheritDoc */
41    public function execute() {
42        $params = $this->extractRequestParams();
43
44        $title = Title::newFromText( $params['title'] );
45
46        if ( !$title ) {
47            $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
48        }
49
50        $this->checkTitleUserPermissions( $title, 'read' );
51
52        $this->sanitizeJson( $title, $params['text'] );
53    }
54
55    private function sanitizeJson( Title $title, string $text ): void {
56        $parserOptions = new ParserOptions( $this->getUser() );
57        $parser = $this->parserFactory->getInstance();
58        $parser->startExternalParse( $title, $parserOptions, Parser::OT_HTML );
59        $parser->setPage( $title );
60        $simpleStyle = new SimpleStyleParser(
61            new MediaWikiWikitextParser( $parser ),
62            $this->getConfig(),
63            [ 'saveUnparsed' => true ]
64        );
65        $status = $simpleStyle->parse( $text );
66        if ( !$status->isOK() ) {
67            $error = Status::wrap( $status )->getHTML( false, false, $this->getLanguage() );
68            $this->getResult()->addValue( null, $this->getModuleName(), [ 'error' => $error ] );
69        } else {
70            $data = $status->getValue()['data'];
71            SimpleStyleParser::updateMarkerSymbolCounters( $data );
72            $this->getResult()
73                ->addValue( null,
74                    $this->getModuleName(),
75                    [ 'sanitized' => FormatJson::encode( $data, false, FormatJson::ALL_OK ) ]
76                );
77        }
78    }
79
80    /** @inheritDoc */
81    public function getAllowedParams() {
82        return [
83            'title' => [
84                ParamValidator::PARAM_TYPE => 'string',
85                ParamValidator::PARAM_DEFAULT => 'Dummy title (called from ' . __CLASS__ . ')',
86            ],
87            'text' => [
88                ParamValidator::PARAM_TYPE => 'text',
89                ParamValidator::PARAM_REQUIRED => true,
90            ]
91        ];
92    }
93
94    /** @inheritDoc */
95    public function mustBePosted() {
96        return true;
97    }
98
99    /** @inheritDoc */
100    protected function getExamplesMessages() {
101        return [
102            'action=sanitize-mapdata&text={"foo":"bar"}' => 'apihelp-sanitize-mapdata-example',
103        ];
104    }
105
106    /**
107     * Indicate that this API can change at any time
108     * @return bool
109     */
110    public function isInternal() {
111        return true;
112    }
113}