Start splitting back-end functions from the Article user-interface class.
authorBrion Vibber <brion@users.mediawiki.org>
Sun, 19 Dec 2004 12:21:29 +0000 (12:21 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Sun, 19 Dec 2004 12:21:29 +0000 (12:21 +0000)
Fix Special:Export for new schema.

12 files changed:
includes/Article.php
includes/DifferenceEngine.php
includes/HistoryBlob.php
includes/MessageCache.php
includes/RawPage.php
includes/Revision.php [new file with mode: 0644]
includes/SpecialExport.php
includes/SpecialRecentchanges.php
includes/SpecialSearch.php
includes/SpecialUndelete.php
maintenance/compressOld.inc
tests/ArticleTest.php

index afeaacd..c6f63cc 100644 (file)
@@ -7,7 +7,8 @@
 /**
  * Need the CacheManager to be loaded
  */
-require_once ( 'CacheManager.php' );
+require_once( 'CacheManager.php' );
+require_once( 'Revision.php' );
 
 $wgArticleCurContentFields = false;
 $wgArticleOldContentFields = false;
@@ -58,96 +59,6 @@ class Article {
                $this->mForUpdate = false;
        }
 
-       /**
-         * Get revision text associated with an old or archive row
-         * $row is usually an object from wfFetchRow(), both the flags and the text
-         * field must be included
-         * @static
-         * @param integer $row Id of a row
-         * @param string $prefix table prefix (default 'old_')
-         * @return string $text|false the text requested
-       */
-       function getRevisionText( $row, $prefix = 'old_' ) {
-               $fname = 'Article::getRevisionText';
-               wfProfileIn( $fname );
-               
-               # Get data
-               $textField = $prefix . 'text';
-               $flagsField = $prefix . 'flags';
-
-               if( isset( $row->$flagsField ) ) {
-                       $flags = explode( ',', $row->$flagsField );
-               } else {
-                       $flags = array();
-               }
-
-               if( isset( $row->$textField ) ) {
-                       $text = $row->$textField;
-               } else {
-                       wfProfileOut( $fname );
-                       return false;
-               }
-
-               if( in_array( 'gzip', $flags ) ) {
-                       # Deal with optional compression of archived pages.
-                       # This can be done periodically via maintenance/compressOld.php, and
-                       # as pages are saved if $wgCompressRevisions is set.
-                       $text = gzinflate( $text );
-               }
-                       
-               if( in_array( 'object', $flags ) ) {
-                       # Generic compressed storage
-                       $obj = unserialize( $text );
-
-                       # Bugger, corrupted my test database by double-serializing
-                       if ( !is_object( $obj ) ) {
-                               $obj = unserialize( $obj );
-                       }
-
-                       $text = $obj->getText();
-               }
-       
-               global $wgLegacyEncoding;
-               if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
-                       # Old revisions kept around in a legacy encoding?
-                       # Upconvert on demand.
-                       global $wgInputEncoding, $wgContLang;
-                       $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
-               }
-               wfProfileOut( $fname );
-               return $text;
-       }
-
-       /**
-        * If $wgCompressRevisions is enabled, we will compress data.
-        * The input string is modified in place.
-        * Return value is the flags field: contains 'gzip' if the
-        * data is compressed, and 'utf-8' if we're saving in UTF-8
-        * mode.
-        *
-        * @static
-        * @param mixed $text reference to a text
-        * @return string
-        */
-       function compressRevisionText( &$text ) {
-               global $wgCompressRevisions, $wgUseLatin1;
-               $flags = array();
-               if( !$wgUseLatin1 ) {
-                       # Revisions not marked this way will be converted
-                       # on load if $wgLegacyCharset is set in the future.
-                       $flags[] = 'utf-8';
-               }
-               if( $wgCompressRevisions ) {
-                       if( function_exists( 'gzdeflate' ) ) {
-                               $text = gzdeflate( $text );
-                               $flags[] = 'gzip';
-                       } else {
-                               wfDebug( "Article::compressRevisionText() -- no zlib support, not compressing\n" );
-                       }
-               }
-               return implode( ',', $flags );
-       }
-
        /**
         * Note that getContent/loadContent may follow redirects if
         * not told otherwise, and so may cause a change to mTitle.
@@ -394,7 +305,7 @@ class Article {
                # If we got a redirect, follow it (unless we've been told
                # not to by either the function parameter or the query
                if ( !$oldid && !$noredir ) {
-                       $rt = Title::newFromRedirect( Article::getRevisionText( $s ) );
+                       $rt = Title::newFromRedirect( Revision::getRevisionText( $s ) );
                        # process if title object is valid and not special:userlogout
                        if ( $rt && ! ( $rt->getNamespace() == NS_SPECIAL && $rt->getText() == 'Userlogout' ) ) {
                                # Gotta hand redirects to special pages differently:
@@ -443,7 +354,7 @@ class Article {
                $this->mTitle->mRestrictionsLoaded = true;
                $this->mTouched = wfTimestamp( TS_MW, $s->page_touched );
 
-               $this->mContent = Article::getRevisionText( $s );
+               $this->mContent = Revision::getRevisionText( $s );
                
                $this->mUser = $s->rev_user;
                $this->mUserText = $s->rev_user_text;
@@ -799,7 +710,7 @@ class Article {
                $isminor = ( $isminor && $wgUser->getID() ) ? 1 : 0;
                
                $mungedText = $text;
-               $flags = Article::compressRevisionText( $mungedText );
+               $flags = Revision::compressRevisionText( $mungedText );
 
                $dbw =& wfGetDB( DB_MASTER );
 
@@ -891,7 +802,7 @@ class Article {
                        $fname );
                $obj = $dbw->fetchObject( $result );
                $dbw->freeResult( $result );
-               $oldtext = Article::getRevisionText( $obj );
+               $oldtext = Revision::getRevisionText( $obj );
                return $oldtext;
        }
        
@@ -1026,7 +937,7 @@ class Article {
                        $won = wfInvertTimestamp( $now );
 
                        $mungedText = $text;
-                       $flags = Article::compressRevisionText( $newtext );
+                       $flags = Revision::compressRevisionText( $newtext );
                        
                        $lastRevision = $dbw->selectField(
                                'page', 'page_latest', array( 'page_id' => $this->getId() ) );
@@ -1517,7 +1428,7 @@ class Article {
                                $text=$s->cur_text;
                        } else {
                                if($old) {
-                                       $text = Article::getRevisionText( $old );
+                                       $text = Revision::getRevisionText( $old );
                                        $blanked = true;
                                }
 
@@ -1849,7 +1760,7 @@ class Article {
                $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
                $wgOut->addHTML( '<h2>' . htmlspecialchars( $newcomment ) . "</h2>\n<hr />\n" );
-               $this->updateArticle( Article::getRevisionText( $s ), $newcomment, 1, $this->mTitle->userIsWatching(), $bot );
+               $this->updateArticle( Revision::getRevisionText( $s ), $newcomment, 1, $this->mTitle->userIsWatching(), $bot );
                Article::onArticleEdit( $this->mTitle );
                $wgOut->returnToMain( false );
        }
index 360e5f0..3af5767 100644 (file)
@@ -5,6 +5,8 @@
  * @subpackage DifferenceEngine
  */
 
