jenkins-bot has submitted this change and it was merged. Change subject: Use page-less Parsoid API for Flow ......................................................................
Use page-less Parsoid API for Flow Relevant Parsoid change at https://gerrit.wikimedia.org/r/#/c/91321/ Bug: 55682 Change-Id: I0cbb164f9ec7cb269b77f765bfdadee074b3770c --- M includes/ParsoidUtils.php 1 file changed, 35 insertions(+), 29 deletions(-) Approvals: EBernhardson: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/ParsoidUtils.php b/includes/ParsoidUtils.php index f015847..970a437 100644 --- a/includes/ParsoidUtils.php +++ b/includes/ParsoidUtils.php @@ -19,7 +19,7 @@ try { // use VE API (which connects to Parsoid) if available... return self::parsoid( $from, $to, $content ); - } catch ( \Exception $e ) { + } catch ( NoParsoidException $e ) { // ... otherwise default to parser return self::parser( $from, $to, $content ); } @@ -36,44 +36,47 @@ * @return string */ protected static function parsoid( $from, $to, $content ) { - if ( !class_exists( 'ApiVisualEditor' ) ) { - throw new \MWException( 'VisualEditor is unavailable' ); + global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix, $wgVisualEditorParsoidTimeout; + + if ( ! isset( $wgVisualEditorParsoidURL ) || ! $wgVisualEditorParsoidURL ) { + throw new NoParsoidException( "VisualEditor parsoid configuration is unavailable" ); } - if ( $to === 'html' ) { - $action = 'parsefragment'; - } elseif ( $to === 'wikitext' ) { - $action = 'serialize'; + if ( $from == 'html' ) { + $from = 'html'; + } elseif ( in_array( $from, array( 'wt', 'wikitext' ) ) ) { + $from = 'wt'; } else { - throw new \MWException( 'Unknown format: '. $to ); + throw new \MWException( 'Unknown source format: ' . $from ); } - global $wgRequest; - $params = new \DerivativeRequest( - $wgRequest, + $response = \Http::post( + $wgVisualEditorParsoidURL . '/' . $wgVisualEditorParsoidPrefix . '/', array( - 'action' => 'visualeditor', - // Bogus title used for parser - 'page' => \Title::newMainPage()->getPrefixedDBkey(), - // 'basetimestamp' => ?, - // 'starttimestamp' => ?, - 'paction' => $action, - 'oldid' => '', - $from => $content, - ), - true // POST + 'postData' => array( $from => $content ), + 'timeout' => $wgVisualEditorParsoidTimeout + ) ); - wfDebugLog( __CLASS__, __FUNCTION__ . ": Roundtripping parsoid for $from => $to" ); - $api = new \ApiMain( $params, true ); - $api->execute(); - $result = $api->getResultData(); - - if ( !isset( $result['visualeditor']['content'] ) ) { - throw new \MWException( 'Unable to parse content' ); + if ( $response === false ) { + throw new \MWException( 'Failed contacting parsoid' ); } - return $result['visualeditor']['content']; + // Full HTML document is returned, we only want what's inside <body> + if ( $to == 'html' ) { + $dom = new \DOMDocument(); + $dom->loadHTML( $response ); + $body = $dom->getElementsByTagName( 'body' )->item(0); + + $response = ''; + foreach( $body->childNodes as $child ) { + $response .= $child->ownerDocument->saveXML( $child ); + } + } elseif ( !in_array( $to, array( 'wt', 'wikitext' ) ) ) { + throw new \MWException( "Unknown format requested: " . $to ); + } + + return $response; } /** @@ -104,3 +107,6 @@ return $output->getText(); } } + +class NoParsoidExceptions extends \MWException {} + -- To view, visit https://gerrit.wikimedia.org/r/92633 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I0cbb164f9ec7cb269b77f765bfdadee074b3770c Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/extensions/Flow Gerrit-Branch: master Gerrit-Owner: Matthias Mullie <[email protected]> Gerrit-Reviewer: EBernhardson <[email protected]> Gerrit-Reviewer: Werdna <[email protected]> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
