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