From 56457b21bee95fce31b83b5436ca21a2e8c77627 Mon Sep 17 00:00:00 2001 From: Daniel Kinzler Date: Tue, 20 Mar 2012 10:15:10 +0000 Subject: [PATCH] getUndoContent() --- includes/Content.php | 11 +++++++++-- includes/ContentHandler.php | 27 ++++++++++++++++++++++++++- includes/WikiPage.php | 31 ++++++++++++++----------------- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/includes/Content.php b/includes/Content.php index fe2783a896..0fcb7179f3 100644 --- a/includes/Content.php +++ b/includes/Content.php @@ -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 diff --git a/includes/ContentHandler.php b/includes/ContentHandler.php index 39f9fcde36..96069adba4 100644 --- a/includes/ContentHandler.php +++ b/includes/ContentHandler.php @@ -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! diff --git a/includes/WikiPage.php b/includes/WikiPage.php index 4bf9ff9a78..42637e23f0 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -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; } /** -- 2.20.1