Santhosh has uploaded a new change for review.
https://gerrit.wikimedia.org/r/203002
Change subject: Use ParsoidVirtualRESTService for HTML to Wikitext
transformation
......................................................................
Use ParsoidVirtualRESTService for HTML to Wikitext transformation
Change-Id: I4b0c4c078efc9fdc98863c22cb72945b67b72733
---
M api/ApiContentTranslationPublish.php
1 file changed, 46 insertions(+), 20 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation
refs/changes/02/203002/1
diff --git a/api/ApiContentTranslationPublish.php
b/api/ApiContentTranslationPublish.php
index c0158cd..b62b2d0 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['error'] );
+ }
+ }
+
+ /**
* Converts html to wikitext
*
* @param Title $title
* @param string $html
- * @return Status
- * @throw MWException
+ * @return string Wiki text
*/
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: newchange
Gerrit-Change-Id: I4b0c4c078efc9fdc98863c22cb72945b67b72733
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Santhosh <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits