Tpt has uploaded a new change for review.

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


Change subject: Add custom DifferenceEngine for Page: pages
......................................................................

Add custom DifferenceEngine for Page: pages

Change-Id: I813fb2f56dd6057f4b87a53fa5f3945f4772875c
---
M ProofreadPage.php
A includes/ProofreadDiffFormatterUtils.php
M includes/page/ProofreadPageContentHandler.php
A includes/page/ProofreadPageDifferenceEngine.php
A tests/includes/ProofreadDiffFormatterUtilsTest.php
5 files changed, 229 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ProofreadPage 
refs/changes/10/97210/1

diff --git a/ProofreadPage.php b/ProofreadPage.php
index f32e9a1..f08edcf 100644
--- a/ProofreadPage.php
+++ b/ProofreadPage.php
@@ -45,6 +45,7 @@
 $wgAutoloadClasses['ProofreadPage'] = $dir . 'ProofreadPage.body.php';
 $wgAutoloadClasses['ProofreadPageInit'] = $dir . 
'includes/ProofreadPageInit.php';
 $wgAutoloadClasses['ProofreadPageRenderer'] = $dir . 
'includes/ProofreadPageRenderer.php';
+$wgAutoloadClasses['ProofreadDiffFormatterUtils'] = $dir . 
'includes/ProofreadDiffFormatterUtils.php';
 
 $wgAutoloadClasses['EditProofreadIndexPage'] = $dir . 
'includes/index/EditProofreadIndexPage.php';
 $wgAutoloadClasses['ProofreadIndexEntry'] = $dir . 
'includes/index/ProofreadIndexEntry.php';
@@ -61,6 +62,7 @@
 $wgAutoloadClasses['ProofreadPageEditAction'] = $dir . 
'includes/page/ProofreadPageEditAction.php';
 $wgAutoloadClasses['ProofreadPageSubmitAction'] = $dir . 
'includes/page/ProofreadPageSubmitAction.php';
 $wgAutoloadClasses['ProofreadPageViewAction'] = $dir . 
'includes/page/ProofreadPageViewAction.php';
+$wgAutoloadClasses['ProofreadPageDifferenceEngine'] = $dir . 
'includes/page/ProofreadPageDifferenceEngine.php';
 
 $wgExtensionCredits['other'][] = array(
        'path'           => __FILE__,
@@ -194,6 +196,7 @@
        $files[] = $dir . 'page/ProofreadPageContentTest.php';
        $files[] = $dir . 'page/ProofreadPageContentHandlerTest.php';
        $files[] = $dir . 'page/ProofreadPagePageTest.php';
+       $files[] = $dir . 'ProofreadDiffFormatterUtilsTest.php';
 
        return true;
 };
