WebsiteBaker Logo
  • *
  • Templates
  • Help
  • Add-ons
  • Download
  • Home
*
Welcome, Guest. Please login or register.

Login with username, password and session length
 

News


WebsiteBaker 2.13.6 is now available!


Will it continue with WB? It goes on! | Geht es mit WB weiter? Es geht weiter!
https://forum.websitebaker.org/index.php/topic,32340.msg226702.html#msg226702


The forum email address board@websitebaker.org is working again
https://forum.websitebaker.org/index.php/topic,32358.0.html


R.I.P Dietmar (luisehahne) and thank you for all your valuable work for WB
https://forum.websitebaker.org/index.php/topic,32355.0.html


* Support WebsiteBaker

Your donations will help to:

  • Pay for our dedicated server
  • Pay for domain registration
  • and much more!

You can donate by clicking on the button below.


  • Home
  • Help
  • Search
  • Login
  • Register

  • WebsiteBaker Community Forum »
  • WebsiteBaker Support (2.8.x) »
  • Droplets & Snippets »
  • Copy content from preview to live site
  • Print
Pages: [1]   Go Down

Author Topic: Copy content from preview to live site  (Read 24940 times)

lutsen

  • Guest
Copy content from preview to live site
« 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: [Select]
<?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(&#39;DB_HOST&#39;, &#39;localhost&#39;);
define(&#39;DB_NAME&#39;, &#39;&#39;);
define(&#39;DB_USERNAME&#39;, &#39;&#39;);
define(&#39;DB_PASSWORD&#39;, &#39;&#39;);

define(&#39;PREFIX_LIVE&#39;, &#39;live_&#39;);

define(&#39;BASE_PATH&#39;, &#39;path/to/your/site&#39;);
define(&#39;BASE_URL&#39;, &#39;http://www.yoursite.nl&#39;);
define(&#39;SOURCE_PATH&#39;, &#39;/preview&#39;);
define(&#39;DEST_PATH&#39;, &#39;/live&#39;);

// End configuration



if ($_POST[&#39;copysite&#39;]) {



// 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 = &#39;SHOW TABLES&#39;;
$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 = &#39;DROP TABLE IF EXISTS &#39;.PREFIX_LIVE.$dbarray[0];
if (!mysql_query($q,$conn)) die("Could not drop table.<br />\n".mysql_error()."<br />\n");
$q = &#39;CREATE TABLE &#39;.PREFIX_LIVE.$dbarray[0].&#39; LIKE &#39;.$dbarray[0];
if (!mysql_query($q,$conn)) die("Could not create table.<br />\n".mysql_error()."<br />\n");
$q = &#39;INSERT INTO &#39;.PREFIX_LIVE.$dbarray[0].&#39; SELECT * FROM &#39;.$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&#39;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 &#39;source directory&#39;, str &#39;destination directory&#39; [, bool &#39;overwrite existing files&#39;] )
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 != &#39;.&#39; && $file != &#39;..&#39;) {
$path = $source . &#39;/&#39; . $file;
if(is_file($basePath . $path)) {
 if(!is_file($basePath . $dest . &#39;/&#39; . $file) || $overwrite)
 if(!@copy($basePath . $path, $basePath . $dest . &#39;/&#39; . $file)){
  echo &#39;File (&#39;.$path.&#39;) could not be copied, likely a permissions problem.&#39;;
 }
} elseif(is_dir($basePath . $path)){
 if(!is_dir($basePath . $dest . &#39;/&#39; . $file))
 mkdir($basePath . $dest . &#39;/&#39; . $file); // make subdirectory before subdirectory is copied
 dircpy($basePath, $path, $dest . &#39;/&#39; . $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 &
#39;<a href="&#39;.BASE_URL.SOURCE_PATH.&#39;">&#39;.BASE_URL.SOURCE_PATH.&#39;</a><br />has been copied to<br /><a href="&#39;.BASE_URL.DEST_PATH.&#39;">&#39;.BASE_URL.DEST_PATH.&#39;</a>&#39;;



} 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

Offline kweitzel

  • WebsiteBaker Org e.V.
  • **
  • Posts: 6983
  • Gender: Male
Re: Copy content from preview to live site
« 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

lutsen

  • Guest
Re: Copy content from preview to live site
« Reply #2 on: November 03, 2006, 04:35:21 PM »
No problem, put it up in the WIKI. Cool!
Logged

Ari Lindholm

  • Guest
Re: Copy content from preview to live site
« Reply #3 on: November 07, 2006, 06:44:04 PM »
What a wonderful piece of code! Very adaptable and suitable for many needs.
Logged

Offline kweitzel

  • WebsiteBaker Org e.V.
  • **
  • Posts: 6983
  • Gender: Male
Re: Copy content from preview to live site
« 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

lutsen

  • Guest
Re: Copy content from preview to live site
« 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  :-)
Logged

Offline Vincent

  • Posts: 376
  • Gender: Male
    • websites voor ondernemers
Re: Copy content from preview to live site
« 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

lutsen

  • Guest
Re: Copy content from preview to live site
« 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

Offline kweitzel

  • WebsiteBaker Org e.V.
  • **
  • Posts: 6983
  • Gender: Male
Re: Copy content from preview to live site
« Reply #8 on: January 24, 2007, 04:35:50 PM »
OK ... updated the WIKI article as well ...

cheers

Klaus
Logged

lutsen

  • Guest
Re: Copy content from preview to live site
« Reply #9 on: January 28, 2007, 02:34:56 PM »
Thanx!
Logged

Offline DGEC

  • Posts: 388
  • Gender: Male
    • EbelTech homepage
Re: Copy content from preview to live site
« 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

Offline kweitzel

  • WebsiteBaker Org e.V.
  • **
  • Posts: 6983
  • Gender: Male
Re: Copy content from preview to live site
« 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

instantflorian

  • Guest
Re: Copy content from preview to live site
« 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: [Select]
<?php
function opff_correctPath(&$content, $page_id, $section_id, $module, $wb) {
  
// add filter here
if (strpos(WB_URL,&#39;PATH_TO_STAGING_VIEW&#39;)==false) {
$content=str_replace(&#39;/PATH_TO_STAGING_VIEW&#39;,&#39;&#39;,$content);  
}
  return(
TRUE);
}
?>

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

Logged

Offline kweitzel

  • WebsiteBaker Org e.V.
  • **
  • Posts: 6983
  • Gender: Male
Re: Copy content from preview to live site
« 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

instantflorian

  • Guest
Re: Copy content from preview to live site
« Reply #14 on: January 15, 2009, 09:14:59 PM »
I can do, aber nur auf deutsch.
Logged

erpe0812

  • Guest
Re: Copy content from preview to live site
« 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

  • Guest
Re: Copy content from preview to live site
« 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

Offline deb01

  • Posts: 79
Re: Copy content from preview to live site
« Reply #17 on: August 05, 2009, 04: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

  • Guest
Re: Copy content from preview to live site
« Reply #18 on: August 06, 2009, 08: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

Offline crnogorac081

  • Posts: 2161
  • Gender: Male
Re: Copy content from preview to live site
« Reply #19 on: August 06, 2009, 12:31:46 PM »
how to make a list in the code which tables will be allowed to copy ?
Logged
Web developer

  • Print
Pages: [1]   Go Up
  • WebsiteBaker Community Forum »
  • WebsiteBaker Support (2.8.x) »
  • Droplets & Snippets »
  • Copy content from preview to live site
 

  • SMF 2.0.19 | SMF © 2017, Simple Machines
  • XHTML
  • RSS
  • WAP2