Matthias Mullie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/95599


Change subject: Allow title to be passed to Parsoid
......................................................................

Allow title to be passed to Parsoid

Change-Id: I6bf62d6cb35b90ca42ee2cf0fabc30b9f78d2d15
---
M includes/ParsoidUtils.php
M includes/api/ApiParsoidUtilsFlow.php
M modules/editor/ext.flow.parsoid.js
3 files changed, 45 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/99/95599/1

diff --git a/includes/ParsoidUtils.php b/includes/ParsoidUtils.php
index c9919fe..037eec9 100644
--- a/includes/ParsoidUtils.php
+++ b/includes/ParsoidUtils.php
@@ -2,45 +2,56 @@
 
 namespace Flow;
 
+use Title;
+
 abstract class ParsoidUtils {
        /**
-        * Convert form/to wikitext/html.
+        * Convert from/to wikitext/html.
         *
         * @param string $from Format of content to convert: html|wikitext
         * @param string $to Format to convert to: html|wikitext
         * @param string $content
+        * @param Title[optional] $title
         * @return string
         */
-       public static function convert( $from, $to, $content ) {
+       public static function convert( $from, $to, $content, Title $title = 
null ) {
                if ( $from === $to || $content === '' ) {
                        return $content;
                }
 
-               //throw new \MWException( "Attempt to round trip '$from' -> 
'$to'" );
+               if ( !$title instanceof Title ) {
+                       $title = Title::newMainPage();
+               } elseif ( !$title->exists() ) {
+                       // Parsoid will fail if title does not exist
+                       throw new \MWException( 'Title "' . 
$title->getPrefixedDBkey() . '" does not exist.' );
+               }
+
                try {
                        // use VE API (which connects to Parsoid) if 
available...
-                       return self::parsoid( $from, $to, $content );
+                       return self::parsoid( $from, $to, $content, $title );
                } catch ( NoParsoidException $e ) {
                        // ... otherwise default to parser
-                       return self::parser( $from, $to, $content );
+                       return self::parser( $from, $to, $content, $title );
                }
        }
 
        /**
-        * Convert form/to wikitext/html using VisualEditor's API.
+        * Convert from/to wikitext/html via Parsoid, piggy-backing on
+        * VisualEditor's globals.
         *
         * This will assume Parsoid is installed, which is a dependency of VE.
         *
         * @param string $from Format of content to convert: html|wikitext
         * @param string $to Format to convert to: html|wikitext
         * @param string $content
+        * @param Title $title
         * @return string
         */
-       protected static function parsoid( $from, $to, $content ) {
+       protected static function parsoid( $from, $to, $content, Title $title ) 
{
                global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix, 
$wgVisualEditorParsoidTimeout;
 
-               if ( ! isset( $wgVisualEditorParsoidURL ) || ! 
$wgVisualEditorParsoidURL ) {
-                       throw new NoParsoidException( "VisualEditor parsoid 
configuration is unavailable" );
+               if ( !isset( $wgVisualEditorParsoidURL ) || ! 
$wgVisualEditorParsoidURL ) {
+                       throw new NoParsoidException( 'VisualEditor Parsoid 
configuration is unavailable' );
                }
 
                if ( $from == 'html' ) {
@@ -52,8 +63,7 @@
                }
 
                $response = \Http::post(
-                       // @todo needs a big refactor to get a page title in 
here, fake Main_Page for now
-                       $wgVisualEditorParsoidURL . '/' . 
$wgVisualEditorParsoidPrefix . '/Main_Page',
+                       $wgVisualEditorParsoidURL . '/' . 
$wgVisualEditorParsoidPrefix . '/' . $title->getPrefixedDBkey(),
                        array(
                                'postData' => array( $from => $content ),
                                'timeout' => $wgVisualEditorParsoidTimeout
@@ -61,7 +71,7 @@
                );
 
                if ( $response === false ) {
-                       throw new \MWException( 'Failed contacting parsoid' );
+                       throw new \MWException( 'Failed contacting Parsoid' );
                }
 
                // Full HTML document is returned, we only want what's inside 
<body>
@@ -91,24 +101,22 @@
        }
 
        /**
-        * Convert form/to wikitext/html using Parser.
+        * Convert from/to wikitext/html using Parser.
         *
         * This only supports wikitext to HTML.
         *
         * @param string $from Format of content to convert: wikitext
         * @param string $to Format to convert to: html
         * @param string $content
+        * @param Title $title
         * @return string
         */
-       protected static function parser( $from, $to, $content ) {
+       protected static function parser( $from, $to, $content, Title $title ) {
                if ( $from !== 'wikitext' && $to !== 'html' ) {
                        throw new \MWException( 'Parser only supports wikitext 
to HTML conversion' );
                }
 
                global $wgParser;
-
-               // Bogus title used for parser
-               $title = \Title::newMainPage();
 
                $options = new \ParserOptions;
                $options->setTidy( true );
@@ -120,4 +128,3 @@
 }
 
 class NoParsoidExceptions extends \MWException {}
-
diff --git a/includes/api/ApiParsoidUtilsFlow.php 
b/includes/api/ApiParsoidUtilsFlow.php
index 0154615..edadd9a 100644
--- a/includes/api/ApiParsoidUtilsFlow.php
+++ b/includes/api/ApiParsoidUtilsFlow.php
@@ -10,9 +10,16 @@
        public function execute() {
                $params = $this->extractRequestParams();
 
+               $title = null;
+               // if either title of pageid is tossed in, we can pass Title to 
Parsoid
+               if ( $this->getParameter( 'title' ) !== null || 
$this->getParameter( 'pageid' ) !== null ) {
+                       $page = $this->getTitleOrPageId( $params );
+                       $title = $page->getTitle();
+               }
+
                $result = array(
                        'format' => $params['to'],
-                       'content' => ParsoidUtils::convert( $params['from'], 
$params['to'], $params['content'] ),
+                       'content' => ParsoidUtils::convert( $params['from'], 
$params['to'], $params['content'], $title ),
                );
                $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
        }
@@ -28,14 +35,22 @@
                        'content' => array(
                                ApiBase::PARAM_REQUIRED => true,
                        ),
+                       'title' => null,
+                       'pageid' => array(
+                               ApiBase::PARAM_ISMULTI => false,
+                               ApiBase::PARAM_TYPE => 'integer'
+                       ),
                );
        }
 
        public function getParamDescription() {
+               $p = $this->getModulePrefix();
                return array(
                        'from' => 'Format of content tossed in (html|wikitext)',
                        'to' => 'Format to convert content to (html|wikitext)',
                        'content' => 'Content to be converted',
+                       'title' => "Title of the page. Cannot be used together 
with {$p}pageid",
+                       'pageid' => "ID of the page. Cannot be used together 
with {$p}title",
                );
        }
 
diff --git a/modules/editor/ext.flow.parsoid.js 
b/modules/editor/ext.flow.parsoid.js
index ac0b145..ab501b2 100644
--- a/modules/editor/ext.flow.parsoid.js
+++ b/modules/editor/ext.flow.parsoid.js
@@ -6,9 +6,10 @@
                 * @param {string} from Input format: html|wikitext
                 * @param {string} to Desired output format: html|wikitext
                 * @param {string} content Content to convert
+                * @param {string} [title] Page title
                 * @return {string}
                 */
-               convert: function ( from, to, content ) {
+               convert: function ( from, to, content, title ) {
                        if ( from !== to ) {
                                var api = new mw.Api( { ajax: { async: false } 
} );
 
@@ -16,7 +17,8 @@
                                        action: 'flow-parsoid-utils',
                                        parsefrom: from,
                                        parseto: to,
-                                       parsecontent: content
+                                       parsecontent: content,
+                                       parsetitle: title
                                } )
                                .done( function ( data ) {
                                        content = 
data['flow-parsoid-utils'].content;

-- 
To view, visit https://gerrit.wikimedia.org/r/95599
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6bf62d6cb35b90ca42ee2cf0fabc30b9f78d2d15
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to