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