MediaWiki master
TransformHandler.php
Go to the documentation of this file.
1<?php
2
22
29
39
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
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
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}
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...
needsWriteAccess()
Indicates whether this route requires write access.The handler should override this if the route does...
execute()
Transform content given in the request from or to wikitext.
getRequest()
Get the current request.
Definition Handler.php:323
getConfig()
Get the configuration array for the current route.
Definition Handler.php:345
This is the base exception class for non-fatal exceptions thrown from REST handlers.
Value object representing a message for i18n.
Service for formatting and validating API parameters.
Copyright (C) 2011-2020 Wikimedia Foundation and others.