Skip to content

Instantly share code, notes, and snippets.

@virullius
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save virullius/b89ab895678a0e3fc2ea to your computer and use it in GitHub Desktop.

Select an option

Save virullius/b89ab895678a0e3fc2ea to your computer and use it in GitHub Desktop.
Convert / Extract EE+MSM site to single site EE database. (ExpressionEngine,MultiSiteManager)
#!/usr/bin/env php
<?php
/* convert Multi Site Manager (MSM) ExpressionEngine (EE) database to single site database.
* used to split a site to own EE instance without MSM.
* Deletes all sites other than site_id specified then changes specified sites id to 1.
* Ignores site_id 0, although I don't rememeber why.
* I ran it from Linux command line, no idea if it will run the same on Window or Mac.
*/
// crude but sufficient argument handling
$usage = array(
'min_args' => '5',
'message' => "$argv[0] --host=example.com --schema=MyEEDb --site_id=3 --user=user_name [--pass=\"secret\"]"
);
if( $argc < $usage['min_args'] ){
exit("{$usage['message']}\n");
}
foreach( $argv as $arg ){
$arg_parts = split('=', $arg);
if( count($arg_parts) == 2 ){
$arg_name = ltrim($arg_parts[0], '-');
$arg_value = $arg_parts[1];
$$arg_name = $arg_value;
}
}
// get password without printing it the console
if( !isset($pass) ){
echo "Enter password:";
`stty -echo`;
$pass = trim(fgets(STDIN));
`stty echo`;
echo "\n";
}
if( !isset($host) || !isset($user) || !isset($pass) || !isset($schema) || !isset($site_id) ){
exit('Unknown failure');
}else{
$mysqli = new mysqli($host, $user, $pass, $schema);
if( $mysqli->connect_errno ){
exit("Failed to connect to MySQL: ({$mysqli->connect_errno}) {$mysqli->connect_error}");
}
echo "Connected to MySQL: {$mysqli->host_info}\n";
$tables = array();
$table_query = "select table_name from information_schema.columns
where table_schema = '$schema'
and column_name = 'site_id'";
if( $table_result = $mysqli->query($table_query) ){
while( $row = $table_result->fetch_row() ){
$tables[] = $row[0];
}
}
foreach( $tables as $table ){
$delete_query = "delete from $table where site_id <> $site_id and site_id <> 0";
$update_query = "update $table set site_id = 1 where site_id = $site_id";
if( $mysqli->query($delete_query) ){
$mysqli->commit();
echo "deleted from $table\n";
if( $mysqli->query($update_query) ){
$mysqli->commit();
echo "updated $table\n";
}else{
echo "FAIL\n\t$update_query\n";
}
}else{
echo "FAIL\n\t$delete_query\n";
}
}
$mysqli->close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment