Debian Codegraph @

includes/Rest/Handler/TransformHandler.php

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