Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
TransformHtmlToWikitextRevisionHandler
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 3
12
0.00% covered (danger)
0.00%
0 / 1
 getParamSettings
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
2
 getRequestBodySchema
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
2
 getRequestBodyDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Rest\Handler;
4
5use MediaWiki\Rest\Handler;
6use Wikimedia\Message\MessageValue;
7use Wikimedia\ParamValidator\ParamValidator;
8
9/**
10 * Handler for transforming content given in the request.
11 *
12 * This class primarily deals with parameter validation and OpenAPI spec generation.
13 * Everything else is handled by the base class.
14 *
15 * TODO: reconsider the ParsoidHandler => TransformHandler hierarchy. Consider consolidating
16 *    classes and moving more validation into functions like getParamSettings(),
17 *    getBodyParamSettings(), etc.
18 *
19 * @unstable Pending consolidation of the Parsoid extension with core code.
20 */
21class TransformHtmlToWikitextRevisionHandler extends TransformHandler {
22
23    /** @inheritDoc */
24    public function getParamSettings() {
25        return [
26            'title' => [
27                self::PARAM_SOURCE => 'path',
28                ParamValidator::PARAM_TYPE => 'string',
29                ParamValidator::PARAM_REQUIRED => true,
30                Handler::PARAM_DESCRIPTION => new MessageValue(
31                        'rest-param-desc-transform-html-to-wikitext-title-revision-title'
32                ),
33                Handler::PARAM_EXAMPLE => 'Main_Page',
34            ],
35            'revision' => [
36                self::PARAM_SOURCE => 'path',
37                ParamValidator::PARAM_TYPE => 'string',
38                ParamValidator::PARAM_REQUIRED => true,
39                Handler::PARAM_DESCRIPTION => new MessageValue(
40                        'rest-param-desc-transform-html-to-wikitext-title-revision-revision'
41                ),
42                Handler::PARAM_EXAMPLE => '1',
43            ],
44        ];
45    }
46
47    /**
48     * The parent TransformHandler class accepts a variety of body parameters in different
49     * situations, some of which can accept values in multiple types and therefore cannot
50     * be described in getBodyParamSettings(). See TransformHandler::execute(), and notice
51     * that both "wikitext" and "html" can be either a string or an object.
52     *
53     * We therefore define a request body schema directly rather than the more common approach of
54     * generating it from definitions in getBodyParamSettings().
55     */
56    protected function getRequestBodySchema( string $mediaType ): array {
57        return [
58            'oneOf' => [
59                [
60                    'type' => 'object',
61                    'properties' => [
62                        'html' => [
63                            'x-i18n-description' => 'rest-property-desc-transform-html',
64                            'example' => '<h2>Hello world</h2>',
65                            'type' => 'string',
66                        ]
67                    ]
68                ],
69                [
70                    'type' => 'object',
71                    'properties' => [
72                        'html' => [
73                            'x-i18n-description' => 'rest-property-desc-transform-html-with-headers',
74                            'example' => [ 'body' => '<h2>Hello world</h2>' ],
75                            'type' => 'object',
76                            'properties' => [
77                                'headers' => [
78                                    'x-i18n-description' => 'rest-property-desc-transform-html-headers',
79                                    'example' => [ 'content-language' => 'en' ],
80                                    'type' => 'object',
81                                ],
82                                'body' => [
83                                    'x-i18n-description' => 'rest-property-desc-transform-html-body',
84                                    'example' => '<h2>Hello world</h2>',
85                                    'type' => 'string',
86                                ]
87                            ]
88                        ]
89                    ]
90                ]
91            ],
92        ];
93    }
94
95    public function getRequestBodyDescription(): MessageValue|string|null {
96        return new MessageValue( 'rest-requestbody-desc-transform-html' );
97    }
98}