mediawiki-extensions-AJAXPoll/maintenance/migrateOldAJAXPollUserColumnsToActor.php
Ostrzyciel 5177fd7ace Fix actor migration script
Current version does not consider anonymous votes at all, which
caused the script to fail horribly on wikis that allowed anons
to vote.

Change-Id: Ic2a42a581d3495150f89a66a58276aa004bd22e8
2020-02-25 18:18:11 +01:00

109 lines
2.4 KiB
PHP

<?php
/**
* @file
* @ingroup Maintenance
*/
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
/**
* Run automatically with update.php
*
* @since January 2020
*/
class MigrateOldAJAXPollUserColumnsToActor extends LoggedUpdateMaintenance {
public function __construct() {
parent::__construct();
// @codingStandardsIgnoreLine
$this->addDescription( 'Migrates data from old poll_user column in the ajaxpoll_vote table to the new actor column.' );
}
/**
* Get the update key name to go in the update log table
*
* @return string
*/
protected function getUpdateKey() {
return __CLASS__;
}
/**
* Message to show that the update was done already and was just skipped
*
* @return string
*/
protected function updateSkippedMessage() {
return 'ajaxpoll_vote has already been migrated to use the actor column.';
}
/**
* Do the actual work.
*
* @return bool True to log the update as done
*/
protected function doDBUpdates() {
$dbw = $this->getDB( DB_MASTER );
if ( $dbw->fieldExists( 'ajaxpoll_vote', 'poll_vote_id', __METHOD__ ) ) {
return true;
}
$dbw->sourceFile( __DIR__ . '/../sql/drop-primary-key.sql' );
$dbw->sourceFile( __DIR__ . '/../sql/add-new-primary-key.sql' );
// Find missing anonymous actors and insert them to the actor table
// Do not attempt doing it with an insertSelect, it's apparently incompatible with postgres
$res = $dbw->select(
[
'ajaxpoll_vote',
'actor'
],
[ 'poll_ip' ],
[
'poll_ip = poll_user',
'actor_id IS NULL'
],
__METHOD__,
[ 'DISTINCT' ],
[
'actor' => [
'LEFT JOIN',
[ 'actor_name = poll_user' ]
]
]
);
$toInsert = [];
foreach ( $res as $row ) {
$toInsert[] = [ 'actor_name' => $row->poll_ip ];
}
if ( !empty( $toInsert ) ) {
$dbw->insert(
'actor',
$toInsert,
__METHOD__
);
}
// Find corresponding actors for votes
$dbw->query(
// @codingStandardsIgnoreLine
"UPDATE {$dbw->tableName( 'ajaxpoll_vote' )} SET poll_actor=(SELECT actor_id FROM {$dbw->tableName( 'actor' )} WHERE actor_name=poll_user)",
__METHOD__
);
// After the update we can create a unique key
$dbw->sourceFile( __DIR__ . '/../sql/create-unique-index-poll_id_actor.sql' );
return true;
}
}
$maintClass = MigrateOldAJAXPollUserColumnsToActor::class;
require_once RUN_MAINTENANCE_IF_MAIN;