| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Rest\Handler; |
| 22 |
|
| 23 |
use MediaWiki\Rest\Handler\Helper\ParsoidFormatHelper; |
| 24 |
use MediaWiki\Rest\HttpException; |
| 25 |
use MediaWiki\Rest\LocalizedHttpException; |
| 26 |
use MediaWiki\Rest\Response; |
| 27 |
use Wikimedia\Message\MessageValue; |
| 28 |
use Wikimedia\ParamValidator\ParamValidator; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class TransformHandler extends ParsoidHandler { |
| 39 |
|
| 40 |
|
| 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 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function needsWriteAccess() { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
public function checkPreconditions() { |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
protected function &getRequestAttributes(): array { |
| 72 |
$attribs =& parent::getRequestAttributes(); |
| 73 |
|
| 74 |
$request = $this->getRequest(); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
$ifMatch = $request->getHeaderLine( 'If-Match' ); |
| 79 |
|
| 80 |
if ( $ifMatch ) { |
| 81 |
$attribs['opts']['original']['etag'] = $ifMatch; |
| 82 |
} |
| 83 |
|
| 84 |
return $attribs; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function execute(): Response { |
| 94 |
$request = $this->getRequest(); |
| 95 |
$from = $request->getPathParam( 'from' ); |
| 96 |
$format = $request->getPathParam( 'format' ); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 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 ) ) { |
| 110 |
throw new LocalizedHttpException( new MessageValue( "rest-unsupported-target-format" ), 406 ); |
| 111 |
} |
| 112 |
if ( $from === ParsoidFormatHelper::FORMAT_WIKITEXT ) { |
| 113 |
|
| 114 |
$wikitext = $attribs['opts']['wikitext'] ?? null; |
| 115 |
if ( is_array( $wikitext ) ) { |
| 116 |
$wikitext = $wikitext['body']; |
| 117 |
|
| 118 |
if ( isset( $attribs['opts']['wikitext']['headers']['content-language'] ) ) { |
| 119 |
$attribs['pagelanguage'] = $attribs['opts']['wikitext']['headers']['content-language']; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if ( $wikitext === null && isset( $attribs['opts']['original']['wikitext'] ) ) { |
| 124 |
$wikitext = $attribs['opts']['original']['wikitext']['body']; |
| 125 |
|
| 126 |
if ( isset( $attribs['opts']['original']['wikitext']['headers']['content-language'] ) ) { |
| 127 |
$attribs['pagelanguage'] = $attribs['opts']['original']['wikitext']['headers']['content-language']; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 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 |
|
| 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 |
|
| 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 |
} |