jenkins-bot has submitted this change and it was merged.

Change subject: Add html to wikitext conversion
......................................................................


Add html to wikitext conversion

Change-Id: Ie166f2fdfdfc59f3d4f2872f3832ed29ff23b949
---
M ContentTranslation.php
M api/ApiContentTranslationPublish.php
M modules/source/ext.cx.source.js
M modules/translation/ext.cx.publish.js
M modules/translation/ext.cx.translation.js
5 files changed, 89 insertions(+), 15 deletions(-)

Approvals:
  Amire80: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/ContentTranslation.php b/ContentTranslation.php
index cdb0cc1..51e2878 100644
--- a/ContentTranslation.php
+++ b/ContentTranslation.php
@@ -48,6 +48,12 @@
 $GLOBALS['wgContentTranslationServerURL'] = 'http://localhost:8000';
 $GLOBALS['wgContentTranslationServerTimeout'] = 15;
 
+$GLOBALS['wgContentTranslationParsoid'] = array(
+       'url' => 'http://parsoid.wmflabs.org/',
+       'timeout' => 15,
+       'prefix' => 'enwiki',
+);
+
 $GLOBALS['wgExtensionMessagesFiles']['ContentTranslationAlias'] =
        "$dir/ContentTranslation.alias.php";
 
diff --git a/api/ApiContentTranslationPublish.php 
b/api/ApiContentTranslationPublish.php
index 1ba4ab3..8edf224 100644
--- a/api/ApiContentTranslationPublish.php
+++ b/api/ApiContentTranslationPublish.php
@@ -10,6 +10,40 @@
  */
 
 class ApiContentTranslationPublish extends ApiBase {
+
+       /**
+        * Converts html to wikitext
+        *
+        * @param Title $title
+        * @param string $html
+        * @return Status
+        * @throw MWException
+        */
+       protected function convertHtmlToWikitext( Title $title, $html ) {
+               global $wgContentTranslationParsoid;
+
+               $conf = $wgContentTranslationParsoid;
+               $page = urlencode( $title->getPrefixedDBkey() );
+
+               $req = MWHttpRequest::factory(
+                       "{$conf['url']}/{$conf['prefix']}/$page",
+                       array(
+                               'method' => 'POST',
+                               'postData' => array(
+                                       'content' => $html,
+                               ),
+                               'timeout' => $conf['timeout'],
+                       )
+               );
+
+               $status = $req->execute();
+               if ( !$status->isOK() ) {
+                       throw new MWException( $status->getMessage()->text() );
+               }
+
+               return $req->getContent();
+       }
+
        protected function saveWikitext( $title, $wikitext, $params ) {
                $summary = $this->msg(
                        'cx-publish-summary',
@@ -39,18 +73,22 @@
 
        public function execute() {
                $params = $this->extractRequestParams();
-               $title = Title::newFromText( $params['title'] );
 
+               $title = Title::newFromText( $params['title'] );
                if ( !$title ) {
                        $this->dieUsageMsg( 'invalidtitle', $params['title'] );
                }
 
-               // TODO Should convert contenteditable HTML to Wikitext
-               $saveresult = $this->saveWikitext( $title, $params['text'], 
$params );
+               try {
+                       $wikitext = $this->convertHtmlToWikitext( $title, 
$params['html'] );
+               } catch ( MWException $e ) {
+                       $this->dieUsage( 'Conversion to wikitext with parsoid 
failed', 'parsoidserver' );
+               }
+
+               $saveresult = $this->saveWikitext( $title, $wikitext, $params );
                $editStatus = $saveresult['edit']['result'];
 
-               if ( isset( $saveresult['edit']['result'] ) || $editStatus === 
'Success' ) {
-                       // TODO Can it be false?
+               if ( $editStatus === 'Success' ) {
                        if ( isset( $saveresult['edit']['newrevid'] ) ) {
                                ChangeTags::addTags( 'contenttranslation', null,
                                        intval( $saveresult['edit']['newrevid'] 
),
@@ -83,7 +121,7 @@
                        'token' => array(
                                ApiBase::PARAM_REQUIRED => true,
                        ),
-                       'text' => array(
+                       'html' => array(
                                ApiBase::PARAM_REQUIRED => true,
                        ),
                        'sourcetitle' => array(
@@ -112,12 +150,12 @@
                return array(
                        'title' => 'The title of the page to perform actions 
on.',
                        'token' => 'Edit token',
-                       'text' => 'The wikitext to save in the page.',
+                       'html' => 'The content to save.',
                        'sourcetitle' => 'The title of the source page.',
                );
        }
 
        public function getDescription() {
-               return 'Save a page created using ContentTranslation to 
MediaWiki.';
+               return 'Save a page created using the content translation 
extension.';
        }
 }
diff --git a/modules/source/ext.cx.source.js b/modules/source/ext.cx.source.js
index fad2c0b..5803098 100644
--- a/modules/source/ext.cx.source.js
+++ b/modules/source/ext.cx.source.js
@@ -73,6 +73,8 @@
 
        ContentTranslationSource.prototype.load = function () {
                this.$content.html( mw.cx.data.segmentedContent );
+               // @todo figure out what should be done here
+               this.$content.find( 'base' ).detach();
                // Disable all links
                this.disableLinks();
                mw.hook( 'mw.cx.source.loaded' ).fire();
diff --git a/modules/translation/ext.cx.publish.js 
b/modules/translation/ext.cx.publish.js
index 863b807..4d48b5d 100644
--- a/modules/translation/ext.cx.publish.js
+++ b/modules/translation/ext.cx.publish.js
@@ -11,14 +11,39 @@
 ( function ( $, mw ) {
        'use strict';
 
+       /**
+        * Prepare the translated content for publishing by removing
+        * unwanted parts.
+        * @return {string} processed html
+        */
+       function prepareTranslationForPublish() {
+               var $translatedContent;
+
+               // TODO: Refactor so that this module is not grabbing random 
dom nodes
+               $translatedContent = $( '.cx-column--translation 
.cx-column__content' ).clone();
+               $translatedContent.find( '.cx-segment' ).replaceWith( function 
() {
+                       return $( this ).html();
+               } );
+
+               // Remove placeholder sections that are empty
+               // TODO: This can be better done if all placeholder sections 
has a semantic
+               // class which get removed when content is inserted.
+               $translatedContent.find( mw.cx.getSectionSelector() ).each( 
function () {
+                       if ( !$( this ).text().trim() ) {
+                               $( this ).remove();
+                       }
+               } );
+
+               return $translatedContent.html();
+
+       }
+
        mw.cx.publish = function () {
                var translatedTitle, translatedContent, sourceTitle;
 
-               // @todo: Refactor so that this module is not grabbing random 
dom nodes
                sourceTitle = $( '.cx-column--source > h2' ).text();
                translatedTitle = $( '.cx-column--translation > h2' ).text();
-               translatedContent = $( '.cx-column--translation 
.cx-column__content' ).text();
-
+               translatedContent = prepareTranslationForPublish();
                // To be saved under User:UserName
                translatedTitle = 'User:' + mw.user.getName() + '/' + 
translatedTitle;
                publishTranslation( translatedTitle, translatedContent, 
sourceTitle )
@@ -46,7 +71,7 @@
                return api.postWithEditToken( {
                        action: 'cxpublish',
                        title: title,
-                       text: content,
+                       html: content,
                        sourcetitle: sourceTitle
                } );
        }
diff --git a/modules/translation/ext.cx.translation.js 
b/modules/translation/ext.cx.translation.js
index bf0a8ce..a9adf14 100644
--- a/modules/translation/ext.cx.translation.js
+++ b/modules/translation/ext.cx.translation.js
@@ -67,6 +67,7 @@
                        this.$container.append( $subHeading );
                }
 
+               // Why is this needed??
                $content = $( '<div>' )
                        .addClass( 'cx-column__content' )
                        .html( '\n' ); // Make sure that it's visible to the 
tests
@@ -117,7 +118,7 @@
         * Generate a jquery selector for all sections
         * @return {string} the section selector string
         */
-       ContentTranslationEditor.prototype.getSectionSelector = function () {
+       mw.cx.getSectionSelector = function () {
                var i, sectionSelector = '',
                        sectionTypes = [
                                'div', 'p',
@@ -217,7 +218,7 @@
        ContentTranslationEditor.prototype.addPlaceholders = function () {
                var $content, template,
                        $sections, i, $section, sourceSectionId, $sourceSection,
-                       cxSectionSelector = this.getSectionSelector();
+                       cxSectionSelector = mw.cx.getSectionSelector();
 
                // Clone the source article and work on this detached object
                // to help performance
@@ -253,7 +254,7 @@
                        } );
 
                        $section.attr( {
-                               'id': 't' + sourceSectionId,
+                               id: 't' + sourceSectionId,
                                'data-source': sourceSectionId,
                                // Sections are editable if they are not 
templates
                                'contenteditable': template ? false : true
@@ -269,6 +270,8 @@
                                .hover( sourceSectionMouseEnterHandler, 
sourceSectionMouseLeaveHandler );
                }
 
+               this.$container.find( '.cx-column__content' ).remove();
+
                // Attach $content to container
                this.$container.append( $content );
        };

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie166f2fdfdfc59f3d4f2872f3832ed29ff23b949
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>
Gerrit-Reviewer: Amire80 <[email protected]>
Gerrit-Reviewer: KartikMistry <[email protected]>
Gerrit-Reviewer: Santhosh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to