#!/usr/bin/php5
<?php
/**
 * Prototype to dump a database in XML format according to the schema
 *
 * @copyright see the html/credits.html file in this release
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 * @subpackage cli
 * @author Marcel van der Boom
 */

/**
 * Set configuration options if necessary
 *
 * Defaults to using the db connection in the application
 * and not logged in as a specific user
 *
 * @todo expose on the cmdline
 * @todo add option to set the schema name
 * @todo add option to specify a list of tables to get
 * @todo move most of this into xaraya proper and make it runnable via cli interface (currently html/ls.php)
 */
$xaruser = 'admin';
$xarpass = 'toedeliedokie';
$dbtype  = 'mysql';
$dbhost  = 'dbserver';
$dbname  =  'dbcore2x';
$dbuser  = 'user';
$dbpass  = 'pass';
/* We need a (fake) ip address to run xar */
putenv("REMOTE_ADDR=127.0.0.1");
/* End configurable stuff */

/* Make sure we're in the docroot */
chdir("../../../../html");
include '../lib/bootstrap.php';
sys::import('xaraya.core');
xarCoreInit(XARCORE_SYSTEM_ALL);

/* Make sure we handle boney instead of fancy */
set_exception_handler(array('ExceptionHandlers','bone'));

if (!empty($xaruser)) {
    /* Authenticate */
    if (!xarUserLogin($xaruser,$xarpass)) {
        fwrite(STDERR,"Authentication failed\n");
        return 1;
    }
    fwrite(STDERR,"Authenticated\n");
}

if (!empty($dbhost)) {
    $dsnArgs = array(
        'databaseType'  =>  $dbtype,
        'databaseHost'  =>  $dbhost,
        'databaseName'  =>  $dbname,
        'userName'      =>  $dbuser,
        'password'      =>  $dbpass
    );
    $dbconn = xarDBNewConn($dsnArgs);
} else {
    $dbconn = xarDB::getConn();
}

$dbInfo = $dbconn->getDatabaseInfo();   // DatabaseInfo object

$tplData = array(
    'schemaName' => $dbInfo->getName(),     // string
    'tables'     => $dbInfo->getTables(),   // array of TableInfo objects
    'types'      => xarDB::getTypeMap()
);

// Produce the xml
$out = xarTplFile('../html/modules/dynamicdata/xartemplates/includes/exportddl.xd',$tplData);
echo $out;
?>