da8742e683660e5f8e570cdcec27f37723b4e87a
[lhc/web/wiklou.git] / maintenance / renamewiki.php
1 <?php
2 /**
3 * Why yes, this *is* another special-purpose Wikimedia maintenance script!
4 * Should be fixed up and generalized.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @ingroup Maintenance
22 * @ingroup Wikimedia
23 */
24
25 require_once( dirname(__FILE__) . '/Maintenance.php' );
26
27 class RenameWiki extends Maintenance {
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "Rename external storage dbs and leave a new one";
31 $this->addArgs( array( 'olddb', 'newdb' ) );
32 }
33
34 protected function getDbType() {
35 return Maintenance::DB_ADMIN;
36 }
37
38 public function execute() {
39 global $wgDefaultExternalStore;
40
41 # Setup
42 $from = $this->getArg( 0 );
43 $to = $this->getArg( 1 );
44 $this->output( "Renaming blob tables in ES from $from to $to...\n" );
45 $this->output( "Sleeping 5 seconds...\n" );
46 sleep(5);
47
48 # Initialise external storage
49 if ( is_array( $wgDefaultExternalStore ) ) {
50 $stores = $wgDefaultExternalStore;
51 } elseif ( $wgDefaultExternalStore ) {
52 $stores = array( $wgDefaultExternalStore );
53 } else {
54 $stores = array();
55 }
56
57 if ( count( $stores ) ) {
58 $this->output( "Initialising external storage $store...\n" );
59 global $wgDBuser, $wgDBpassword, $wgExternalServers;
60 foreach ( $stores as $storeURL ) {
61 $m = array();
62 if ( !preg_match( '!^DB://(.*)$!', $storeURL, $m ) ) {
63 continue;
64 }
65
66 $cluster = $m[1];
67
68 # Hack
69 $wgExternalServers[$cluster][0]['user'] = $wgDBuser;
70 $wgExternalServers[$cluster][0]['password'] = $wgDBpassword;
71
72 $store = new ExternalStoreDB;
73 $extdb =& $store->getMaster( $cluster );
74 $extdb->query( "SET table_type=InnoDB" );
75 $extdb->query( "CREATE DATABASE {$to}" );
76 $extdb->query( "ALTER TABLE {$from}.blobs RENAME TO {$to}.blobs" );
77 $extdb->selectDB( $from );
78 $extdb->sourceFile( $this->getDir() . '/storage/blobs.sql' );
79 $extdb->immediateCommit();
80 }
81 }
82 $this->output( "done.\n" );
83 }
84 }
85
86 $maintClass = "RenameWiki";
87 require_once( DO_MAINTENANCE );