From f8dc579261dcea5744cb909785821b8996c8d312 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 22 Mar 2019 13:26:21 +0100 Subject: [PATCH] Only load latest revision in MessageCache::loadFromDB In Id044b8dcd7c, we lost a condition that ensured that the cache would be populated with the latest revision. Now it was being populated with all revisions, with a random one winning. Bug: T218918 Change-Id: I1a47356ea35f0abf35bb1a3489d0d3442a3400a5 --- includes/cache/MessageCache.php | 5 +- .../includes/cache/MessageCacheTest.php | 47 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/includes/cache/MessageCache.php b/includes/cache/MessageCache.php index e78dfa16bb..157d88eb34 100644 --- a/includes/cache/MessageCache.php +++ b/includes/cache/MessageCache.php @@ -540,7 +540,10 @@ class MessageCache { $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], - array_merge( $conds, [ 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ] ), + array_merge( $conds, [ + 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ), + 'page_latest = rev_id' // get the latest revision only + ] ), __METHOD__ . "($code)-small", [], $revQuery['joins'] diff --git a/tests/phpunit/includes/cache/MessageCacheTest.php b/tests/phpunit/includes/cache/MessageCacheTest.php index c340c08e49..8fdc1f1d8f 100644 --- a/tests/phpunit/includes/cache/MessageCacheTest.php +++ b/tests/phpunit/includes/cache/MessageCacheTest.php @@ -1,6 +1,7 @@ doEditContent( $contentHandler, "$lang translation test case" ); + $status = $wikiPage->doEditContent( $contentHandler, "$lang translation test case" ); + + // sanity + $this->assertTrue( $status->isOK(), 'Create page ' . $title->getPrefixedDBkey() ); + return $status->value['revision']; } /** @@ -213,4 +220,42 @@ class MessageCacheTest extends MediaWikiLangTestCase { $this->assertEquals( 0, $dbr->trxLevel(), "No DB read queries" ); } + + /** + * Regression test for T218918 + */ + public function testLoadFromDB_fetchLatestRevision() { + // Create three revisions of the same message page. + // Must be an existing message key. + $key = 'Log'; + $this->makePage( $key, 'de', 'Test eins' ); + $this->makePage( $key, 'de', 'Test zwei' ); + $r3 = $this->makePage( $key, 'de', 'Test drei' ); + + // Create an out-of-sequence revision by importing a + // revision with an old timestamp. Hacky. + $importRevision = new WikiRevision( new HashConfig() ); + $importRevision->setTitle( $r3->getTitle() ); + $importRevision->setComment( 'Imported edit' ); + $importRevision->setTimestamp( '19991122334455' ); + $importRevision->setText( 'IMPORTED OLD TEST' ); + $importRevision->setUsername( 'Alan Smithee' ); + + $importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates(); + $importer->import( $importRevision ); + + // Now, load the message from the wiki page + MessageCache::destroyInstance(); + $messageCache = MessageCache::singleton(); + $messageCache->enable(); + $messageCache = TestingAccessWrapper::newFromObject( $messageCache ); + + $cache = $messageCache->loadFromDB( 'de' ); + + $this->assertArrayHasKey( $key, $cache ); + + // Text in the cache has an extra space in front! + $this->assertSame( ' ' . 'Test drei', $cache[$key] ); + } + } -- 2.20.1