MediaWiki 1.40.4
TransformHandler.php
Go to the documentation of this file.
1<?php
2
22
27use Wikimedia\Parsoid\Parsoid;
28
38
40 public function getParamSettings() {
41 return [
42 'from' => [ self::PARAM_SOURCE => 'path',
43 ParamValidator::PARAM_TYPE => 'string',
44 ParamValidator::PARAM_REQUIRED => true, ],
45 'format' => [ self::PARAM_SOURCE => 'path',
46 ParamValidator::PARAM_TYPE => 'string',
47 ParamValidator::PARAM_REQUIRED => true, ],
48 'title' => [ self::PARAM_SOURCE => 'path',
49 ParamValidator::PARAM_TYPE => 'string',
50 ParamValidator::PARAM_REQUIRED => false, ],
51 'revision' => [ self::PARAM_SOURCE => 'path',
52 ParamValidator::PARAM_TYPE => 'string',
53 ParamValidator::PARAM_REQUIRED => false, ], ];
54 }
55
56 public function checkPreconditions() {
57 // NOTE: disable all precondition checks.
58 // If-(not)-Modified-Since is not supported by the /transform/ handler.
59 // If-None-Match is not supported by the /transform/ handler.
60 // If-Match for wt2html is handled in getRequestAttributes.
61 }
62
63 protected function &getRequestAttributes(): array {
64 $attribs =& parent::getRequestAttributes();
65
66 $request = $this->getRequest();
67
68 // NOTE: If there is more than one ETag, this will break.
69 // We don't have a good way to test multiple ETag to see if one of them is a working stash key.
70 $ifMatch = $request->getHeaderLine( 'If-Match' );
71
72 if ( $ifMatch ) {
73 $attribs['opts']['original']['etag'] = $ifMatch;
74 }
75
76 return $attribs;
77 }
78
85 public function execute(): Response {
86 $request = $this->getRequest();
87 $from = $request->getPathParam( 'from' );
88 $format = $request->getPathParam( 'format' );
89
90 // XXX: Fallback to the default valid transforms in case the request is
91 // coming from a legacy client (restbase) that supports everything
92 // in the default valid transforms.
93 $validTransformations = $this->getConfig()['transformations'] ?? ParsoidFormatHelper::VALID_TRANSFORM;
94
95 if ( !isset( $validTransformations[$from] ) || !in_array( $format,
96 $validTransformations[$from],
97 true ) ) {
98 throw new HttpException( "Invalid transform: {$from}/to/{$format}",
99 404 );
100 }
101 $attribs = &$this->getRequestAttributes();
102 if ( !$this->acceptable( $attribs ) ) { // mutates $attribs
103 throw new HttpException( 'Not acceptable',
104 406 );
105 }
106 if ( $from === ParsoidFormatHelper::FORMAT_WIKITEXT ) {
107 // Accept wikitext as a string or object{body,headers}
108 $wikitext = $attribs['opts']['wikitext'] ?? null;
109 if ( is_array( $wikitext ) ) {
110 $wikitext = $wikitext['body'];
111 // We've been given a pagelanguage for this page.
112 if ( isset( $attribs['opts']['wikitext']['headers']['content-language'] ) ) {
113 $attribs['pagelanguage'] = $attribs['opts']['wikitext']['headers']['content-language'];
114 }
115 }
116 // We've been given source for this page
117 if ( $wikitext === null && isset( $attribs['opts']['original']['wikitext'] ) ) {
118 $wikitext = $attribs['opts']['original']['wikitext']['body'];
119 // We've been given a pagelanguage for this page.
120 if ( isset( $attribs['opts']['original']['wikitext']['headers']['content-language'] ) ) {
121 $attribs['pagelanguage'] = $attribs['opts']['original']['wikitext']['headers']['content-language'];
122 }
123 }
124 // Abort if no wikitext or title.
125 if ( $wikitext === null && $attribs['titleMissing'] ) {
126 throw new HttpException( 'No title or wikitext was provided.',
127 400 );
128 }
129 $pageConfig = $this->tryToCreatePageConfig( $attribs,
130 $wikitext );
131
132 return $this->wt2html( $pageConfig,
133 $attribs,
134 $wikitext );
135 } elseif ( $format === ParsoidFormatHelper::FORMAT_WIKITEXT ) {
136 $html = $attribs['opts']['html'] ?? null;
137 // Accept html as a string or object{body,headers}
138 if ( is_array( $html ) ) {
139 $html = $html['body'];
140 }
141 if ( $html === null ) {
142 throw new HttpException( 'No html was supplied.',
143 400 );
144 }
145
146 // TODO: use ETag from If-Match header, for compat!
147
148 $page = $this->tryToCreatePageIdentity( $attribs );
149
150 return $this->html2wt(
151 $page,
152 $attribs,
153 $html
154 );
155 } else {
156 return $this->pb2pb( $attribs );
157 }
158 }
159}
Base class for Parsoid handlers.
Handler for transforming content given in the request.
& getRequestAttributes()
Rough equivalent of req.local from Parsoid-JS.
checkPreconditions()
Check the conditional request headers and generate a response if appropriate.
getParamSettings()
Fetch ParamValidator settings for parameters.Every setting must include self::PARAM_SOURCE to specify...
execute()
Transform content given in the request from or to wikitext.
getRequest()
Get the current request.
Definition Handler.php:155
getConfig()
Get the configuration array for the current route.
Definition Handler.php:177
This is the base exception class for non-fatal exceptions thrown from REST handlers.
Service for formatting and validating API parameters.
Copyright (C) 2011-2020 Wikimedia Foundation and others.