MediaWiki  master
TransformHandler.php
Go to the documentation of this file.
1 <?php
2 
21 namespace MediaWiki\Rest\Handler;
22 
26 
37  public function getParamSettings() {
38  return [
39  'from' => [ self::PARAM_SOURCE => 'path',
40  ParamValidator::PARAM_TYPE => 'string',
42  'format' => [ self::PARAM_SOURCE => 'path',
43  ParamValidator::PARAM_TYPE => 'string',
45  'title' => [ self::PARAM_SOURCE => 'path',
46  ParamValidator::PARAM_TYPE => 'string',
48  'revision' => [ self::PARAM_SOURCE => 'path',
49  ParamValidator::PARAM_TYPE => 'string',
50  ParamValidator::PARAM_REQUIRED => false, ], ];
51  }
52 
59  public function execute(): Response {
60  $request = $this->getRequest();
61  $from = $request->getPathParam( 'from' );
62  $format = $request->getPathParam( 'format' );
63 
64  // XXX: Fallback to the default valid transforms in case the request is
65  // coming from a legacy client (restbase) that supports everything
66  // in the default valid transforms.
67  $validTransformations = $this->getConfig()['transformations'] ?? ParsoidFormatHelper::VALID_TRANSFORM;
68 
69  if ( !isset( $validTransformations[$from] ) || !in_array( $format,
70  $validTransformations[$from],
71  true ) ) {
72  throw new HttpException( "Invalid transform: {$from}/to/{$format}",
73  404 );
74  }
75  $attribs = &$this->getRequestAttributes();
76  if ( !$this->acceptable( $attribs ) ) { // mutates $attribs
77  throw new HttpException( 'Not acceptable',
78  406 );
79  }
80  if ( $from === ParsoidFormatHelper::FORMAT_WIKITEXT ) {
81  // Accept wikitext as a string or object{body,headers}
82  $wikitext = $attribs['opts']['wikitext'] ?? null;
83  if ( is_array( $wikitext ) ) {
84  $wikitext = $wikitext['body'];
85  // We've been given a pagelanguage for this page.
86  if ( isset( $attribs['opts']['wikitext']['headers']['content-language'] ) ) {
87  $attribs['pagelanguage'] = $attribs['opts']['wikitext']['headers']['content-language'];
88  }
89  }
90  // We've been given source for this page
91  if ( $wikitext === null && isset( $attribs['opts']['original']['wikitext'] ) ) {
92  $wikitext = $attribs['opts']['original']['wikitext']['body'];
93  // We've been given a pagelanguage for this page.
94  if ( isset( $attribs['opts']['original']['wikitext']['headers']['content-language'] ) ) {
95  $attribs['pagelanguage'] = $attribs['opts']['original']['wikitext']['headers']['content-language'];
96  }
97  }
98  // Abort if no wikitext or title.
99  if ( $wikitext === null && $attribs['titleMissing'] ) {
100  throw new HttpException( 'No title or wikitext was provided.',
101  400 );
102  }
103  $pageConfig = $this->tryToCreatePageConfig( $attribs,
104  $wikitext );
105 
106  return $this->wt2html( $pageConfig,
107  $attribs,
108  $wikitext );
109  } elseif ( $format === ParsoidFormatHelper::FORMAT_WIKITEXT ) {
110  $html = $attribs['opts']['html'] ?? null;
111  // Accept html as a string or object{body,headers}
112  if ( is_array( $html ) ) {
113  $html = $html['body'];
114  }
115  if ( $html === null ) {
116  throw new HttpException( 'No html was supplied.',
117  400 );
118  }
119 
120  // TODO: use ETag from If-Match header, for compat!
121 
122  $page = $this->tryToCreatePageIdentity( $attribs );
123 
124  return $this->html2wt(
125  $page,
126  $attribs,
127  $html
128  );
129  } else {
130  return $this->pb2pb( $attribs );
131  }
132  }
133 }
Base class for Parsoid handlers.
acceptable(array &$attribs)
This method checks if we support the requested content formats As a side-effect, it updates $attribs ...
tryToCreatePageIdentity(array $attribs)
Try to create a PageIdentity object.
wt2html(PageConfig $pageConfig, array $attribs, ?string $wikitext=null)
Wikitext -> HTML helper.
& getRequestAttributes()
Rough equivalent of req.local from Parsoid-JS.
tryToCreatePageConfig(array $attribs, ?string $wikitext=null, bool $html2WtMode=false)
Try to create a PageConfig object.
pb2pb(array $attribs)
Pagebundle -> pagebundle helper.
html2wt( $page, array $attribs, string $html)
Handler for transforming content given in the request.
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:146
getConfig()
Get the configuration array for the current route.
Definition: Handler.php:168
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.