+require_once( 'Revision.php' );
+
 /**
  * @todo document
  * @access public
@@ -258,79 +260,44 @@ class DifferenceEngine {
                $fname = 'DifferenceEngine::loadText';
 
                $dbr =& wfGetDB( DB_SLAVE );
-               if ( 0 == $this->mNewid || 0 == $this->mOldid ) {
-                       /* Fetch current revision */
-                       $wgOut->setArticleFlag( true );
-                       $newLink = $wgTitle->escapeLocalUrl();
+               if( $this->mNewid ) {
+                       $this->newRev =& Revision::newFromId( $this->mNewid );
+               } else {
+                       $this->newRev =& Revision::newFromTitle( $wgTitle );
+               }
+               
+               if( $this->newRev->isCurrent() ) {
                        $this->mPagetitle = htmlspecialchars( wfMsg( 'currentrev' ) );
+                       $this->mNewPage = $wgTitle;
+                       $newLink = $this->mNewPage->escapeLocalUrl();
                        $this->mNewtitle = "<a href='$newLink'>{$this->mPagetitle}</a>";
-                       $id = $wgTitle->getArticleID();
-
-                       $s = $dbr->selectRow( array( 'page', 'revision', 'text' ),
-                               array( 'old_flags', 'old_text', 'rev_user_text', 'rev_comment' ),
-                               "page_id=$id AND page_id=rev_page AND rev_id=old_id", $fname );
-                       if ( $s === false ) {
-                               wfDebug( "Unable to load page_id $id\n" );
-                               return false;
-                       }
-
-                       $this->mNewPage = &$wgTitle;
-                       $this->mNewtext = Article::getRevisionText( $s );
-                       $this->mNewUser = $s->rev_user_text;
-                       $this->mNewComment = $s->rev_comment;
                } else {
-                       $s = $dbr->selectRow( array( 'page', 'revision', 'text' ),
-                               array( 'page_namespace','page_title','rev_timestamp', 'old_text',
-                               'old_flags','rev_user_text','rev_comment' ),
-                               "page_id=rev_page AND rev_id=old_id AND rev_id={$this->mNewid}",
-                               $fname );
-
-                       if ( $s === false ) {
-                               wfDebug( "Unable to load old_id {$this->mNewid}\n" );
-                               return false;
-                       }
-
-                       $this->mNewtext = Article::getRevisionText( $s );
-
-                       $t = $wgLang->timeanddate( $s->rev_timestamp, true );
-                       $this->mNewPage = Title::MakeTitle( $s->page_namespace, $s->page_title );
-                       $newLink = $wgTitle->escapeLocalUrl ('oldid=' . $this->mNewid);
+                       $this->mNewPage = $this->newRev->getTitle();
+                       $newLink = $this->mNewPage->escapeLocalUrl ('oldid=' . $this->mNewid );
+                       $t = $wgLang->timeanddate( $this->newRev->getTimestamp(), true );
                        $this->mPagetitle = htmlspecialchars( wfMsg( 'revisionasof', $t ) );
                        $this->mNewtitle = "<a href='$newLink'>{$this->mPagetitle}</a>";
-                       $this->mNewUser = $s->rev_user_text;
-                       $this->mNewComment = $s->rev_comment;
                }
