Tpt has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/371136 )

Change subject: Implements a difference engine for Index: pages
......................................................................

Implements a difference engine for Index: pages

Change-Id: I8a29d94022ff86c9823414e7bae1ab8ce0f63b13
---
M extension.json
M includes/index/IndexContentHandler.php
A includes/index/IndexDifferenceEngine.php
M includes/page/PageDifferenceEngine.php
4 files changed, 103 insertions(+), 3 deletions(-)


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

diff --git a/extension.json b/extension.json
index 266871c..042b62b 100644
--- a/extension.json
+++ b/extension.json
@@ -53,6 +53,7 @@
                "ProofreadPage\\Index\\IndexContent": 
"includes/index/IndexContent.php",
                "ProofreadPage\\Index\\IndexRedirectContent": 
"includes/index/IndexRedirectContent.php",
                "ProofreadPage\\Index\\IndexContentHandler": 
"includes/index/IndexContentHandler.php",
+               "ProofreadPage\\Index\\IndexDifferenceEngine": 
"includes/index/IndexDifferenceEngine.php",
                "ProofreadPage\\Index\\IndexEditAction": 
"includes/index/IndexEditAction.php",
                "ProofreadPage\\Index\\IndexSubmitAction": 
"includes/index/IndexSubmitAction.php",
                "ProofreadPage\\Index\\EditIndexPage": 
"includes/index/EditIndexPage.php",
diff --git a/includes/index/IndexContentHandler.php 
b/includes/index/IndexContentHandler.php
index 5d18c7f..2b1f606 100644
--- a/includes/index/IndexContentHandler.php
+++ b/includes/index/IndexContentHandler.php
@@ -129,6 +129,13 @@
        }
 
        /**
+        * @see ContentHandler::getDiffEngineClass
+        */
+       protected function getDiffEngineClass() {
+               return '\ProofreadPage\Index\IndexDifferenceEngine';
+       }
+
+       /**
         * @see ContentHandler::makeEmptyContent
         */
        public function makeEmptyContent() {
diff --git a/includes/index/IndexDifferenceEngine.php 
b/includes/index/IndexDifferenceEngine.php
new file mode 100644
index 0000000..5b7a654
--- /dev/null
+++ b/includes/index/IndexDifferenceEngine.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace ProofreadPage\Index;
+
+use Content;
+use DifferenceEngine;
+use InvalidArgumentException;
+use ProofreadPage\Context;
+use ProofreadPage\DiffFormatterUtils;
+use Title;
+
+/**
+ * DifferenceEngine for Index: pages
+ */
+class IndexDifferenceEngine extends DifferenceEngine {
+
+       /**
+        * @var DiffFormatterUtils
+        */
+       private $diffFormatterUtils;
+
+       /**
+        * @var CustomIndexFieldsParser
+        */
+       private $customIndexFieldsParser;
+
+       /**
+        * @see DifferenceEngine::__construct
+        */
+       public function __construct(
+               $context = null, $old = 0, $new = 0, $rcid = 0, $refreshCache = 
false, $unhide = false,
+               Context $extContext = null
+       ) {
+               parent::__construct( $context, $old, $new, $rcid, 
$refreshCache, $unhide );
+
+               $this->diffFormatterUtils = new DiffFormatterUtils();
+               $extContext = ($extContext === null) ? 
Context::getDefaultContext() : $extContext;
+               $this->customIndexFieldsParser = 
$extContext->getCustomIndexFieldsParser();
+       }
+
+       /**
+        * @see DifferenceEngine::generateContentDiffBody
+        */
+       public function generateContentDiffBody( Content $old, Content $new ) {
+               if ( $old instanceof IndexRedirectContent ) {
+                       if ( $new instanceof IndexRedirectContent ) {
+                               return $this->createRedirectionDiff( 
$old->getRedirectTarget(), $new->getRedirectTarget() );
+                       } elseif ($new instanceof IndexContent ) {
+                               return $this->createRedirectionDiff( 
$old->getRedirectTarget(), null ) .
+                                       $this->createIndexContentDiff( null, 
$new );
+                       }
+               } elseif( $old instanceof IndexContent ) {
+                       if ( $new instanceof IndexRedirectContent ) {
+                               return $this->createRedirectionDiff( null, 
$new->getRedirectTarget() ) .
+                                       $this->createIndexContentDiff( $old, 
null );
+                       } elseif ($new instanceof IndexContent ) {
+                               return $this->createIndexContentDiff( $old, 
$new );
+                       }
+               }
+               throw new InvalidArgumentException( 'IndexDifferenceEngine is 
only able to output diffs between IndexContents' );
+       }
+
+       private function createRedirectionDiff( Title $old = null, Title $new = 
null ) {
+               $old = ($old === null) ? '' : $old->getFullText();
+               $new = ($new === null) ? '' : $new->getFullText();
+               return $this->createTextDiffOutput($old, $new,
+                       $this->msg( 'isredirect' )->escaped()
+               );
+       }
+
+       private function createIndexContentDiff( IndexContent $old = null, 
IndexContent $new = null ) {
+               $oldCustomFields = ($old === null) ? [] : 
$this->customIndexFieldsParser->parseCustomIndexFields( $old );
+               $newCustomFields = ($new === null) ? [] : 
$this->customIndexFieldsParser->parseCustomIndexFields( $new );
+               $diff = '';
+               foreach ($oldCustomFields as $oldField) {
+                       $diff .= $this->createTextDiffOutput(
+                               $oldField->getStringValue(),
+                               
$newCustomFields[$oldField->getKey()]->getStringValue(),
+                               $oldField->getLabel()
+                       );
+               }
+               return $diff;
+       }
+
+       private function createTextDiffOutput( $old, $new, $header ) {
+               $diff = parent::generateTextDiffBody( $old, $new );
+               if ( $diff === '' ) {
+                       return '';
+               }
+               return $this->diffFormatterUtils->createHeader( $header ) . 
$diff;
+       }
+}
diff --git a/includes/page/PageDifferenceEngine.php 
b/includes/page/PageDifferenceEngine.php
index 3e180e7..2f9c77c 100644
--- a/includes/page/PageDifferenceEngine.php
+++ b/includes/page/PageDifferenceEngine.php
@@ -17,7 +17,7 @@
        /**
         * @var DiffFormatterUtils
         */
-       protected $diffFormatterUtils;
+       private $diffFormatterUtils;
 
        /**
         * @see DifferenceEngine::__construct
@@ -56,7 +56,7 @@
         * @param PageLevel $new
         * @return string
         */
-       protected function createLevelDiffs( PageLevel $old, PageLevel $new ) {
+       private function createLevelDiffs( PageLevel $old, PageLevel $new ) {
                if ( $old->equals( $new ) ) {
                        return '';
                }
@@ -82,7 +82,7 @@
         * @param string $headerMsg the message to use for header
         * @return string
         */
-       protected function createTextDiffOutput( TextContent $old, TextContent 
$new, $headerMsg ) {
+       private function createTextDiffOutput( TextContent $old, TextContent 
$new, $headerMsg ) {
                $diff = parent::generateContentDiffBody( $old, $new );
                if ( $diff === '' ) {
                        return '';

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8a29d94022ff86c9823414e7bae1ab8ce0f63b13
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ProofreadPage
Gerrit-Branch: master
Gerrit-Owner: Tpt <thoma...@hotmail.fr>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to