1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<?php
/**
* Copyright (C) 2011-2020 Wikimedia Foundation and others.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
namespace MediaWiki\Rest\Handler;
use MediaWiki\Rest\Handler\Helper\ParsoidFormatHelper;
use MediaWiki\Rest\HttpException;
use MediaWiki\Rest\Response;
use Wikimedia\ParamValidator\ParamValidator;
/**
* Handler for transforming content given in the request.
* - /v1/transform/{from}/to/{format}
* - /v1/transform/{from}/to/{format}/{title}
* - /v1/transform/{from}/to/{format}/{title}/{revision}
*
* @see https://www.mediawiki.org/wiki/Parsoid/API#POST
*/
class TransformHandler extends ParsoidHandler {
/** @inheritDoc */
public function getParamSettings() {
return [
'from' => [ self::PARAM_SOURCE => 'path',
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => true, ],
'format' => [ self::PARAM_SOURCE => 'path',
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => true, ],
'title' => [ self::PARAM_SOURCE => 'path',
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => false, ],
'revision' => [ self::PARAM_SOURCE => 'path',
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => false, ], ];
}
public function checkPreconditions() {
// NOTE: disable all precondition checks.
// If-(not)-Modified-Since is not supported by the /transform/ handler.
// If-None-Match is not supported by the /transform/ handler.
// If-Match for wt2html is handled in getRequestAttributes.
}
protected function &getRequestAttributes(): array {
$attribs =& parent::getRequestAttributes();
$request = $this->getRequest();
// NOTE: If there is more than one ETag, this will break.
// We don't have a good way to test multiple ETag to see if one of them is a working stash key.
$ifMatch = $request->getHeaderLine( 'If-Match' );
if ( $ifMatch ) {
$attribs['opts']['original']['etag'] = $ifMatch;
}
return $attribs;
}
/**
* Transform content given in the request from or to wikitext.
*
* @return Response
* @throws HttpException
*/
public function execute(): Response {
$request = $this->getRequest();
$from = $request->getPathParam( 'from' );
$format = $request->getPathParam( 'format' );
// XXX: Fallback to the default valid transforms in case the request is
// coming from a legacy client (restbase) that supports everything
// in the default valid transforms.
$validTransformations = $this->getConfig()['transformations'] ?? ParsoidFormatHelper::VALID_TRANSFORM;
if ( !isset( $validTransformations[$from] ) || !in_array( $format,
$validTransformations[$from],
true ) ) {
throw new HttpException( "Invalid transform: {$from}/to/{$format}",
404 );
}
$attribs = &$this->getRequestAttributes();
if ( !$this->acceptable( $attribs ) ) { // mutates $attribs
throw new HttpException( 'Not acceptable',
406 );
}
if ( $from === ParsoidFormatHelper::FORMAT_WIKITEXT ) {
// Accept wikitext as a string or object{body,headers}
$wikitext = $attribs['opts']['wikitext'] ?? null;
if ( is_array( $wikitext ) ) {
$wikitext = $wikitext['body'];
// We've been given a pagelanguage for this page.
if ( isset( $attribs['opts']['wikitext']['headers']['content-language'] ) ) {
$attribs['pagelanguage'] = $attribs['opts']['wikitext']['headers']['content-language'];
}
}
// We've been given source for this page
if ( $wikitext === null && isset( $attribs['opts']['original']['wikitext'] ) ) {
$wikitext = $attribs['opts']['original']['wikitext']['body'];
// We've been given a pagelanguage for this page.
if ( isset( $attribs['opts']['original']['wikitext']['headers']['content-language'] ) ) {
$attribs['pagelanguage'] = $attribs['opts']['original']['wikitext']['headers']['content-language'];
}
}
// Abort if no wikitext or title.
if ( $wikitext === null && empty( $attribs['pageName'] ) ) {
throw new HttpException( 'No title or wikitext was provided.',
400 );
}
$pageConfig = $this->tryToCreatePageConfig( $attribs,
$wikitext );
return $this->wt2html( $pageConfig,
$attribs,
$wikitext );
} elseif ( $format === ParsoidFormatHelper::FORMAT_WIKITEXT ) {
$html = $attribs['opts']['html'] ?? null;
// Accept html as a string or object{body,headers}
if ( is_array( $html ) ) {
$html = $html['body'];
}
if ( $html === null ) {
throw new HttpException( 'No html was supplied.',
400 );
}
// TODO: use ETag from If-Match header, for compat!
$page = $this->tryToCreatePageIdentity( $attribs );
return $this->html2wt(
$page,
$attribs,
$html
);
} else {
return $this->pb2pb( $attribs );
}
}
}
|