Nikerabbit has uploaded a new change for review.

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

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, 67 insertions(+), 17 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation 
refs/changes/49/126949/1

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..606b533 100644
--- a/modules/translation/ext.cx.publish.js
+++ b/modules/translation/ext.cx.publish.js
@@ -17,7 +17,9 @@
                // @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 = $( '.cx-column--translation 
.cx-column__content' ).clone();
+               translatedContent.find( '.cx-segment' ).replaceWith( function 
() { return $( this ).html(); } );
+               translatedContent = translatedContent.html();
 
                // To be saved under User:UserName
                translatedTitle = 'User:' + mw.user.getName() + '/' + 
translatedTitle;
@@ -46,7 +48,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 447a053..f6ecc06 100644
--- a/modules/translation/ext.cx.translation.js
+++ b/modules/translation/ext.cx.translation.js
@@ -62,6 +62,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
@@ -181,17 +182,17 @@
                return selector;
        }
 
-       function souceSectionClickHandler() {
+       function sourceSectionClickHandler() {
                /*jshint validthis:true */
                $( jquerySelectorForId( $( this ).attr( 'id' ), 't' ) ).click();
        }
 
-       function souceSectionMouseEnterHandler() {
+       function sourceSectionMouseEnterHandler() {
                /*jshint validthis:true */
                $( jquerySelectorForId( $( this ).attr( 'id' ), 't' ) 
).mouseenter();
        }
 
-       function souceSectionMouseLeaveHandler() {
+       function sourceSectionMouseLeaveHandler() {
                /*jshint validthis:true */
                $( jquerySelectorForId( $( this ).attr( 'id' ), 't' ) 
).mouseleave();
        }
@@ -224,10 +225,10 @@
                                width: $sourceSection.width()
                        } );
                        $section.attr( {
-                               'id': 't' + sourceSectionId,
+                               id: 't' + sourceSectionId,
                                'data-source': sourceSectionId,
                                // Sections are editable
-                               'contenteditable': true
+                               contenteditable: true
                        } );
 
                        // Attach event handlers for sections
@@ -235,9 +236,10 @@
                                .hover( sectionMouseEnterHandler, 
sectionMouseLeaveHandler )
                                .on( 'click', sectionClick );
                        // Bind events to the placeholder sections
-                       $sourceSection.click( souceSectionClickHandler )
-                               .hover( souceSectionMouseEnterHandler, 
souceSectionMouseLeaveHandler );
+                       $sourceSection.click( sourceSectionClickHandler )
+                               .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: newchange
Gerrit-Change-Id: Ie166f2fdfdfc59f3d4f2872f3832ed29ff23b949
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>

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

Reply via email to