AlterSharedConstraints::getDbType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 30.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License along
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
15
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
 * http://www.gnu.org/copyleft/gpl.html
17
 *
18
 * @file
19
 * @ingroup Maintenance
20
 */
21
22
/**
23
 * When using shared tables that are referenced by foreign keys on local
24
 * tables you have to change the constraints on local tables.
25
 *
26
 * The shared tables have to have GRANT REFERENCE on shared tables to local schema
27
 * i.e.: GRANT REFERENCES (user_id) ON mwuser TO hubclient;
28
 */
29
30
require_once __DIR__ . '/../Maintenance.php';
31
32
class AlterSharedConstraints extends Maintenance {
33
	public function __construct() {
34
		parent::__construct();
35
		$this->addDescription( 'Alter foreign key to reference master tables in shared database setup.' );
36
	}
37
38
	public function getDbType() {
39
		return Maintenance::DB_ADMIN;
40
	}
41
42
	public function execute() {
43
		global $wgSharedDB, $wgSharedTables, $wgSharedPrefix, $wgDBprefix;
44
45
		if ( $wgSharedDB == null ) {
46
			$this->output( "Database sharing is not enabled\n" );
47
48
			return;
49
		}
50
51
		$dbw = $this->getDB( DB_MASTER );
52
		foreach ( $wgSharedTables as $table ) {
53
			$stable = $dbw->tableNameInternal( $table );
0 ignored issues
show
Bug introduced by
The method tableNameInternal() does not exist on Database. Did you maybe mean tableName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
54
			if ( $wgSharedPrefix != null ) {
55
				$ltable = preg_replace( "/^$wgSharedPrefix(.*)/i", "$wgDBprefix\\1", $stable );
56
			} else {
57
				$ltable = "{$wgDBprefix}{$stable}";
58
			}
59
60
			$result = $dbw->query( "SELECT uc.constraint_name, uc.table_name, ucc.column_name,
61
						uccpk.table_name pk_table_name, uccpk.column_name pk_column_name,
62
						uc.delete_rule, uc.deferrable, uc.deferred
63
					FROM user_constraints uc, user_cons_columns ucc, user_cons_columns uccpk
64
					WHERE uc.constraint_type = 'R'
65
						AND ucc.constraint_name = uc.constraint_name
66
						AND uccpk.constraint_name = uc.r_constraint_name
67
						AND uccpk.table_name = '$ltable'" );
68
			while ( ( $row = $result->fetchRow() ) !== false ) {
69
70
				$this->output( "Altering {$row['constraint_name']} ..." );
71
72
				try {
73
					$dbw->query( "ALTER TABLE {$row['table_name']}
74
							DROP CONSTRAINT {$wgDBprefix}{$row['constraint_name']}" );
75
				} catch ( DBQueryError $exdb ) {
76
					if ( $exdb->errno != 2443 ) {
77
						throw $exdb;
78
					}
79
				}
80
81
				$deleteRule = $row['delete_rule'] == 'NO ACTION' ? '' : "ON DELETE {$row['delete_rule']}";
82
				$dbw->query( "ALTER TABLE {$row['table_name']}
83
						ADD CONSTRAINT {$wgDBprefix}{$row['constraint_name']}
84
						FOREIGN KEY ({$row['column_name']})
85
						REFERENCES {$wgSharedDB}.$stable({$row['pk_column_name']})
86
						{$deleteRule} {$row['deferrable']} INITIALLY {$row['deferred']}" );
87
88
				$this->output( "DONE\n" );
89
			}
90
		}
91
	}
92
}
93
94
$maintClass = "AlterSharedConstraints";
95
require_once RUN_MAINTENANCE_IF_MAIN;
96