From c4f071139d32ad979cab3183acba4169d331ca38 Mon Sep 17 00:00:00 2001 From: Edward Chernenko Date: Sun, 22 Apr 2018 00:40:05 +0300 Subject: [PATCH] (compat) Use underscores instead of spaces in mod_title All other MediaWiki tables use underscores in *_title fields. Having the same format is good for JOIN queries, etc. --- extension.json | 2 +- hooks/ModerationEditHooks.php | 6 +----- hooks/ModerationPreload.php | 2 +- hooks/ModerationUpdater.php | 8 ++++++++ sql/patch-fix-titledbkey.sql | 5 +++++ tests/phpunit/ModerationShowTest.php | 6 +++--- tests/phpunit/RollbackResistantQueryTest.php | 2 +- util/ModerationVersionCheck.php | 16 ++++++++++++++++ 8 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 sql/patch-fix-titledbkey.sql diff --git a/extension.json b/extension.json index bd2d43daa..2357d9c07 100644 --- a/extension.json +++ b/extension.json @@ -1,6 +1,6 @@ { "name": "Moderation", - "version": "1.1.30", + "version": "1.1.31", "author": "Edward Chernenko", "url": "https://www.mediawiki.org/wiki/Extension:Moderation", "descriptionmsg": "moderation-desc", diff --git a/hooks/ModerationEditHooks.php b/hooks/ModerationEditHooks.php index 116460225..535ace201 100644 --- a/hooks/ModerationEditHooks.php +++ b/hooks/ModerationEditHooks.php @@ -99,11 +99,7 @@ public static function onPageContentSave( &$page, &$user, &$content, &$summary, 'mod_user_text' => $user->getName(), 'mod_cur_id' => $page->getId(), 'mod_namespace' => $title->getNamespace(), - 'mod_title' => $title->getText(), /* FIXME (cosmetic): should be getDBKey(), as in other MediaWiki tables. - Not fixed right away because existing database would need to be updated - (which can only be done in a release, not a minor version change, - as noone runs update.php for those). - TODO: use wasDbUpdatedAfter() for seamless update. */ + 'mod_title' => ModerationVersionCheck::getModTitleFor( $title ), 'mod_comment' => $summary, 'mod_minor' => $is_minor, 'mod_bot' => $flags & EDIT_FORCE_BOT, diff --git a/hooks/ModerationPreload.php b/hooks/ModerationPreload.php index e68828533..b132b4de2 100644 --- a/hooks/ModerationPreload.php +++ b/hooks/ModerationPreload.php @@ -172,7 +172,7 @@ public function loadUnmoderatedEdit( $title ) { $where = [ 'mod_preloadable' => 1, 'mod_namespace' => $title->getNamespace(), - 'mod_title' => $title->getText(), + 'mod_title' => ModerationVersionCheck::getModTitleFor( $title ), 'mod_preload_id' => $id ]; diff --git a/hooks/ModerationUpdater.php b/hooks/ModerationUpdater.php index 67901037b..44220072a 100644 --- a/hooks/ModerationUpdater.php +++ b/hooks/ModerationUpdater.php @@ -24,10 +24,18 @@ class ModerationUpdater { public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) { $base = dirname( __FILE__ ); + /* Main database schema */ $updater->addExtensionTable( 'moderation', "$base/../sql/patch-moderation.sql" ); $updater->addExtensionTable( 'moderation_block', "$base/../sql/patch-moderation_block.sql" ); + + /* DB changes needed when updating Moderation from its previous version */ + + // ... to Moderation 1.1.29 $updater->addExtensionField( 'moderation', 'mod_tags', "$base/../sql/patch-moderation-mod_tags.sql" ); + // ... to Moderation 1.1.31 + $updater->modifyField( 'moderation', 'mod_title', "$base/../sql/patch-fix-titledbkey.sql", true ); + ModerationVersionCheck::markDbAsUpdated(); return true; } diff --git a/sql/patch-fix-titledbkey.sql b/sql/patch-fix-titledbkey.sql new file mode 100644 index 000000000..fede730e8 --- /dev/null +++ b/sql/patch-fix-titledbkey.sql @@ -0,0 +1,5 @@ +-- Replace spaces with underscores in mod_title (since Moderation 1.1.31). +-- This is for standardization: other MediaWiki tables use underscores in *_title. + +UPDATE /*_*/moderation + SET mod_title=REPLACE(mod_title,' ','_'); diff --git a/tests/phpunit/ModerationShowTest.php b/tests/phpunit/ModerationShowTest.php index e671e837e..c58c56817 100644 --- a/tests/phpunit/ModerationShowTest.php +++ b/tests/phpunit/ModerationShowTest.php @@ -171,7 +171,7 @@ public function testShowUpload() { $this->assertEquals( $t->lastEdit['SHA1'], sha1( $req->getContent() ), "testShowUpload(): Checksum of image downloaded via modaction=showimg doesn't match the checksum of original image" ); - $this->assertEquals( "inline;filename*=UTF-8''Test%20image%201.png", $req->getResponseHeader( 'Content-Disposition' ), + $this->assertEquals( "inline;filename*=UTF-8''Test_image_1.png", $req->getResponseHeader( 'Content-Disposition' ), "testShowUpload(640x50): Wrong Content-Disposition header from modaction=showimg" ); # Check the thumbnail @@ -181,7 +181,7 @@ public function testShowUpload() { $this->assertRegExp( '/^image\//', $req->getResponseHeader( 'Content-Type' ), "testShowUpload(640x50): Wrong Content-Type header from modaction=showimg&thumb=1" ); $this->assertEquals( "inline;filename*=UTF-8''" . - ModerationActionShowImage::THUMB_WIDTH . "px-Test%20image%201.png", + ModerationActionShowImage::THUMB_WIDTH . "px-Test_image_1.png", $req->getResponseHeader( 'Content-Disposition' ), "testShowUpload(640x50): Wrong Content-Disposition header from modaction=showimg&thumb=1" ); @@ -215,7 +215,7 @@ public function testShowUpload() { "testShowUpload(100x100): Wrong Content-Type header from modaction=showimg&thumb=1" ); # No "px-" in the filename, because this thumbnail isn't different from the original file - $this->assertEquals( "inline;filename*=UTF-8''Test%20image%202.png", + $this->assertEquals( "inline;filename*=UTF-8''Test_image_2.png", $req->getResponseHeader( 'Content-Disposition' ), "testShowUpload(100x100): Wrong Content-Disposition header from modaction=showimg&thumb=1" ); diff --git a/tests/phpunit/RollbackResistantQueryTest.php b/tests/phpunit/RollbackResistantQueryTest.php index e92e3a4be..7e8b81eeb 100644 --- a/tests/phpunit/RollbackResistantQueryTest.php +++ b/tests/phpunit/RollbackResistantQueryTest.php @@ -139,7 +139,7 @@ protected function subtestRollbackResistantQuery( $isTrxAutomatic, $isExplicitTr $wasCreated = $dbw->selectField( 'moderation', '1', [ 'mod_namespace' => $page->getTitle()->getNamespace(), - 'mod_title' => $page->getTitle()->getText() # FIXME: ModerationEditHooks::onPageContentSave() uses getText(), not getDBKey() + 'mod_title' => ModerationVersionCheck::getModTitleFor( $page->getTitle() ) ], __METHOD__ ); diff --git a/util/ModerationVersionCheck.php b/util/ModerationVersionCheck.php index d66dbed63..f7701e1dd 100644 --- a/util/ModerationVersionCheck.php +++ b/util/ModerationVersionCheck.php @@ -27,6 +27,22 @@ public static function areTagsSupported() { return self::wasDbUpdatedAfter( '1.1.29' ); } + /** @brief Returns false if mod_title contains spaces (obsolete behavior), true if underscores (correct behavior) */ + public static function usesDbKeyAsTitle() { + return self::wasDbUpdatedAfter( '1.1.31' ); + } + + /** @brief Calculate mod_title for $title. + Backward compatible with old Moderation databases that used spaces instead of underscores. + */ + public static function getModTitleFor( Title $title ) { + if ( self::usesDbKeyAsTitle() ) { + return $title->getDBKey(); + } + + return $title->getText(); /* Legacy approach */ + } + /*-------------------------------------------------------------------*/ const EXTENSION_NAME = 'Moderation'; /**< Name of extension (as listed in extension.json) */