diff --git a/includes/ProofreadDiffFormatterUtils.php 
b/includes/ProofreadDiffFormatterUtils.php
new file mode 100644
index 0000000..b567c54
--- /dev/null
+++ b/includes/ProofreadDiffFormatterUtils.php
@@ -0,0 +1,82 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup ProofreadPage
+ */
+
+/**
+ * Utility functions to format diffs
+ */
+class ProofreadDiffFormatterUtils {
+
+       /**
+        * Create an header in the two columns
+        *
+        * @param $text string the header text
+        * @return string
+        */
+       public function createHeader( $text ) {
+               return Html::openElement( 'tr' ) .
+               Html::element( 'td', array( 'colspan' => '2', 'class' => 
'diff-lineno' ), $text ) .
+               Html::element( 'td', array( 'colspan' => '2', 'class' => 
'diff-lineno' ), $text ) .
+               Html::closeElement( 'tr' );
+       }
+
+       /**
+        * Output an added line
+        *
+        * @param $content string the content of the line
+        * @return string
+        */
+       public function createAddedLine( $content ) {
+               return $this->createLineWrapper(
+                       Html::element( 'ins',  array( 'class' => 'diffchange 
diffchange-inline' ), $content ),
+                       'diff-addedline',
+                       '+'
+               );
+       }
+
+       /**
+        * Output a deleted line
+        *
+        * @param $content string the content of the line
+        * @return string
+        */
+       public function createDeletedLine( $content ) {
+               return $this->createLineWrapper(
+                               Html::element( 'del',  array( 'class' => 
'diffchange diffchange-inline' ), $content ),
+                               'diff-deletedline',
+                               '-'
+                       );
+       }
+
+       /**
+        * Create the container for a line
+        *
+        * @param $line string the line
+        * @param $class string the container class (diff-deletedline or 
diff-addedline)
+        * @param $marker string the diff marker (+ or -)
+        * @return string
+        */
+       protected function createLineWrapper( $line, $class, $marker ) {
+               return Html::element( 'td', array( 'class' => 'diff-marker' ), 
$marker ) .
+                       Html::openElement( 'td', array( 'class' => $class ) ).
+                       Html::rawelement( 'div', array(), $line ) .
+                       Html::closeElement( 'td' );
+       }
+}
\ No newline at end of file
diff --git a/includes/page/ProofreadPageContentHandler.php 
b/includes/page/ProofreadPageContentHandler.php
index c6b16d1..38f3cb6 100644
--- a/includes/page/ProofreadPageContentHandler.php
+++ b/includes/page/ProofreadPageContentHandler.php
@@ -121,6 +121,13 @@
        }
 
        /**
+        * @see ContentHandler::getDiffEngineClass
+        */
+       protected function getDiffEngineClass() {
+               return 'ProofreadPageDifferenceEngine';
+       }
+
+       /**
         * @see ContentHandler::makeEmptyContent
         */
        public function makeEmptyContent() {
diff --git a/includes/page/ProofreadPageDifferenceEngine.php 
b/includes/page/ProofreadPageDifferenceEngine.php
new file mode 100644
index 0000000..b093c2f
--- /dev/null
+++ b/includes/page/ProofreadPageDifferenceEngine.php
@@ -0,0 +1,98 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup ProofreadPage
+ */
+
+/**
+ * DifferenceEngine for Page: pages
+ */
+class ProofreadPageDifferenceEngine extends DifferenceEngine {
+
+       /**
+        * @var ProofreadDiffFormatterUtils
+        */
+       protected $diffFormatterUtils;
+
+       /**
+        * @see DifferenceEngine::__construct
+        */
+       public function __construct( $context = null, $old = 0, $new = 0, $rcid 
= 0, $refreshCache = false, $unhide = false ) {
+               parent::__construct( $context, $old, $new, $rcid, 
$refreshCache, $unhide );
+
+               $this->diffFormatterUtils = new ProofreadDiffFormatterUtils();
+       }
+
+       /**
+        * @see DifferenceEngine::generateContentDiffBody
+        */
+       public function generateContentDiffBody( Content $old, Content $new ) {
+               if ( !( $old instanceof ProofreadPageContent ) || !( $new 
instanceof ProofreadPageContent ) ) {
+                       throw new MWException( "ProofreadPageDifferenceEngine 
works only for ProofreadPageContent." );
+               }
+
+               return $this->createLevelDiffs( $old->getLevel(), 
$new->getLevel() ) .
+                       $this->createTextDiffOutput( $old->getHeader(), 
$new->getHeader(), 'proofreadpage_header' ) .
+                       $this->createTextDiffOutput( $old->getBody(), 
$new->getBody(), 'proofreadpage_body' ) .
+                       $this->createTextDiffOutput( $old->getFooter(), 
$new->getFooter(), 'proofreadpage_footer' );
+       }
+
+       /**
+        * Create the diffs for a ProofreadPageLevel
+        *
+        * @param $old ProofreadPageLevel
+        * @param $new ProofreadPageLevel
+        * @return string
+        */
+       protected function createLevelDiffs( ProofreadPageLevel $old, 
ProofreadPageLevel $new ) {
+               if ( $old->equals( $new ) ) {
+                       return '';
+               }
+
+               return $this->diffFormatterUtils->createHeader( wfMessage( 
'pproofreadpage_page_status' )->text() ) .
+                       Html::openElement( 'tr' ) .
+                       $this->diffFormatterUtils->createDeletedLine(
+                               wfMessage( 'proofreadpage_quality' . 
$old->getLevel() . '_category' )->plain(),
+                               'diff-deletedline',
+                               '-'
+                       ) .
+                       $this->diffFormatterUtils->createAddedLine(
+                               wfMessage( 'proofreadpage_quality' . 
$new->getLevel() . '_category' )->plain(),
+                               'diff-addedline',
+                               '+'
+                       ) .
+                       Html::closeElement( 'tr' );
+       }
+
+       /**
+        * Create the diffs for a textual content
+        *
+        * @param $old TextContent $old
+        * @param $new TextContent $new
+        * @param $headerMsg string the message to use for header
+        * @return string
+        */
+       protected function createTextDiffOutput( TextContent $old, TextContent 
$new, $headerMsg ) {
+               $diff = parent::generateContentDiffBody( $old, $new );
+               if ( $diff === '' ) {
+                       return '';
+               }
+
+               return $this->diffFormatterUtils->createHeader( wfMessage( 
$headerMsg )->text() ) . $diff;
+       }
+}
\ No newline at end of file
diff --git a/tests/includes/ProofreadDiffFormatterUtilsTest.php 
b/tests/includes/ProofreadDiffFormatterUtilsTest.php
new file mode 100644
index 0000000..efe86ad
--- /dev/null
+++ b/tests/includes/ProofreadDiffFormatterUtilsTest.php
@@ -0,0 +1,39 @@
+<?php
+
+/**
+ * @group ProofreadPage
+ */
+class ProofreadDiffFormatterUtilsTest extends ProofreadPageTestCase {
+
+       /**
+        * @var ProofreadDiffFormatterUtils
+        */
+       protected $diffFormatterUtils;
+
+       public function setUp() {
+               parent::setUp();
+
+               $this->diffFormatterUtils = new ProofreadDiffFormatterUtils();
+       }
+
+       public function testCreateHeader() {
+               $this->assertEquals(
+                       '<tr><td colspan="2" class="diff-lineno">Test</td><td 
colspan="2" class="diff-lineno">Test</td></tr>',
+                       $this->diffFormatterUtils->createHeader( 'Test' )
+               );
+       }
+
+       public function testCreateAddedLine() {
+               $this->assertEquals(
+                       '<td class="diff-marker">+</td><td 
class="diff-addedline"><div><ins class="diffchange 
diffchange-inline">Test</ins></div></td>',
+                       $this->diffFormatterUtils->createAddedLine( 'Test' )
+               );
+       }
+
+       public function testCreateDeletedLine() {
+               $this->assertEquals(
+                       '<td class="diff-marker">-</td><td 
class="diff-deletedline"><div><del class="diffchange 
diffchange-inline">Test</del></div></td>',
+                       $this->diffFormatterUtils->createDeletedLine( 'Test' )
+               );
+       }
+}
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I813fb2f56dd6057f4b87a53fa5f3945f4772875c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ProofreadPage
Gerrit-Branch: master
Gerrit-Owner: Tpt <[email protected]>

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

Reply via email to