2020-03-16 23:30:26 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2022-08-09 23:22:02 +00:00
|
|
|
* Helper functions for contacting Parsoid/RESTBase from the action API.
|
2020-03-16 23:30:26 +00:00
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
2022-03-13 01:38:23 +00:00
|
|
|
namespace MediaWiki\Extension\VisualEditor;
|
|
|
|
|
|
|
|
use Language;
|
2020-03-16 23:30:26 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-06-10 15:03:26 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2022-03-13 01:38:23 +00:00
|
|
|
use Message;
|
2020-05-19 17:53:29 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\NullLogger;
|
2022-03-13 01:38:23 +00:00
|
|
|
use Title;
|
|
|
|
use WebRequest;
|
2020-03-16 23:30:26 +00:00
|
|
|
|
|
|
|
trait ApiParsoidTrait {
|
|
|
|
|
|
|
|
/**
|
2022-08-09 23:22:02 +00:00
|
|
|
* @var LoggerInterface
|
2020-03-16 23:30:26 +00:00
|
|
|
*/
|
2022-08-09 23:22:02 +00:00
|
|
|
private $logger = null;
|
2020-03-16 23:30:26 +00:00
|
|
|
|
|
|
|
/**
|
2020-05-19 17:53:29 +00:00
|
|
|
* @return LoggerInterface
|
2020-03-16 23:30:26 +00:00
|
|
|
*/
|
2021-07-21 19:15:31 +00:00
|
|
|
protected function getLogger(): LoggerInterface {
|
2020-05-19 17:53:29 +00:00
|
|
|
return $this->logger ?: new NullLogger();
|
2020-03-16 23:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-19 17:53:29 +00:00
|
|
|
* @param LoggerInterface $logger
|
2020-03-16 23:30:26 +00:00
|
|
|
*/
|
2020-06-10 15:18:06 +00:00
|
|
|
protected function setLogger( LoggerInterface $logger ) {
|
2020-03-16 23:30:26 +00:00
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
2020-06-10 15:03:26 +00:00
|
|
|
/**
|
|
|
|
* Get the latest revision of a title
|
|
|
|
*
|
|
|
|
* @param Title $title Page title
|
|
|
|
* @return RevisionRecord A revision record
|
|
|
|
*/
|
2021-07-21 19:15:31 +00:00
|
|
|
protected function getLatestRevision( Title $title ): RevisionRecord {
|
2020-06-10 15:03:26 +00:00
|
|
|
$revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
|
|
|
|
$latestRevision = $revisionLookup->getRevisionByTitle( $title );
|
|
|
|
if ( $latestRevision !== null ) {
|
|
|
|
return $latestRevision;
|
|
|
|
}
|
|
|
|
$this->dieWithError( 'apierror-visualeditor-latestnotfound', 'latestnotfound' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific revision of a title
|
|
|
|
*
|
|
|
|
* If the oldid is ommitted or is 0, the latest revision will be fetched.
|
|
|
|
*
|
|
|
|
* If the oldid is invalid, an API error will be reported.
|
|
|
|
*
|
2022-02-08 15:09:28 +00:00
|
|
|
* @param Title|null $title Page title, not required if $oldid is used
|
2020-06-10 15:03:26 +00:00
|
|
|
* @param int|string|null $oldid Optional revision ID.
|
|
|
|
* Should be an integer but will validate and convert user input strings.
|
|
|
|
* @return RevisionRecord A revision record
|
|
|
|
*/
|
2022-02-08 15:09:28 +00:00
|
|
|
protected function getValidRevision( Title $title = null, $oldid = null ): RevisionRecord {
|
2020-06-10 15:03:26 +00:00
|
|
|
$revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup();
|
|
|
|
if ( $oldid === null || $oldid === 0 ) {
|
|
|
|
return $this->getLatestRevision( $title );
|
|
|
|
} else {
|
|
|
|
$revisionRecord = $revisionLookup->getRevisionById( $oldid );
|
|
|
|
if ( $revisionRecord ) {
|
|
|
|
return $revisionRecord;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->dieWithError( [ 'apierror-nosuchrevid', $oldid ], 'oldidnotfound' );
|
|
|
|
}
|
|
|
|
|
2022-08-09 23:22:02 +00:00
|
|
|
/**
|
2022-09-05 10:36:16 +00:00
|
|
|
* @param array $response
|
2022-08-09 23:22:02 +00:00
|
|
|
*/
|
2022-09-05 10:36:16 +00:00
|
|
|
private function forwardErrorsAndCacheHeaders( array $response ) {
|
2022-09-19 12:55:27 +00:00
|
|
|
if ( !empty( $response['error'] ) ) {
|
2022-09-05 10:36:16 +00:00
|
|
|
$this->dieWithError( $response['error'] );
|
2022-08-09 23:22:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
// If response was received directly from Varnish, use the response
|
|
|
|
// (RP) header to declare the cache hit and pass the data to the client.
|
|
|
|
$headers = $response['headers'] ?? [];
|
|
|
|
if ( isset( $headers['x-cache'] ) && strpos( $headers['x-cache'], 'hit' ) !== false ) {
|
|
|
|
$this->getRequest()->response()->header( 'X-Cache: cached-response=true' );
|
2022-08-09 23:22:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 15:03:26 +00:00
|
|
|
/**
|
|
|
|
* Request page HTML from RESTBase
|
|
|
|
*
|
|
|
|
* @param RevisionRecord $revision Page revision
|
|
|
|
* @return array The RESTBase server's response
|
|
|
|
*/
|
2021-07-21 19:15:31 +00:00
|
|
|
protected function requestRestbasePageHtml( RevisionRecord $revision ): array {
|
2020-06-10 15:03:26 +00:00
|
|
|
$title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
|
2022-08-09 23:22:02 +00:00
|
|
|
$lang = self::getPageLanguage( $title );
|
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
$response = $this->getParsoidClient()->getPageHtml( $revision, $lang );
|
2022-08-09 23:22:02 +00:00
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
$this->forwardErrorsAndCacheHeaders( $response );
|
2022-08-09 23:22:02 +00:00
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
return $response;
|
2020-06-10 15:03:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 23:30:26 +00:00
|
|
|
/**
|
2020-07-22 13:35:59 +00:00
|
|
|
* Transform HTML to wikitext via Parsoid through RESTbase. Wrapper for ::postData().
|
2020-03-16 23:30:26 +00:00
|
|
|
*
|
|
|
|
* @param Title $title The title of the page
|
2020-07-22 13:35:59 +00:00
|
|
|
* @param string $html The HTML of the page to be transformed
|
|
|
|
* @param int|null $oldid What oldid revision, if any, to base the request from (default: `null`)
|
2020-06-26 15:33:32 +00:00
|
|
|
* @param string|null $etag The ETag to set in the HTTP request header
|
2020-07-22 13:35:59 +00:00
|
|
|
* @return array The RESTbase server's response, 'code', 'reason', 'headers' and 'body'
|
2020-03-16 23:30:26 +00:00
|
|
|
*/
|
2020-07-22 13:38:33 +00:00
|
|
|
protected function transformHTML(
|
2020-07-22 13:35:59 +00:00
|
|
|
Title $title, string $html, int $oldid = null, string $etag = null
|
2021-07-21 19:15:31 +00:00
|
|
|
): array {
|
2022-08-09 23:22:02 +00:00
|
|
|
$lang = self::getPageLanguage( $title );
|
2020-07-22 13:35:59 +00:00
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
$response = $this->getParsoidClient()->transformHTML( $title, $lang, $html, $oldid, $etag );
|
2022-08-09 23:22:02 +00:00
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
$this->forwardErrorsAndCacheHeaders( $response );
|
2022-08-09 23:22:02 +00:00
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
return $response;
|
2020-03-16 23:30:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:38:33 +00:00
|
|
|
/**
|
|
|
|
* Transform wikitext to HTML via Parsoid through RESTbase. Wrapper for ::postData().
|
|
|
|
*
|
|
|
|
* @param Title $title The title of the page to use as the parsing context
|
|
|
|
* @param string $wikitext The wikitext fragment to parse
|
|
|
|
* @param bool $bodyOnly Whether to provide only the contents of the `<body>` tag
|
|
|
|
* @param int|null $oldid What oldid revision, if any, to base the request from (default: `null`)
|
|
|
|
* @param bool $stash Whether to stash the result in the server-side cache (default: `false`)
|
|
|
|
* @return array The RESTbase server's response, 'code', 'reason', 'headers' and 'body'
|
|
|
|
*/
|
|
|
|
protected function transformWikitext(
|
|
|
|
Title $title, string $wikitext, bool $bodyOnly, int $oldid = null, bool $stash = false
|
2021-07-21 19:15:31 +00:00
|
|
|
): array {
|
2022-08-09 23:22:02 +00:00
|
|
|
$lang = self::getPageLanguage( $title );
|
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
$response = $this->getParsoidClient()->transformWikitext(
|
|
|
|
$title,
|
|
|
|
$lang,
|
|
|
|
$wikitext,
|
|
|
|
$bodyOnly,
|
|
|
|
$oldid,
|
|
|
|
$stash
|
|
|
|
);
|
2022-08-09 23:22:02 +00:00
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
$this->forwardErrorsAndCacheHeaders( $response );
|
2022-08-09 23:22:02 +00:00
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
return $response;
|
2020-07-22 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 23:30:26 +00:00
|
|
|
/**
|
|
|
|
* Get the page language from a title, using the content language as fallback on special pages
|
2020-07-22 13:35:59 +00:00
|
|
|
*
|
2020-10-07 13:24:50 +00:00
|
|
|
* @param Title $title
|
2020-03-16 23:30:26 +00:00
|
|
|
* @return Language Content language
|
|
|
|
*/
|
2021-07-21 19:15:31 +00:00
|
|
|
public static function getPageLanguage( Title $title ): Language {
|
2020-03-16 23:30:26 +00:00
|
|
|
if ( $title->isSpecial( 'CollabPad' ) ) {
|
|
|
|
// Use the site language for CollabPad, as getPageLanguage just
|
|
|
|
// returns the interface language for special pages.
|
|
|
|
// TODO: Let the user change the document language on multi-lingual sites.
|
|
|
|
return MediaWikiServices::getInstance()->getContentLanguage();
|
|
|
|
} else {
|
|
|
|
return $title->getPageLanguage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 10:36:16 +00:00
|
|
|
/**
|
|
|
|
* @see VisualEditorParsoidClientFactory
|
|
|
|
* @return ParsoidClient
|
|
|
|
*/
|
|
|
|
abstract protected function getParsoidClient(): ParsoidClient;
|
|
|
|
|
2020-03-16 23:30:26 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase
|
|
|
|
* @param string|array|Message $msg See ApiErrorFormatter::addError()
|
|
|
|
* @param string|null $code See ApiErrorFormatter::addError()
|
|
|
|
* @param array|null $data See ApiErrorFormatter::addError()
|
|
|
|
* @param int|null $httpCode HTTP error code to use
|
2022-02-05 15:49:40 +00:00
|
|
|
* @return never
|
2020-03-16 23:30:26 +00:00
|
|
|
*/
|
|
|
|
abstract public function dieWithError( $msg, $code = null, $data = null, $httpCode = null );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see ContextSource
|
|
|
|
* @return WebRequest
|
|
|
|
*/
|
|
|
|
abstract public function getRequest();
|
|
|
|
}
|