MediaWiki  1.28.1
CloneDatabase.php
Go to the documentation of this file.
1 <?php
27 
30  private $newTablePrefix = '';
31 
33  private $oldTablePrefix = '';
34 
36  private $tablesToClone = [];
37 
39  private $dropCurrentTables = true;
40 
42  private $useTemporaryTables = true;
43 
45  private $db;
46 
58  ) {
59  $this->db = $db;
60  $this->tablesToClone = $tablesToClone;
61  $this->newTablePrefix = $newTablePrefix;
62  $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
63  $this->dropCurrentTables = $dropCurrentTables;
64  }
65 
70  public function useTemporaryTables( $u = true ) {
71  $this->useTemporaryTables = $u;
72  }
73 
77  public function cloneTableStructure() {
79  foreach ( $this->tablesToClone as $tbl ) {
80  if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) {
81  // Shared tables don't work properly when cloning due to
82  // how prefixes are handled (bug 65654)
83  throw new RuntimeException( "Cannot clone shared table $tbl." );
84  }
85  # Clean up from previous aborted run. So that table escaping
86  # works correctly across DB engines, we need to change the pre-
87  # fix back and forth so tableName() works right.
88 
89  self::changePrefix( $this->oldTablePrefix );
90  $oldTableName = $this->db->tableName( $tbl, 'raw' );
91 
92  self::changePrefix( $this->newTablePrefix );
93  $newTableName = $this->db->tableName( $tbl, 'raw' );
94 
95  if ( $this->dropCurrentTables
96  && !in_array( $this->db->getType(), [ 'postgres', 'oracle' ] )
97  ) {
98  if ( $oldTableName === $newTableName ) {
99  // Last ditch check to avoid data loss
100  throw new LogicException( "Not dropping new table, as '$newTableName'"
101  . " is name of both the old and the new table." );
102  }
103  $this->db->dropTable( $tbl, __METHOD__ );
104  wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
105  // Dropping the oldTable because the prefix was changed
106  }
107 
108  # Create new table
109  wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
110  $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
111  }
112  }
113 
118  public function destroy( $dropTables = false ) {
119  if ( $dropTables ) {
120  self::changePrefix( $this->newTablePrefix );
121  foreach ( $this->tablesToClone as $tbl ) {
122  $this->db->dropTable( $tbl );
123  }
124  }
125  self::changePrefix( $this->oldTablePrefix );
126  }
127 
134  public static function changePrefix( $prefix ) {
136 
137  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
138  $lbFactory->setDomainPrefix( $prefix );
139  $wgDBprefix = $prefix;
140  }
141 }
string $newTablePrefix
Table prefix for cloning.
the array() calling protocol came about after MediaWiki 1.4rc1.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
destroy($dropTables=false)
Change the prefix back to the original.
$wgSharedTables
static changePrefix($prefix)
Change the table prefix on all open DB connections/.
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 MediaWikiServices
Definition: injection.txt:23
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
bool $dropCurrentTables
Should we DROP tables containing the new names?
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
cloneTableStructure()
Clone the table structure.
string $oldTablePrefix
Current table prefix.
__construct(Database $db, array $tablesToClone, $newTablePrefix, $oldTablePrefix= '', $dropCurrentTables=true)
Constructor.
$wgSharedDB
Shared database for multiple wikis.
bool $useTemporaryTables
Whether to use temporary tables or not.
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
Definition: distributors.txt:9
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:35
$lbFactory
$wgDBprefix
Table name prefix.
array $tablesToClone
List of tables to be cloned.
useTemporaryTables($u=true)
Set whether to use temporary tables or not.