Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
exportxmlfld.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Export Document from Folder
8  *
9  * @author Anakeen
10  * @version $Id: exportfld.php,v 1.44 2009/01/12 13:23:11 eric Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 
17 include_once ("FDL/exportfld.php");
18 /**
19  * Exportation as xml of documents from folder or searches
20  * @param Action &$action current action
21  * @global string $fldid Http var : folder identifier to export
22  * @global string $wfile Http var : (Y|N) if Y export attached file export format will be tgz
23  * @global string $flat Http var : (Y|N) if Y specid column is set with identifier of document
24  * @global string $eformat Http var : (X|Y) I: Y: only one xml, X: zip by document with files
25  * @global string $log Http var : log file output
26  * @global string $selection Http var : JSON document selection object
27  * @param string $afldid folder identifier to export
28  * @param string $famid restrict to specific family for folder
29  * @param SearchDoc $specSearch use this search instead folder
30  * @param string $outputFile put result into this file instead download it
31  * @param string $eformat X : zip (xml inside), Y: global xml file
32  * @param string $eformat X : zip (xml inside), Y: global xml file
33  */
34 function exportxmlfld(Action & $action, $aflid = "0", $famid = "", SearchDoc $specSearch = null, $outputFile = '', $eformat = "", $wident = 'Y', Fdl_DocumentSelection $aSelection = null, $toDownload = true)
35 {
36  setMaxExecutionTimeTo(3600); // 60 minutes
37  $dbaccess = $action->dbaccess;
38  $fldid = $action->getArgument("id", $aflid);
39  $wfile = (substr(strtolower($action->getArgument("wfile", "N")) , 0, 1) == "y"); // with files
40  $wident = (substr(strtolower($action->getArgument("wident", $wident)) , 0, 1) == "y"); // with numeric identifier
41  $flat = (substr(strtolower($action->getArgument("flat")) , 0, 1) == "y"); // flat xml
42  if (!$eformat) $eformat = strtoupper($action->getArgument("eformat", "X")); // export format
43  $selection = $action->getArgument("selection"); // export selection object (JSON)
44  $log = $action->getArgument("log"); // log file
45  $configxml = $action->getArgument("config");
46  $exportId = $action->getArgument("exportId"); // export status id
47  $flog = false;
48  if ($log) {
49  $flog = fopen($log, "w");
50  if (!$flog) {
51  exportExit($action, sprintf(_("cannot write log in %s") , $log));
52  }
53  fputs($flog, sprintf("EXPORT BEGIN OK : %s\n", Doc::getTimeDate(0, true)));
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"));
57  }
58  // constitution options for filter attributes
59  $exportAttribute = array();
60  if ($configxml) {
61  if (!file_exists($configxml)) exportExit($action, sprintf(_("config file %s not found") , $configxml));
62 
63  $xml = @simplexml_load_file($configxml);
64 
65  if ($xml === false) {
66  exportExit($action, sprintf(_("parse error config file %s : %s") , $configxml, print_r(libxml_get_last_error() , true)));
67  }
68  /*
69  * @var SimpleXmlElement $family
70  */
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));
74  $fam = new_doc($dbaccess, $afamid);
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();
77  foreach ($family->attribute as $attribute) {
78  $aid = @current($attribute->attributes()->name);
79 
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;
85  }
86  }
87  }
88  // set the export's search
89  $exportname = '';
90  $fld = null;
91  if ($specSearch) {
92  $s = $specSearch;
93  $s->setObjectReturn();
94  $s->reset();
95  } elseif ((!$fldid) && ($selection || $aSelection)) {
96  if ($aSelection) {
97  $os = $aSelection;
98  } else {
99  $selection = json_decode($selection);
100  include_once ("DATA/Class.DocumentSelection.php");
101  $os = new Fdl_DocumentSelection($selection);
102  }
103  $ids = $os->getIdentificators();
104  $s = new SearchDoc($dbaccess);
105 
106  $s->addFilter(getSqlCond($ids, "id", true));
107  $s->setObjectReturn();
108  $exportname = "selection";
109  } else {
110  if (!$fldid) exportExit($action, _("no export folder specified"));
111 
112  $fld = new_Doc($dbaccess, $fldid);
113  if ($fldid && (!$fld->isAlive())) exportExit($action, sprintf(_("folder/search %s not found") , $fldid));
114  if ($famid == "") $famid = $action->getArgument("famid");
115  $exportname = str_replace(array(
116  " ",
117  "'",
118  '/'
119  ) , array(
120  "_",
121  "",
122  "-"
123  ) , $fld->title);
124  //$tdoc = getChildDoc($dbaccess, $fldid,"0","ALL",array(),$action->user->id,"TABLE",$famid);
125  $s = new SearchDoc($dbaccess, $famid);
126  $s->setObjectReturn();
127 
128  $s->dirid = $fld->id;
129  }
130 
131  recordStatus($action, $exportId, _("Retrieve documents from database"));
132  $s->search();
133  $err = $s->searchError();
134  if ($err) exportExit($action, $err);
135 
136  $foutdir = uniqid(getTmpDir() . "/exportxml");
137  if (!mkdir($foutdir)) exportExit($action, sprintf("cannot create directory %s", $foutdir));
138  //$fname=sprintf("%s/FDL/Layout/fdl.xsd",DEFAULT_PUBDIR);
139  //copy($fname,"$foutdir/fdl.xsd");
140  $xsd = array();
141  $count = 0;
142  if ($flog && $fld) {
143  fputs($flog, sprintf("EXPORT OPTION ID : %s <%s>\n", $fldid, $fld->getTitle()));
144  }
145 
146  $rc = $s->count();
147  $c = 0;
148  while ($doc = $s->getNextDoc()) {
149  //print $doc->exportXml();
150  $c++;
151  if ($c % 20 == 0) {
152  recordStatus($action, $exportId, sprintf(_("Record documents %d/%d") , $c, $rc));
153  }
154  if ($doc->doctype != 'C') {
155  $ftitle = str_replace(array(
156  '/',
157  '\\',
158  '?',
159  '*',
160  ':'
161  ) , '-', $doc->getTitle());
162  /*
163  * The file name should not exceed MAX_FILENAME_LEN bytes and, as the string is in UTF-8,
164  * we must take care not to cut in the middle of a multi-byte char.
165  */
166  $suffix = sprintf("{%d}.xml", $doc->id);
167  $maxBytesLen = MAX_FILENAME_LEN - strlen($suffix);
168  $fname = sprintf("%s/%s%s", $foutdir, mb_strcut($ftitle, 0, $maxBytesLen, 'UTF-8') , $suffix);
169 
170  $err = $doc->exportXml($xml, $wfile, $fname, $wident, $flat, $exportAttribute);
171  // file_put_contents($fname,$doc->exportXml($wfile));
172  if ($err) exportExit($action, $err);
173  $count++;
174  if ($flog) fputs($flog, sprintf("EXPORT DOC OK : <%s> [%d]\n", $doc->getTitle() , $doc->id));
175  if (!isset($xsd[$doc->fromid])) {
176  /*
177  * @var DocFam $fam
178  */
179  $fam = new_doc($dbaccess, $doc->fromid);
180  $fname = sprintf("%s/%s.xsd", $foutdir, strtolower($fam->name));
181  file_put_contents($fname, $fam->getXmlSchema());
182  $xsd[$doc->fromid] = true;
183  }
184  }
185  }
186 
187  if ($flog) {
188  fputs($flog, sprintf("EXPORT COUNT OK : %d\n", $count));
189  fputs($flog, sprintf("EXPORT END OK : %s\n", Doc::getTimeDate(0, true)));
190  fclose($flog);
191  }
192 
193  if ($eformat == "X") {
194 
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);
202  } else {
203  exportExit($action, _("Zip Archive cannot be created"));
204  }
205  } elseif ($eformat == "Y") {
206  if ($outputFile) $xmlfile = $outputFile;
207  else $xmlfile = uniqid(getTmpDir() . "/xml") . ".xml";
208 
209  $fh = fopen($xmlfile, 'x');
210  if ($fh === false) {
211  exportExit($action, sprintf("%s (Error creating file '%s')", _("Xml file cannot be created") , htmlspecialchars($xmlfile)));
212  }
213  /* Print XML header */
214  $xml_head = <<<EOF
215 <?xml version="1.0" encoding="UTF-8"?>
216 <documents date="%s" author="%s" name="%s">
217 
218 EOF;
219  $xml_head = sprintf($xml_head, htmlspecialchars(strftime("%FT%T")) , htmlspecialchars(Account::getDisplayName($action->user->id)) , htmlspecialchars($exportname));
220  $xml_footer = "</documents>";
221 
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)));
225  }
226  fflush($fh);
227  /* chdir into dir containing the XML files
228  * and concatenate them into the output file
229  */
230  $cwd = getcwd();
231  $ret = chdir($foutdir);
232  if ($ret === false) {
233  exportExit($action, sprintf("%s (Error chdir to '%s')", _("Xml file cannot be created") , htmlspecialchars($foutdir)));
234  }
235 
236  if ($s->count() > 0) {
237  $cmd = sprintf("cat -- *xml | grep -v '<?xml version=\"1.0\" encoding=\"UTF-8\"?>' >> %s", escapeshellarg($xmlfile));
238  system($cmd, $ret);
239  }
240 
241  $ret = chdir($cwd);
242  if ($ret === false) {
243  exportExit($action, sprintf("%s (Error chdir to '%s')", _("Xml file cannot be created") , htmlspecialchars($cwd)));
244  }
245  /* Print XML footer */
246  $ret = fseek($fh, 0, SEEK_END);
247  if ($ret === - 1) {
248  exportExit($action, sprintf("%s (Error fseek '%s')", _("Xml file cannot be created") , htmlspecialchars($xmlfile)));
249  }
250 
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)));
254  }
255  fflush($fh);
256  fclose($fh);
257 
258  if (is_file($xmlfile)) {
259  system(sprintf("rm -fr %s", escapeshellarg($foutdir)));
260 
261  if (!$outputFile) {
262  recordStatus($action, $exportId, _("Export done") , true);
263 
264  Http_DownloadFile($xmlfile, "$exportname.xml", "text/xml", false, false, true);
265  }
266  } else {
267  exportExit($action, _("Xml file cannot be created"));
268  }
269  }
270  recordStatus($action, $exportId, _("Export done") , true);
271 }
273 {
274  $log = $action->getArgument("log");
275  if ($log) {
276  if (file_put_contents($log, "EXPORT " . _("ERROR :") . $err) === false) {
277  $err = sprintf(_("Cannot write to log %s") , $log) . "\n" . $err;
278  }
279  }
280  $action->exitError($err);
281 }
global $action
const MAX_FILENAME_LEN
Definition: Lib.Prefix.php:30
exportxmlfld(Action &$action, $aflid="0", $famid="", SearchDoc $specSearch=null, $outputFile= '', $eformat="", $wident= 'Y', Fdl_DocumentSelection $aSelection=null, $toDownload=true)
print $fam getTitle() $fam name
$attribute
$ret
exportExit(Action &$action, $err)
if($famId) $s
$log
Definition: wsh.php:33
recordStatus(Action &$action, $exportId, $msg, $endStatus=false)
Definition: exportfld.php:246
exitError($texterr, $exit=true, $code="")
foreach($argv as $arg) $cmd
static getDisplayName($uid)
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
new_Doc($dbaccess, $id= '', $latest=false)
Http_DownloadFile($filename, $name, $mime_type= '', $inline=false, $cache=true, $deleteafter=false)
Definition: Lib.Http.php:225
if($_REQUEST['submitted']) else $cwd
Definition: test.php:66
static getTimeDate($hourdelta=0, $second=false)
Definition: Class.Doc.php:8826
setMaxExecutionTimeTo($limit)
Definition: Lib.Common.php:121
static getArgument($k, $def= '')
$dbaccess
Definition: checkVault.php:17
if($file) if($subject==""&&$file) if($subject=="") $err
← centre documentaire © anakeen