Welcome, Guest. Please login or register.
Did you miss your activation email?
May 27, 2012, 01:16:43 AM

Login with username, password and session length
Search:     Advanced search
Interested in joining the WebsiteBaker team?
For more Information read here or on our new website.
155554 Posts in 21715 Topics by 7737 Members
Latest Member: gx-world
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Copy content from preview to live site  (Read 7518 times)
Lútsen

Offline Offline

Posts: 10


WWW
« on: November 03, 2006, 10:55:32 AM »

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:

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.
« Last Edit: January 24, 2007, 04:18:52 PM by Lútsen » Logged
kweitzel
Forum administrator
*****
Offline Offline

Posts: 6977


WWW
« Reply #1 on: November 03, 2006, 03:14:20 PM »

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

cheers

Klaus
Logged

WebsiteBaker Org e.V. - for WebsiteBaker

Lútsen

Offline Offline

Posts: 10


WWW
« Reply #2 on: November 03, 2006, 04:35:21 PM »

No problem, put it up in the WIKI. Cool!
Logged
Ari Lindholm

Offline Offline

Posts: 31


WWW
« Reply #3 on: November 07, 2006, 06:44:04 PM »

What a wonderful piece of code! Very adaptable and suitable for many needs.
Logged
kweitzel
Forum administrator
*****
Offline Offline

Posts: 6977


WWW
« Reply #4 on: November 08, 2006, 09:54:43 AM »

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
Logged

WebsiteBaker Org e.V. - for WebsiteBaker

Lútsen

Offline Offline

Posts: 10


WWW
« Reply #5 on: November 08, 2006, 03:54:59 PM »

Hi Klaus, the WIKI entry is fine. Feels good to be in the WB WIKI  smiley
Logged
Vincent

Offline Offline

Posts: 360


WWW
« Reply #6 on: November 10, 2006, 03:07:21 PM »

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

Vincent
Logged
Lútsen

Offline Offline

Posts: 10


WWW
« Reply #7 on: January 24, 2007, 04:20:05 PM »

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.
Logged
kweitzel
Forum administrator
*****
Offline Offline

Posts: 6977


WWW
« Reply #8 on: January 24, 2007, 04:35:50 PM »

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

cheers

Klaus
Logged

WebsiteBaker Org e.V. - for WebsiteBaker

Lútsen

Offline Offline

Posts: 10


WWW
« Reply #9 on: January 28, 2007, 02:34:56 PM »

Thanx!
Logged
DGEC

Offline Offline

Posts: 386


WWW
« Reply #10 on: February 06, 2007, 10:12:26 PM »

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?
Logged
kweitzel
Forum administrator
*****
Offline Offline

Posts: 6977


WWW
« Reply #11 on: February 07, 2007, 08:13:20 AM »

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

Quote
This 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
Logged

WebsiteBaker Org e.V. - for WebsiteBaker

instantflorian
Betatester
*
Offline Offline

Posts: 804



WWW
« Reply #12 on: January 13, 2009, 11:27:42 AM »

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:

Code:
<?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_VIE W by the name of your subdirectory with the edit view)

Logged
kweitzel
Forum administrator
*****
Offline Offline

Posts: 6977


WWW
« Reply #13 on: January 14, 2009, 10:29:07 AM »

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
Logged

WebsiteBaker Org e.V. - for WebsiteBaker

instantflorian
Betatester
*
Offline Offline

Posts: 804



WWW
« Reply #14 on: January 15, 2009, 09:14:59 PM »

I can do, aber nur auf deutsch.
Logged
erpe

Offline Offline

Posts: 2077


WWW
« Reply #15 on: January 15, 2009, 10:54:19 PM »

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

Gruss

erpe

Logged

Argos
Moderator
**
Online Online

Posts: 2161


WWW
« Reply #16 on: February 27, 2009, 12:38:23 PM »

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.
Logged

Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
WB Showcase: http://www.mywebsitebaker.com/pages/showcase.php?v&category_id=1242&count=30
----------------------------------------------------------------
Please don't request personal support, use the forums!
deb01

Offline Offline

Posts: 75


« Reply #17 on: August 05, 2009, 03:56:05 PM »

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!
Logged
instantflorian
Betatester
*
Offline Offline

Posts: 804



WWW
« Reply #18 on: August 06, 2009, 07:50:36 AM »

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
Logged
crnogorac081
AddOn Development
*
Offline Offline

Posts: 1706



« Reply #19 on: August 06, 2009, 11:31:46 AM »

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

Wow, I coded something myself: PM Messanger Modul ,Searchbox with suggestions
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!