jenkins-bot has submitted this change and it was merged. Change subject: Use ParsoidVirtualRESTService for HTML to Wikitext transformation ......................................................................
Use ParsoidVirtualRESTService for HTML to Wikitext transformation Bug: T95542 Change-Id: I4b0c4c078efc9fdc98863c22cb72945b67b72733 --- M api/ApiContentTranslationPublish.php 1 file changed, 46 insertions(+), 20 deletions(-) Approvals: Nikerabbit: Looks good to me, approved jenkins-bot: Verified diff --git a/api/ApiContentTranslationPublish.php b/api/ApiContentTranslationPublish.php index c0158cd..c776152 100644 --- a/api/ApiContentTranslationPublish.php +++ b/api/ApiContentTranslationPublish.php @@ -18,36 +18,62 @@ class ApiContentTranslationPublish extends ApiBase { /** + * @var VirtualRESTServiceClient + */ + protected $serviceClient; + + public function __construct( ApiMain $main, $name ) { + global $wgContentTranslationParsoid; + parent::__construct( $main, $name ); + $this->serviceClient = new VirtualRESTServiceClient( new MultiHttpClient( array() ) ); + $parsoidConfig = $wgContentTranslationParsoid; + $this->serviceClient->mount( '/parsoid/', new ParsoidVirtualRESTService( array( + 'URL' => $parsoidConfig['url'], + 'prefix' => $parsoidConfig['prefix'], + 'timeout' => $parsoidConfig['timeout'], + ) ) ); + } + + private function requestParsoid( $method, $path, $params ) { + $request = array( + 'method' => $method, + 'url' => '/parsoid/local/v1/' . $path + ); + if ( $method === 'GET' ) { + $request['query'] = $params; + } else { + $request['body'] = $params; + } + $response = $this->serviceClient->run( $request ); + if ( $response['code'] === 200 && $response['error'] === '' ) { + return $response['body']; + } elseif ( $response['error'] !== '' ) { + $this->dieUsage( 'parsoidserver-http-error: ' . $response['code'], $response['error'] ); + } else { // error null, code not 200 + $this->dieUsage( 'parsoidserver-http: HTTP ' . $response['code'], $response['code'] ); + } + } + + /** * Converts html to wikitext * * @param Title $title * @param string $html - * @return Status - * @throw MWException + * @return string wikitext */ protected function convertHtmlToWikitext( Title $title, $html ) { - global $wgContentTranslationParsoid; - - $conf = $wgContentTranslationParsoid; - $page = urlencode( $title->getPrefixedDBkey() ); - - $req = MWHttpRequest::factory( - "{$conf['url']}/{$conf['prefix']}/$page", + $wikitext = $this->requestParsoid( + 'POST', + 'transform/html/to/wikitext/' . urlencode( $title->getPrefixedDBkey() ), array( - 'method' => 'POST', - 'postData' => array( - 'content' => $html, - ), - 'timeout' => $conf['timeout'], + 'html' => $html, + 'oldid' => $parserParams['oldid'], ) ); - - $status = $req->execute(); - if ( !$status->isOK() ) { - throw new MWException( $status ); + if ( $wikitext === false ) { + $this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' ); } - - return $req->getContent(); + return $wikitext; } protected function saveWikitext( $title, $wikitext, $params ) { -- To view, visit https://gerrit.wikimedia.org/r/203002 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I4b0c4c078efc9fdc98863c22cb72945b67b72733 Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/extensions/ContentTranslation Gerrit-Branch: master Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com> Gerrit-Reviewer: Nikerabbit <niklas.laxst...@gmail.com> Gerrit-Reviewer: Santhosh <santhosh.thottin...@gmail.com> Gerrit-Reviewer: Subramanya Sastry <ssas...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits