Move parseWikitextFragment up to ApiParsoidTrait

Rename to 'transformWikitext' and rename 'postHTML' to match.

Change-Id: Icb1f519c7a3a88f36c51479333be8cfb5617dbe0
This commit is contained in:
Ed Sanders 2020-07-22 14:38:33 +01:00
parent 90bf7e0488
commit eb98439453
3 changed files with 31 additions and 31 deletions

View file

@ -240,7 +240,7 @@ trait ApiParsoidTrait {
* @param string|null $etag The ETag to set in the HTTP request header
* @return array The RESTbase server's response, 'code', 'reason', 'headers' and 'body'
*/
protected function postHTML(
protected function transformHTML(
Title $title, string $html, int $oldid = null, string $etag = null
) : array {
$data = [ 'html' => $html, 'scrub_wikitext' => 1 ];
@ -271,6 +271,32 @@ trait ApiParsoidTrait {
);
}
/**
* 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
) : array {
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
]
);
}
/**
* Get the page language from a title, using the content language as fallback on special pages
*

View file

@ -43,32 +43,6 @@ class ApiVisualEditor extends ApiBase {
->serialize( 'text/x-wiki' );
}
/**
* Provide the RESTbase-parsed HTML of a given fragment of wikitext
*
* @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 parseWikitextFragment(
Title $title, $wikitext, $bodyOnly, $oldid = null, $stash = false
) {
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
]
);
}
/**
* Provide the preload content for a page being created from another page
*
@ -169,7 +143,7 @@ class ApiVisualEditor extends ApiBase {
'@phan-var WikitextContent $newSectionContent';
$wikitext = $newSectionContent->getText();
}
$response = $this->parseWikitextFragment(
$response = $this->transformWikitext(
$title, $wikitext, false, $oldid, $stash
);
} else {
@ -248,7 +222,7 @@ class ApiVisualEditor extends ApiBase {
}
if ( $content !== '' && $params['paction'] !== 'wikitext' ) {
$response = $this->parseWikitextFragment( $title, $content, false, null, true );
$response = $this->transformWikitext( $title, $content, false, null, true );
$content = $response['body'];
$restbaseHeaders = $response['headers'];
}
@ -552,7 +526,7 @@ class ApiVisualEditor extends ApiBase {
if ( $params['pst'] ) {
$wikitext = $this->pstWikitext( $title, $wikitext );
}
$content = $this->parseWikitextFragment(
$content = $this->transformWikitext(
$title, $wikitext, $bodyOnly
)['body'];
if ( $content === false ) {

View file

@ -185,7 +185,7 @@ class ApiVisualEditorEdit extends ApiBase {
} else {
$html = $params['html'];
}
$wikitext = $this->postHTML(
$wikitext = $this->transformHTML(
$title, $html, $parserParams['oldid'] ?? null, $params['etag'] ?? null
)['body'];
return $wikitext;