MediaWiki REL1_33
CloneDatabase.php
Go to the documentation of this file.
1<?php
25
29
32
35
38
40 private $useTemporaryTables = true;
41
43 private $db;
44
54 ) {
55 if ( !$tablesToClone ) {
56 throw new InvalidArgumentException( 'Empty list of tables to clone' );
57 }
58 $this->db = $db;
59 $this->tablesToClone = $tablesToClone;
60 $this->newTablePrefix = $newTablePrefix;
61 $this->oldTablePrefix = $oldTablePrefix ?? $this->db->tablePrefix();
62 $this->dropCurrentTables = $dropCurrentTables;
63 }
64
69 public function useTemporaryTables( $u = true ) {
70 $this->useTemporaryTables = $u;
71 }
72
76 public function cloneTableStructure() {
78 foreach ( $this->tablesToClone as $tbl ) {
79 if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) {
80 // Shared tables don't work properly when cloning due to
81 // how prefixes are handled (T67654)
82 throw new RuntimeException( "Cannot clone shared table $tbl." );
83 }
84 # Clean up from previous aborted run. So that table escaping
85 # works correctly across DB engines, we need to change the pre-
86 # fix back and forth so tableName() works right.
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 // Postgres: Temp tables are automatically deleted upon end of session
95 // Same Temp table name hides existing table for current session
96 if ( $this->dropCurrentTables
97 && !in_array( $this->db->getType(), [ 'oracle' ] )
98 ) {
99 if ( $oldTableName === $newTableName ) {
100 // Last ditch check to avoid data loss
101 throw new LogicException( "Not dropping new table, as '$newTableName'"
102 . " is name of both the old and the new table." );
103 }
104 $this->db->dropTable( $tbl, __METHOD__ );
105 wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
106 // Dropping the oldTable because the prefix was changed
107 }
108
109 # Create new table
110 wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
111 $this->db->duplicateTableStructure(
112 $oldTableName, $newTableName, $this->useTemporaryTables );
113 }
114 }
115
120 public function destroy( $dropTables = false ) {
121 if ( $dropTables ) {
122 $this->db->tablePrefix( $this->newTablePrefix );
123 foreach ( $this->tablesToClone as $tbl ) {
124 $this->db->dropTable( $tbl );
125 }
126 }
127 $this->db->tablePrefix( $this->oldTablePrefix );
128 }
129
136 public static function changePrefix( $prefix ) {
137 global $wgDBprefix;
138
139 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
140 $lbFactory->setLocalDomainPrefix( $prefix );
141 $wgDBprefix = $prefix;
142 }
143}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgDBprefix
Table name prefix.
$wgSharedTables
$wgSharedDB
Shared database for multiple wikis.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
bool $useTemporaryTables
Whether to use temporary tables or not.
bool $dropCurrentTables
Should we DROP tables containing the new names?
cloneTableStructure()
Clone the table structure.
IMaintainableDatabase $db
static changePrefix( $prefix)
Change the table prefix on all open DB connections.
string $newTablePrefix
Table prefix for cloning.
__construct(IMaintainableDatabase $db, array $tablesToClone, $newTablePrefix, $oldTablePrefix=null, $dropCurrentTables=true)
destroy( $dropTables=false)
Change the prefix back to the original.
array $tablesToClone
List of tables to be cloned.
useTemporaryTables( $u=true)
Set whether to use temporary tables or not.
string $oldTablePrefix
Current table prefix.
MediaWikiServices is the service locator for the application scope of MediaWiki.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
tablePrefix( $prefix=null)
Get/set the table prefix.
Advanced database interface for IDatabase handles that include maintenance methods.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))