Copy content from preview to live site

crnogorac081

how to make a list in the code which tables will be allowed to copy ?
Web developer

instantflorian

Yes, you're right. Hm. A solution could be not to install the guestbook module in the preview site, so the table with the entries is maybe not changed. But that's only a guess, you should test it.
Another possibilty is to modify the update script, e.g. tables with special names should not be droppe/copied.

Regards
instantflorian

deb01

A question left. I understand how the script works.

But when using -say- a guestbook on the live system by syncing with preview system all entries would get lost - am I right?

Can you tell me some about your experiences in such scenario?

Thanks!

Argos

I think this snippet should be renamed. It does not just copies content, it's more of a installments synchronizer. We already have several copy related snippets/modules/tools and it's important to give them proper names to prevent confusion.
Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
----------------------------------------------------------------
Please don't request personal support, use the forums!

erpe0812

Das ist doch prima.
Wir finden dann bestimmt jemanden, der das übersetzt un EN und NL.

Gruss

erpe


instantflorian


kweitzel

The whole process needs to be reviewed, since it has been written in 2007 ... would you volunteer to write an article about this for our Helppage?

cheers

Klaus

instantflorian

To avoid that the script copies the absolute staging view path of images or manually added popup links to the live view, I think it's useful to install the tool "Frontend Filter" from http://www.websitebakers.com/pages/admin/admin-tools/frontendfilter.php and configure a page filter like this:


<?php
function opff_correctPath(&$content$page_id$section_id$module$wb) {
  
// add filter here
if (strpos(WB_URL,'PATH_TO_STAGING_VIEW')==false) {
$content=str_replace('/PATH_TO_STAGING_VIEW','',$content);  
}
  return(
TRUE);
}
?>


(Replace PATH_TO_STAGING_VIEW by the name of your subdirectory with the edit view)


kweitzel

The answer lies within the first line of the initial post:

QuoteThis is some code I wrote to copy the databse tables and the media- and pages folders from one WB installation to another.

You need to have it setup before since it does not copy the whole site.

cheers

Klaus

DGEC

I assume you need to set up the live site before you try to do the copy?  Or does it completely recreate it each time?


kweitzel

OK ... updated the WIKI article as well ...

cheers

Klaus

lutsen

I updated the code a little (in the original post). 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.

Vincent

Wonderful contribution, Lútsen. Haven't tried it yet, but will certainly be using it in the furure. Thanks!

Vincent

lutsen

Hi Klaus, the WIKI entry is fine. Feels good to be in the WB WIKI  :-)

kweitzel

OK ... it has been put on the WIKI. I just copied and pasted this piece, so if you think, that the documentation needs to be "rewritten", please let me know (send me what you would like to have in there as text or HTML) and I'll update.

The Link: http://projects.websitebaker.org/websitebaker2/wiki/Docs-EN-Advanced-Howtos-stagingwebsite

cheers

Klaus

Ari Lindholm

What a wonderful piece of code! Very adaptable and suitable for many needs.

lutsen


kweitzel

now that is a useful piece of code ... thanks a lot. You mind, if we put it up in the WIKI?

cheers

Klaus

lutsen

#1
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_HOSTDB_USERNAMEDB_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], 0strlen(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, -11) != "/") {
       
$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_PATHSOURCE_PATH.$dirDEST_PATH.$dirtrue);
echo 
"Copied folder ".$dir.".<br />\n";

$dir "/pages";
dircpy(BASE_PATHSOURCE_PATH.$dirDEST_PATH.$dirtrue);
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.