<?php
/*========================================================*\
||########################################################||
||# #||
||# WB News v2.0.0 #||
||# ---------------------------------------------------- #||
||# Copyright (c) 2004-2007 #||
||# Created: 8th December 2007 #||
||# Filename: restore_database.php #||
||# #||
||########################################################||
/*========================================================*/
/**
* @author $Author: pmcilwaine $
* @version $Id: restore_database.php,v 1.1.2.1 2008/02/03 09:37:25 pmcilwaine Exp $
*/
class RestoreDatabase extends DBase
{
var $filename;
var $err_msg;
var $current_tag = NULL;
var $current_table = NULL;
var $field_name = NULL;
var $record = -1;
var $tabledata = array();
var $restore_version = "2.0"; // use 2nd class to handle original
function RestoreDatabase( $filename )
{
if ( !file_exists( $filename ) )
{
$this->err_msg = "";
return FALSE;
}
$this->filename = $filename;
}
function Parse()
{
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
xml_set_object($xml_parser, $this);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($this->filename, "r")))
{
die("could not open XML input");
}
while ($data = fread($fp, 4096))
{
if (!xml_parse($xml_parser, $data, feof($fp)))
{
$this->err_msg = sprintf("XML error: %s at line %d (%s)",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser),
$this->filename );
}
}
xml_parser_free($xml_parser);
fclose( $fp );
return TRUE;
}
function startElement( $parser, $name, $attrs )
{
switch ( $name )
{
case "wbsml":
$this->restore_version = 1;
if ( isset($attrs["version"]) )
{
$this->restore_version = intval( $attrs["version"] );
}
break;
case "table":
if ( isset($attrs["name"]) && "" != $attrs["name"] )
{
$this->tabledata[ $attrs["name"] ] = array();
$this->current_table = $attrs["name"];
$this->record = -1;
}
break;
case "record":
if ( NULL != $this->current_table )
{
++$this->record;
$this->tabledata[ $this->current_table ][ $this->record ] = array();
}
break;
case "field":
if ( NULL != $this->current_table && isset($attrs["fieldname"]) && "" != $attrs["fieldname"] )
{
$this->field_name = $attrs["fieldname"];
$this->tabledata[ $this->current_table ][ $this->record ][ $this->field_name ] = NULL;
}
break;
}
$this->current_tag = $name;
}
function endElement( $parser, $name )
{
return;
}
function characterData( $parser, $data )
{
$data = trim( $data );
if ( $this->current_tag == "field"
&& NULL != $this->current_table
&& NULL != $this->field_name && "" != $data )
{
$this->tabledata[ $this->current_table ][ $this->record ][ $this->field_name ] = $data;
}
}
function RestoreData( $overwrite )
{
$this->overwrite = $overwrite;
switch ( $this->restore_version )
{
case 1:
$restore = new RestoreDatabase_v1( $this );
$restore->RestoreData();
break;
}
}
}
class RestoreDatabase_v1
{
var $restore;
function RestoreDatabase_v1( $obj )
{
$this->restore =& $obj;
}
function RestoreData()
{
foreach( $this->restore->tabledata as $tablename => $records )
{
foreach ( $records as $_ => $record )
{
if ( !$this->restore->overwrite )
{
$idname = "";
$id = "";
switch ( $tablename )
{
}
$this->query( "SELECT \"$idname\" FROM \"$tablename\" WHERE \"$idname\"='$id'" );
if ( $this->next_record() )
{
continue;
}
}
/** get data **/
}
}
}
}
return;
?>