Use __DIR__ instead of dirname( __FILE__ )
[lhc/web/wiklou.git] / maintenance / oracle / alterSharedConstraints.php
1 <?php
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 * @ingroup Maintenance
19 */
20
21 /**
22 * When using shared tables that are referenced by foreign keys on local
23 * tables you have to change the constraints on local tables.
24 *
25 * The shared tables have to have GRANT REFERENCE on shared tables to local schema
26 * i.e.: GRANT REFERENCES (user_id) ON mwuser TO hubclient;
27 */
28
29 require_once( __DIR__ . '/../Maintenance.php' );
30
31 class AlterSharedConstraints extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Alter foreign key to reference master tables in shared database setup.";
35 }
36
37 public function getDbType() {
38 return Maintenance::DB_ADMIN;
39 }
40
41 public function execute() {
42 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix, $wgDBprefix;
43
44 if ( $wgSharedDB == null ) {
45 $this->output( "Database sharing is not enabled\n" );
46 return;
47 }
48
49 $dbw = wfGetDB( DB_MASTER );
50 foreach ( $wgSharedTables as $table ) {
51 $stable = $dbw->tableNameInternal($table);
52 if ( $wgSharedPrefix != null ) {
53 $ltable = preg_replace( "/^$wgSharedPrefix(.*)/i", "$wgDBprefix\\1", $stable );
54 } else {
55 $ltable = "{$wgDBprefix}{$stable}" ;
56 }
57
58 $result = $dbw->query( "SELECT uc.constraint_name, uc.table_name, ucc.column_name, uccpk.table_name pk_table_name, uccpk.column_name pk_column_name, uc.delete_rule, uc.deferrable, uc.deferred
59 FROM user_constraints uc, user_cons_columns ucc, user_cons_columns uccpk
60 WHERE uc.constraint_type = 'R'
61 AND ucc.constraint_name = uc.constraint_name
62 AND uccpk.constraint_name = uc.r_constraint_name
63 AND uccpk.table_name = '$ltable'" );
64 while (($row = $result->fetchRow()) !== false) {
65
66 $this->output( "Altering {$row['constraint_name']} ...");
67
68 try {
69 $dbw->query( "ALTER TABLE {$row['table_name']} DROP CONSTRAINT {$wgDBprefix}{$row['constraint_name']}" );
70 } catch (DBQueryError $exdb) {
71 if ($exdb->errno != 2443) {
72 throw $exdb;
73 }
74 }
75
76 $deleteRule = $row['delete_rule'] == 'NO ACTION' ? '' : "ON DELETE {$row['delete_rule']}";
77 $dbw->query( "ALTER TABLE {$row['table_name']} ADD CONSTRAINT {$wgDBprefix}{$row['constraint_name']}
78 FOREIGN KEY ({$row['column_name']})
79 REFERENCES {$wgSharedDB}.$stable({$row['pk_column_name']})
80 {$deleteRule} {$row['deferrable']} INITIALLY {$row['deferred']}" );
81
82 $this->output( "DONE\n" );
83 }
84 }
85 }
86
87 }
88
89 $maintClass = "AlterSharedConstraints";
90 require_once( RUN_MAINTENANCE_IF_MAIN );