OK, before I go to bed... - below is the export (for host-root page and
styles) and the associated import (for style, and the psuedocode for host-root
page).
attached is a sample export file, for somebody to experiment with import.. as
export is a little more difficult to set up...
import just needs a host page with a style that is ROOT = (&content), eg. no
style! and the code below in the content of the page...
basic concept is - write the database into a set of files - in /tmp/$id or
/tmp/page/ - then tar cz this and return it to the browser..
import then uploads it, untars it and parses it, doing various checks to see
if it already has the dependant styles or styles with the same name.....
- I have started looking at incorporating the export into the frames admin
pages.. - I think it could prove an easy way to distribute 'midgard
snippits...'???
anyway the code so far.....
<HTML><HEAD></HEAD><BODY>
<H1>Warning - Use at your own risk</H1>
<?
// now I have some data!!!!
function lookforstyle($stylename) {
if ($s = mgd_list_styles()) {
while ($s->fetch()) {
$ts=mgd_get_style($s->id);
if (trim($ts->name) == trim($stylename)) {
return $ts->id;
}
}
}
return 0;
}
function importpage($id,$basedir) {
// open up the $basedir/$id.info
// read the stuff
// open up the $basedir/$id.data
// read the stuff.
// convert the style into an id!
// make the element...
// parse the subpage list..
// call myself again!
print "making $id";
}
if( $stylefile && $stylefile_size ) {
exec( "cd /tmp;tar xvfz $stylefile", $lines);
// first look for the article name (style.info)
for ($i=0;$i<count($lines);$i++) {
if (ereg("style\.info$",$lines[$i])) {
$infolines = file("/tmp/".$lines[$i]);
$stylename = $infolines[0];
$styleinheritname = $infolines[1];
}
}
// walk through styles and see if a style with this name exists!!!!
$styleid = lookforstyle($stylename);
if ($styleid) {
$stylename .= "_rename_me";
$styleid = lookforstyle($stylename);
}
if ($styleid) {
print "can not import $stylename as it already exists - try renaming it!";
exit;
}
//make a style!!!
// check for inheritance
if ($styleinheritname) {
$inheritid = lookforstyle($styleinheritname);
if (!$inheritid) {
print "ERROR: inherited style not available!<BR>
$styleinheritname<BR>please import it first!!";
exit;
}
} else {
$inheritid=0; // it's a top level one!!!
}
if (!$stylename) {
print "no stylename found!!"; exit;
}
$id = mgd_create_style($inheritid , $stylename , 0);
print "made id $id $inheritid , $stylename";
// now for the babies.....
for ($i=0;$i<count($lines);$i++) {
if (ereg("\.element$",$lines[$i])) {
$name = ereg_replace("\.element$","", $lines[$i]);
$namexplode = explode("/",$name);
$realname = $namexplode[count($namexplode)-1];
$bodylines = file("/tmp/" . $lines[$i]);
$body = implode("", $bodylines);
mgd_create_element($id, $realname, $body);
print "<BR>made $realname";
}
}
exec("rm /tmp/tmp -r");
} elseif ($pagefile) {
exec( "cd /tmp;tar xvfz $pagefile", $lines);
for ($i=0;$i<count($lines);$i++) {
if (ereg("start\.point$",$lines[$i])) {
$infolines = file("/tmp/".$lines[$i]);
$basedir = ereg_replace("/start\.point$","", "/tmp/" . $lines[$i]);
$startpoint = $infolines[0];
for ($j=1;$j < count($startpoint); $j++) {
// get a style and see if it exists!!!
if (!lookforstyle($infolines[$j])) {
print "You need to have imported style " . $infolines[$j] . " for this to
work";
exit;
}
}
}
}
// now I know I have the styles, lets go for it!!!!
importpage($startpoint, $basedir);
}
?>
<form action="/import/" enctype="multipart/form-data" METHOD=POST>
import stylesheet! : <input type=file name=stylefile size=33 value="">
<BR>
import pages (for host) <input type=file name=pagefile size=33 value=""><BR>
<input type=submit name=action value="Import it">
</form>
</BODY>
</HTML>
/------------------------------------------------
/ NOW FOR EXPORT this is called with /export/?type=style&id=12
<?
switch ($type) {
case "style":
$exportstyle = $id; // -- this is the input!
mkdir("/tmp/$exportstyle",0770);
$style = mgd_get_style($exportstyle);
if ($style->up) {
$styleup = mgd_get_style($style->up);
$styleinherit = $styleup->name;
}
$fh = fopen("/tmp/$exportstyle/style.info", "w");
fwrite($fh, $style->name . "\n");
fwrite($fh, $styleinherit);
fclose($fh);
$e = mgd_list_elements($exportstyle);
while ($e->fetch()) {
$element=mgd_get_element($e->id);
$fh = fopen("/tmp/$exportstyle/".$e->name . ".element", "w");
fwrite($fh, ereg_replace("\r", "", $element->value));
fclose($fh);
}
header("Content-Disposition: attachment; filename=style"
$exportstyle.".tgz" );
header("Content-Type: application/x-gtar");
passthru("tar cz /tmp/$exportstyle/");
exec("rm /tmp/$exportstyle -r");
break;
// end #STYLE# output
case "page":
// how this is a bit more difficult!!!
mkdir("/tmp/page",0770);
// makes 3 files for each page = .info .data .elements!
// info contains a list of subelement ids
// data contains
//also write startpoint, and a list of dependant styles!!!!
function writeelement($id) {
$element = mgd_get_element($id);
$fh = fopen("/tmp/page/$id.elementinfo", "w");
fwrite($fh, $element->name . "\n");
fwrite($fh, $element->inherit . "\n");
fclose($fh);
$fh = fopen("/tmp/page/$id.elementdata", "w");
fwrite($fh, $element->value);
fclose($fh);
}
function writepage($id) {
global $stylelist,$elements;
$page = mgd_get_page($id);
if ($page->style) {
$style = mgd_get_style($page->style);
$stylelist[$style->name] = "YES";
$stylename = $style->name;
} else {
$stylename = " 0 ";
}
// write info!
$fh = fopen("/tmp/page/$id.info", "w");
fwrite($fh, $page->name . "\n");
fwrite($fh, $page->title . "\n");
fwrite($fh, $page->active . "\n");
fwrite($fh, $page->auth . "\n");
fwrite($fh, $stylename . "\n");
// now write all the id's of the subelements!!!!
if ($p = mgd_list_pages($page->id)) {
while ($p->fetch()) {
fwrite($fh, $p->id . "\n");
}
}
fclose($fh);
// now write the content!
$fh = fopen("/tmp/page/$id.data", "w");
fwrite($fh, $page->content );
fclose($fh);
$fh = fopen("/tmp/page/$id.elements", "w");
if ($e = mgd_list_page_elements($page->id)) {
while($e->fetch()) {
fwrite($fh, $e->id . "\n");
$elements[$e->id] = "YES";
}
}
fclose($fh);
// and now force the subpages out!
if ($p = mgd_list_pages($page->id)) {
while ($p->fetch()) {
// dont you just love recursive functions!
writepage( $p->id );
}
}
}
// this creates all the page info!!!
writepage($id);
$fh = fopen("/tmp/page/start.point", "w");
fwrite($fh,$id . "\n");
while (list($k,$v) = each($stylelist)) {
fwrite($fh, $k . "\n");
}
fclose($fh);
if ($elements) {
while (list($k,$v) = each($elements)) {
writeelement($k);
}
}
header("Content-Disposition: attachment; filename=page" .$id.".tgz" );
header("Content-Type: application/x-gtar");
passthru("tar cz /tmp/page/");
exec("rm /tmp/page -r");
} // end switch
?>
--
Technical Director
Linux Center (HK) Ltd.
www.hklc.com
--
This is The Midgard Project's mailing list. For more information,
please visit the project's web site at http://www.midgard-project.org
To unsubscribe the list, send an empty email message to address
[EMAIL PROTECTED]