fix handling of per-revision content model if different from title's default content...
[lhc/web/wiklou.git] / includes / Revision.php
index 4c62d04..82a4a94 100644 (file)
@@ -327,6 +327,7 @@ class Revision {
        /**
         * Return the list of revision fields that should be selected to create
         * a new revision.
+        * @return array
         */
        public static function selectFields() {
                return array(
@@ -350,6 +351,7 @@ class Revision {
        /**
         * Return the list of text fields that should be selected to read the
         * revision text
+        * @return array
         */
        public static function selectTextFields() {
                return array(
@@ -360,6 +362,7 @@ class Revision {
 
        /**
         * Return the list of page fields that should be selected from page table
+        * @return array
         */
        public static function selectPageFields() {
                return array(
@@ -372,6 +375,7 @@ class Revision {
 
        /**
         * Return the list of user fields that should be selected from user table
+        * @return array
         */
        public static function selectUserFields() {
                return array( 'user_name' );
@@ -479,13 +483,7 @@ class Revision {
                        $this->mSha1      = isset( $row['sha1']  )      ? strval( $row['sha1']  )      : null;
 
             $this->mContentModelName = isset( $row['content_model']  )  ? strval( $row['content_model'] ) : null;
-            $this->mContentFormat      = isset( $row['content_format']  )   ? strval( $row['content_format'] ) : null;
-
-            if( !isset( $row->rev_content_format ) || is_null( $row->rev_content_format ) ) {
-                $this->mContentFormat = null; # determine on demand if needed
-            } else {
-                $this->mContentFormat = $row->rev_content_format;
-            }
+            $this->mContentFormat    = isset( $row['content_format']  )   ? strval( $row['content_format'] ) : null;
 
                        // Enforce spacing trimming on supplied text
                        $this->mComment   = isset( $row['comment']    ) ?  trim( strval( $row['comment'] ) ) : null;
@@ -510,15 +508,15 @@ class Revision {
             # if we have a content object, serialize it, overriding mText
             if ( !empty( $row['content'] ) ) {
                 $handler = $this->getContentHandler();
-                $this->mText = $handler->serialize( $row['content'], $this->getContentFormat() );
+                $this->mText = $handler->serializeContent( $row['content'], $this->getContentFormat() );
             }
                } else {
                        throw new MWException( 'Revision constructor passed invalid row format.' );
                }
                $this->mUnpatrolled = null;
 
-        #FIXME: add patch for ar_content_format, ar_content_model, rev_content_format, rev_content_model to installer
-        #FIXME: add support for ar_content_format, ar_content_model, rev_content_format, rev_content_model to API
+        // @TODO: add support for ar_content_format, ar_content_model, rev_content_format, rev_content_model to API
+        // @TODO: get rid of $mText
        }
 
        /**
@@ -552,7 +550,7 @@ class Revision {
        /**
         * Get parent revision ID (the original previous page revision)
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getParentId() {
                return $this->mParentId;
@@ -588,12 +586,12 @@ class Revision {
                $dbr = wfGetDB( DB_SLAVE );
                $row = $dbr->selectRow(
                        array( 'page', 'revision' ),
-                       array( 'page_namespace', 'page_title' ),
+                       self::selectPageFields(),
                        array( 'page_id=rev_page',
                                   'rev_id' => $this->mId ),
-                       'Revision::getTitle' );
-               if( $row ) {
-                       $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
+                       __METHOD__ );
+               if ( $row ) {
+                       $this->mTitle = Title::newFromRow( $row );
                }
                return $this->mTitle;
        }
@@ -843,7 +841,7 @@ class Revision {
                 $this->mText = $this->loadText();
             }
 
-            $this->mContent = $handler->unserialize( $this->mText, $title, $format );
+            $this->mContent = is_null( $this->mText ) ? null : $handler->unserializeContent( $this->mText, $format );
         }
 
         return $this->mContent;
@@ -869,15 +867,10 @@ class Revision {
 
     public function getContentHandler() {
         if ( !$this->mContentHandler ) {
-            $title = $this->getTitle();
-
-            if ( $title ) $model = $title->getContentModelName();
-            else $model = CONTENT_MODEL_WIKITEXT;
-
+            $model = $this->getContentModelName();
             $this->mContentHandler = ContentHandler::getForModelName( $model );
 
-            #XXX: do we need to verify that mContentHandler supports mContentFormat?
-            #     otherwise, a fixed content format may cause problems on insert.
+            assert( $this->mContentHandler->isSupportedFormat( $this->getContentFormat() ) );
         }
 
         return $this->mContentHandler;
@@ -1104,28 +1097,29 @@ class Revision {
                $rev_id = isset( $this->mId )
                        ? $this->mId
                        : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
-               $dbw->insert( 'revision',
-                       array(
-                               'rev_id'         => $rev_id,
-                               'rev_page'       => $this->mPage,
-                               'rev_text_id'    => $this->mTextId,
-                               'rev_comment'    => $this->mComment,
-                               'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
-                               'rev_user'       => $this->mUser,
-                               'rev_user_text'  => $this->mUserText,
-                               'rev_timestamp'  => $dbw->timestamp( $this->mTimestamp ),
-                               'rev_deleted'    => $this->mDeleted,
-                               'rev_len'        => $this->mSize,
-                               'rev_parent_id'  => is_null( $this->mParentId )
-                                       ? $this->getPreviousRevisionId( $dbw )
-                                       : $this->mParentId,
-                               'rev_sha1'       => is_null( $this->mSha1 )
-                                       ? Revision::base36Sha1( $this->mText )
-                                       : $this->mSha1,
-                'rev_content_model'       => $this->getContentModelName(),
-                'rev_content_format'        => $this->getContentFormat(),
-                       ), __METHOD__
-               );
+
+        $row = array(
+            'rev_id'         => $rev_id,
+            'rev_page'       => $this->mPage,
+            'rev_text_id'    => $this->mTextId,
+            'rev_comment'    => $this->mComment,
+            'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
+            'rev_user'       => $this->mUser,
+            'rev_user_text'  => $this->mUserText,
+            'rev_timestamp'  => $dbw->timestamp( $this->mTimestamp ),
+            'rev_deleted'    => $this->mDeleted,
+            'rev_len'        => $this->mSize,
+            'rev_parent_id'  => is_null( $this->mParentId )
+                ? $this->getPreviousRevisionId( $dbw )
+                : $this->mParentId,
+            'rev_sha1'       => is_null( $this->mSha1 )
+                ? Revision::base36Sha1( $this->mText )
+                : $this->mSha1,
+            'rev_content_model'       => $this->getContentModelName(),
+            'rev_content_format'        => $this->getContentFormat(),
+        );
+
+               $dbw->insert( 'revision', $row, __METHOD__ );
 
                $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
 
@@ -1311,7 +1305,7 @@ class Revision {
                        $id = 0;
                }
                $conds = array( 'rev_id' => $id );
-               $conds['rev_page'] = $title->getArticleId();
+               $conds['rev_page'] = $title->getArticleID();
                $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
                if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
                        # Not in slave, try master
@@ -1345,7 +1339,7 @@ class Revision {
         * @return Integer
         */
        static function countByTitle( $db, $title ) {
-               $id = $title->getArticleId();
+               $id = $title->getArticleID();
                if( $id ) {
                        return Revision::countByPageId( $db, $id );
                }