This is some code I wrote to copy the databse tables and the media- and pages folders from one WB installation to another.
The idea behind this is that you can have two installations of WB for the same site:
1 installation is the "live" site; this is the site visitors of the site will see.
1 installation is the "preview" site; this is the site the administrator of the site will make the changes in. Once the administrator is satisfied with the changes made he or she will use this script to copy the changes from the preview to the live site.
This way visitors will only see the finished site, and not the work in progress as the administrator is changing the site. And the administrator can preview the changes first, and copy them to the live site when he or she is finished.
For the script to work it is required that the table-names of the live site have a prefix, and the table-names of the preview site do NOT. And that the tables of the preview and live site are in the same database. And that there are no other tables in this database, or they will be copied as well.
The code is a separate PHP file, so it is not implemented in WB. You should protect it with a password (with .htaccess or something), or else anybody can copy the preview to the live site.
When this script is used, all changes to a site should only be made in the preview site, since changes made in the live site will be overwritten when the site is copied. The preview and live installation should allways be the same, so for example modules installed in the preview site should be installed in the live site too.
The code:
<?php
// Code by Lutsen Stellingwerff (www.biepenlu.nl)
// Copy preview site to live site
// Do this bij copying the database tables and the media and pages folder from the preview to the live site.
// The tables of the live site need to have a prefix,
// and the tables of the preview site mus NOT have a prefix.
// Start configuration
define('DB_HOST', 'localhost');
define('DB_NAME', '');
define('DB_USERNAME', '');
define('DB_PASSWORD', '');
define('PREFIX_LIVE', 'live_');
define('BASE_PATH', 'path/to/your/site');
define('BASE_URL', 'http://www.yoursite.nl');
define('SOURCE_PATH', '/preview');
define('DEST_PATH', '/live');
// End configuration
if ($_POST['copysite']) {
// Connect to the mysql database.
$conn = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
mysql_select_db(DB_NAME, $conn) or die(mysql_error());
// Get existing tables
$q = 'SHOW TABLES';
$result = mysql_query($q,$conn);
if (!$result) die("Could not read tables.<br />\n".mysql_error()."<br />\n");
// Copy tables without PREFIX_LIVE to tables with PREFIX_LIVE
while($dbarray = mysql_fetch_array($result)){
if(substr($dbarray[0], 0, strlen(PREFIX_LIVE)) != PREFIX_LIVE) {
$q = 'DROP TABLE IF EXISTS '.PREFIX_LIVE.$dbarray[0];
if (!mysql_query($q,$conn)) die("Could not drop table.<br />\n".mysql_error()."<br />\n");
$q = 'CREATE TABLE '.PREFIX_LIVE.$dbarray[0].' LIKE '.$dbarray[0];
if (!mysql_query($q,$conn)) die("Could not create table.<br />\n".mysql_error()."<br />\n");
$q = 'INSERT INTO '.PREFIX_LIVE.$dbarray[0].' SELECT * FROM '.$dbarray[0];
if (!mysql_query($q,$conn)) die("Could not copy table.<br />\n".mysql_error()."<br />\n");
echo "Copied table ".$dbarray[0]."<br />\n";
}
}
function removeDir($path) {
// Add trailing slash to $path if one is not there
if (substr($path, -1, 1) != "/") {
$path .= "/";
}
$normal_files = glob($path . "*");
$hidden_files = glob($path . "\.?*");
$all_files = array_merge($normal_files, $hidden_files);
foreach ($all_files as $file) {
# Skip pseudo links to current and parent dirs (./ and ../).
if (preg_match("/(\.|\.\.)$/", $file))
{
continue;
}
if (is_file($file) === TRUE) {
// Remove each file in this Directory
@unlink($file);
}
else if (is_dir($file) === TRUE) {
// If this Directory contains a Subdirectory, run this Function on it
removeDir($file);
}
}
// Remove Directory once Files have been removed (If Exists)
if (is_dir($path) === TRUE) {
@rmdir($path);
}
}
// Function from SBoisvert at Don'tSpamMe dot Bryxal dot ca (http://nl3.php.net/manual/en/function.copy.php#68383)
// copy a directory and all subdirectories and files (recursive)
// void dircpy( str 'source directory', str 'destination directory' [, bool 'overwrite existing files'] )
function dircpy($basePath, $source, $dest, $overwrite = false){
if(is_dir($basePath . $dest)) {
removeDir($basePath . $dest); // remove existing directory
}
if(!is_dir($basePath . $dest)) {
mkdir($basePath . $dest); // Make sure new folder exists
}
if($handle = opendir($basePath . $source)) { // if the folder exploration is sucsessful, continue
while(false !== ($file = readdir($handle))) { // as long as storing the next file to $file is successful, continue
if($file != '.' && $file != '..') {
$path = $source . '/' . $file;
if(is_file($basePath . $path)) {
if(!is_file($basePath . $dest . '/' . $file) || $overwrite)
if(!@copy($basePath . $path, $basePath . $dest . '/' . $file)){
echo 'File ('.$path.') could not be copied, likely a permissions problem.';
}
} elseif(is_dir($basePath . $path)){
if(!is_dir($basePath . $dest . '/' . $file))
mkdir($basePath . $dest . '/' . $file); // make subdirectory before subdirectory is copied
dircpy($basePath, $path, $dest . '/' . $file, $overwrite); //recurse!
}
}
}
closedir($handle);
}
}
echo "<br />\n";
// Copy media and pages directory
$dir = "/media";
dircpy(BASE_PATH, SOURCE_PATH.$dir, DEST_PATH.$dir, true);
echo "Copied folder ".$dir.".<br />\n";
$dir = "/pages";
dircpy(BASE_PATH, SOURCE_PATH.$dir, DEST_PATH.$dir, true);
echo "Copied folder ".$dir.".<br />\n";
echo "<br />\n";
echo '<a href="'.BASE_URL.SOURCE_PATH.'">'.BASE_URL.SOURCE_PATH.'</a><br />has been copied to<br /><a href="'.BASE_URL.DEST_PATH.'">'.BASE_URL.DEST_PATH.'</a>';
} else {
// HTML form
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Copy PREVIEW -> LIVE</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
<!--
function doConfirm(message) {
var name = confirm(message);
// If OK is pressed the action is executed
if (name == true){
return true;
} else {
return false;
}
}
-->
</script>
</head>
<body>
<div id="page">
<p>
To copy<br />
<a href="<?php echo BASE_URL.SOURCE_PATH; ?>"><?php echo BASE_URL.SOURCE_PATH; ?></a><br />
to<br />
<a href="<?php echo BASE_URL.DEST_PATH; ?>"><?php echo BASE_URL.DEST_PATH; ?></a>,<br />
press the "copy" button.
</p>
<form name="create" action="" method="post" onsubmit="javascript:return doConfirm('Are you sure?');">
<input type="submit" name="copysite" value="copy" />
</form>
</div>
</body>
</html>
<?php
}
?>
UPDATE: I updated the code a little. It now also copies the structure of the database table (like auto-increment and primary key fields), which is important if you want to be able to edit the live site with WebsiteBaker as well (not recomended, but might be usefull sometimes), or if you want to copy the content from the live site back to the preview site. I also changed the way the media and pages directory are copied. They are now deleted in the live site first before they are copied from the preview site.