17 include_once (
"FDL/exportfld.php");
39 $wfile = (substr(strtolower($action->
getArgument(
"wfile",
"N")) , 0, 1) ==
"y");
40 $wident = (substr(strtolower($action->
getArgument(
"wident", $wident)) , 0, 1) ==
"y");
41 $flat = (substr(strtolower($action->
getArgument(
"flat")) , 0, 1) ==
"y");
42 if (!$eformat) $eformat = strtoupper($action->
getArgument(
"eformat",
"X"));
49 $flog = fopen(
$log,
"w");
54 fputs($flog, sprintf(
"EXPORT OPTION FLAT : %s\n", ($flat) ?
"yes" :
"no"));
55 fputs($flog, sprintf(
"EXPORT OPTION WFILE : %s\n", ($wfile) ?
"yes" :
"no"));
56 fputs($flog, sprintf(
"EXPORT OPTION CONFIG : %s\n", ($configxml) ?
"yes" :
"no"));
59 $exportAttribute = array();
61 if (!file_exists($configxml))
exportExit($action, sprintf(_(
"config file %s not found") , $configxml));
63 $xml = @simplexml_load_file($configxml);
66 exportExit($action, sprintf(_(
"parse error config file %s : %s") , $configxml, print_r(libxml_get_last_error() ,
true)));
71 foreach ($xml->family as $family) {
72 $afamid = @current($family->attributes()->name);
73 if (!$afamid)
exportExit($action, sprintf(_(
"Config file %s : family name not set") , $configxml));
75 if ((!$fam->isAlive()) || ($fam->doctype !=
'C'))
exportExit($action, sprintf(_(
"Config file %s : family name [%s] not match a know family") , $configxml, $afamid));
76 $exportAttribute[$fam->id] = array();
78 $aid = @current($attribute->attributes()->name);
80 if (!$aid)
exportExit($action, sprintf(_(
"Config file %s : attribute name not set") , $configxml));
81 $oa = $fam->getAttribute($aid);
82 if (!$oa)
exportExit($action, sprintf(_(
"Config file %s : unknow attribute name %s") , $configxml, $aid));
83 $exportAttribute[$fam->id][$oa->id] = $oa->id;
84 $exportAttribute[$fam->id][$oa->fieldSet->id] = $oa->fieldSet->id;
93 $s->setObjectReturn();
95 } elseif ((!
$fldid) && ($selection || $aSelection)) {
99 $selection = json_decode($selection);
100 include_once (
"DATA/Class.DocumentSelection.php");
103 $ids = $os->getIdentificators();
106 $s->addFilter(getSqlCond($ids,
"id",
true));
107 $s->setObjectReturn();
108 $exportname =
"selection";
115 $exportname = str_replace(array(
126 $s->setObjectReturn();
128 $s->dirid = $fld->id;
131 recordStatus($action, $exportId, _(
"Retrieve documents from database"));
136 $foutdir = uniqid(
getTmpDir() .
"/exportxml");
137 if (!mkdir($foutdir))
exportExit($action, sprintf(
"cannot create directory %s", $foutdir));
143 fputs($flog, sprintf(
"EXPORT OPTION ID : %s <%s>\n",
$fldid, $fld->getTitle()));
148 while (
$doc =
$s->getNextDoc()) {
152 recordStatus($action, $exportId, sprintf(_(
"Record documents %d/%d") ,
$c, $rc));
154 if (
$doc->doctype !=
'C') {
155 $ftitle = str_replace(array(
161 ) ,
'-',
$doc->getTitle());
166 $suffix = sprintf(
"{%d}.xml",
$doc->id);
168 $fname = sprintf(
"%s/%s%s", $foutdir, mb_strcut($ftitle, 0, $maxBytesLen,
'UTF-8') , $suffix);
170 $err =
$doc->exportXml($xml, $wfile, $fname, $wident, $flat, $exportAttribute);
174 if ($flog) fputs($flog, sprintf(
"EXPORT DOC OK : <%s> [%d]\n",
$doc->getTitle() ,
$doc->id));
175 if (!isset($xsd[
$doc->fromid])) {
180 $fname = sprintf(
"%s/%s.xsd", $foutdir, strtolower($fam->name));
181 file_put_contents($fname, $fam->getXmlSchema());
182 $xsd[
$doc->fromid] =
true;
188 fputs($flog, sprintf(
"EXPORT COUNT OK : %d\n", $count));
193 if ($eformat ==
"X") {
195 if ($outputFile) $zipfile = $outputFile;
196 else $zipfile = uniqid(
getTmpDir() .
"/xml") .
".zip";
197 system(sprintf(
"cd %s && zip -r %s -- * > /dev/null", escapeshellarg($foutdir) , escapeshellarg($zipfile)) ,
$ret);
198 if (is_file($zipfile)) {
199 system(sprintf(
"rm -fr %s", $foutdir));
200 recordStatus($action, $exportId, _(
"Export done") ,
true);
201 if ($toDownload)
Http_DownloadFile($zipfile,
"$exportname.zip",
"application/x-zip",
false,
false,
true);
203 exportExit($action, _(
"Zip Archive cannot be created"));
205 } elseif ($eformat ==
"Y") {
206 if ($outputFile) $xmlfile = $outputFile;
207 else $xmlfile = uniqid(
getTmpDir() .
"/xml") .
".xml";
209 $fh = fopen($xmlfile,
'x');
211 exportExit($action, sprintf(
"%s (Error creating file '%s')", _(
"Xml file cannot be created") , htmlspecialchars($xmlfile)));
215 <?xml version=
"1.0" encoding=
"UTF-8"?>
216 <documents date=
"%s" author=
"%s" name=
"%s">
219 $xml_head = sprintf($xml_head, htmlspecialchars(strftime(
"%FT%T")) , htmlspecialchars(
Account::getDisplayName($action->user->id)) , htmlspecialchars($exportname));
220 $xml_footer =
"</documents>";
222 $ret = fwrite($fh, $xml_head);
223 if (
$ret ===
false) {
224 exportExit($action, sprintf(
"%s (Error writing to file '%s')", _(
"Xml file cannot be created") , htmlspecialchars($xmlfile)));
231 $ret = chdir($foutdir);
232 if (
$ret ===
false) {
233 exportExit($action, sprintf(
"%s (Error chdir to '%s')", _(
"Xml file cannot be created") , htmlspecialchars($foutdir)));
236 if (
$s->count() > 0) {
237 $cmd = sprintf(
"cat -- *xml | grep -v '<?xml version=\"1.0\" encoding=\"UTF-8\"?>' >> %s", escapeshellarg($xmlfile));
242 if (
$ret ===
false) {
243 exportExit($action, sprintf(
"%s (Error chdir to '%s')", _(
"Xml file cannot be created") , htmlspecialchars(
$cwd)));
246 $ret = fseek($fh, 0, SEEK_END);
248 exportExit($action, sprintf(
"%s (Error fseek '%s')", _(
"Xml file cannot be created") , htmlspecialchars($xmlfile)));
251 $ret = fwrite($fh, $xml_footer);
252 if (
$ret ===
false) {
253 exportExit($action, sprintf(
"%s (Error writing to file '%s')", _(
"Xml file cannot be created") , htmlspecialchars($xmlfile)));
258 if (is_file($xmlfile)) {
259 system(sprintf(
"rm -fr %s", escapeshellarg($foutdir)));
262 recordStatus($action, $exportId, _(
"Export done") ,
true);
267 exportExit($action, _(
"Xml file cannot be created"));
270 recordStatus($action, $exportId, _(
"Export done") ,
true);
276 if (file_put_contents(
$log,
"EXPORT " . _(
"ERROR :") .
$err) ===
false) {
277 $err = sprintf(_(
"Cannot write to log %s") ,
$log) .
"\n" .
$err;
exportxmlfld(Action &$action, $aflid="0", $famid="", SearchDoc $specSearch=null, $outputFile= '', $eformat="", $wident= 'Y', Fdl_DocumentSelection $aSelection=null, $toDownload=true)
print $fam getTitle() $fam name
exportExit(Action &$action, $err)
recordStatus(Action &$action, $exportId, $msg, $endStatus=false)
exitError($texterr, $exit=true, $code="")
foreach($argv as $arg) $cmd
static getDisplayName($uid)
new_Doc($dbaccess, $id= '', $latest=false)
Http_DownloadFile($filename, $name, $mime_type= '', $inline=false, $cache=true, $deleteafter=false)
if($_REQUEST['submitted']) else $cwd
static getTimeDate($hourdelta=0, $second=false)
setMaxExecutionTimeTo($limit)
static getArgument($k, $def= '')
if($file) if($subject==""&&$file) if($subject=="") $err