MediaWiki  master
TransformHandler.php
Go to the documentation of this file.
1 <?php
2 
21 namespace MediaWiki\Rest\Handler;
22 
27 
37 
39  public function getParamSettings() {
40  return [
41  'from' => [ self::PARAM_SOURCE => 'path',
42  ParamValidator::PARAM_TYPE => 'string',
44  'format' => [ self::PARAM_SOURCE => 'path',
45  ParamValidator::PARAM_TYPE => 'string',
47  'title' => [ self::PARAM_SOURCE => 'path',
48  ParamValidator::PARAM_TYPE => 'string',
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}",
98  404 );
99  }
100  $attribs = &$this->getRequestAttributes();
101  if ( !$this->acceptable( $attribs ) ) { // mutates $attribs
102  throw new HttpException( 'Not acceptable',
103  406 );
104  }
105  if ( $from === ParsoidFormatHelper::FORMAT_WIKITEXT ) {
106  // Accept wikitext as a string or object{body,headers}
107  $wikitext = $attribs['opts']['wikitext'] ?? null;
108  if ( is_array( $wikitext ) ) {
109  $wikitext = $wikitext['body'];
110  // We've been given a pagelanguage for this page.
111  if ( isset( $attribs['opts']['wikitext']['headers']['content-language'] ) ) {
112  $attribs['pagelanguage'] = $attribs['opts']['wikitext']['headers']['content-language'];
113  }
114  }
115  // We've been given source for this page
116  if ( $wikitext === null && isset( $attribs['opts']['original']['wikitext'] ) ) {
117  $wikitext = $attribs['opts']['original']['wikitext']['body'];
118  // We've been given a pagelanguage for this page.
119  if ( isset( $attribs['opts']['original']['wikitext']['headers']['content-language'] ) ) {
120  $attribs['pagelanguage'] = $attribs['opts']['original']['wikitext']['headers']['content-language'];
121  }
122  }
123  // Abort if no wikitext or title.
124  if ( $wikitext === null && empty( $attribs['pageName'] ) ) {
125  throw new HttpException( 'No title or wikitext was provided.',
126  400 );
127  }
128  $pageConfig = $this->tryToCreatePageConfig( $attribs,
129  $wikitext );
130 
131  return $this->wt2html( $pageConfig,
132  $attribs,
133  $wikitext );
134  } elseif ( $format === ParsoidFormatHelper::FORMAT_WIKITEXT ) {
135  $html = $attribs['opts']['html'] ?? null;
136  // Accept html as a string or object{body,headers}
137  if ( is_array( $html ) ) {
138  $html = $html['body'];
139  }
140  if ( $html === null ) {
141  throw new HttpException( 'No html was supplied.',
142  400 );
143  }
144 
145  // TODO: use ETag from If-Match header, for compat!
146 
147  $page = $this->tryToCreatePageIdentity( $attribs );
148 
149  return $this->html2wt(
150  $page,
151  $attribs,
152  $html
153  );
154  } else {
155  return $this->pb2pb( $attribs );
156  }
157  }
158 }
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.
const PARAM_TYPE
(string|array) Type of the parameter.
const PARAM_REQUIRED
(bool) Indicate that the parameter is required.
Copyright (C) 2011-2020 Wikimedia Foundation and others.