summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/scripts/migrate-schema2.php
blob: 8cf7d6ee632e4080b582e72da290c11fe8f416c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
 * Script to convert Translate extension database schema to v2
 *
 * @author Niklas Laxström
 * @copyright Copyright © 2011, Niklas Laxström
 * @license GPL-2.0-or-later
 * @file
 */

// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$dir = __DIR__;
	$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";

/**
 * Script to convert Translate extension database schema to v2.
 * Essentially gets rid of revtag_type table, which was unnecessary
 * abstraction.
 */
class TSchema2 extends Maintenance {

	public function __construct() {
		parent::__construct();
		$this->mDescription = 'Migrates database schema to version 2.';
	}

	public function execute() {
		$dbw = wfGetDB( DB_MASTER );
		if ( !$dbw->tableExists( 'revtag' ) ) {
			$this->error( "Table revtag doesn't exist. Translate extension is not installed?", 1 );
		}

		if ( !$dbw->tableExists( 'revtag_type' ) ) {
			$this->error( "Table revtag_type doesn't exist. Migration is already done.", 1 );
		}

		if ( $dbw->getType() !== 'mysql' ) {
			$this->error( 'This migration script only supports mysql. Please help ' .
				"us to write routine for {$dbw->getType()}.", 1 );
		}

		$table = $dbw->tableName( 'revtag' );
		$dbw->query( "ALTER TABLE $table MODIFY rt_type varbinary(60) not null", __METHOD__ );

		$res = $dbw->select(
			'revtag_type',
			[ 'rtt_id', 'rtt_name' ],
			[],
			__METHOD__
		);

		foreach ( $res as $row ) {
			$dbw->update(
				'revtag',
				[ 'rt_type' => $row->rtt_name ],
				[ 'rt_type' => (string)$row->rtt_id ],
				__METHOD__
			);
		}

		$dbw->dropTable( 'revtag_type', __METHOD__ );
	}
}

$maintClass = TSchema2::class;
require_once RUN_MAINTENANCE_IF_MAIN;