| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace MediaWiki\Rest\Handler; |
| 21 |
|
| 22 |
use Composer\Semver\Semver; |
| 23 |
use InvalidArgumentException; |
| 24 |
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface; |
| 25 |
use LogicException; |
| 26 |
use MediaWiki\Content\WikitextContent; |
| 27 |
use MediaWiki\Context\RequestContext; |
| 28 |
use MediaWiki\Language\LanguageCode; |
| 29 |
use MediaWiki\Logger\LoggerFactory; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\MediaWikiServices; |
| 32 |
use MediaWiki\Page\PageIdentity; |
| 33 |
use MediaWiki\Page\ProperPageIdentity; |
| 34 |
use MediaWiki\Parser\ParserOutput; |
| 35 |
use MediaWiki\Parser\Parsoid\Config\SiteConfig; |
| 36 |
use MediaWiki\Registration\ExtensionRegistry; |
| 37 |
use MediaWiki\Rest\Handler; |
| 38 |
use MediaWiki\Rest\Handler\Helper\HtmlInputTransformHelper; |
| 39 |
use MediaWiki\Rest\Handler\Helper\HtmlOutputRendererHelper; |
| 40 |
use MediaWiki\Rest\Handler\Helper\ParsoidFormatHelper; |
| 41 |
use MediaWiki\Rest\HttpException; |
| 42 |
use MediaWiki\Rest\LocalizedHttpException; |
| 43 |
use MediaWiki\Rest\Response; |
| 44 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 45 |
use MediaWiki\Revision\RevisionAccessException; |
| 46 |
use MediaWiki\Revision\RevisionLookup; |
| 47 |
use MediaWiki\Revision\SlotRecord; |
| 48 |
use MediaWiki\Revision\SuppressedDataException; |
| 49 |
use MediaWiki\Title\MalformedTitleException; |
| 50 |
use MediaWiki\Title\Title; |
| 51 |
use MediaWiki\WikiMap\WikiMap; |
| 52 |
use MobileContext; |
| 53 |
use Wikimedia\Http\HttpAcceptParser; |
| 54 |
use Wikimedia\Message\DataMessageValue; |
| 55 |
use Wikimedia\Message\MessageValue; |
| 56 |
use Wikimedia\Parsoid\Config\DataAccess; |
| 57 |
use Wikimedia\Parsoid\Config\PageConfig; |
| 58 |
use Wikimedia\Parsoid\Config\PageConfigFactory; |
| 59 |
use Wikimedia\Parsoid\Core\ClientError; |
| 60 |
use Wikimedia\Parsoid\Core\PageBundle; |
| 61 |
use Wikimedia\Parsoid\Core\ResourceLimitExceededException; |
| 62 |
use Wikimedia\Parsoid\DOM\Document; |
| 63 |
use Wikimedia\Parsoid\Parsoid; |
| 64 |
use Wikimedia\Parsoid\Utils\ContentUtils; |
| 65 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 66 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 67 |
use Wikimedia\Parsoid\Utils\Timing; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
abstract class ParsoidHandler extends Handler { |
| 78 |
|
| 79 |
private RevisionLookup $revisionLookup; |
| 80 |
protected SiteConfig $siteConfig; |
| 81 |
protected PageConfigFactory $pageConfigFactory; |
| 82 |
protected DataAccess $dataAccess; |
| 83 |
|
| 84 |
|
| 85 |
protected $extensionRegistry; |
| 86 |
|
| 87 |
|
| 88 |
protected $metrics; |
| 89 |
|
| 90 |
|
| 91 |
private $requestAttributes; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public static function factory(): ParsoidHandler { |
| 97 |
$services = MediaWikiServices::getInstance(); |
| 98 |
|
| 99 |
return new static( |
| 100 |
$services->getRevisionLookup(), |
| 101 |
$services->getParsoidSiteConfig(), |
| 102 |
$services->getParsoidPageConfigFactory(), |
| 103 |
$services->getParsoidDataAccess() |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
public function __construct( |
| 108 |
RevisionLookup $revisionLookup, |
| 109 |
SiteConfig $siteConfig, |
| 110 |
PageConfigFactory $pageConfigFactory, |
| 111 |
DataAccess $dataAccess |
| 112 |
) { |
| 113 |
$this->revisionLookup = $revisionLookup; |
| 114 |
$this->siteConfig = $siteConfig; |
| 115 |
$this->pageConfigFactory = $pageConfigFactory; |
| 116 |
$this->dataAccess = $dataAccess; |
| 117 |
$this->extensionRegistry = ExtensionRegistry::getInstance(); |
| 118 |
$this->metrics = $siteConfig->metrics(); |
| 119 |
} |
| 120 |
|
| 121 |
public function getSupportedRequestTypes(): array { |
| 122 |
return array_merge( parent::getSupportedRequestTypes(), [ |
| 123 |
'application/x-www-form-urlencoded', |
| 124 |
'multipart/form-data' |
| 125 |
] ); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
protected function assertDomainIsCorrect( $domain ): void { |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
$server = RequestContext::getMain()->getConfig()->get( MainConfigNames::Server ); |
| 140 |
$expectedDomain = parse_url( $server, PHP_URL_HOST ); |
| 141 |
if ( !$expectedDomain ) { |
| 142 |
throw new LogicException( 'Cannot parse $wgServer' ); |
| 143 |
} |
| 144 |
if ( strcasecmp( $expectedDomain, $domain ) === 0 ) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
if ( $this->extensionRegistry->isLoaded( 'MobileFrontend' ) ) { |
| 152 |
|
| 153 |
$mobileServer = MobileContext::singleton()->getMobileUrl( $server ); |
| 154 |
$expectedMobileDomain = parse_url( $mobileServer, PHP_URL_HOST ); |
| 155 |
if ( $expectedMobileDomain && strcasecmp( $expectedMobileDomain, $domain ) === 0 ) { |
| 156 |
return; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
$msg = new DataMessageValue( |
| 161 |
'mwparsoid-invalid-domain', |
| 162 |
[], |
| 163 |
'invalid-domain', |
| 164 |
[ 'expected' => $expectedDomain, 'actual' => $domain, ] |
| 165 |
); |
| 166 |
|
| 167 |
throw new LocalizedHttpException( $msg, 400, [ |
| 168 |
'error' => 'parameter-validation-failed', |
| 169 |
'name' => 'domain', |
| 170 |
'value' => $domain, |
| 171 |
'failureCode' => $msg->getCode(), |
| 172 |
'failureData' => $msg->getData(), |
| 173 |
] ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
protected function getParsedBody(): array { |
| 182 |
$request = $this->getRequest(); |
| 183 |
[ $contentType ] = explode( ';', $request->getHeader( 'Content-Type' )[0] ?? '', 2 ); |
| 184 |
switch ( $contentType ) { |
| 185 |
case 'application/x-www-form-urlencoded': |
| 186 |
case 'multipart/form-data': |
| 187 |
return $request->getPostParams(); |
| 188 |
case 'application/json': |
| 189 |
$json = json_decode( $request->getBody()->getContents(), true ); |
| 190 |
if ( !is_array( $json ) ) { |
| 191 |
throw new LocalizedHttpException( |
| 192 |
new MessageValue( "rest-json-body-parse-error", [ 'not a valid JSON object' ] ), 400 ); |
| 193 |
} |
| 194 |
return $json; |
| 195 |
default: |
| 196 |
throw new LocalizedHttpException( |
| 197 |
new MessageValue( "rest-unsupported-content-type", [ $contentType ?? '(null)' ] ), |
| 198 |
415 |
| 199 |
); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
protected function &getRequestAttributes(): array { |
| 209 |
if ( $this->requestAttributes ) { |
| 210 |
return $this->requestAttributes; |
| 211 |
} |
| 212 |
|
| 213 |
$request = $this->getRequest(); |
| 214 |
$body = ( $request->getMethod() === 'POST' ) ? $this->getParsedBody() : []; |
| 215 |
$opts = array_merge( $body, array_intersect_key( $request->getPathParams(), |
| 216 |
[ 'from' => true, 'format' => true ] ) ); |
| 217 |
'@phan-var array<string,array|bool|string> $opts'; |
| 218 |
$contentLanguage = $request->getHeaderLine( 'Content-Language' ) ?: null; |
| 219 |
if ( $contentLanguage ) { |
| 220 |
$contentLanguage = LanguageCode::normalizeNonstandardCodeAndWarn( |
| 221 |
$contentLanguage |
| 222 |
); |
| 223 |
} |
| 224 |
$attribs = [ |
| 225 |
'pageName' => $request->getPathParam( 'title' ) ?? '', |
| 226 |
'oldid' => $request->getPathParam( 'revision' ), |
| 227 |
|
| 228 |
|
| 229 |
'body_only' => $request->getQueryParams()['body_only'] ?? $body['body_only'] ?? null, |
| 230 |
'errorEnc' => ParsoidFormatHelper::ERROR_ENCODING[$opts['format']] ?? 'plain', |
| 231 |
'iwp' => WikiMap::getCurrentWikiId(), |
| 232 |
'offsetType' => $body['offsetType'] |
| 233 |
?? $request->getQueryParams()['offsetType'] |
| 234 |
|
| 235 |
?? ( $opts['format'] === ParsoidFormatHelper::FORMAT_LINT ? 'ucs2' : 'byte' ), |
| 236 |
'pagelanguage' => $contentLanguage, |
| 237 |
]; |
| 238 |
|
| 239 |
|
| 240 |
$opts['stash'] = $request->getQueryParams()['stash'] ?? false; |
| 241 |
|
| 242 |
if ( $request->getMethod() === 'POST' ) { |
| 243 |
if ( isset( $opts['original']['revid'] ) ) { |
| 244 |
$attribs['oldid'] = $opts['original']['revid']; |
| 245 |
} |
| 246 |
if ( isset( $opts['original']['title'] ) ) { |
| 247 |
$attribs['pageName'] = $opts['original']['title']; |
| 248 |
} |
| 249 |
} |
| 250 |
if ( $attribs['oldid'] !== null ) { |
| 251 |
if ( $attribs['oldid'] === '' ) { |
| 252 |
$attribs['oldid'] = null; |
| 253 |
} else { |
| 254 |
$attribs['oldid'] = (int)$attribs['oldid']; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
$opts['accept-language'] = $request->getHeaderLine( 'Accept-Language' ) ?: null; |
| 260 |
|
| 261 |
$acceptLanguage = null; |
| 262 |
if ( $opts['accept-language'] !== null ) { |
| 263 |
$acceptLanguage = LanguageCode::normalizeNonstandardCodeAndWarn( |
| 264 |
$opts['accept-language'] |
| 265 |
); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
if ( ( $attribs['pageName'] === '' ) && $attribs['oldid'] ) { |
| 270 |
$rev = $this->revisionLookup->getRevisionById( $attribs['oldid'] ); |
| 271 |
if ( $rev ) { |
| 272 |
$attribs['pageName'] = $rev->getPage()->getDBkey(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
$attribs['envOptions'] = [ |
| 277 |
|
| 278 |
'prefix' => $attribs['iwp'], |
| 279 |
|
| 280 |
|
| 281 |
'domain' => $request->getPathParam( 'domain' ), |
| 282 |
'pageName' => $attribs['pageName'], |
| 283 |
'cookie' => $request->getHeaderLine( 'Cookie' ), |
| 284 |
'reqId' => $request->getHeaderLine( 'X-Request-Id' ), |
| 285 |
'userAgent' => $request->getHeaderLine( 'User-Agent' ), |
| 286 |
'htmlVariantLanguage' => $acceptLanguage, |
| 287 |
|
| 288 |
|
| 289 |
'outputContentVersion' => Parsoid::defaultHTMLVersion(), |
| 290 |
]; |
| 291 |
|
| 292 |
|
| 293 |
$sourceVariant = $opts['updates']['variant']['source'] ?? null; |
| 294 |
if ( $sourceVariant ) { |
| 295 |
$sourceVariant = LanguageCode::normalizeNonstandardCodeAndWarn( |
| 296 |
$sourceVariant |
| 297 |
); |
| 298 |
$opts['updates']['variant']['source'] = $sourceVariant; |
| 299 |
} |
| 300 |
$targetVariant = $opts['updates']['variant']['target'] ?? null; |
| 301 |
if ( $targetVariant ) { |
| 302 |
$targetVariant = LanguageCode::normalizeNonstandardCodeAndWarn( |
| 303 |
$targetVariant |
| 304 |
); |
| 305 |
$opts['updates']['variant']['target'] = $targetVariant; |
| 306 |
} |
| 307 |
if ( isset( $opts['wikitext']['headers']['content-language'] ) ) { |
| 308 |
$contentLanguage = $opts['wikitext']['headers']['content-language']; |
| 309 |
$contentLanguage = LanguageCode::normalizeNonstandardCodeAndWarn( |
| 310 |
$contentLanguage |
| 311 |
); |
| 312 |
$opts['wikitext']['headers']['content-language'] = $contentLanguage; |
| 313 |
} |
| 314 |
if ( isset( $opts['original']['wikitext']['headers']['content-language'] ) ) { |
| 315 |
$contentLanguage = $opts['original']['wikitext']['headers']['content-language']; |
| 316 |
$contentLanguage = LanguageCode::normalizeNonstandardCodeAndWarn( |
| 317 |
$contentLanguage |
| 318 |
); |
| 319 |
$opts['original']['wikitext']['headers']['content-language'] = $contentLanguage; |
| 320 |
} |
| 321 |
|
| 322 |
$attribs['opts'] = $opts; |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
if ( $attribs['envOptions']['domain'] !== null ) { |
| 327 |
$this->assertDomainIsCorrect( $attribs['envOptions']['domain'] ); |
| 328 |
} |
| 329 |
|
| 330 |
$this->requestAttributes = $attribs; |
| 331 |
return $this->requestAttributes; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
private function getHtmlOutputRendererHelper( |
| 343 |
array $attribs, |
| 344 |
?string $source, |
| 345 |
PageIdentity $page, |
| 346 |
?int $revId |
| 347 |
): HtmlOutputRendererHelper { |
| 348 |
$services = MediaWikiServices::getInstance(); |
| 349 |
|
| 350 |
|
| 351 |
$lenientRevHandling = true; |
| 352 |
|
| 353 |
$authority = $this->getAuthority(); |
| 354 |
|
| 355 |
$params = []; |
| 356 |
$helper = $services->getPageRestHelperFactory()->newHtmlOutputRendererHelper( |
| 357 |
$page, $params, $authority, $revId, $lenientRevHandling |
| 358 |
); |
| 359 |
|
| 360 |
|
| 361 |
$model = $attribs['opts']['contentmodel'] |
| 362 |
?? ( $attribs['envOptions']['contentmodel'] ?? CONTENT_MODEL_WIKITEXT ); |
| 363 |
|
| 364 |
if ( $source !== null ) { |
| 365 |
$helper->setContentSource( $source, $model ); |
| 366 |
} |
| 367 |
|
| 368 |
if ( isset( $attribs['opts']['stash'] ) ) { |
| 369 |
$helper->setStashingEnabled( $attribs['opts']['stash'] ); |
| 370 |
} |
| 371 |
|
| 372 |
if ( isset( $attribs['envOptions']['outputContentVersion'] ) ) { |
| 373 |
$helper->setOutputProfileVersion( $attribs['envOptions']['outputContentVersion'] ); |
| 374 |
} |
| 375 |
|
| 376 |
if ( isset( $attribs['pagelanguage'] ) ) { |
| 377 |
$helper->setPageLanguage( $attribs['pagelanguage'] ); |
| 378 |
} |
| 379 |
|
| 380 |
if ( isset( $attribs['opts']['accept-language'] ) ) { |
| 381 |
$helper->setVariantConversionLanguage( $attribs['opts']['accept-language'] ); |
| 382 |
} |
| 383 |
|
| 384 |
return $helper; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
protected function getHtmlInputTransformHelper( |
| 395 |
array $attribs, |
| 396 |
string $html, |
| 397 |
PageIdentity $page |
| 398 |
): HtmlInputTransformHelper { |
| 399 |
$services = MediaWikiServices::getInstance(); |
| 400 |
|
| 401 |
$parameters = $attribs['opts'] + $attribs; |
| 402 |
$body = $attribs['opts']; |
| 403 |
|
| 404 |
$body['html'] = $html; |
| 405 |
|
| 406 |
$helper = $services->getPageRestHelperFactory()->newHtmlInputTransformHelper( |
| 407 |
$attribs['envOptions'] + [ |
| 408 |
'offsetType' => $attribs['offsetType'], |
| 409 |
], |
| 410 |
$page, |
| 411 |
$body, |
| 412 |
$parameters |
| 413 |
); |
| 414 |
|
| 415 |
$helper->setMetrics( $this->siteConfig->prefixedStatsFactory() ); |
| 416 |
|
| 417 |
return $helper; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
private const NEW_SPEC = |
| 424 |
'#^https://www.mediawiki.org/wiki/Specs/(HTML|pagebundle)/(\d+\.\d+\.\d+)$#D'; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
protected function acceptable( array &$attribs ): bool { |
| 435 |
$request = $this->getRequest(); |
| 436 |
$format = $attribs['opts']['format']; |
| 437 |
|
| 438 |
if ( $format === ParsoidFormatHelper::FORMAT_WIKITEXT ) { |
| 439 |
return true; |
| 440 |
} |
| 441 |
|
| 442 |
$acceptHeader = $request->getHeader( 'Accept' ); |
| 443 |
if ( !$acceptHeader ) { |
| 444 |
return true; |
| 445 |
} |
| 446 |
|
| 447 |
$parser = new HttpAcceptParser(); |
| 448 |
$acceptableTypes = $parser->parseAccept( $acceptHeader[0] ); |
| 449 |
if ( !$acceptableTypes ) { |
| 450 |
return true; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
foreach ( $acceptableTypes as $t ) { |
| 455 |
$type = "{$t['type']}/{$t['subtype']}"; |
| 456 |
$profile = $t['params']['profile'] ?? null; |
| 457 |
if ( |
| 458 |
( $format === ParsoidFormatHelper::FORMAT_HTML && $type === 'text/html' ) || |
| 459 |
( $format === ParsoidFormatHelper::FORMAT_PAGEBUNDLE && $type === 'application/json' ) |
| 460 |
) { |
| 461 |
if ( $profile ) { |
| 462 |
preg_match( self::NEW_SPEC, $profile, $matches ); |
| 463 |
if ( $matches && strtolower( $matches[1] ) === $format ) { |
| 464 |
$contentVersion = Parsoid::resolveContentVersion( $matches[2] ); |
| 465 |
if ( $contentVersion ) { |
| 466 |
|
| 467 |
$attribs['envOptions']['outputContentVersion'] = $contentVersion; |
| 468 |
return true; |
| 469 |
} else { |
| 470 |
continue; |
| 471 |
} |
| 472 |
} else { |
| 473 |
continue; |
| 474 |
} |
| 475 |
} else { |
| 476 |
return true; |
| 477 |
} |
| 478 |
} elseif ( |
| 479 |
( $type === '*/*' ) || |
| 480 |
( $format === ParsoidFormatHelper::FORMAT_HTML && $type === 'text/*' ) |
| 481 |
) { |
| 482 |
return true; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
return false; |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
protected function tryToCreatePageConfig( |
| 502 |
array $attribs, ?string $wikitextOverride = null, bool $html2WtMode = false |
| 503 |
): PageConfig { |
| 504 |
$revId = $attribs['oldid']; |
| 505 |
$pagelanguageOverride = $attribs['pagelanguage']; |
| 506 |
$title = $attribs['pageName']; |
| 507 |
|
| 508 |
$title = ( $title !== '' ) ? Title::newFromText( $title ) : Title::newMainPage(); |
| 509 |
if ( !$title ) { |
| 510 |
|
| 511 |
throw new LogicException( 'Title not found!' ); |
| 512 |
} |
| 513 |
$user = RequestContext::getMain()->getUser(); |
| 514 |
|
| 515 |
if ( $wikitextOverride === null ) { |
| 516 |
$revisionRecord = null; |
| 517 |
} else { |
| 518 |
|
| 519 |
|
| 520 |
$revisionRecord = new MutableRevisionRecord( $title ); |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
$revisionRecord->setId( 0 ); |
| 525 |
$revisionRecord->setSlot( |
| 526 |
SlotRecord::newUnsaved( |
| 527 |
SlotRecord::MAIN, |
| 528 |
new WikitextContent( $wikitextOverride ) |
| 529 |
) |
| 530 |
); |
| 531 |
} |
| 532 |
|
| 533 |
$hasOldId = ( $revId !== null ); |
| 534 |
$ensureAccessibleContent = !$html2WtMode || $hasOldId; |
| 535 |
|
| 536 |
try { |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
$pageConfig = $this->pageConfigFactory->create( |
| 544 |
$title, $user, $revisionRecord ?? $revId, null, $pagelanguageOverride, |
| 545 |
$ensureAccessibleContent |
| 546 |
); |
| 547 |
} catch ( SuppressedDataException $e ) { |
| 548 |
throw new LocalizedHttpException( |
| 549 |
new MessageValue( "rest-permission-denied-revision", [ $e->getMessage() ] ), 403 |
| 550 |
); |
| 551 |
} catch ( RevisionAccessException $e ) { |
| 552 |
throw new LocalizedHttpException( |
| 553 |
new MessageValue( "rest-specified-revision-unavailable", [ $e->getMessage() ] ), 404 |
| 554 |
); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
return $pageConfig; |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
protected function tryToCreatePageIdentity( array $attribs ): PageIdentity { |
| 571 |
if ( $attribs['pageName'] === '' ) { |
| 572 |
return Title::newMainPage(); |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
$pageStore = MediaWikiServices::getInstance()->getPageStore(); |
| 578 |
|
| 579 |
$page = $pageStore->getPageByText( $attribs['pageName'] ); |
| 580 |
|
| 581 |
if ( !$page ) { |
| 582 |
throw new LocalizedHttpException( |
| 583 |
new MessageValue( "rest-invalid-title", [ 'pageName' ] ), 400 |
| 584 |
); |
| 585 |
} |
| 586 |
|
| 587 |
return $page; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
protected function getTransformEndpoint( string $format = ParsoidFormatHelper::FORMAT_HTML ): string { |
| 603 |
return '/coredev/v0/transform/{from}/to/{format}/{title}/{revision}'; |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
protected function getPageContentEndpoint( string $format = ParsoidFormatHelper::FORMAT_HTML ): string { |
| 619 |
if ( $format !== ParsoidFormatHelper::FORMAT_HTML ) { |
| 620 |
throw new InvalidArgumentException( 'Unsupported page content format: ' . $format ); |
| 621 |
} |
| 622 |
return '/v1/page/{title}/html'; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
protected function getRevisionContentEndpoint( string $format = ParsoidFormatHelper::FORMAT_HTML ): string { |
| 638 |
if ( $format !== ParsoidFormatHelper::FORMAT_HTML ) { |
| 639 |
throw new InvalidArgumentException( 'Unsupported revision content format: ' . $format ); |
| 640 |
} |
| 641 |
return '/v1/revision/{revision}/html'; |
| 642 |
} |
| 643 |
|
| 644 |
private function wtLint( |
| 645 |
PageConfig $pageConfig, array $attribs, ?array $linterOverrides = [] |
| 646 |
) { |
| 647 |
$envOptions = $attribs['envOptions'] + [ |
| 648 |
'linterOverrides' => $linterOverrides, |
| 649 |
'offsetType' => $attribs['offsetType'], |
| 650 |
]; |
| 651 |
try { |
| 652 |
$parsoid = $this->newParsoid(); |
| 653 |
$parserOutput = new ParserOutput(); |
| 654 |
return $parsoid->wikitext2lint( $pageConfig, $envOptions, $parserOutput ); |
| 655 |
} catch ( ClientError $e ) { |
| 656 |
throw new LocalizedHttpException( new MessageValue( "rest-parsoid-error", [ $e->getMessage() ] ), 400 ); |
| 657 |
} catch ( ResourceLimitExceededException $e ) { |
| 658 |
throw new LocalizedHttpException( |
| 659 |
new MessageValue( "rest-parsoid-resource-exceeded", [ $e->getMessage() ] ), 413 |
| 660 |
); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
protected function wt2html( |
| 676 |
PageConfig $pageConfig, array $attribs, ?string $wikitext = null |
| 677 |
) { |
| 678 |
$request = $this->getRequest(); |
| 679 |
$opts = $attribs['opts']; |
| 680 |
$format = $opts['format']; |
| 681 |
$oldid = $attribs['oldid']; |
| 682 |
$stash = $opts['stash'] ?? false; |
| 683 |
|
| 684 |
if ( $format === ParsoidFormatHelper::FORMAT_LINT ) { |
| 685 |
$linterOverrides = []; |
| 686 |
if ( $this->extensionRegistry->isLoaded( 'Linter' ) ) { |
| 687 |
$disabled = []; |
| 688 |
$services = MediaWikiServices::getInstance(); |
| 689 |
$linterCategories = $services->getMainConfig()->get( 'LinterCategories' ); |
| 690 |
foreach ( $linterCategories as $name => $cat ) { |
| 691 |
if ( $cat['priority'] === 'none' ) { |
| 692 |
$disabled[] = $name; |
| 693 |
} |
| 694 |
} |
| 695 |
$linterOverrides['disabled'] = $disabled; |
| 696 |
} |
| 697 |
$lints = $this->wtLint( $pageConfig, $attribs, $linterOverrides ); |
| 698 |
$response = $this->getResponseFactory()->createJson( $lints ); |
| 699 |
return $response; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
$helper = $this->getHtmlOutputRendererHelper( |
| 705 |
$attribs, |
| 706 |
$wikitext, |
| 707 |
$this->pageConfigToPageIdentity( $pageConfig ), |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
$pageConfig->getRevisionId() ?: null |
| 714 |
); |
| 715 |
|
| 716 |
$needsPageBundle = ( $format === ParsoidFormatHelper::FORMAT_PAGEBUNDLE ); |
| 717 |
|
| 718 |
if ( $attribs['body_only'] ) { |
| 719 |
$helper->setFlavor( 'fragment' ); |
| 720 |
} elseif ( !$needsPageBundle ) { |
| 721 |
|
| 722 |
$helper->setFlavor( 'edit' ); |
| 723 |
} |
| 724 |
|
| 725 |
if ( $wikitext === null && $oldid !== null ) { |
| 726 |
$mstr = 'pageWithOldid'; |
| 727 |
} else { |
| 728 |
$mstr = 'wt'; |
| 729 |
} |
| 730 |
|
| 731 |
$parseTiming = Timing::start(); |
| 732 |
|
| 733 |
if ( $needsPageBundle ) { |
| 734 |
$pb = $helper->getPageBundle(); |
| 735 |
|
| 736 |
|
| 737 |
if ( $attribs['offsetType'] !== 'byte' ) { |
| 738 |
$parsoid = $this->newParsoid(); |
| 739 |
$pb = $parsoid->pb2pb( |
| 740 |
$pageConfig, |
| 741 |
'convertoffsets', |
| 742 |
$pb, |
| 743 |
[ |
| 744 |
'inputOffsetType' => 'byte', |
| 745 |
'outputOffsetType' => $attribs['offsetType'] |
| 746 |
] |
| 747 |
); |
| 748 |
} |
| 749 |
|
| 750 |
$response = $this->getResponseFactory()->createJson( $pb->responseData() ); |
| 751 |
$helper->putHeaders( $response, false ); |
| 752 |
|
| 753 |
ParsoidFormatHelper::setContentType( |
| 754 |
$response, |
| 755 |
ParsoidFormatHelper::FORMAT_PAGEBUNDLE, |
| 756 |
$pb->version |
| 757 |
); |
| 758 |
} else { |
| 759 |
$out = $helper->getHtml(); |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
$response = $this->getResponseFactory()->create(); |
| 765 |
$response->getBody()->write( $out->getRawText() ); |
| 766 |
|
| 767 |
$helper->putHeaders( $response, true ); |
| 768 |
|
| 769 |
|
| 770 |
if ( $stash ) { |
| 771 |
$eTag = $helper->getETag(); |
| 772 |
if ( $eTag ) { |
| 773 |
$response->setHeader( 'ETag', $eTag ); |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
$outSize = $response->getBody()->getSize(); |
| 781 |
$parseTime = $parseTiming->end(); |
| 782 |
|
| 783 |
|
| 784 |
if ( $mstr === 'pageWithOldid' ) { |
| 785 |
if ( $parseTime > 3000 ) { |
| 786 |
LoggerFactory::getInstance( 'slow-parsoid' ) |
| 787 |
->info( 'Parsing {title} was slow, took {time} seconds', [ |
| 788 |
'time' => number_format( $parseTime / 1000, 2 ), |
| 789 |
'title' => Title::newFromLinkTarget( $pageConfig->getLinkTarget() )->getPrefixedText(), |
| 790 |
] ); |
| 791 |
} |
| 792 |
|
| 793 |
if ( $parseTime > 10 && $outSize > 100 ) { |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
$timePerKB = $parseTime * 1024 / $outSize; |
| 805 |
if ( $timePerKB > 500 ) { |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
LoggerFactory::getInstance( 'slow-parsoid' ) |
| 810 |
->info( 'Parsing {title} was slow, timePerKB took {timePerKB} ms, total: {time} seconds', [ |
| 811 |
'time' => number_format( $parseTime / 1000, 2 ), |
| 812 |
'timePerKB' => number_format( $timePerKB, 1 ), |
| 813 |
'title' => Title::newFromLinkTarget( $pageConfig->getLinkTarget() )->getPrefixedText(), |
| 814 |
] ); |
| 815 |
} |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
if ( $wikitext !== null ) { |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
$response->setHeader( 'Cache-Control', 'private,no-cache,s-maxage=0' ); |
| 824 |
} elseif ( $oldid !== null ) { |
| 825 |
|
| 826 |
if ( $request->getHeaderLine( 'Cookie' ) || |
| 827 |
$request->getHeaderLine( 'Authorization' ) ) { |
| 828 |
|
| 829 |
$response->setHeader( 'Cache-Control', 'private,no-cache,s-maxage=0' ); |
| 830 |
} |
| 831 |
} |
| 832 |
return $response; |
| 833 |
} |
| 834 |
|
| 835 |
protected function newParsoid(): Parsoid { |
| 836 |
return new Parsoid( $this->siteConfig, $this->dataAccess ); |
| 837 |
} |
| 838 |
|
| 839 |
protected function parseHTML( string $html, bool $validateXMLNames = false ): Document { |
| 840 |
return DOMUtils::parseHTML( $html, $validateXMLNames ); |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
protected function html2wt( |
| 852 |
$page, array $attribs, string $html |
| 853 |
) { |
| 854 |
if ( $page instanceof PageConfig ) { |
| 855 |
|
| 856 |
|
| 857 |
$page = Title::newFromLinkTarget( $page->getLinkTarget() ); |
| 858 |
} |
| 859 |
|
| 860 |
try { |
| 861 |
$transform = $this->getHtmlInputTransformHelper( $attribs, $html, $page ); |
| 862 |
|
| 863 |
$response = $this->getResponseFactory()->create(); |
| 864 |
$transform->putContent( $response ); |
| 865 |
|
| 866 |
return $response; |
| 867 |
} catch ( ClientError $e ) { |
| 868 |
throw new LocalizedHttpException( new MessageValue( "rest-parsoid-error", [ $e->getMessage() ] ), 400 ); |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
protected function pb2pb( array $attribs ) { |
| 880 |
$opts = $attribs['opts']; |
| 881 |
|
| 882 |
$revision = $opts['previous'] ?? $opts['original'] ?? null; |
| 883 |
if ( !isset( $revision['html'] ) ) { |
| 884 |
throw new LocalizedHttpException( new MessageValue( "rest-missing-revision-html" ), 400 ); |
| 885 |
} |
| 886 |
|
| 887 |
$vOriginal = ParsoidFormatHelper::parseContentTypeHeader( |
| 888 |
$revision['html']['headers']['content-type'] ?? '' ); |
| 889 |
if ( $vOriginal === null ) { |
| 890 |
throw new LocalizedHttpException( new MessageValue( "rest-missing-revision-html-content-type" ), 400 ); |
| 891 |
} |
| 892 |
$attribs['envOptions']['inputContentVersion'] = $vOriginal; |
| 893 |
'@phan-var array<string,array|string> $attribs'; |
| 894 |
|
| 895 |
$this->metrics->increment( |
| 896 |
'pb2pb.original.version.' . $attribs['envOptions']['inputContentVersion'] |
| 897 |
); |
| 898 |
|
| 899 |
if ( !empty( $opts['updates'] ) ) { |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
$pageConfig = $this->tryToCreatePageConfig( $attribs, null, true ); |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
if ( !empty( $opts['updates']['redlinks'] ) ) { |
| 922 |
|
| 923 |
|
| 924 |
return $this->updateRedLinks( $pageConfig, $attribs, $revision ); |
| 925 |
} elseif ( isset( $opts['updates']['variant'] ) ) { |
| 926 |
return $this->languageConversion( $pageConfig, $attribs, $revision ); |
| 927 |
} else { |
| 928 |
throw new LocalizedHttpException( new MessageValue( "rest-unknown-parsoid-transformation" ), 400 ); |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
$downgrade = Parsoid::findDowngrade( |
| 936 |
$attribs['envOptions']['inputContentVersion'], |
| 937 |
$attribs['envOptions']['outputContentVersion'] |
| 938 |
); |
| 939 |
if ( $downgrade ) { |
| 940 |
$pb = new PageBundle( |
| 941 |
$revision['html']['body'], |
| 942 |
$revision['data-parsoid']['body'] ?? null, |
| 943 |
$revision['data-mw']['body'] ?? null |
| 944 |
); |
| 945 |
$this->validatePb( $pb, $attribs['envOptions']['inputContentVersion'] ); |
| 946 |
Parsoid::downgrade( $downgrade, $pb ); |
| 947 |
|
| 948 |
if ( !empty( $attribs['body_only'] ) ) { |
| 949 |
$doc = $this->parseHTML( $pb->html ); |
| 950 |
$body = DOMCompat::getBody( $doc ); |
| 951 |
$pb->html = ContentUtils::toXML( $body, [ 'innerXML' => true ] ); |
| 952 |
} |
| 953 |
|
| 954 |
$response = $this->getResponseFactory()->createJson( $pb->responseData() ); |
| 955 |
ParsoidFormatHelper::setContentType( |
| 956 |
$response, ParsoidFormatHelper::FORMAT_PAGEBUNDLE, $pb->version |
| 957 |
); |
| 958 |
return $response; |
| 959 |
|
| 960 |
} elseif ( Semver::satisfies( $attribs['envOptions']['outputContentVersion'], |
| 961 |
'^' . $attribs['envOptions']['inputContentVersion'] ) ) { |
| 962 |
$pageConfig = $this->tryToCreatePageConfig( $attribs ); |
| 963 |
return $this->wt2html( $pageConfig, $attribs ); |
| 964 |
} else { |
| 965 |
throw new LocalizedHttpException( new MessageValue( "rest-unsupported-profile-conversion" ), 415 ); |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
protected function updateRedLinks( |
| 978 |
PageConfig $pageConfig, array $attribs, array $revision |
| 979 |
) { |
| 980 |
$parsoid = $this->newParsoid(); |
| 981 |
|
| 982 |
$pb = new PageBundle( |
| 983 |
$revision['html']['body'], |
| 984 |
$revision['data-parsoid']['body'] ?? null, |
| 985 |
$revision['data-mw']['body'] ?? null, |
| 986 |
$attribs['envOptions']['inputContentVersion'], |
| 987 |
$revision['html']['headers'] ?? null, |
| 988 |
$revision['contentmodel'] ?? null |
| 989 |
); |
| 990 |
|
| 991 |
$out = $parsoid->pb2pb( $pageConfig, 'redlinks', $pb, [] ); |
| 992 |
|
| 993 |
$this->validatePb( $out, $attribs['envOptions']['inputContentVersion'] ); |
| 994 |
|
| 995 |
$response = $this->getResponseFactory()->createJson( $out->responseData() ); |
| 996 |
ParsoidFormatHelper::setContentType( |
| 997 |
$response, ParsoidFormatHelper::FORMAT_PAGEBUNDLE, $out->version |
| 998 |
); |
| 999 |
return $response; |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
protected function languageConversion( |
| 1012 |
PageConfig $pageConfig, array $attribs, array $revision |
| 1013 |
) { |
| 1014 |
$opts = $attribs['opts']; |
| 1015 |
$target = $opts['updates']['variant']['target'] ?? |
| 1016 |
$attribs['envOptions']['htmlVariantLanguage']; |
| 1017 |
$source = $opts['updates']['variant']['source'] ?? null; |
| 1018 |
|
| 1019 |
if ( !$target ) { |
| 1020 |
throw new LocalizedHttpException( new MessageValue( "rest-target-variant-required" ), 400 ); |
| 1021 |
} |
| 1022 |
|
| 1023 |
$pageIdentity = $this->tryToCreatePageIdentity( $attribs ); |
| 1024 |
|
| 1025 |
$pb = new PageBundle( |
| 1026 |
$revision['html']['body'], |
| 1027 |
$revision['data-parsoid']['body'] ?? null, |
| 1028 |
$revision['data-mw']['body'] ?? null, |
| 1029 |
$attribs['envOptions']['inputContentVersion'], |
| 1030 |
$revision['html']['headers'] ?? null, |
| 1031 |
$revision['contentmodel'] ?? null |
| 1032 |
); |
| 1033 |
|
| 1034 |
|
| 1035 |
$languageVariantConverter = MediaWikiServices::getInstance() |
| 1036 |
->getHtmlTransformFactory() |
| 1037 |
->getLanguageVariantConverter( $pageIdentity ); |
| 1038 |
$languageVariantConverter->setPageConfig( $pageConfig ); |
| 1039 |
$httpContentLanguage = $attribs['pagelanguage' ] ?? null; |
| 1040 |
if ( $httpContentLanguage ) { |
| 1041 |
$languageVariantConverter->setPageLanguageOverride( $httpContentLanguage ); |
| 1042 |
} |
| 1043 |
|
| 1044 |
try { |
| 1045 |
$out = $languageVariantConverter->convertPageBundleVariant( $pb, $target, $source ); |
| 1046 |
} catch ( InvalidArgumentException $e ) { |
| 1047 |
throw new LocalizedHttpException( |
| 1048 |
new MessageValue( "rest-unsupported-language-conversion", [ $source ?? '(unspecified)', $target ] ), |
| 1049 |
400, |
| 1050 |
[ 'reason' => $e->getMessage() ] |
| 1051 |
); |
| 1052 |
} |
| 1053 |
|
| 1054 |
$response = $this->getResponseFactory()->createJson( $out->responseData() ); |
| 1055 |
ParsoidFormatHelper::setContentType( |
| 1056 |
$response, ParsoidFormatHelper::FORMAT_PAGEBUNDLE, $out->version |
| 1057 |
); |
| 1058 |
return $response; |
| 1059 |
} |
| 1060 |
|
| 1061 |
|
| 1062 |
abstract public function execute(): Response; |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
private function validatePb( PageBundle $pb, string $contentVersion ): void { |
| 1072 |
$errorMessage = ''; |
| 1073 |
if ( !$pb->validate( $contentVersion, $errorMessage ) ) { |
| 1074 |
throw new LocalizedHttpException( |
| 1075 |
new MessageValue( "rest-page-bundle-validation-error", [ $errorMessage ] ), |
| 1076 |
400 |
| 1077 |
); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
private function pageConfigToPageIdentity( PageConfig $page ): ProperPageIdentity { |
| 1088 |
$services = MediaWikiServices::getInstance(); |
| 1089 |
|
| 1090 |
$title = $page->getLinkTarget(); |
| 1091 |
try { |
| 1092 |
$page = $services->getPageStore()->getPageForLink( $title ); |
| 1093 |
} catch ( MalformedTitleException | InvalidArgumentException $e ) { |
| 1094 |
|
| 1095 |
|
| 1096 |
throw new HttpException( |
| 1097 |
"Bad title: $title", |
| 1098 |
400 |
| 1099 |
); |
| 1100 |
} |
| 1101 |
|
| 1102 |
return $page; |
| 1103 |
} |
| 1104 |
|
| 1105 |
} |