getUndoContent()
authorDaniel Kinzler <daniel.kinzler@wikimedia.de>
Tue, 20 Mar 2012 10:15:10 +0000 (10:15 +0000)
committerDaniel Kinzler <daniel.kinzler@wikimedia.de>
Wed, 4 Apr 2012 17:56:53 +0000 (19:56 +0200)
includes/Content.php
includes/ContentHandler.php
includes/WikiPage.php

index fe2783a..0fcb717 100644 (file)
@@ -40,6 +40,14 @@ abstract class Content {
         return $this->getSize() == 0;
     }
 
+    public function equals( Content $that ) {
+        if ( empty( $that ) ) return false;
+        if ( $that === $this ) return true;
+        if ( $that->getModelName() !== $this->getModelName() ) return false;
+
+        return $this->getNativeData() == $that->getNativeData();
+    }
+
     /**
      * Returns true if this content is countable as a "real" wiki page, provided
      * that it's also in a countable location (e.g. a current revision in the main namespace).
@@ -93,11 +101,10 @@ abstract class Content {
     # XXX: isCacheable( ) # can/should we do this here?
 
     # TODO: WikiPage::getUndoText( Revision $undo, Revision $undoafter = null )
-    # TODO: WikiPage::getAutosummary( $oldtext, $text, $flags )
 
     # TODO: EditPage::getPreloadedText( $preload ) // $wgParser->getPreloadText
     # TODO: tie into EditPage, make it use Content-objects throughout, make edit form aware of content model and format
-    # TODO: tie into WikiPage, make it use Content-objects throughout, especially in doEdit(), doDelete(), etc
+    # TODO: tie into WikiPage, make it use Content-objects throughout, especially in doEdit(), doDelete(), updateRevisionOn(), etc
     # TODO: make model-aware diff view!
     # TODO: handle ImagePage and CategoryPage
 
index 39f9fcd..96069ad 100644 (file)
@@ -380,8 +380,33 @@ abstract class ContentHandler {
         return $reason;
     }
 
+    /**
+     * Get the Content object that needs to be saved in order to undo all revisions
+     * between $undo and $undoafter. Revisions must belong to the same page,
+     * must exist and must not be deleted
+     * @param $undo Revision
+     * @param $undoafter Revision Must be an earlier revision than $undo
+     * @return mixed string on success, false on failure
+     */
+    public function getUndoContent( Revision $current, Revision $undo, Revision $undoafter ) {
+        $cur_content = $current->getContent();
+
+        if ( empty( $cur_content ) ) {
+            return false; // no page
+        }
+
+        $undo_content = $undo->getContent();
+        $undoafter_content = $undoafter->getContent();
 
-    #TODO: cover patch/undo just like merge3.
+        if ( $cur_content->equals( $undo_content ) ) {
+            # No use doing a merge if it's just a straight revert.
+            return $undoafter_content;
+        }
+
+        $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
+
+        return $undone_content;
+    }
 
     #TODO: how to handle extra message for JS/CSS previews??
     #TODO: Article::showCssOrJsPage ---> specialized classes!
index 4bf9ff9..42637e2 100644 (file)
@@ -424,6 +424,11 @@ class WikiPage extends Page {
                return $this->getText( Revision::RAW );
        }
 
+    /**
+     * Get the content of the current revision. No side-effects...
+     *
+     * @return Contet|false The text of the current revision
+     */
     protected function getNativeData() {
         $content = $this->getContent( Revision::RAW );
         if ( !$content ) return null;
@@ -1097,27 +1102,19 @@ class WikiPage extends Page {
         * @param $undo Revision
         * @param $undoafter Revision Must be an earlier revision than $undo
         * @return mixed string on success, false on failure
+     * @deprecated since 1.20: use ContentHandler::getUndoContent() instead.
         */
-       public function getUndoText( Revision $undo, Revision $undoafter = null ) { #FIXME: move undo logic to ContentHandler
-               $cur_text = $this->getRawText();
-               if ( $cur_text === false ) {
-                       return false; // no page
-               }
-               $undo_text = $undo->getText();
-               $undoafter_text = $undoafter->getText();
-
-               if ( $cur_text == $undo_text ) {
-                       # No use doing a merge if it's just a straight revert.
-                       return $undoafter_text;
-               }
+       public function getUndoText( Revision $undo, Revision $undoafter = null ) { #FIXME: replace usages.
+        $this->loadLastEdit();
 
-               $undone_text = '';
+        if ( $this->mLastRevision ) {
+            $handler = ContentHandler::getForTitle( $this->getTitle() );
+            $undone = $handler->getUndoContent( $this->mLastRevision, $undo, $undoafter );
 
-               if ( !wfMerge( $undo_text, $undoafter_text, $cur_text, $undone_text ) ) {
-                       return false;
-               }
+            return ContentHandler::getContentText( $undone );
+        }
 
-               return $undone_text;
+        return false;
        }
 
        /**