MediaWiki  REL1_21
alterSharedConstraints.php
Go to the documentation of this file.
00001 <?php
00030 require_once( __DIR__ . '/../Maintenance.php' );
00031 
00032 class AlterSharedConstraints extends Maintenance {
00033         public function __construct() {
00034                 parent::__construct();
00035                 $this->mDescription = "Alter foreign key to reference master tables in shared database setup.";
00036         }
00037 
00038         public function getDbType() {
00039                 return Maintenance::DB_ADMIN;
00040         }
00041 
00042         public function execute() {
00043                 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix, $wgDBprefix;
00044 
00045                 if ( $wgSharedDB == null ) {
00046                         $this->output( "Database sharing is not enabled\n" );
00047                         return;
00048                 }
00049 
00050                 $dbw = wfGetDB( DB_MASTER );
00051                 foreach ( $wgSharedTables as $table ) {
00052                         $stable = $dbw->tableNameInternal($table);
00053                         if ( $wgSharedPrefix != null ) {
00054                                 $ltable = preg_replace( "/^$wgSharedPrefix(.*)/i", "$wgDBprefix\\1", $stable );
00055                         } else {
00056                                 $ltable = "{$wgDBprefix}{$stable}" ;
00057                         }
00058 
00059                         $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
00060                                           FROM user_constraints uc, user_cons_columns ucc, user_cons_columns uccpk
00061                                          WHERE uc.constraint_type = 'R'
00062                                            AND ucc.constraint_name = uc.constraint_name
00063                                            AND uccpk.constraint_name = uc.r_constraint_name
00064                                            AND uccpk.table_name = '$ltable'" );
00065                         while (($row = $result->fetchRow()) !== false) {
00066 
00067                                         $this->output( "Altering {$row['constraint_name']} ...");
00068 
00069                                         try {
00070                                                 $dbw->query( "ALTER TABLE {$row['table_name']} DROP CONSTRAINT {$wgDBprefix}{$row['constraint_name']}" );
00071                                         } catch (DBQueryError $exdb) {
00072                                                 if ($exdb->errno != 2443) {
00073                                                         throw $exdb;
00074                                                 }
00075                                         }
00076 
00077                                         $deleteRule = $row['delete_rule'] == 'NO ACTION' ? '' : "ON DELETE {$row['delete_rule']}";
00078                                         $dbw->query( "ALTER TABLE {$row['table_name']} ADD CONSTRAINT {$wgDBprefix}{$row['constraint_name']}
00079                                                 FOREIGN KEY ({$row['column_name']})
00080                                                 REFERENCES {$wgSharedDB}.$stable({$row['pk_column_name']})
00081                                                 {$deleteRule} {$row['deferrable']} INITIALLY {$row['deferred']}" );
00082 
00083                                         $this->output( "DONE\n" );
00084                         }
00085                 }
00086         }
00087 
00088 }
00089 
00090 $maintClass = "AlterSharedConstraints";
00091 require_once( RUN_MAINTENANCE_IF_MAIN );