-               if ( 0 == $this->mOldid ) {
-                       $s = $dbr->selectRow( 'old',
-                               array( 'old_namespace','old_title','old_timestamp','old_text', 'old_flags','old_user_text','old_comment' ),
-                               array( /* WHERE */
-                                       'old_namespace' => $this->mNewPage->getNamespace(),
-                                       'old_title' => $this->mNewPage->getDBkey()
-                               ), $fname, array( 'ORDER BY' => 'inverse_timestamp', 'USE INDEX' => 'name_title_timestamp' )
-                       );
-                       if ( $s === false ) {
-                               wfDebug( 'Unable to load ' . $this->mNewPage->getPrefixedDBkey() . " from old\n" );
-                               return false;
-                       }
+               
+               if( $this->mOldid ) {
+                       $this->oldRev =& Revision::newFromId( $this->mOldid );
                } else {
-                       $s = $dbr->selectRow( array( 'page', 'revision', 'text' ),
-                               array( 'page_namespace','page_title','rev_timestamp','old_text','old_flags','rev_user_text','rev_comment'),
-                               "page_id=rev_page AND rev_id=old_id AND rev_id={$this->mOldid}",
-                               $fname
-                       );
-                       if ( $s === false ) {
-                               wfDebug( "Unable to load old_id {$this->mOldid}\n" );
-                               return false;
-                       }
+                       $this->oldRev =& $this->newRev->getPrevious();
                }
-               $this->mOldPage = Title::MakeTitle( $s->page_namespace, $s->page_title );
-               $this->mOldtext = Article::getRevisionText( $s );
+                       
+               $this->mOldPage = $this->oldRev->getTitle();
 
-               $t = $wgLang->timeanddate( $s->rev_timestamp, true );
-               $oldLink = $this->mOldPage->escapeLocalUrl ('oldid=' . $this->mOldid);
+               $t = $wgLang->timeanddate( $this->oldRev->getTimestamp(), true );
+               $oldLink = $this->mOldPage->escapeLocalUrl( 'oldid=' . $this->mOldid );
                $this->mOldtitle = "<a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) ) . '</a>';
-               $this->mOldUser = $s->rev_user_text;
-               $this->mOldComment = $s->rev_comment;
+               
+               $this->mNewUser = $this->newRev->getUserText();
+               $this->mNewComment = $this->newRev->getComment();
+               $this->mNewtext = $this->newRev->getText();
+               
+               $this->mOldUser = $this->oldRev->getUserText();
+               $this->mOldComment = $this->oldRev->getComment();
+               $this->mOldtext = $this->oldRev->getText();
 
                return true;
        }
index 611e34d..c416bdd 100644 (file)
@@ -30,7 +30,7 @@ class HistoryBlob
        # be other revisions in the same object
        function setText() {}
 
-       # Get default text. This is called from Article::getRevisionText()
+       # Get default text. This is called from Revision::getRevisionText()
        function getText() {}
 }
 
index 9191d60..0476622 100755 (executable)
@@ -4,6 +4,8 @@
  * @package MediaWiki
  */
 
+require_once( 'Revision.php' );
+
 /**
  *
  */
@@ -149,7 +151,7 @@ class MessageCache
 
                $this->mCache = array();
                for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
-                       $this->mCache[$row->page_title] = Article::getRevisionText( $row );
+                       $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
                }
 
                $dbr->freeResult( $res );
