replacing deprecated getText, etc
authorDaniel Kinzler <daniel.kinzler@wikimedia.de>
Mon, 5 Mar 2012 17:09:41 +0000 (17:09 +0000)
committerDaniel Kinzler <daniel.kinzler@wikimedia.de>
Wed, 4 Apr 2012 17:54:06 +0000 (19:54 +0200)
12 files changed:
includes/Article.php
includes/Content.php
includes/ContentHandler.php
includes/EditPage.php
includes/ImagePage.php
includes/Revision.php
includes/WikiPage.php
includes/api/ApiEditPage.php
includes/api/ApiParse.php
includes/api/ApiPurge.php
includes/resourceloader/ResourceLoaderWikiModule.php
maintenance/populateRevisionLength.php

index 895619f..906f06f 100644 (file)
@@ -37,7 +37,7 @@ class Article extends Page {
         */
        public $mParserOptions;
 
-       var $mContent;                    // !<  #FIXME: use Content object!
+       var $mContent;                    // !< #BC cruft
     var $mContentObject;              // !<
        var $mContentLoaded = false;      // !<
        var $mOldId;                      // !<
@@ -192,7 +192,7 @@ class Article extends Page {
         * @return Return the text of this revision
      * @deprecated in 1.20; use getContentObject() instead
         */
-       public function getContent() { #FIXME: deprecated! replace usage! use content object!
+       public function getContent() {
         wfDeprecated( __METHOD__, '1.20' );
         $content = $this->getContentObject();
         return ContentHandler::getContentText( $content );
@@ -226,7 +226,7 @@ class Article extends Page {
                        }
                        wfProfileOut( __METHOD__ );
 
-                       return new WikitextContent( $text, $this->getTitle() );
+                       return ContentHandler::makeContent( $text, $this->getTitle() );
                } else {
                        $this->fetchContentObject();
                        wfProfileOut( __METHOD__ );
@@ -312,7 +312,7 @@ class Article extends Page {
         * @return mixed string containing article contents, or false if null
      * @deprecated in 1.20, use getContentObject() instead
         */
-       function fetchContent() { #BC cruft! #FIXME: deprecated, replace usage
+       protected function fetchContent() { #BC cruft!
         wfDeprecated( __METHOD__, '1.20' );
 
                if ( $this->mContentLoaded && $this->mContent ) {
@@ -338,7 +338,7 @@ class Article extends Page {
      *
      * @return Content object containing article contents, or null
      */
-    function fetchContentObject() {
+    protected function fetchContentObject() {
         if ( $this->mContentLoaded ) {
             return $this->mContentObject;
         }
@@ -423,7 +423,7 @@ class Article extends Page {
         * @return Revision|null
         */
        public function getRevisionFetched() {
-               $this->fetchContent();
+               $this->fetchContentObject();
 
                return $this->mRevision;
        }
@@ -582,7 +582,7 @@ class Article extends Page {
                                        break;
                                case 3:
                                        # This will set $this->mRevision if needed
-                                       $this->fetchContent();
+                                       $this->fetchContentObject();
 
                                        # Are we looking at an old revision
                                        if ( $oldid && $this->mRevision ) {
@@ -603,7 +603,7 @@ class Article extends Page {
 
                                        # Pages containing custom CSS or JavaScript get special treatment
                                        if ( $this->getTitle()->isCssOrJsPage() || $this->getTitle()->isCssJsSubpage() ) {
-                        #FIXME: use Content object instead!
+                        #FIXME: use ContentHandler for specialized actions insetad.
                                                wfDebug( __METHOD__ . ": showing CSS/JS source\n" );
                                                $this->showCssOrJsPage();
                                                $outputDone = true;
@@ -611,14 +611,14 @@ class Article extends Page {
                                                # Allow extensions do their own custom view for certain pages
                                                $outputDone = true;
                                        } else {
-                                               $text = $this->getContent();
-                                               $rt = Title::newFromRedirectArray( $text );
+                                               $content = $this->getContentObject();
+                                               $rt = $content->getRedirectChain();
                                                if ( $rt ) {
                                                        wfDebug( __METHOD__ . ": showing redirect=no page\n" );
                                                        # Viewing a redirect page (e.g. with parameter redirect=no)
                                                        $wgOut->addHTML( $this->viewRedirect( $rt ) );
                                                        # Parse just to get categories, displaytitle, etc.
-                                                       $this->mParserOutput = $wgParser->parse( $text, $this->getTitle(), $parserOptions );
+                                                       $this->mParserOutput = $content->getParserOutput( $parserOptions );
                                                        $wgOut->addParserOutputNoText( $this->mParserOutput );
                                                        $outputDone = true;
                                                }
@@ -629,7 +629,7 @@ class Article extends Page {
                                        wfDebug( __METHOD__ . ": doing uncached parse\n" );
 
                                        $poolArticleView = new PoolWorkArticleView( $this, $parserOptions,
-                                               $this->getRevIdFetched(), $useParserCache, $this->getContent() );
+                                               $this->getRevIdFetched(), $useParserCache, $this->getContentObject() );
 
                                        if ( !$poolArticleView->execute() ) {
                                                $error = $poolArticleView->getError();
@@ -741,24 +741,18 @@ class Article extends Page {
         * This is hooked by SyntaxHighlight_GeSHi to do syntax highlighting of these
         * page views.
         */
-       protected function showCssOrJsPage() { #FIXME: deprecate, keep for BC
+       protected function showCssOrJsPage() { #FIXME: move this to handler!
                global $wgOut;
 
                $dir = $this->getContext()->getLanguage()->getDir();
                $lang = $this->getContext()->getLanguage()->getCode();
 
                $wgOut->wrapWikiMsg( "<div id='mw-clearyourcache' lang='$lang' dir='$dir' class='mw-content-$dir'>\n$1\n</div>",
-                       'clearyourcache' ); #FIXME: get this from handler
+                       'clearyourcache' ); #FIXME: do this in handler
 
                // Give hooks a chance to customise the output
                if ( wfRunHooks( 'ShowRawCssJs', array( $this->mContent, $this->getTitle(), $wgOut ) ) ) {
-            #FIXME: use content object instead
-                       // Wrap the whole lot in a <pre> and don't parse
-                       $m = array();
-                       preg_match( '!\.(css|js)$!u', $this->getTitle()->getText(), $m );
-                       $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
-                       $wgOut->addHTML( htmlspecialchars( $this->mContent ) );
-                       $wgOut->addHTML( "\n</pre>\n" );
+                       $wgOut->addHTML( $this->mContentObject->getHTML() );
                }
        }
 
index 1a445c6..a8a0ba6 100644 (file)
@@ -6,7 +6,7 @@
  */
 abstract class Content {
     
-    public function __construct( Title $title, $revId, $modelName ) {
+    public function __construct( Title $title, $revId, $modelName ) { #FIXME: really need revId? annoying! #FIXME: really $title? or just when parsing, every time?
         $this->mModelName = $modelName;
         $this->mTitle = $title;
         $this->mRevId = $revId;
@@ -43,6 +43,14 @@ abstract class Content {
         return $update;
     }
 
+    public function getRedirectChain() {
+        return null;
+    }
+
+    public function getSection( $section ) { #FIXME: should this return text? or a Content object? or what??
+        return null;
+    }
+
     #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
 }
 
@@ -90,6 +98,11 @@ class TextContent extends Content {
         return $text;
     }
 
+    public function getRedirectChain() {
+        #XXX: really do this for all text, or just in WikitextContent
+        $text = $this->getRawData();
+        return Title::newFromRedirectArray( $text );
+    }
 }
 
 class WikitextContent extends TextContent {
@@ -121,11 +134,18 @@ class WikitextContent extends TextContent {
             $options = $this->getDefaultParserOptions();
         }
 
-        $po = $wgParser->parse( $this->mText, $this->getTitle(), $options );
+        $po = $wgParser->parse( $this->mText, $this->getTitle(), $options, true, true, $this->mRevId );
 
         return $po;
     }
 
+    public function getSection( $section ) {
+        global $wgParser;
+
+        $text = $this->getRawData();
+        return $wgParser->getSection( $text, $section, false );
+    }
+
 }
 
 class MessageContent extends TextContent {
index 025c740..f586c94 100644 (file)
@@ -25,6 +25,13 @@ abstract class ContentHandler {
         return null;
     }
 
+    public static function makeContent( $text, Title $title, $format = null, $revId = null ) {
+        $handler = ContentHandler::getForTitle( $title );
+
+        #FIXME: pass revid?
+        return $handler->unserialize( $text, $title, $format );
+    }
+
     public static function getDefaultModelFor( Title $title ) {
         global $wgNamespaceContentModels;
 
@@ -130,33 +137,15 @@ abstract class ContentHandler {
         return $this->mSupportedFormats[0];
     }
 
-    public abstract function serialize( $obj, $format = null );
+    public abstract function serialize( $obj, Title $title, $format = null );
             # for wikitext, do nothing (in the future: serialise ast/dom)
             # for wikidata: serialize arrays to json
 
-    public abstract function unserialize( $blob, $format = null );
+    public abstract function unserialize( $blob, Title $title, $format = null ); #FIXME: ...and revId?
             # for wikitext, do nothing (in the future: parse into ast/dom)
             # for wikidata: serialize arrays to json
 
 
-    public function getSearchText( $obj ) {
-            # for wikitext, return wikitext
-            # for wikidata, return pseudo-wikitext composed of property values (or some such)
-        $text = $this->serialize( $obj ); 
-        return $text; # return the default serialization.
-    }
-
-    public function getWikitextForTransclusion( $obj ) {
-        # for wikitext, return text
-        # for wikidata, return false, or some generated wikitext
-        $text = $this->serialize( $obj ); 
-        return '<pre>' . $text . '</pre>'; # return a pre-formatted block containing the default serialization.
-    }
-
-    public abstract function render( $obj, Title $title, ParserOptions $options, $revid = null );
-            # returns a ParserOutput instance!
-            # are parser options, generic?!
-
     public abstract function doPreSaveTransform( $title, $obj );
 
     # TODO: getPreloadText()
index 0f32c70..3425bb5 100644 (file)
@@ -864,7 +864,9 @@ class EditPage {
                if ( $revision === null ) {
                        return '';
                }
-               return $this->mArticle->getContent();
+
+        $content = $this->mArticle->getContentObject();
+               return $content->getRawData(); # this editor is for editing the raw text. so use the raw text.
        }
 
        /**
@@ -900,7 +902,7 @@ class EditPage {
         * @param $preload String: representing the title to preload from.
         * @return String
         */
-       protected function getPreloadedText( $preload ) {
+       protected function getPreloadedText( $preload ) { #FIXME: change to getPreloadedContent()
                global $wgUser, $wgParser;
 
                if ( !empty( $this->mPreloadText ) ) {
@@ -928,7 +930,7 @@ class EditPage {
                }
 
                $parserOptions = ParserOptions::newFromUser( $wgUser );
-               return $wgParser->getPreloadText( $page->getRawText(), $title, $parserOptions );
+               return $wgParser->getPreloadText( $page->getRawText(), $title, $parserOptions ); #FIXME: create Content::getPreloadCopy
        }
 
        /**
index e1cc6e7..cd086c7 100644 (file)
@@ -245,20 +245,20 @@ class ImagePage extends Article {
                return $r;
        }
 
-       /**
-        * Overloading Article's getContent method.
-        *
-        * Omit noarticletext if sharedupload; text will be fetched from the
-        * shared upload server if possible.
-        * @return string
-        */
-       public function getContent() {
-               $this->loadFile();
-               if ( $this->mPage->getFile() && !$this->mPage->getFile()->isLocal() && 0 == $this->getID() ) {
-                       return '';
-               }
-               return parent::getContent();
-       }
+    /**
+     * Overloading Article's getContentObject method.
+     *
+     * Omit noarticletext if sharedupload; text will be fetched from the
+     * shared upload server if possible.
+     * @return string
+     */
+    public function getContentObject() {
+        $this->loadFile();
+        if ( $this->mPage->getFile() && !$this->mPage->getFile()->isLocal() && 0 == $this->getID() ) {
+            return null;
+        }
+        return parent::getContentObject();
+    }
 
        protected function openShowImage() {
                global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
index 9bc9469..b40cd71 100644 (file)
@@ -496,7 +496,7 @@ class Revision {
                        $this->mCurrent   = false;
                        # If we still have no length, see it we have the text to figure it out
                        if ( !$this->mSize ) {
-                               $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
+                               $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText ); #FIXME: do strlen in Content object
                        }
                        # Same for sha1
                        if ( $this->mSha1 === null ) {
@@ -779,7 +779,7 @@ class Revision {
         * @return String
      * @deprectaed in 1.20, use getContent() instead
         */
-       public function getText( $audience = self::FOR_PUBLIC, User $user = null ) { #FIXME: deprecated, replace usage!
+       public function getText( $audience = self::FOR_PUBLIC, User $user = null ) { #FIXME: deprecated, replace usage! #FIXME: used a LOT!
         wfDeprecated( __METHOD__, '1.20' );
 
         $content = $this->getContent();
index e68a697..e5462b5 100644 (file)
@@ -429,6 +429,11 @@ class WikiPage extends Page {
                return $this->getText( Revision::RAW );
        }
 
+    protected function getRawData() {
+        $content = $this->getContent( Revision::RAW );
+        return $content->getRawData();
+    }
+
        /**
         * @return string MW timestamp of last article revision
         */
@@ -542,7 +547,7 @@ class WikiPage extends Page {
         *        if false, the current database state will be used
         * @return Boolean
         */
-       public function isCountable( $editInfo = false ) {
+       public function isCountable( $editInfo = false ) { #FIXME: move this to Content object
                global $wgArticleCountMethod;
 
                if ( !$this->mTitle->isContentPage() ) {
@@ -620,7 +625,7 @@ class WikiPage extends Page {
         */
        public function insertRedirect() {
                // recurse through to only get the final target
-               $retval = Title::newFromRedirectRecurse( $this->getRawText() );
+               $retval = Title::newFromRedirectRecurse( $this->getRawText() ); #FIXME: move this to Content object
                if ( !$retval ) {
                        return null;
                }
@@ -904,7 +909,7 @@ class WikiPage extends Page {
 
                if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
                        if ( $this->mTitle->exists() ) {
-                               $text = $this->getRawText();
+                               $text = $this->getRawData();
                        } else {
                                $text = false;
                        }
@@ -1093,7 +1098,7 @@ class WikiPage extends Page {
         * @param $undoafter Revision Must be an earlier revision than $undo
         * @return mixed string on success, false on failure
         */
-       public function getUndoText( Revision $undo, Revision $undoafter = null ) {
+       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
@@ -1122,7 +1127,7 @@ class WikiPage extends Page {
         * @param $edittime String: revision timestamp or null to use the current revision
         * @return string Complete article text, or null if error
         */
-       public function replaceSection( $section, $text, $sectionTitle = '', $edittime = null ) {
+       public function replaceSection( $section, $text, $sectionTitle = '', $edittime = null ) { #FIXME: move to Content object!
                wfProfileIn( __METHOD__ );
 
                if ( strval( $section ) == '' ) {
@@ -1233,7 +1238,7 @@ class WikiPage extends Page {
         *
         *  Compatibility note: this function previously returned a boolean value indicating success/failure
         */
-       public function doEdit( $text, $summary, $flags = 0, $baseRevId = false, $user = null ) {
+       public function doEdit( $text, $summary, $flags = 0, $baseRevId = false, $user = null ) { #FIXME: change $text to $content
                global $wgUser, $wgDBtransactions, $wgUseAutomaticEditSummaries;
 
                # Low-level sanity check
@@ -1268,7 +1273,7 @@ class WikiPage extends Page {
                $isminor = ( $flags & EDIT_MINOR ) && $user->isAllowed( 'minoredit' );
                $bot = $flags & EDIT_FORCE_BOT;
 
-               $oldtext = $this->getRawText(); // current revision
+               $oldtext = $this->getRawData(); // current revision
                $oldsize = strlen( $oldtext );
                $oldid = $this->getLatest();
                $oldIsRedirect = $this->isRedirect();
@@ -1276,7 +1281,7 @@ class WikiPage extends Page {
 
                # Provide autosummaries if one is not provided and autosummaries are enabled.
                if ( $wgUseAutomaticEditSummaries && $flags & EDIT_AUTOSUMMARY && $summary == '' ) {
-                       $summary = self::getAutosummary( $oldtext, $text, $flags );
+                       $summary = self::getAutosummary( $oldtext, $text, $flags ); #FIXME: auto-summary from ContentHandler
                }
 
                $editInfo = $this->prepareTextForEdit( $text, null, $user );
@@ -1309,7 +1314,7 @@ class WikiPage extends Page {
                                'page'       => $this->getId(),
                                'comment'    => $summary,
                                'minor_edit' => $isminor,
-                               'text'       => $text, #FIXME: set content instead, leavfe serialization to revision?!
+                               'text'       => $text, #FIXME: set content instead, leave serialization to revision?!
                                'parent_id'  => $oldid,
                                'user'       => $user->getId(),
                                'user_text'  => $user->getName(),
@@ -1512,7 +1517,7 @@ class WikiPage extends Page {
                $edit->pst = $wgParser->preSaveTransform( $text, $this->mTitle, $user, $popts );
                $edit->popts = $this->makeParserOptions( 'canonical' );
                $edit->output = $wgParser->parse( $edit->pst, $this->mTitle, $edit->popts, true, true, $revid );
-               $edit->oldText = $this->getRawText();
+               $edit->oldText = $this->getRawText(); #FIXME: $oldcontent instead?!
 
                $this->mPreparedEdit = $edit;
 
@@ -2820,14 +2825,18 @@ class PoolWorkArticleView extends PoolCounterWork {
         * @param $revid Integer: ID of the revision being parsed
         * @param $useParserCache Boolean: whether to use the parser cache
         * @param $parserOptions parserOptions to use for the parse operation
-        * @param $text String: text to parse or null to load it
+        * @param $content Content|String: content to parse or null to load it; may also be given as a wikitext string, for BC
         */
-       function __construct( Page $page, ParserOptions $parserOptions, $revid, $useParserCache, $text = null ) {
+       function __construct( Page $page, ParserOptions $parserOptions, $revid, $useParserCache, $content = null ) {
+        if ( is_string($content) ) { #BC: old style call
+            $content = ContentHandler::makeContent( $content, $page->getTitle(), null, $this->revid ); #FIXME: format? from revision?
+        }
+
                $this->page = $page;
                $this->revid = $revid;
                $this->cacheable = $useParserCache;
                $this->parserOptions = $parserOptions;
-               $this->text = $text;
+               $this->content = $content;
                $this->cacheKey = ParserCache::singleton()->getKey( $page, $parserOptions );
                parent::__construct( 'ArticleView', $this->cacheKey . ':revid:' . $revid );
        }
@@ -2867,21 +2876,20 @@ class PoolWorkArticleView extends PoolCounterWork {
 
                $isCurrent = $this->revid === $this->page->getLatest();
 
-               if ( $this->text !== null ) {
-                       $text = $this->text;
+               if ( $this->content !== null ) {
+                       $content = $this->content;
                } elseif ( $isCurrent ) {
-                       $text = $this->page->getRawText();
+            $content = $this->page->getContent( Revision::RAW ); #XXX: why use RAW audience here, and PUBLIC (default) below?
                } else {
                        $rev = Revision::newFromTitle( $this->page->getTitle(), $this->revid );
                        if ( $rev === null ) {
                                return false;
                        }
-                       $text = $rev->getText();
+            $content = $rev->getContent(); #XXX: why use PUBLIC audience here (default), and RAW above?
                }
 
                $time = - wfTime();
-               $this->parserOutput = $wgParser->parse( $text, $this->page->getTitle(),
-                       $this->parserOptions, true, true, $this->revid );
+               $this->parserOutput = $content->getParserOutput( $this->parserOptions );
                $time += wfTime();
 
                # Timing hack
index 9ed6d08..cd07e30 100644 (file)
@@ -108,21 +108,22 @@ class ApiEditPage extends ApiBase {
                        // We do want getContent()'s behavior for non-existent
                        // MediaWiki: pages, though
                        if ( $articleObj->getID() == 0 && $titleObj->getNamespace() != NS_MEDIAWIKI ) {
-                               $content = '';
+                               $content = null;
+                $text = '';
                        } else {
-                               $content = $articleObj->getContent();
+                $content = $articleObj->getContentObject();
+                $text = $content->getRawData();
                        }
 
                        if ( !is_null( $params['section'] ) ) {
                                // Process the content for section edits
-                               global $wgParser;
                                $section = intval( $params['section'] );
-                               $content = $wgParser->getSection( $content, $section, false );
-                               if ( $content === false ) {
+                $text = $content->getSection( $section, false );
+                               if ( $text === false || $text === null ) {
                                        $this->dieUsage( "There is no section {$section}.", 'nosuchsection' );
                                }
                        }
-                       $params['text'] = $params['prependtext'] . $content . $params['appendtext'];
+                       $params['text'] = $params['prependtext'] . $text . $params['appendtext'];
                        $toMD5 = $params['prependtext'] . $params['appendtext'];
                }
 
index 893491b..7824c35 100644 (file)
@@ -320,7 +320,7 @@ class ApiParse extends ApiBase {
 
                if ( $this->section !== false ) {
                        $this->text = $this->getSectionText( $page->getRawText(), !is_null( $pageId )
-                                       ? 'page id ' . $pageId : $titleObj->getText() );
+                                       ? 'page id ' . $pageId : $titleObj->getText() ); #FIXME: get section...
 
                        // Not cached (save or load)
                        return $wgParser->parse( $this->text, $titleObj, $popts );
@@ -329,7 +329,8 @@ class ApiParse extends ApiBase {
                        // getParserOutput will save to Parser cache if able
                        $pout = $page->getParserOutput( $popts );
                        if ( $getWikitext ) {
-                               $this->text = $page->getRawText();
+                $this->content = $page->getContent( Revision::RAW ); #FIXME: use $this->content everywhere
+                               $this->text = $this->content->getRawData(); #FIXME: change $this->text to $this->data?!
                        }
                        return $pout;
                }
index 9e9320f..c43c032 100644 (file)
@@ -90,7 +90,7 @@ class ApiPurge extends ApiBase {
 
                                        $popts = ParserOptions::newFromContext( $this->getContext() );
                                        $p_result = $wgParser->parse( $page->getRawText(), $title, $popts,
-                                               true, true, $page->getLatest() );
+                                               true, true, $page->getLatest() ); #FIXME: content!
 
                                        # Update the links tables
                                        $u = new LinksUpdate( $title, $p_result );
index b137531..f93330c 100644 (file)
@@ -80,7 +80,7 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
                if ( !$revision ) {
                        return null;
                }
-               return $revision->getRawText();
+               return $revision->getRawText(); #FIXME: get raw data from content object after checking the type;
        }
 
        /* Methods */
index 6626cbc..cec91fb 100644 (file)
@@ -67,7 +67,7 @@ class PopulateRevisionLength extends LoggedUpdateMaintenance {
                        # Go through and update rev_len from these rows.
                        foreach ( $res as $row ) {
                                $rev = new Revision( $row );
-                               $text = $rev->getRawText();
+                               $text = $rev->getRawText(); #FIXME: go via Content object; #FIXME: get size via Content object
                                if ( !is_string( $text ) ) {
                                        # This should not happen, but sometimes does (bug 20757)
                                        $this->output( "Text of revision {$row->rev_id} unavailable!\n" );