<?php
#
#copys9ydata.php License (based on BSD license)
#Copyright (c) 2005, Justin B. Alcorn
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#
#1) Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
#2) Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
#3) Neither the name of Justin B. Alcorn nor the names of its
#   contributors may be used to endorse or promote products derived from this
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#POSSIBILITY OF SUCH DAMAGE.
#
# copys9ydata.php
#
# Used to copy the data from one s9y-based blog to another, maintaining
#   category and property data.  Tested on Serendipity-0.8 with mysql only.
#
# Instructions: Put the script in your blog root directory, on a webserver with
#  a working s9y installation (can be empty, or contain data).   Make sure the
#  source and destination tables are in the same database, with different prefixes.
#  (This can be accomplished in phpMyAdmin using the "Operations" tab to copy tables).
#
#  The tables needed are:
#    $prefix_authors
#    $prefix_comments
#    $prefix_category
#    $prefix_entries
#    $prefix_entrycat
#    $prefix_entryproperties
#    $prefix_exits
#    $prefix_images
#    $prefix_references
#
#    Images will have to be copied from the uploads/ directory of one blog to another.
#        if you have images with conflicting names, you'll have to handle that by hand.
#
# Edit the following variables to match your setup
#
# prefix for original blog data
$from "serendipity_";
# prefix for new blog data
$to "serendipity2_";
$debug 0;
# End of modifications
require_once("serendipity_config.inc.php");
set_magic_quotes_runtime(TRUE);
print 
"<p>Authors:\n";
$authorassoc = array();
$authorassoc[0] = 0;
$res mysql_query("SELECT * FROM ".$from."authors");
while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
    
$saveid $row['authorid'];
    
$row['authorid'] = "";
    print(
"<br>".$row['realname']);
    
$sql "INSERT INTO ".$to."authors (" implode(', 'array_keys($row)) . ') VALUES ("' implode('", "'array_values($row)) . '")';
    if (
$debug) print "<br>$sql\n";
    
mysql_query($sql);
    
$authorassoc[$saveid] = mysql_insert_id();
}

print(
"<p>Categories:\n");
$catassoc = array();
$catassoc[0] = 0;
$res mysql_query("SELECT * FROM ".$from."category");
while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
    
$row['authorid'] = $authorassoc[$row['authorid']];
    
$saveid $row['categoryid'];
    
$row['categoryid'] = "";
    print(
"<br>".$row['category_name']."\n");
    
$sql "INSERT INTO ".$to."category (" implode(', 'array_keys($row)) . ') VALUES ("' implode('", "'array_values($row)) . '")';
    if (
$debug) print "<br>$sql\n";
    
mysql_query($sql);
    
$catassoc[$saveid] = mysql_insert_id();
}

$res mysql_query("SELECT categoryid,parentid FROM ".$from."category");
while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
    
$sql "UPDATE ".$to.'category SET parentid = "' $catassoc[$row['parentid']] .'" WHERE categoryid = "' $catassoc[$row['categoryid']] . '"';
    if (
$debug) print "<br>$sql\n";
    
mysql_query($sql);
}
$rc serendipity_rebuildCategoryTree();

print(
"<p>Entries:\n");
$res mysql_query("SELECT * FROM ".$from."entries");
$assoc = array();
while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
    
$saveid $row['id'];
    
$row['id'] = "";
    
$row['authorid'] = $authorassoc[$row['authorid']];
    print(
"<br>".$row['title']."\n");
    
$sql "INSERT INTO ".$to."entries (" implode(', 'array_keys($row)) . ') VALUES ("' implode('", "'array_values($row)) . '")';
    if (
$debug) print "<br>$sql\n";
    
mysql_query($sql);
    
$assoc[$saveid] = mysql_insert_id();


$commentassoc = array();
$commentassoc[0] = 0;
print(
"<p>Comments:\n");
$res mysql_query("SELECT * FROM ".$from."comments");
while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
    
$saveid $row['id'];
    
$row['id'] = "";
    
$row['entry_id'] = $assoc[$row['entry_id']];
    print(
"<br>$saveid\n");
    
$sql "INSERT INTO ".$to."comments (" implode(', 'array_keys($row)) . ') VALUES ("' implode('", "'array_values($row)) . '")';
    if (
$debug) print "<br>$sql\n";
    
mysql_query($sql);
    
$commentassoc[$saveid] = mysql_insert_id();
}

$res mysql_query("SELECT id,parent_id FROM ".$to."comments");
while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
    
$sql "UPDATE ".$to.'comments SET parent_id = "' $commentassoc[$row['parent_id']] .'" WHERE id = "' $row['id'] . '"';
    if (
$debug) print "<br>$sql\n";
    
mysql_query($sql);
}

print(
"<p>Entrycat\n");
$res mysql_query("SELECT * FROM ".$from."entrycat");
while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
    
$row['entryid'] = $assoc[$row['entryid']];
    
$row['categoryid'] = $catassoc[$row['categoryid']];
    
$sql "INSERT INTO ".$to."entrycat (" implode(', 'array_keys($row)) . ') VALUES ("' implode('", "'array_values($row)) . '")';
    if (
$debug) print "<br>$sql\n";
    
mysql_query($sql);
}

print(
"<p>Entryproperties\n");
$res mysql_query("SELECT * FROM ".$from."entryproperties");
while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
    
$row['entryid'] = $assoc[$row['entryid']];
    
$sql "INSERT INTO ".$to."entryproperties (" implode(', 'array_keys($row)) . ') VALUES ("' implode('", "'array_values($row)) . '")';
    if (
$debug) print "<br>$sql\n";
    
mysql_query($sql);
}

print(
"<p>Images\n");
$res mysql_query("SELECT * FROM ".$from."images");
while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
    
$row['authorid'] = $authorassoc[$row['authorid']];
    
$sql "INSERT INTO ".$to."images (" implode(', 'array_keys($row)) . ') VALUES ("' implode('", "'array_values($row)) . '")';
    if (
$debug) print "<br>$sql\n";
    
mysql_query($sql);
}

print(
"<p>References\n");
$res mysql_query("SELECT * FROM ".$from."references");
while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
    
$row['id'] = "";
    
$row['entry_id'] = $assoc[$row['entry_id']];
    
$sql "INSERT INTO ".$to."references (" implode(', 'array_keys($row)) . ') VALUES ("' implode('", "'array_values($row)) . '")';
    if (
$debug) print "<br>$sql\n";
    
mysql_query($sql);
}

print(
"<p>Done\n");