If rev_content_model is null, use page_content_model.
authordaniel <daniel.kinzler@wikimedia.de>
Thu, 7 Jul 2016 13:06:12 +0000 (15:06 +0200)
committerJdlrobson <jrobson@wikimedia.org>
Fri, 8 Jul 2016 21:47:12 +0000 (21:47 +0000)
We currently leave rev_content_model = null  if it'S the default, to save space.
When loading a revision, we so far fell back to the per-namespace default content
model if rev_content_model was null, even if page_content_model was null.

This patch changes the fallback from rev_contentModel -> namespace-default-model
to rev_content_model -> page_content_model -> namespace-default-model.

This will prevent errors triggered when chaing a namespace'sdefault content model:
so far, revisions with the old content model would fail to deserialize becaue,
because they were being interpreted according to the changed namespace defrault,
instead of the correct model in page_content_model.

Bug: T128466
Change-Id: I75c60eb129428b0b433480443ab9153cc58cda8f

includes/Revision.php
tests/phpunit/includes/RevisionStorageTest.php

index eda8989..5c4302e 100644 (file)
@@ -1110,7 +1110,7 @@ class Revision implements IDBAccessObject {
                if ( !$this->mContentModel ) {
                        $title = $this->getTitle();
                        if ( $title ) {
-                               $this->mContentModel = ContentHandler::getDefaultModelFor( $title );
+                               $this->mContentModel = $title->getContentModel();
                        } else {
                                $this->mContentModel = CONTENT_MODEL_WIKITEXT;
                        }
@@ -1455,7 +1455,7 @@ class Revision implements IDBAccessObject {
                                        . "revision's page!" );
                        }
 
-                       $defaultModel = ContentHandler::getDefaultModelFor( $title );
+                       $defaultModel = $title->getContentModel();
                        $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat();
 
                        $row['rev_content_model'] = ( $model === $defaultModel ) ? null : $model;
index 7d3007b..3866190 100644 (file)
@@ -317,7 +317,42 @@ class RevisionStorageTest extends MediaWikiTestCase {
        /**
         * @covers Revision::getContentModel
         */
-       public function testGetContentModel() {
+       public function testGetContentModel_default_model() {
+               global $wgContentHandlerUseDB;
+
+               if ( !$wgContentHandlerUseDB ) {
+                       $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
+               }
+
+               $orig = $this->makeRevision( [ 'text' => 'hello hello.', ] );
+
+               // Change namespace default content model, to make sure getContentModel() uses the
+               // page content model, not the namespace default.
+               $ns = $this->getDefaultWikitextNS();
+               $this->mergeMwGlobalArrayValue( 'wgNamespaceContentModels', [ $ns => CONTENT_MODEL_JSON ] );
+
+               $rev = Revision::newFromId( $orig->getId() );
+
+               // we should still get the correct content model from the page table
+               $this->assertEquals( CONTENT_MODEL_WIKITEXT, $rev->getContentModel() );
+
+               // check that we don't null the revision model if it's equal to the default, but not to
+               // the current page model.
+               $second = $this->makeRevision( [
+                       'text' => '{ "say":"hello" }',
+                       'content_model' => CONTENT_MODEL_JAVASCRIPT
+               ] );
+
+               $rev = Revision::newFromId( $second->getId() );
+
+               // we should now get the content model from the revision table
+               $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
+       }
+
+       /**
+        * @covers Revision::getContentModel
+        */
+       public function testGetContentModel_other_model() {
                global $wgContentHandlerUseDB;
 
                if ( !$wgContentHandlerUseDB ) {
@@ -325,7 +360,7 @@ class RevisionStorageTest extends MediaWikiTestCase {
                }
 
                $orig = $this->makeRevision( [ 'text' => 'hello hello.',
-                       'content_model' => CONTENT_MODEL_JAVASCRIPT ] );
+                                                    'content_model' => CONTENT_MODEL_JAVASCRIPT ] );
                $rev = Revision::newFromId( $orig->getId() );
 
                $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );