| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 25 |
|
| 26 |
class CloneDatabase { |
| 27 |
|
| 28 |
private string $newTablePrefix; |
| 29 |
|
| 30 |
|
| 31 |
private string $oldTablePrefix; |
| 32 |
|
| 33 |
|
| 34 |
private array $tablesToClone; |
| 35 |
|
| 36 |
|
| 37 |
private bool $dropCurrentTables; |
| 38 |
|
| 39 |
|
| 40 |
private bool $useTemporaryTables = true; |
| 41 |
|
| 42 |
private IMaintainableDatabase $db; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct( |
| 52 |
IMaintainableDatabase $db, |
| 53 |
array $tablesToClone, |
| 54 |
string $newTablePrefix, |
| 55 |
?string $oldTablePrefix = null, |
| 56 |
bool $dropCurrentTables = true |
| 57 |
) { |
| 58 |
if ( !$tablesToClone ) { |
| 59 |
throw new InvalidArgumentException( 'Empty list of tables to clone' ); |
| 60 |
} |
| 61 |
$this->db = $db; |
| 62 |
$this->tablesToClone = $tablesToClone; |
| 63 |
$this->newTablePrefix = $newTablePrefix; |
| 64 |
$this->oldTablePrefix = $oldTablePrefix ?? $this->db->tablePrefix(); |
| 65 |
$this->dropCurrentTables = $dropCurrentTables; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function useTemporaryTables( bool $u = true ): void { |
| 73 |
$this->useTemporaryTables = $u; |
| 74 |
} |
| 75 |
|
| 76 |
public function cloneTableStructure(): void { |
| 77 |
global $wgSharedTables, $wgSharedDB; |
| 78 |
foreach ( $this->tablesToClone as $tbl ) { |
| 79 |
if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) { |
| 80 |
|
| 81 |
|
| 82 |
throw new RuntimeException( "Cannot clone shared table $tbl." ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
$this->db->tablePrefix( $this->oldTablePrefix ); |
| 89 |
$oldTableName = $this->db->tableName( $tbl, 'raw' ); |
| 90 |
|
| 91 |
$this->db->tablePrefix( $this->newTablePrefix ); |
| 92 |
$newTableName = $this->db->tableName( $tbl, 'raw' ); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
if ( $this->dropCurrentTables ) { |
| 97 |
if ( $oldTableName === $newTableName ) { |
| 98 |
|
| 99 |
throw new LogicException( "Not dropping new table, as '$newTableName'" |
| 100 |
. " is name of both the old and the new table." ); |
| 101 |
} |
| 102 |
$this->db->dropTable( $tbl, __METHOD__ ); |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
$this->db->duplicateTableStructure( |
| 108 |
$oldTableName, $newTableName, $this->useTemporaryTables, __METHOD__ ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function destroy( bool $dropTables = false ): void { |
| 117 |
if ( $dropTables ) { |
| 118 |
$this->db->tablePrefix( $this->newTablePrefix ); |
| 119 |
foreach ( $this->tablesToClone as $tbl ) { |
| 120 |
$this->db->dropTable( $tbl, __METHOD__ ); |
| 121 |
} |
| 122 |
} |
| 123 |
$this->db->tablePrefix( $this->oldTablePrefix ); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public static function changePrefix( string $prefix ): void { |
| 133 |
global $wgDBprefix, $wgDBname; |
| 134 |
|
| 135 |
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
| 136 |
$lbFactory->setLocalDomainPrefix( $prefix ); |
| 137 |
|
| 138 |
$aliases = [ |
| 139 |
$wgDBname => $lbFactory->getLocalDomainID() |
| 140 |
]; |
| 141 |
$lbFactory->setDomainAliases( $aliases ); |
| 142 |
foreach ( $lbFactory->getAllLBs() as $lb ) { |
| 143 |
$lb->setDomainAliases( $aliases ); |
| 144 |
} |
| 145 |
|
| 146 |
$wgDBprefix = $prefix; |
| 147 |
} |
| 148 |
} |