MediaWiki REL1_31
alterSharedConstraints.php
Go to the documentation of this file.
1<?php
23
32require_once __DIR__ . '/../Maintenance.php';
33
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Alter foreign key to reference master tables in shared database setup.' );
38 }
39
40 public function getDbType() {
42 }
43
44 public function execute() {
46
47 if ( $wgSharedDB == null ) {
48 $this->output( "Database sharing is not enabled\n" );
49
50 return;
51 }
52
53 $dbw = $this->getDB( DB_MASTER );
54 foreach ( $wgSharedTables as $table ) {
55 $stable = $dbw->tableNameInternal( $table );
56 if ( $wgSharedPrefix != null ) {
57 $ltable = preg_replace( "/^$wgSharedPrefix(.*)/i", "$wgDBprefix\\1", $stable );
58 } else {
59 $ltable = "{$wgDBprefix}{$stable}";
60 }
61
62 $result = $dbw->query( "SELECT uc.constraint_name, uc.table_name, ucc.column_name,
63 uccpk.table_name pk_table_name, uccpk.column_name pk_column_name,
64 uc.delete_rule, uc.deferrable, uc.deferred
65 FROM user_constraints uc, user_cons_columns ucc, user_cons_columns uccpk
66 WHERE uc.constraint_type = 'R'
67 AND ucc.constraint_name = uc.constraint_name
68 AND uccpk.constraint_name = uc.r_constraint_name
69 AND uccpk.table_name = '$ltable'" );
70
71 while ( ( $row = $result->fetchRow() ) !== false ) {
72 $this->output( "Altering {$row['constraint_name']} ..." );
73
74 try {
75 $dbw->query( "ALTER TABLE {$row['table_name']}
76 DROP CONSTRAINT {$wgDBprefix}{$row['constraint_name']}" );
77 } catch ( DBQueryError $exdb ) {
78 if ( $exdb->errno != 2443 ) {
79 throw $exdb;
80 }
81 }
82
83 $deleteRule = $row['delete_rule'] == 'NO ACTION' ? '' : "ON DELETE {$row['delete_rule']}";
84 $dbw->query( "ALTER TABLE {$row['table_name']}
85 ADD CONSTRAINT {$wgDBprefix}{$row['constraint_name']}
86 FOREIGN KEY ({$row['column_name']})
87 REFERENCES {$wgSharedDB}.$stable({$row['pk_column_name']})
88 {$deleteRule} {$row['deferrable']} INITIALLY {$row['deferred']}" );
89
90 $this->output( "DONE\n" );
91 }
92 }
93 }
94}
95
96$maintClass = AlterSharedConstraints::class;
97require_once RUN_MAINTENANCE_IF_MAIN;
$wgDBprefix
Table name prefix.
$wgSharedTables
$wgSharedDB
Shared database for multiple wikis.
$wgSharedPrefix
When using shared tables that are referenced by foreign keys on local tables you have to change the c...
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
addDescription( $text)
Set the description text.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
require_once RUN_MAINTENANCE_IF_MAIN
const DB_MASTER
Definition defines.php:29