19 include_once (
"FDL/Lib.Dir.php");
20 include_once (
"FDL/Lib.Util.php");
21 include_once (
"FDL/Class.DocAttr.php");
22 include_once (
"VAULT/Class.VaultFile.php");
23 include_once (
"FDL/import_file.php");
24 include_once (
"FDL/Class.SearchDoc.php");
37 if (ini_get(
"max_execution_time") < 3600) ini_set(
"max_execution_time", 3600);
41 $wfile = (substr(strtolower($action->
getArgument(
"wfile",
"N")) , 0, 1) ==
"y");
42 $wident = (substr(strtolower($action->
getArgument(
"wident",
"Y")) , 0, 1) ==
"y");
43 $flat = (substr(strtolower($action->
getArgument(
"flat")) , 0, 1) ==
"y");
44 $eformat = strtoupper($action->
getArgument(
"eformat",
"X"));
50 $flog = fopen(
$log,
"w");
55 fputs($flog, sprintf(
"EXPORT OPTION FLAT : %s\n", ($flat) ?
"yes" :
"no"));
56 fputs($flog, sprintf(
"EXPORT OPTION WFILE : %s\n", ($wfile) ?
"yes" :
"no"));
57 fputs($flog, sprintf(
"EXPORT OPTION CONFIG : %s\n", ($configxml) ?
"yes" :
"no"));
60 $exportAttribute = array();
62 if (!file_exists($configxml))
exportExit($action, sprintf(_(
"config file %s not found") , $configxml));
64 $xml = @simplexml_load_file($configxml);
67 exportExit($action, sprintf(_(
"parse error config file %s : %s") , $configxml, print_r(libxml_get_last_error() ,
true)));
69 foreach ($xml->family as
$family) {
70 $afamid = @current($family->attributes()->name);
71 if (!$afamid)
exportExit($action, sprintf(_(
"Config file %s : family name not set") , $configxml));
73 if ((!$fam->isAlive()) || ($fam->doctype !=
'C'))
exportExit($action, sprintf(_(
"Config file %s : family name [%s] not match a know family") , $configxml, $afamid));
74 $exportAttribute[$fam->id] = array();
76 $aid = @current($attribute->attributes()->name);
78 if (!$aid)
exportExit($action, sprintf(_(
"Config file %s : attribute name not set") , $configxml));
79 $oa = $fam->getAttribute($aid);
80 if (!$oa)
exportExit($action, sprintf(_(
"Config file %s : unknow attribute name %s") , $configxml, $aid));
81 $exportAttribute[$fam->id][$oa->id] = $oa->id;
82 $exportAttribute[$fam->id][$oa->fieldSet->id] = $oa->fieldSet->id;
87 if ((!
$fldid) && $selection) {
88 $selection = json_decode($selection);
89 include_once (
"DATA/Class.DocumentSelection.php");
91 $ids = $os->getIdentificators();
94 $s->addFilter(getSqlCond($ids,
"id",
true));
95 $s->setObjectReturn();
96 $exportname =
"selection";
103 $exportname = str_replace(array(
114 $s->setObjectReturn();
116 $s->dirid = $fld->id;
122 $foutdir = uniqid(
getTmpDir() .
"/exportxml");
123 if (!mkdir($foutdir))
exportExit($action, sprintf(
"cannot create directory %s", $foutdir));
129 fputs($flog, sprintf(
"EXPORT OPTION ID : %s <%s>\n",
$fldid, $fld->getTitle()));
132 while (
$doc =
$s->nextDoc()) {
134 if (
$doc->doctype !=
'C') {
135 $ftitle = str_replace(array(
141 ) ,
'-',
$doc->getTitle());
142 $fname = sprintf(
"%s/%s{%d}.xml", $foutdir, $ftitle,
$doc->id);
143 $err =
$doc->exportXml($xml, $wfile, $fname, $wident, $flat, $exportAttribute);
147 if ($flog) fputs($flog, sprintf(
"EXPORT DOC OK : <%s> [%d]\n",
$doc->getTitle() ,
$doc->id));
148 if (!isset($xsd[
$doc->fromid])) {
150 $fname = sprintf(
"%s/%s.xsd", $foutdir, strtolower($fam->name));
151 file_put_contents($fname, $fam->getXmlSchema());
152 $xsd[
$doc->fromid] =
true;
158 fputs($flog, sprintf(
"EXPORT COUNT OK : %d\n", $count));
163 if ($eformat ==
"X") {
164 $zipfile = uniqid(
getTmpDir() .
"/xml") .
".zip";
165 system(sprintf(
"cd %s && zip -r %s -- * > /dev/null", escapeshellarg($foutdir) , escapeshellarg($zipfile)) ,
$ret);
166 if (is_file($zipfile)) {
167 system(sprintf(
"rm -fr %s", $foutdir));
168 Http_DownloadFile($zipfile,
"$exportname.zip",
"application/x-zip",
false,
false,
true);
170 exportExit($action, _(
"Zip Archive cannot be created"));
172 } elseif ($eformat ==
"Y") {
173 $xmlfile = uniqid(
getTmpDir() .
"/xml") .
".xml";
175 $fh = fopen($xmlfile,
'x');
177 return exportExit($action, sprintf(
"%s (Error creating file '%s')", _(
"Xml file cannot be created") , htmlspecialchars($xmlfile)));
181 <?xml version=
"1.0" encoding=
"UTF-8"?>
182 <documents date=
"%s" author=
"%s" name=
"%s">
185 $xml_head = sprintf($xml_head, htmlspecialchars(strftime(
"%FT%T")) , htmlspecialchars(
User::getDisplayName($action->user->id)) , htmlspecialchars($exportname));
186 $xml_footer =
"</documents>";
188 $ret = fwrite($fh, $xml_head);
189 if (
$ret ===
false) {
190 return exportExit($action, sprintf(
"%s (Error writing to file '%s')", _(
"Xml file cannot be created") , htmlspecialchars($xmlfile)));
197 $ret = chdir($foutdir);
198 if (
$ret ===
false) {
199 return exportExit($action, sprintf(
"%s (Error chdir to '%s')", _(
"Xml file cannot be created") , htmlspecialchars($foutdir)));
202 $cmd = sprintf(
"cat -- *xml | grep -v '<?xml version=\"1.0\" encoding=\"UTF-8\"?>' >> %s", escapeshellarg($xmlfile));
206 if (
$ret ===
false) {
207 return exportExit($action, sprintf(
"%s (Error chdir to '%s')", _(
"Xml file cannot be created") , htmlspecialchars(
$cwd)));
210 $ret = fseek($fh, 0, SEEK_END);
212 return exportExit($action, sprintf(
"%s (Error fseek '%s')", _(
"Xml file cannot be created") , htmlspecialchars($xmlfile)));
215 $ret = fwrite($fh, $xml_footer);
216 if (
$ret ===
false) {
217 return exportExit($action, sprintf(
"%s (Error writing to file '%s')", _(
"Xml file cannot be created") , htmlspecialchars($xmlfile)));
222 if (is_file($xmlfile)) {
223 system(sprintf(
"rm -fr %s", escapeshellarg($foutdir)));
226 exportExit($action, _(
"Xml file cannot be created"));
234 if (file_put_contents(
$log,
"EXPORT " . _(
"ERROR :") .
$err) ===
false) {
235 $err = sprintf(_(
"Cannot write to log %s") ,
$log) .
"\n" .
$err;
238 $action->exitError(
$err);