@@ -264,7 +266,7 @@ class MessageCache
                                  ' AND page_latest=old_id',
                                  'MessageCache::get' );
                                if ( $result ) {
-                                       $message = Article::getRevisionText( $result );
+                                       $message = Revision::getRevisionText( $result );
                                        if ($this->mUseCache) {
                                                $this->mCache[$title]=$message;
                                                /* individual messages may be often 
index 8873dc8..2c9b8c5 100644 (file)
@@ -10,6 +10,8 @@
  * @package MediaWiki
  */
 
+require_once( 'Revision.php' );
+
 /**
  * @todo document
  * @package MediaWiki
@@ -92,31 +94,19 @@ class RawPage {
                $fname = 'RawPage::getrawtext';
                
                if( !$this->mTitle ) return '';
-               $dbr =& wfGetDB( DB_SLAVE );
-               extract( $dbr->tableNames( 'revision', 'page', 'text' ) );
-
-               $t = $dbr->strencode( $this->mTitle->getDBKey() );
-               $ns = $this->mTitle->getNamespace();
-               # special case
-               if($ns == NS_MEDIAWIKI) {
-                       $rawtext = wfMsg($t);
+               
+               # Special case for MediaWiki: messages; we can hit the message cache.
+               if( $this->mTitle->getNamespace() == NS_MEDIAWIKI) {
+                       $rawtext = wfMsg( $this->mTitle->getDbkey() );
                        return $rawtext;
                }
+               
                # else get it from the DB
-               $sql = "SELECT old_text AS text, rev_timestamp AS timestamp
-                       FROM $text, $revision";
-               if(!empty($this->mOldId)) {
-                       $sql .= " WHERE old_id={$this->mOldId} AND rev_id={$this->mOldId}";
-               } else {
-                       $sql .= ", $page
-                               WHERE page_namespace=$ns AND page_title='$t'
-                                 AND page_latest=rev_id AND rev_id=old_id";
-               }
-               $res = $dbr->query( $sql, $fname );
-               if( $s = $dbr->fetchObject( $res ) ) {
-                       $rawtext = Article::getRevisionText( $s, "" );
-                       header( 'Last-modified: '.gmdate( "D, j M Y H:i:s", wfTimestamp( TS_UNIX, $s->timestamp )).' GMT' );
-                       return $rawtext;
+               $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
+               if( $rev ) {
+                       $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
+                       header( 'Last-modified: ' . $lastmod );
+                       return $rev->getText();
                } else {
                        return '';
                }
diff --git a/includes/Revision.php b/includes/Revision.php
new file mode 100644 (file)
index 0000000..8561049
--- /dev/null
@@ -0,0 +1,345 @@
+<?php
+
+require_once( 'Database.php' );
+require_once( 'Article.php' );
+
+class Revision {
+       /**
+        * Load a page revision from a given revision ID number.
+        * Returns null if no such revision can be found.
+        *
+        * @param int $id
+        * @static
+        * @access public
+        */
+       function &newFromId( $id ) {
+               return Revision::newFromConds(
+                       array( 'page_id=rev_page',
+                              'rev_id' => IntVal( $id ),
+                              'rev_id=old_id' ) );
+       }
+       
+       /**
+        * Load either the current, or a specified, revision
+        * that's attached to a given title. If not attached
+        * to that title, will return null.
+        *
+        * @param Title $title
+        * @param int $id
+        * @return Revision
+        * @access public
+        */
+       function &newFromTitle( &$title, $id = 0 ) {
+               if( $id ) {
+                       $matchId = IntVal( $id );
+               } else {
+                       $matchId = 'page_latest';
+               }
+               return Revision::newFromConds(
+                       array( "rev_id=$matchId",
+                              'page_id=rev_page',
+                              'page_namespace' => $title->getNamespace(),
+                              'page_title'     => $title->getDbkey(),
+                              'rev_id=old_id' ) );
+       }
+       
+       /**
+        * Given a set of conditions, fetch a revision.
+        *
+        * @param array $conditions
+        * @return Revision
+        * @static
+        * @access private
+        */
+       function &newFromConds( $conditions ) {
+               $res =& Revision::fetchFromConds( $conditions );
+               if( $res ) {
+                       $row = $res->fetchObject();
+                       $res->free();
+                       if( $row ) {
+                               return new Revision( $row );
+                       }
+               }
+               return null;
+       }
+       
+       /**
+        * Return a wrapper for a series of database rows to
+        * fetch all of a given page's revisions in turn.
+        * Each row can be fed to the constructor to get objects.
+        *
+        * @param Title $title
+        * @return ResultWrapper
+        * @static
+        * @access public
+        */
+       function &fetchAllRevisions( &$title ) {
+               return Revision::fetchFromConds(
+                       array( 'page_namespace' => $title->getNamespace(),
+                              'page_title'     => $title->getDbkey(),
+                              'page_id=rev_page',
+                              'rev_id=old_id' ) );             
+       }
+       
+       /**
+        * Return a wrapper for a series of database rows to
+        * fetch all of a given page's revisions in turn.
+        * Each row can be fed to the constructor to get objects.
+        *
+        * @param Title $title
+        * @return ResultWrapper
+        * @static
+        * @access public
+        */
+       function &fetchRevision( &$title ) {
+               return Revision::fetchFromConds(
+                       array( 'rev_id=page_latest',
+                              'page_namespace' => $title->getNamespace(),
+                              'page_title'     => $title->getDbkey(),
+                              'page_id=rev_page',
+                              'rev_id=old_id' ) );             
+       }
+               /**
+        * Given a set of conditions, return a ResultWrapper
+        * which will return matching database rows with the
+        * fields necessary to build Revision objects.
+        *
+        * @param array $conditions
+        * @return ResultWrapper
+        * @static
+        * @access private
+        */
+       function &fetchFromConds( $conditions ) {
+               $dbr =& wfGetDB( DB_SLAVE );
+               $res = $dbr->select(
+                       array( 'page', 'revision', 'text' ),
+                       array( 'page_namespace',
+                              'page_title',
+                              'page_latest',
+                              'rev_id',
+                              'rev_page',
+                              'rev_comment',
+                              'rev_user_text',
+                              'rev_user',
+                              'rev_minor_edit',
+                              'rev_timestamp',
+                              'old_flags',
+                              'old_text' ),
+                       $conditions,
+                       'Revision::fetchRow' );
+               return $dbr->resultObject( $res );
+       }
+       
+       /**
+        * @param object $row
+        * @access private
+        */
+       function Revision( $row ) {
+               $this->mId        = IntVal( $row->rev_id );
+               $this->mPage      = IntVal( $row->rev_page );
+               $this->mComment   =         $row->rev_comment;
+               $this->mUserText  =         $row->rev_user_text;
+               $this->mUser      = IntVal( $row->rev_user );
+               $this->mMinorEdit = IntVal( $row->rev_minor_edit );
+               $this->mTimestamp =         $row->rev_timestamp;
+               
+               $this->mCurrent   = ( $row->rev_id == $row->page_latest );
+               $this->mTitle     = Title::makeTitle( $row->page_namespace,
+                                                     $row->page_title );
+               $this->mText      = $this->getRevisionText( $row );
+       }
+       
+       /** @+
+        * @access public
+        */
+       
+       /**
+        * @return int
+        */
+       function getId() {
+               return $this->mId;
+       }
+       
+       /**
+        * Returns the title of the page associated with this entry.
+        * @return Title
+        */
+       function &getTitle() {
+               if( isset( $this->mTitle ) ) {
+                       return $this->mTitle;
+               }
+               $dbr =& wfGetDB( DB_SLAVE );
+               $row = $dbr->selectRow(
+                       array( 'page', 'revision' ),
+                       array( 'page_namespace', 'page_title' ),
+                       array( 'page_id=rev_page',
+                              'rev_id' => $this->mId ),
+                       'Revision::getTItle' );
+               if( $row ) {
+                       $this->mTitle =& Title::makeTitle( $row->page_namespace,
+                                                          $row->page_title );
+               }
+               return $this->mTitle;
+       }
+       
+       /**
+        * @return int
+        */
+       function getUser() {
+               return $this->mUser;
+       }
+       
+       /**
+        * @return string
+        */
+       function getUserText() {
+               return $this->mUserText;
+       }
+       
+       /**
+        * @return string
+        */
+       function getComment() {
+               return $this->mComment;
+       }
+       
+       /**
+        * @return bool
+        */
+       function isMinor() {
+               return (bool)$this->mMinorEdit;
+       }
+       
+       /**
+        * @return string
+        */
+       function getText() {
+               return $this->mText;
+       }
+       
+       /**
+        * @return string
+        */
+       function getTimestamp() {
+               return $this->mTimestamp;
+       }
+       
+       /**
+        * @return bool
+        */
+       function isCurrent() {
+               return $this->mCurrent;
+       }
+       
+       /**
+        * @return Revision
+        */
+       function &getPrevious() {
+               $prev = $this->mTitle->getPreviousRevisionID( $this->mId );
+               return Revision::newFromTitle( $this->mTitle, $prev );
+       }
+
+       /**
+        * @return Revision
+        */
+       function &getNext() {
+               $next = $this->mTitle->getNextRevisionID( $this->mId );
+               return Revision::newFromTitle( $this->mTitle, $next );
+       }
+       /** @- */
+
+       /**
+         * Get revision text associated with an old or archive row
+         * $row is usually an object from wfFetchRow(), both the flags and the text
+         * field must be included
+         * @static
+         * @param integer $row Id of a row
+         * @param string $prefix table prefix (default 'old_')
+         * @return string $text|false the text requested
+         */
+       function getRevisionText( $row, $prefix = 'old_' ) {
+               $fname = 'Revision::getRevisionText';
+               wfProfileIn( $fname );
+               
+               # Get data
+               $textField = $prefix . 'text';
+               $flagsField = $prefix . 'flags';
+
+               if( isset( $row->$flagsField ) ) {
+                       $flags = explode( ',', $row->$flagsField );
+               } else {
+                       $flags = array();
+               }
+
+               if( isset( $row->$textField ) ) {
+                       $text = $row->$textField;
+               } else {
+                       wfProfileOut( $fname );
+                       return false;
+               }
+
+               if( in_array( 'gzip', $flags ) ) {
+                       # Deal with optional compression of archived pages.
+                       # This can be done periodically via maintenance/compressOld.php, and
+                       # as pages are saved if $wgCompressRevisions is set.
+                       $text = gzinflate( $text );
+               }
+                       
+               if( in_array( 'object', $flags ) ) {
+                       # Generic compressed storage
+                       $obj = unserialize( $text );
+
+                       # Bugger, corrupted my test database by double-serializing
+                       if ( !is_object( $obj ) ) {
+                               $obj = unserialize( $obj );
+                       }
+
+                       $text = $obj->getText();
+               }
+       
+               global $wgLegacyEncoding;
+               if( $wgLegacyEncoding && !in_array( 'utf-8', $flags ) ) {
+                       # Old revisions kept around in a legacy encoding?
+                       # Upconvert on demand.
+                       global $wgInputEncoding, $wgContLang;
+                       $text = $wgContLang->iconv( $wgLegacyEncoding, $wgInputEncoding, $text );
+               }
+               wfProfileOut( $fname );
+               return $text;
+       }
+
+       /**
+        * If $wgCompressRevisions is enabled, we will compress data.
+        * The input string is modified in place.
+        * Return value is the flags field: contains 'gzip' if the
+        * data is compressed, and 'utf-8' if we're saving in UTF-8
+        * mode.
+        *
+        * @static
+        * @param mixed $text reference to a text
+        * @return string
+        */
+       function compressRevisionText( &$text ) {
+               global $wgCompressRevisions, $wgUseLatin1;
+               $flags = array();
+               if( !$wgUseLatin1 ) {
+                       # Revisions not marked this way will be converted
+                       # on load if $wgLegacyCharset is set in the future.
+                       $flags[] = 'utf-8';
+               }
+               if( $wgCompressRevisions ) {
+                       if( function_exists( 'gzdeflate' ) ) {
+                               $text = gzdeflate( $text );
+                               $flags[] = 'gzip';
+                       } else {
+                               wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
+                       }
+               }
+               return implode( ',', $flags );
+       }
+
+
+}
+
+
+?>
\ No newline at end of file
index 78ce26f..c8f3052 100644 (file)
@@ -17,6 +17,8 @@
 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 # http://www.gnu.org/copyleft/gpl.html
 
+require_once( 'Revision.php' );
+
 /**
  *
  * @package MediaWiki
@@ -90,31 +92,35 @@ function page2xml( $page, $curonly, $full = false ) {
        }
 
        $dbr =& wfGetDB( DB_SLAVE );
-       $s = $dbr->selectRow( 'cur', array( 'cur_id as id','cur_timestamp as timestamp','cur_user as user',
-               'cur_user_text as user_text', 'cur_restrictions as restrictions','cur_comment as comment',
-               'cur_text as text' ), $title->curCond(), $fname );
-       if( $s !== false ) {
+       $s = $dbr->selectRow( 'page',
+               array( 'page_id', 'page_restrictions' ),
+               array( 'page_namespace' => $title->getNamespace(),
+                          'page_title'     => $title->getDbkey() ) );
+       if( $s ) {
                $tl = xmlsafe( $title->getPrefixedText() );
                $xml = "  <page>\n";
                $xml .= "    <title>$tl</title>\n";
+               
                if( $full ) {
-                       $xml .= "    <id>$s->id</id>\n";
+                       $xml .= "    <id>$s->page_id</id>\n";
                }
-               if( $s->restrictions ) {
-                       $xml .= "    <restrictions>$s->restrictions</restrictions>\n";
+               if( $s->page_restrictions ) {
+                       $xml .= "    <restrictions>" . xmlsafe( $s->page_restrictions ) . "</restrictions>\n";
                }
-               if( !$curonly ) {
-                       $res = $dbr->select( 'old', array( 'old_id as id','old_timestamp as timestamp', 
-                               'old_user as user', 'old_user_text as user_text', 'old_comment as comment', 
-                               'old_text as text', 'old_flags as flags' ), $title->oldCond(), 
-                               $fname, array( 'ORDER BY' => 'old_timestamp' )
-                       );
 
-                       while( $s2 = $dbr->fetchObject( $res ) ) {
-                               $xml .= revision2xml( $s2, $full, false );
+               if( $curonly ) {
+                       $res = Revision::fetchRevision( $title );
+               } else {
+                       $res = Revision::fetchAllRevisions( $title );
+               }
+               if( $res ) {
+                       while( $s = $res->fetchObject() ) {
+                               $rev = new Revision( $s );
+                               $xml .= revision2xml( $rev, $full, false );
                        }
+                       $res->free();
                }
-               $xml .= revision2xml( $s, $full, true );
+               
                $xml .= "  </page>\n";
                wfProfileOut( $fname );
                return $xml;
@@ -124,31 +130,40 @@ function page2xml( $page, $curonly, $full = false ) {
        }
 }
 
-function revision2xml( $s, $full, $cur ) {
+/**
+ * @return string
+ * @param Revision $rev
+ * @param bool $full
+ * @access private
+ */
+function revision2xml( $rev, $full ) {
        $fname = 'revision2xml';
        wfProfileIn( $fname );
        
-       $ts = wfTimestamp2ISO8601( $s->timestamp );
        $xml = "    <revision>\n";
-       if($full && !$cur)
-               $xml .= "    <id>$s->id</id>\n";
+       if( $full )
+               $xml .= "    <id>" . $rev->getId() . "</id>\n";
+       
+       $ts = wfTimestamp2ISO8601( $rev->getTimestamp() );
        $xml .= "      <timestamp>$ts</timestamp>\n";
-       if($s->user) {
-               $u = "<username>" . xmlsafe( $s->user_text ) . "</username>";
-               if($full)
-                       $u .= "<id>$s->user</id>";
+       
+       if( $rev->getUser() ) {
+               $u = "<username>" . xmlsafe( $rev->getUserText() ) . "</username>";
+               if( $full )
+                       $u .= "<id>" . $rev->getUser() . "</id>";
        } else {
-               $u = "<ip>" . xmlsafe( $s->user_text ) . "</ip>";
+               $u = "<ip>" . xmlsafe( $rev->getUserText() ) . "</ip>";
        }
        $xml .= "      <contributor>$u</contributor>\n";
-       if( !empty( $s->minor ) ) {
+       
+       if( $rev->isMinor() ) {
                $xml .= "      <minor/>\n";
        }
-       if($s->comment != "") {
-               $c = xmlsafe( $s->comment );
+       if($rev->getComment() != "") {
+               $c = xmlsafe( $rev->getComment() );
                $xml .= "      <comment>$c</comment>\n";
        }
-       $t = xmlsafe( Article::getRevisionText( $s, "" ) );
+       $t = xmlsafe( $rev->getText() );
        $xml .= "      <text>$t</text>\n";
        $xml .= "    </revision>\n";
        wfProfileOut( $fname );
index f5e6cf9..1782843 100644 (file)
@@ -10,6 +10,7 @@
  */
 require_once( 'Feed.php' );
 require_once( 'ChangesList.php' );
+require_once( 'Revision.php' );
 
 /**
  * Constructor
@@ -349,37 +350,38 @@ function rcFormatDiff( $row ) {
                #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
                #$diff->showDiffPage();
                
+               $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
                $dbr =& wfGetDB( DB_SLAVE );
-               if( $row->rc_this_oldid ) {
-                       $newrow = $dbr->selectRow( 'old',
-                               array( 'old_flags', 'old_text' ),
-                               array( 'old_id' => $row->rc_this_oldid ) );
-                       $newtext = Article::getRevisionText( $newrow );
+               $newrev =& Revision::newFromTitle( $titleObj, $row->rc_this_oldid );
+               if( $newrev ) {
+                       $newtext = $newrev->getText();
                } else {
-                       $newrow = $dbr->selectRow( 'cur',
-                               array( 'cur_text' ),
-                               array( 'cur_id' => $row->rc_cur_id ) );
-                       $newtext = $newrow->cur_text;
+                       $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
+                       wfProfileOut( $fname );
+                       return $comment . $diffText;
                }
+
                if( $row->rc_last_oldid ) {
                        wfProfileIn( "$fname-dodiff" );
-                       $oldrow = $dbr->selectRow( 'old',
-                               array( 'old_flags', 'old_text' ),
-                               array( 'old_id' => $row->rc_last_oldid ) );
-                       $oldtext = Article::getRevisionText( $oldrow );
-                       
+                       $oldrev =& Revision::newFromId( $row->rc_last_oldid );
+                       if( !$oldrev ) {
+                               $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
+                               wfProfileOut( $fname );
+                               return $comment . $diffText;
+                       }
+                       $oldtext = $oldrev->getText();
+                               
                        global $wgFeedDiffCutoff;
                        if( strlen( $newtext ) > $wgFeedDiffCutoff ||
-                           strlen( $oldtext ) > $wgFeedDiffCutoff ) {
-                           $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
+                               strlen( $oldtext ) > $wgFeedDiffCutoff ) {
                                $diffLink = $titleObj->escapeFullUrl(
                                        'diff=' . $row->rc_this_oldid .
                                        '&oldid=' . $row->rc_last_oldid );
-                           $diffText = '<a href="' .
-                               $diffLink .
-                               '">' .
-                               htmlspecialchars( wfMsgForContent( 'difference' ) ) .
-                               '</a>';
+                               $diffText = '<a href="' .
+                                       $diffLink .
+                                       '">' .
+                                       htmlspecialchars( wfMsgForContent( 'difference' ) ) .
+                                       '</a>';
                        } else {
                                $diffText = DifferenceEngine::getDiff( $oldtext, $newtext,
                                  wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp ) ),
index d4e5e64..bebbfd2 100644 (file)
@@ -24,6 +24,7 @@
  */
 
 require_once( 'SearchEngine.php' );
+require_once( 'Revision.php' );
 
 function wfSpecialSearch( $par='' ) {
        global $wgRequest, $wgUser;
@@ -305,7 +306,7 @@ class SpecialSearch {
                if ( '' == $contextchars ) { $contextchars = 50; }
 
                $link = $sk->makeKnownLinkObj( $t, '' );
-               $text = Article::getRevisionText( $row );
+               $text = Revision::getRevisionText( $row );
                $size = wfMsg( 'nbytes', strlen( $text ) );
 
                $lines = explode( "\n", $text );
index d450960..3b5d385 100644 (file)
@@ -5,6 +5,8 @@
  * @subpackage SpecialPage
  */
 
+require_once( 'Revision.php' );
+
 /**
  *
  */
@@ -76,7 +78,7 @@ class PageArchive {
                        array( 'ar_namespace' => $this->title->getNamespace(),
                               'ar_title' => $this->title->getDbkey(),
                               'ar_timestamp' => $dbr->timestamp( $timestamp ) ) );
-               return Article::getRevisionText( $row, "ar_" );
+               return Revision::getRevisionText( $row, "ar_" );
        }
        
        /**
@@ -96,7 +98,7 @@ class PageArchive {
                        'PageArchive::getLastRevisionText',
                        'ORDER BY ar_timestamp DESC LIMIT 1' );
                if( $row ) {
-                       return Article::getRevisionText( $row, "ar_" );
+                       return Revision::getRevisionText( $row, "ar_" );
                } else {
                        return NULL;
                }
@@ -162,7 +164,7 @@ class PageArchive {
                        if( $restoreAll ) {
                                $max = $s->ar_timestamp;
                        }
-                       $text = Article::getRevisionText( $s, "ar_" );
+                       $text = Revision::getRevisionText( $s, "ar_" );
                        
                        $redirect = MagicWord::get( MAG_REDIRECT );
                        $redir = $redirect->matchStart( $text ) ? 1 : 0;
index 30dec07..28ee161 100644 (file)
@@ -4,6 +4,8 @@
  * @subpackage Maintenance
  */
 
+require_once( '../includes/Revision.php' );
+
 /** */
 function compressOldPages( $start = 0 ) {
        $fname = 'compressOldPages';
@@ -135,9 +137,9 @@ function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorTh
                                                $fname,
                                                'FOR UPDATE'
                                        );
-                                       $text = Article::getRevisionText( $textRow );
+                                       $text = Revision::getRevisionText( $textRow );
                                } else {
-                                       $text = Article::getRevisionText( $revs[$i + $j] );
+                                       $text = Revision::getRevisionText( $revs[$i + $j] );
                                }
 
                                if ( $text === false ) {
index 15b8eba..953fd43 100644 (file)
@@ -36,7 +36,7 @@ class ArticleTest extends PHPUnit_TestCase {
                $row->old_text = 'This is a bunch of revision text.';
                $this->assertEquals(
                        'This is a bunch of revision text.',
-                       Article::getRevisionText( $row ) );
+                       Revision::getRevisionText( $row ) );
        }
        
        function testGetRevisionTextGzip() {
@@ -45,7 +45,7 @@ class ArticleTest extends PHPUnit_TestCase {
                $row->old_text = gzdeflate( 'This is a bunch of revision text.' );
                $this->assertEquals(
                        'This is a bunch of revision text.',
-                       Article::getRevisionText( $row ) );
+                       Revision::getRevisionText( $row ) );
        }
        
        function testGetRevisionTextUtf8Native() {
@@ -55,7 +55,7 @@ class ArticleTest extends PHPUnit_TestCase {
                $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
                $this->assertEquals(
                        "Wiki est l'\xc3\xa9cole superieur !",
-                       Article::getRevisionText( $row ) );
+                       Revision::getRevisionText( $row ) );
        }
        
        function testGetRevisionTextUtf8Legacy() {
@@ -65,7 +65,7 @@ class ArticleTest extends PHPUnit_TestCase {
                $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
                $this->assertEquals(
                        "Wiki est l'\xc3\xa9cole superieur !",
-                       Article::getRevisionText( $row ) );
+                       Revision::getRevisionText( $row ) );
        }
        
        function testGetRevisionTextUtf8NativeGzip() {
@@ -75,7 +75,7 @@ class ArticleTest extends PHPUnit_TestCase {
                $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
                $this->assertEquals(
                        "Wiki est l'\xc3\xa9cole superieur !",
-                       Article::getRevisionText( $row ) );
+                       Revision::getRevisionText( $row ) );
        }
        
        function testGetRevisionTextUtf8LegacyGzip() {
@@ -85,12 +85,12 @@ class ArticleTest extends PHPUnit_TestCase {
                $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
                $this->assertEquals(
                        "Wiki est l'\xc3\xa9cole superieur !",
-                       Article::getRevisionText( $row ) );
+                       Revision::getRevisionText( $row ) );
        }
        
        function testCompressRevisionTextUtf8() {
                $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
-               $row->old_flags = Article::compressRevisionText( $row->old_text );
+               $row->old_flags = Revision::compressRevisionText( $row->old_text );
                $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
                        "Flags should contain 'utf-8'" );
                $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
@@ -98,13 +98,13 @@ class ArticleTest extends PHPUnit_TestCase {
                $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
                        $row->old_text, "Direct check" );
                $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
-                       Article::getRevisionText( $row ), "getRevisionText" );
+                       Revision::getRevisionText( $row ), "getRevisionText" );
        }
 
        function testCompressRevisionTextLatin1() {
                $GLOBALS['wgUseLatin1'] = true;
                $row->old_text = "Wiki est l'\xe9cole superieur !";
-               $row->old_flags = Article::compressRevisionText( $row->old_text );
+               $row->old_flags = Revision::compressRevisionText( $row->old_text );
                $this->assertFalse( false !== strpos( $row->old_flags, 'utf-8' ),
                        "Flags should not contain 'utf-8'" );
                $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
@@ -112,13 +112,13 @@ class ArticleTest extends PHPUnit_TestCase {
                $this->assertEquals( "Wiki est l'\xe9cole superieur !",
                        $row->old_text, "Direct check" );
                $this->assertEquals( "Wiki est l'\xe9cole superieur !",
-                       Article::getRevisionText( $row ), "getRevisionText" );
+                       Revision::getRevisionText( $row ), "getRevisionText" );
        }
        
        function testCompressRevisionTextUtf8Gzip() {
                $GLOBALS['wgCompressRevisions'] = true;
                $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
-               $row->old_flags = Article::compressRevisionText( $row->old_text );
+               $row->old_flags = Revision::compressRevisionText( $row->old_text );
                $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
                        "Flags should contain 'utf-8'" );
                $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
@@ -126,7 +126,7 @@ class ArticleTest extends PHPUnit_TestCase {
                $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
                        gzinflate( $row->old_text ), "Direct check" );
                $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
-                       Article::getRevisionText( $row ), "getRevisionText" );
+                       Revision::getRevisionText( $row ), "getRevisionText" );
        }
 
        function testCompressRevisionTextLatin1Gzip() {
@@ -134,7 +134,7 @@ class ArticleTest extends PHPUnit_TestCase {
                $GLOBALS['wgUseLatin1'] = true;
                $row = new stdClass;
                $row->old_text = "Wiki est l'\xe9cole superieur !";
-               $row->old_flags = Article::compressRevisionText( $row->old_text );
+               $row->old_flags = Revision::compressRevisionText( $row->old_text );
                $this->assertFalse( false !== strpos( $row->old_flags, 'utf-8' ),
                        "Flags should not contain 'utf-8'" );
                $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
@@ -142,7 +142,7 @@ class ArticleTest extends PHPUnit_TestCase {
                $this->assertEquals( "Wiki est l'\xe9cole superieur !",
                        gzinflate( $row->old_text ), "Direct check" );
                $this->assertEquals( "Wiki est l'\xe9cole superieur !",
-                       Article::getRevisionText( $row ), "getRevisionText" );
+                       Revision::getRevisionText( $row ), "getRevisionText" );
        }
 
 }