Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extract a single ExpressionEngine site from a MSM install
<?php
/*
* Instructions:
* First add your database connection details below.
* Then either upload this script to your server and visit the page,
* or run it using the command line: php no_more_msm.php
*/
// database connection details
$server = '127.0.0.1';
$database = 'your_database';
$username = 'your_username';
$password = 'your_password';
$table_prefix = 'exp_';
$site_id = '2'; // the MSM site id you want to keep!
// no need to edit below this line...
function show_error()
{
$error = mysql_error();
echo "Error: $error\n";
exit;
}
$db = mysql_connect($server, $username, $password) or show_error();
mysql_select_db($database) or show_error();
// check target site exists
$site_id = (int)$site_id;
$query = mysql_query("SELECT * FROM `{$table_prefix}sites` WHERE `site_id` = $site_id");
$row = mysql_fetch_assoc($query);
mysql_free_result($query);
if ($row) {
echo "Found site: {$row['site_label']}\n\n";
} else {
echo "Can't find site ID $site_id!\n";
exit;
}
// update tables
$tables = array();
$query = mysql_query("SHOW TABLES LIKE '$table_prefix%'") or show_error();
while ($row = mysql_fetch_row($query)) {
$tables[] = $row[0];
}
mysql_free_result($query);
foreach ($tables as $table) {
echo "Table $table:\n";
$query = mysql_query("SHOW COLUMNS FROM `$table` LIKE 'site_id'") or show_error();
$site_row = mysql_fetch_assoc($query);
mysql_free_result($query);
if ($site_row) {
// delete all rows with unwanted site ids
mysql_query("DELETE FROM `$table` WHERE `site_id` != $site_id") or show_error();
echo " Deleted ".mysql_affected_rows()." unwanted rows.\n";
// migrate all remaining rows to site id 1
mysql_query("UPDATE `$table` SET `site_id` = 1") or show_error();
echo " Migrated ".mysql_affected_rows()." rows.\n";
}
}
// update target site name to "default_site"
mysql_query("UPDATE `{$table_prefix}sites` SET `site_name` = 'default_site'") or show_error();
echo <<<EOF
Complete!
!!! Remember to remove \$config['multiple_sites_enabled'] = 'y'; from config.php!
EOF;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.