get rid of Revision::getText
authordaniel <daniel.kinzler@wikimedia.de>
Fri, 14 Sep 2012 11:13:29 +0000 (13:13 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Fri, 14 Sep 2012 16:23:21 +0000 (18:23 +0200)
Change-Id: Ia0b16813fa1688b6faccbe8ca81b1099001b1f3b

includes/FeedUtils.php
includes/Import.php
includes/api/ApiFeedContributions.php
includes/job/DoubleRedirectJob.php
maintenance/Maintenance.php
maintenance/compareParsers.php
maintenance/dumpIterator.php
maintenance/getText.php
maintenance/preprocessDump.php
maintenance/renderDump.php

index 242f65c..b0a0e2b 100644 (file)
@@ -172,16 +172,36 @@ class FeedUtils {
                } else {
                        $rev = Revision::newFromId( $newid );
                        if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
-                               $newtext = '';
+                               $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
                        } else {
-                               $newtext = $rev->getText();
+                               $newContent = $rev->getContent();
                        }
-                       if ( $wgFeedDiffCutoff <= 0 || strlen( $newtext ) > $wgFeedDiffCutoff ) {
+
+                       if ( $newContent instanceof TextContent ) {
+                               // only textual content has a "source view".
+                               $text = $newContent->getNativeData();
+
+                               if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
+                                       $html = null;
+                               } else {
+                                       $html = nl2br( htmlspecialchars( $text ) );
+                               }
+                       } else {
+                               //XXX: we could get an HTML representation of the content via getParserOutput, but that may
+                               //     contain JS magic and generally may not be suitable for inclusion in a feed.
+                               //     Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
+                               //Compare also ApiFeedContributions::feedItemDesc
+                               $html = null;
+                       }
+
+                       if ( $html === null ) {
+
                                // Omit large new page diffs, bug 29110
+                               // Also use diff link for non-textual content
                                $diffText = self::getDiffLink( $title, $newid );
                        } else {
                                $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
-                                       '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
+                                       '<div>' . $html . '</div>';
                        }
                }
                $completeText .= $diffText;
index 5f59b1c..3035005 100644 (file)
@@ -1017,6 +1017,7 @@ class WikiRevision {
        var $model = null;
        var $format = null;
        var $text = "";
+       var $content = null;
        var $comment = "";
        var $minor = false;
        var $type = "";
@@ -1209,11 +1210,32 @@ class WikiRevision {
 
        /**
         * @return string
+        *
+        * @deprecated Since 1.WD, use getContent() instead.
         */
        function getText() {
+               wfDeprecated( "Use getContent() instead." );
+
                return $this->text;
        }
 
+       /**
+        * @return Content
+        */
+       function getContent() {
+               if ( is_null( $this->content ) ) {
+                       $this->content =
+                               ContentHandler::makeContent(
+                                       $this->text,
+                                       $this->getTitle(),
+                                       $this->getModel(),
+                                       $this->getFormat()
+                               );
+               }
+
+               return $this->content;
+       }
+
        /**
         * @return String
         */
@@ -1377,7 +1399,7 @@ class WikiRevision {
                        'page'       => $pageId,
                        'content_model'  => $this->getModel(),
                        'content_format' => $this->getFormat(),
-                       'text'       => $this->getText(),
+                       'text'       => $this->getContent()->serialize( $this->getFormat() ), //XXX: just set 'content' => $this->getContent()?
                        'comment'    => $this->getComment(),
                        'user'       => $userId,
                        'user_text'  => $userText,
index 1cf760a..fb6a06f 100644 (file)
@@ -130,10 +130,22 @@ class ApiFeedContributions extends ApiBase {
        protected function feedItemDesc( $revision ) {
                if( $revision ) {
                        $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
+                       $content = $revision->getContent();
+
+                       if ( $content instanceof TextContent ) {
+                               // only textual content has a "source view".
+                               $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
+                       } else {
+                               //XXX: we could get an HTML representation of the content via getParserOutput, but that may
+                               //     contain JS magic and generally may not be suitable for inclusion in a feed.
+                               //     Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
+                               //Compare also FeedUtils::formatDiffRow.
+                               $html = '';
+                       }
+
                        return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
                                htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
-                               "</p>\n<hr />\n<div>" .
-                               nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
+                               "</p>\n<hr />\n<div>" . $html . "</div>";
                }
                return '';
        }
index 2d12908..ebbd834 100644 (file)
@@ -124,8 +124,6 @@ class DoubleRedirectJob extends Job {
                $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
                        $currentDest->getFragment() );
 
-               $text = ContentHandler::getContentText( $content ); #FIXME: get rid of this!
-
                # Fix the text
                $newContent = $content->updateRedirect( $newTitle );
 
index 69d1131..ec89cd7 100644 (file)
@@ -1144,7 +1144,8 @@ abstract class Maintenance {
                        $title = $titleObj->getPrefixedDBkey();
                        $this->output( "$title..." );
                        # Update searchindex
-                       $u = new SearchUpdate( $pageId, $titleObj->getText(), $rev->getText() );
+                       # TODO: pass the Content object to SearchUpdate, let the search engine decide how to deal with it.
+                       $u = new SearchUpdate( $pageId, $titleObj->getText(), $rev->getContent()->getTextForSearchIndex() );
                        $u->doUpdate();
                        $this->output( "\n" );
                }
index a333717..1f3ac1c 100644 (file)
@@ -114,15 +114,24 @@ class CompareParsers extends DumpIterator {
                $parser1 = new $parser1Name();
                $parser2 = new $parser2Name();
 
-               $output1 = $parser1->parse( $rev->getText(), $title, $this->options );
-               $output2 = $parser2->parse( $rev->getText(), $title, $this->options );
+               $content = $rev->getContent();
+
+               if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
+                       $this->error( "Page {$title->getPrefixedText()} does not contain wikitext but {$content->getModel()}\n" );
+                       return;
+               }
+
+               $text = strval( $content->getNativeData() );
+
+               $output1 = $parser1->parse( $text, $title, $this->options );
+               $output2 = $parser2->parse( $text, $title, $this->options );
 
                if ( $output1->getText() != $output2->getText() ) {
                        $this->failed++;
                        $this->error( "Parsing for {$title->getPrefixedText()} differs\n" );
 
                        if ( $this->saveFailed ) {
-                               file_put_contents( $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt", $rev->getText());
+                               file_put_contents( $this->saveFailed . '/' . rawurlencode( $title->getPrefixedText() ) . ".txt", $text );
                        }
                        if ( $this->showDiff ) {
                                $this->output( wfDiff( $this->stripParameters( $output1->getText() ), $this->stripParameters( $output2->getText() ), '' ) );
index 3657f96..870d632 100644 (file)
@@ -168,7 +168,7 @@ class SearchDump extends DumpIterator {
         * @param $rev Revision
         */
        public function processRevision( $rev ) {
-               if ( preg_match( $this->getOption( 'regex' ), $rev->getText() ) ) {
+               if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) {
                        $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
                }
        }
index 3e2f854..f6adfe2 100644 (file)
@@ -52,12 +52,12 @@ class GetTextMaint extends Maintenance {
                        $titleText = $title->getPrefixedText();
                        $this->error( "Page $titleText does not exist.\n", true );
                }
-               $text = $rev->getText( $this->hasOption( 'show-private' ) ? Revision::RAW : Revision::FOR_PUBLIC );
-               if ( $text === false ) {
+               $content = $rev->getContent( $this->hasOption( 'show-private' ) ? Revision::RAW : Revision::FOR_PUBLIC );
+               if ( $content === false ) {
                        $titleText = $title->getPrefixedText();
                        $this->error( "Couldn't extract the text from $titleText.\n", true );
                }
-               $this->output( $text );
+               $this->output( $content->serialize() );
        }
 }
 
index 5952fd9..87fc997 100644 (file)
@@ -78,8 +78,14 @@ class PreprocessDump extends DumpIterator {
         * @param $rev Revision
         */
        public function processRevision( $rev ) {
+               $content = $rev->getContent( Revision::RAW );
+
+               if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
+                       return;
+               }
+
                try {
-                       $this->mPreprocessor->preprocessToObj( $rev->getText(), 0 );
+                       $this->mPreprocessor->preprocessToObj( strval( $content->getNativeData() ), 0 );
                }
                catch(Exception $e) {
                        $this->error("Caught exception " . $e->getMessage() . " in " . $rev->getTitle()->getPrefixedText() );
index 24bedfa..ad9a380 100644 (file)
@@ -100,10 +100,10 @@ class DumpRenderer extends Maintenance {
                $this->output( sprintf( "%s\n", $filename, $display ) );
 
                $user = new User();
-               $parser = new $wgParserConf['class']();
                $options = ParserOptions::newFromUser( $user );
 
-               $output = $parser->parse( $rev->getText(), $title, $options );
+               $content = $rev->getContent();
+               $output = $content->getParserOutput( $title, null, $options );
 
                file_put_contents( $filename,
                        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " .