2020-03-16 23:30:26 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Helper functions for contacting Parsoid/RESTBase.
|
|
|
|
*
|
|
|
|
* @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 Config;
|
|
|
|
use Exception;
|
|
|
|
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;
|
|
|
|
use ParsoidVirtualRESTService;
|
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 RestbaseVirtualRESTService;
|
|
|
|
use Title;
|
|
|
|
use VirtualRESTService;
|
|
|
|
use VirtualRESTServiceClient;
|
|
|
|
use WebRequest;
|
2020-03-16 23:30:26 +00:00
|
|
|
|
|
|
|
trait ApiParsoidTrait {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var VirtualRESTServiceClient
|
|
|
|
*/
|
|
|
|
protected $serviceClient = null;
|
|
|
|
|
|
|
|
/**
|
2020-05-19 17:53:29 +00:00
|
|
|
* @var LoggerInterface
|
2020-03-16 23:30:26 +00:00
|
|
|
*/
|
|
|
|
protected $logger = null;
|
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the virtual REST service object to be used in VE's API calls. The
|
|
|
|
* method determines whether to instantiate a ParsoidVirtualRESTService or a
|
|
|
|
* RestbaseVirtualRESTService object based on configuration directives: if
|
|
|
|
* $wgVirtualRestConfig['modules']['restbase'] is defined, RESTBase is chosen,
|
|
|
|
* otherwise Parsoid is used (either by using the MW Core config, or the
|
|
|
|
* VE-local one).
|
|
|
|
*
|
|
|
|
* @return VirtualRESTService the VirtualRESTService object to use
|
|
|
|
*/
|
2021-07-21 19:15:31 +00:00
|
|
|
protected function getVRSObject(): VirtualRESTService {
|
2020-07-07 20:16:06 +00:00
|
|
|
global $wgVisualEditorParsoidAutoConfig;
|
2020-03-16 23:30:26 +00:00
|
|
|
// the params array to create the service object with
|
|
|
|
$params = [];
|
|
|
|
// the VRS class to use, defaults to Parsoid
|
|
|
|
$class = ParsoidVirtualRESTService::class;
|
|
|
|
// The global virtual rest service config object, if any
|
|
|
|
$vrs = $this->getConfig()->get( 'VirtualRestConfig' );
|
|
|
|
if ( isset( $vrs['modules'] ) && isset( $vrs['modules']['restbase'] ) ) {
|
|
|
|
// if restbase is available, use it
|
|
|
|
$params = $vrs['modules']['restbase'];
|
|
|
|
// backward compatibility
|
|
|
|
$params['parsoidCompat'] = false;
|
|
|
|
$class = RestbaseVirtualRESTService::class;
|
|
|
|
} elseif ( isset( $vrs['modules'] ) && isset( $vrs['modules']['parsoid'] ) ) {
|
|
|
|
// there's a global parsoid config, use it next
|
|
|
|
$params = $vrs['modules']['parsoid'];
|
|
|
|
$params['restbaseCompat'] = true;
|
2020-07-07 20:16:06 +00:00
|
|
|
} elseif ( $wgVisualEditorParsoidAutoConfig ) {
|
|
|
|
$params = $vrs['modules']['parsoid'] ?? [];
|
|
|
|
$params['restbaseCompat'] = true;
|
2020-08-11 23:40:44 +00:00
|
|
|
// forward cookies on private wikis
|
|
|
|
$params['forwardCookies'] = !MediaWikiServices::getInstance()
|
|
|
|
->getPermissionManager()->isEveryoneAllowed( 'read' );
|
2020-03-16 23:30:26 +00:00
|
|
|
} else {
|
|
|
|
// No global modules defined, so no way to contact the document server.
|
|
|
|
$this->dieWithError( 'apierror-visualeditor-docserver-unconfigured', 'no_vrs' );
|
|
|
|
}
|
|
|
|
// merge the global and service-specific params
|
|
|
|
if ( isset( $vrs['global'] ) ) {
|
|
|
|
$params = array_merge( $vrs['global'], $params );
|
|
|
|
}
|
|
|
|
// set up cookie forwarding
|
|
|
|
if ( $params['forwardCookies'] ) {
|
|
|
|
$params['forwardCookies'] = $this->getRequest()->getHeader( 'Cookie' );
|
|
|
|
} else {
|
|
|
|
$params['forwardCookies'] = false;
|
|
|
|
}
|
|
|
|
// create the VRS object and return it
|
|
|
|
return new $class( $params );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the object which directs queries to the virtual REST service, depending on the path.
|
|
|
|
*
|
|
|
|
* @return VirtualRESTServiceClient
|
|
|
|
*/
|
2021-07-21 19:15:31 +00:00
|
|
|
protected function getVRSClient(): VirtualRESTServiceClient {
|
2020-03-16 23:30:26 +00:00
|
|
|
if ( !$this->serviceClient ) {
|
2020-05-21 04:54:55 +00:00
|
|
|
$this->serviceClient = new VirtualRESTServiceClient(
|
|
|
|
MediaWikiServices::getInstance()->getHttpRequestFactory()->createMultiClient() );
|
2020-03-16 23:30:26 +00:00
|
|
|
$this->serviceClient->mount( '/restbase/', $this->getVRSObject() );
|
|
|
|
}
|
|
|
|
return $this->serviceClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accessor function for all RESTbase requests
|
|
|
|
*
|
|
|
|
* @param Title $title The title of the page to use as the parsing context
|
|
|
|
* @param string $method The HTTP method, either 'GET' or 'POST'
|
|
|
|
* @param string $path The RESTbase api path
|
|
|
|
* @param array $params Request parameters
|
|
|
|
* @param array $reqheaders Request headers
|
|
|
|
* @return array The RESTbase server's response, 'code', 'reason', 'headers' and 'body'
|
|
|
|
*/
|
2020-06-10 15:18:06 +00:00
|
|
|
protected function requestRestbase(
|
|
|
|
Title $title, string $method, string $path, array $params, array $reqheaders = []
|
2021-07-21 19:15:31 +00:00
|
|
|
): array {
|
2020-03-16 23:30:26 +00:00
|
|
|
// Should be synchronised with modules/ve-mw/init/ve.init.mw.ArticleTargetLoader.js
|
2021-08-26 09:37:13 +00:00
|
|
|
$reqheaders += [
|
2020-03-16 23:30:26 +00:00
|
|
|
'Accept' =>
|
|
|
|
'text/html; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/HTML/2.0.0"',
|
|
|
|
'Accept-Language' => self::getPageLanguage( $title )->getCode(),
|
2020-05-15 19:09:24 +00:00
|
|
|
'User-Agent' => 'VisualEditor-MediaWiki/' . MW_VERSION,
|
|
|
|
'Api-User-Agent' => 'VisualEditor-MediaWiki/' . MW_VERSION,
|
2021-02-04 17:52:43 +00:00
|
|
|
'Promise-Non-Write-API-Action' => 'true',
|
2020-03-16 23:30:26 +00:00
|
|
|
];
|
2021-08-26 09:37:13 +00:00
|
|
|
$request = [
|
|
|
|
'method' => $method,
|
|
|
|
'url' => '/restbase/local/v1/' . $path,
|
|
|
|
( $method === 'GET' ? 'query' : 'body' ) => $params,
|
|
|
|
'headers' => $reqheaders,
|
|
|
|
];
|
2020-03-16 23:30:26 +00:00
|
|
|
$response = $this->getVRSClient()->run( $request );
|
|
|
|
if ( $response['code'] === 200 && $response['error'] === "" ) {
|
|
|
|
// If response was served 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 ) {
|
2021-08-26 09:37:13 +00:00
|
|
|
$this->getRequest()->response()->header( 'X-Cache: cached-response=true' );
|
2020-03-16 23:30:26 +00:00
|
|
|
}
|
|
|
|
} elseif ( $response['error'] !== '' ) {
|
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-visualeditor-docserver-http-error', wfEscapeWikiText( $response['error'] ) ],
|
|
|
|
'apierror-visualeditor-docserver-http-error'
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// error null, code not 200
|
|
|
|
$this->getLogger()->warning(
|
|
|
|
__METHOD__ . ": Received HTTP {code} from RESTBase",
|
|
|
|
[
|
|
|
|
'code' => $response['code'],
|
|
|
|
'trace' => ( new Exception )->getTraceAsString(),
|
2021-11-04 21:27:07 +00:00
|
|
|
'response' => [ 'body' => $response['body'] ],
|
2020-03-16 23:30:26 +00:00
|
|
|
'requestPath' => $path,
|
|
|
|
'requestIfMatch' => $reqheaders['If-Match'] ?? '',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-visualeditor-docserver-http', $response['code'] ],
|
|
|
|
'apierror-visualeditor-docserver-http'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
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' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() );
|
|
|
|
return $this->requestRestbase(
|
|
|
|
$title,
|
|
|
|
'GET',
|
|
|
|
'page/html/' . urlencode( $title->getPrefixedDBkey() ) .
|
|
|
|
'/' . $revision->getId() .
|
|
|
|
'?redirect=false&stash=true',
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-07-22 13:35:59 +00:00
|
|
|
$data = [ 'html' => $html, 'scrub_wikitext' => 1 ];
|
|
|
|
$path = 'transform/html/to/wikitext/' . urlencode( $title->getPrefixedDBkey() ) .
|
|
|
|
( $oldid === null ? '' : '/' . $oldid );
|
|
|
|
|
2020-03-16 23:30:26 +00:00
|
|
|
// Adapted from RESTBase mwUtil.parseETag()
|
|
|
|
// ETag is not expected when creating a new page (oldid=0)
|
2020-08-17 22:36:46 +00:00
|
|
|
if ( $oldid && !( preg_match( '/
|
2020-03-16 23:30:26 +00:00
|
|
|
^(?:W\\/)?"?
|
2020-08-17 22:36:46 +00:00
|
|
|
' . preg_quote( "$oldid", '/' ) . '
|
2020-03-16 23:30:26 +00:00
|
|
|
(?:\\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}))
|
|
|
|
(?:\\/([^"]+))?
|
|
|
|
"?$
|
2020-08-17 22:36:46 +00:00
|
|
|
/x', $etag ) ) ) {
|
2020-03-16 23:30:26 +00:00
|
|
|
$this->getLogger()->info(
|
2020-08-17 22:36:46 +00:00
|
|
|
__METHOD__ . ": Received funny ETag from client: '{etag}'",
|
2020-03-16 23:30:26 +00:00
|
|
|
[
|
|
|
|
'etag' => $etag,
|
2020-08-17 22:36:46 +00:00
|
|
|
'oldid' => $oldid,
|
2020-03-16 23:30:26 +00:00
|
|
|
'requestPath' => $path,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $this->requestRestbase(
|
|
|
|
$title,
|
|
|
|
'POST', $path, $data,
|
|
|
|
[ 'If-Match' => $etag ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-07-22 13:38:33 +00:00
|
|
|
return $this->requestRestbase(
|
|
|
|
$title,
|
|
|
|
'POST',
|
|
|
|
'transform/wikitext/to/html/' . urlencode( $title->getPrefixedDBkey() ) .
|
|
|
|
( $oldid === null ? '' : '/' . $oldid ),
|
|
|
|
[
|
|
|
|
'wikitext' => $wikitext,
|
|
|
|
'body_only' => $bodyOnly ? 1 : 0,
|
|
|
|
'stash' => $stash ? 1 : 0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 Config
|
|
|
|
*/
|
|
|
|
abstract public function getConfig();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see ContextSource
|
|
|
|
* @return WebRequest
|
|
|
|
*/
|
|
|
|
abstract public function getRequest();
|
|
|
|
}
|
2022-03-13 01:38:23 +00:00
|
|
|
|
|
|
|
class_alias( ApiParsoidTrait::class, 'ApiParsoidTrait' );
|