Skip to content

Commit

Permalink
(compat) Use underscores instead of spaces in mod_title
Browse files Browse the repository at this point in the history
All other MediaWiki tables use underscores in *_title fields.
Having the same format is good for JOIN queries, etc.
  • Loading branch information
edwardspec committed Apr 21, 2018
1 parent 7f81f9f commit c4f0711
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 1 addition & 5 deletions hooks/ModerationEditHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion hooks/ModerationPreload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];

Expand Down
8 changes: 8 additions & 0 deletions hooks/ModerationUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions sql/patch-fix-titledbkey.sql
Original file line number Diff line number Diff line change
@@ -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,' ','_');
6 changes: 3 additions & 3 deletions tests/phpunit/ModerationShowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" );

Expand Down Expand Up @@ -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" );

Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/RollbackResistantQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__
);
Expand Down
16 changes: 16 additions & 0 deletions util/ModerationVersionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down

0 comments on commit c4f0711

Please sign in to comment.