Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.importDocument.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 include_once ("FDL/import_file.php");
8 {
9 
10  private $begtime = 0;
11  /**
12  * @var array report
13  */
14  private $cr = array();
15  /**
16  * @var bool strict mode
17  */
18  private $strict = true;
19 
20  private $onlyAnalyze = false;
21  protected $fileName = '';
22  /**
23  * @var int folder to insert documents
24  */
25  private $dirid = 0;
26  /**
27  * @var string csv separator character
28  */
29  protected $csvSeparator = ';';
30  /**
31  * @var string csv enclose character
32  */
33  protected $csvEnclosure = '';
34  /**
35  * @var string csv line-break sequence
36  */
37  protected $csvLinebreak = '\n';
38  /**
39  * @var string update|add|keep
40  */
41  protected $policy = "update";
42  /**
43  * To verify visibility "I" of atttribute
44  * @var bool
45  */
46  protected $verifyAttributeAccess = true;
47 
48  protected $reset = array();
49  /**
50  * set strict mode
51  * @param bool $strict set to false to accept error when import
52  * @return void
53  */
54  public function setStrict($strict)
55  {
56  $this->strict = ($strict && true);
57  }
58  public function setPolicy($policy)
59  {
60  $this->policy = $policy;
61  }
62  public function setReset($reset)
63  {
64  if (is_array($reset)) {
65  $this->reset = $reset;
66  } elseif (is_string($reset)) {
67  $this->reset[] = $reset;
68  }
69  }
70 
71  public function setCsvOptions($csvSeparator = ';', $csvEnclosure = '"', $csvLinebreak = '\n')
72  {
73  $this->csvSeparator = $csvSeparator;
74  $this->csvEnclosure = $csvEnclosure;
75  $this->csvLinebreak = $csvLinebreak;
76  }
77 
78  public function setTargetDirectory($dirid)
79  {
80  $this->dirid = $dirid;
81  }
82  /**
83  * @param Action $action current action
84  * @param string $file filename path to import
85  * @param bool $onlyAnalyze if true only analyze not import really
86  * @param bool $archive if true to import file like an standard archive
87  * @return array analyze report
88  */
89  public function importDocuments(Action & $action, $file, $onlyAnalyze = false, $archive = false)
90  {
91  $point = '';
92  if ($this->strict) {
93  $point = 'dcp:importDocument';
94  //$action->debug=true;
95  $action->savePoint($point);
96  }
97  $this->onlyAnalyze = $onlyAnalyze;
98  $this->fileName = $file;
99  try {
100  if ($archive) {
101  include_once ("FREEDOM/freedom_ana_tar.php");
102  $untardir = getTarExtractDir($action, basename($file));
103  $mime = getSysMimeFile($file, basename($file));
104  //print_r(array($untardir, $file, $mime));
105  $err = extractTar($file, $untardir, $mime);
106  if ($err !== '') {
107  $err = sprintf(_("cannot extract archive %s: status : %s") , $file, $err);
108  $this->cr[] = array(
109  "err" => $err,
110  "msg" => "",
111  "specmsg" => "",
112  "folderid" => 0,
113  "foldername" => "",
114  "filename" => "",
115  "title" => "",
116  "id" => "",
117  "values" => array() ,
118  "familyid" => 0,
119  "familyname" => "",
120  "action" => " "
121  );
122  } else {
123  $onlycsv = hasfdlpointcsv($untardir);
124  $simpleFamilyFile = 7; // file
125  $simpleFamilyFolder = 2; // folder
126  $dirid = $this->dirid; // directory to insert imported doc
127  $this->cr = import_directory($action, $untardir, $dirid, $simpleFamilyFile, $simpleFamilyFolder, $onlycsv, $onlyAnalyze, $this->csvLinebreak);
128  }
129  } else {
130  $ext = substr($file, strrpos($file, '.') + 1);
131  $this->begtime = Doc::getTimeDate(0, true);
132  if ($ext == "xml") {
133  $iXml = new \Dcp\Core\importXml();
134  $iXml->setPolicy($this->policy);
135  $iXml->setImportDirectory($this->dirid);
136  $iXml->setVerifyAttributeAccess($this->verifyAttributeAccess);
137  $iXml->analyzeOnly($this->onlyAnalyze);
138  $this->cr = $iXml->importSingleXmlFile($file);
139  } else if ($ext == "zip") {
140  $iXml = new \Dcp\Core\importXml();
141  $iXml->setPolicy($this->policy);
142  $iXml->setImportDirectory($this->dirid);
143  $iXml->setVerifyAttributeAccess($this->verifyAttributeAccess);
144  $iXml->analyzeOnly($this->onlyAnalyze);
145  $this->cr = $iXml->importZipFile($file);
146  } else {
147  $this->cr = $this->importSingleFile($file);
148  }
149  }
150  }
151  catch(Exception $e) {
152  $this->cr = array(
153  array(
154  "title" => "unable to import",
155  "foldername" => "unable to import",
156  "filename" => "unable to import",
157  "familyname" => "unable to import",
158  "action" => "ignored",
159  "id" => "unable to import",
160  "specmsg" => "unable to import",
161  "err" => $e->getMessage()
162  )
163  );
164  }
165 
166  if ($this->strict) {
167  if ($this->getErrorMessage()) {
168  error_log("Import aborted :" . $this->getErrorMessage());
169  $action->rollbackPoint($point);
170  } else {
171  $action->commitPoint($point);
172  }
173  }
174  return $this->cr;
175  }
176  /**
177  * @param boolean $verifyAttributeAccess
178  */
180  {
181  $this->verifyAttributeAccess = $verifyAttributeAccess;
182  }
183  public function importSingleFile($file)
184  {
185  $if = new importDocumentDescription($file);
186  $if->setImportDirectory($this->dirid);
187  $if->analyzeOnly($this->onlyAnalyze);
188  $if->setPolicy($this->policy);
189  $if->setVerifyAttributeAccess($this->verifyAttributeAccess);
190  $if->reset($this->reset);
191  $if->setCsvOptions($this->csvSeparator, $this->csvEnclosure, $this->csvLinebreak);
192  return $if->import();
193  }
194  /**
195  * return all error message concatenated
196  * @return string
197  */
198  public function getErrorMessage()
199  {
200  $terr = array();
201  foreach ($this->cr as $cr) {
202  if ($cr["err"]) $terr[] = $cr["err"];
203  }
204  if (count($terr) > 0) {
205  return '[' . implode("]\n[", $terr) . ']';
206  } else {
207  return '';
208  }
209  }
210  /**
211  * write report in file
212  * @param string $log filename path to write in
213  * @return void
214  */
215  public function writeHTMLImportLog($log)
216  {
217  if ($log) {
218  $flog = fopen($log, "w");
219  if (!$flog) {
220  addWarningMsg(sprintf(_("cannot write log in %s") , $log));
221  } else {
222  global $action;
223  $lay = new Layout(getLayoutFile("FREEDOM", "freedom_import.xml") , $action);
224  $this->writeHtmlCr($lay);
225  fputs($flog, $lay->gen());
226  fclose($flog);
227  }
228  }
229  }
230  /**
231  * internal method use only from freedom_import
232  * @param Layout $lay
233  * @return void
234  */
235  public function writeHtmlCr(Layout & $lay)
236  {
237  $hasError = false;
238  $haswarning = false;
239  foreach ($this->cr as $k => $v) {
240  /*
241  * [action]
242  * [order]
243  * [title]
244  * [foldername]
245  * [id]
246  * [familyid]
247  * [familyname]
248  * [taction]
249  * [msg]
250  * [specmsg]
251  * [svalues]
252  * [err]
253  */
254  if (!isset($v["msg"])) $v["msg"] = '';
255  if (!isset($v["values"])) $v["values"] = null;
256  $this->cr[$k]["title"] = htmlspecialchars($v["title"], ENT_QUOTES);
257  $this->cr[$k]["foldername"] = htmlspecialchars($v["foldername"], ENT_QUOTES);
258  $this->cr[$k]["id"] = htmlspecialchars($v["id"], ENT_QUOTES);
259  $this->cr[$k]["familyid"] = htmlspecialchars($v["familyid"], ENT_QUOTES);
260  $this->cr[$k]["familyname"] = htmlspecialchars($v["familyname"], ENT_QUOTES);
261  $this->cr[$k]["taction"] = htmlspecialchars(_($v["action"]) , ENT_QUOTES); // translate action
262  $this->cr[$k]["order"] = htmlspecialchars($k, ENT_QUOTES); // translate action
263  $this->cr[$k]["svalues"] = htmlspecialchars("", ENT_QUOTES);
264  $this->cr[$k]["msg"] = nl2br(htmlspecialchars($v["msg"], ENT_QUOTES));
265  if (is_array($v["values"])) {
266  foreach ($v["values"] as $ka => $va) {
267  $this->cr[$k]["svalues"].= sprintf("<LI %s>[%s:%s]</LI>", (($va == "/no change/") ? ' class="no"' : '') , htmlspecialchars($ka, ENT_QUOTES) , htmlspecialchars($va, ENT_QUOTES));
268  }
269  }
270  if ($v["action"] == "ignored") $hasError = true;
271  if ($v["action"] == "warning") $haswarning = true;
272  $this->cr[$k]["err"] = (($this->cr[$k]["err"] != '') ? "<ul><li>" . join("</li><li>", explode("\n", htmlspecialchars($this->cr[$k]["err"], ENT_QUOTES))) . "</li></ul>" : "");
273  $this->cr[$k]["action"] = htmlspecialchars($v["action"], ENT_QUOTES);
274  $this->cr[$k]["specmsg"] = htmlspecialchars($v["specmsg"], ENT_QUOTES);
275  }
276  $nbdoc = count(array_filter($this->cr, array(
277  $this,
278  "isdoc"
279  )));
280  $lay->SetBlockData("ADDEDDOC", $this->cr);
281  $errmsg = $this->getErrorMessage();
282  $lay->set("haserror", $hasError || !empty($errmsg));
283  $lay->set("basename", $this->fileName);
284  $lay->set("haswarning", $haswarning);
285  $lay->Set("nbdoc", $nbdoc);
286  $lay->set("analyze", ($this->onlyAnalyze));
287  if ($this->onlyAnalyze) {
288  $lay->set("processMessage", sprintf(n___("%d document detected", "%d documents detected", $nbdoc) , $nbdoc));
289  } else {
290  $lay->set("processMessage", sprintf(n___("%d document processed", "%d documents processed", $nbdoc) , $nbdoc));
291  }
292 
293  $lay->Set("nbprof", count(array_filter($this->cr, array(
294  $this,
295  "isprof"
296  ))));
297  }
298  /**
299  * record a log file from import results
300  *
301  * @param string $log output file path
302  */
303  public function writeImportLog($log)
304  {
305  if ($log) {
306  $flog = fopen($log, "w");
307  if (!$flog) {
308  addWarningMsg(sprintf(_("cannot write log in %s") , $log));
309  } else {
310  fputs($flog, sprintf("IMPORT BEGIN OK : %s\n", $this->begtime));
311  $countok = 0;
312  $counterr = 0;
313  foreach ($this->cr as $v) {
314 
315  if (!isset($v["msg"])) $v["msg"] = '';
316  if (!isset($v["values"])) $v["values"] = null;
317  $chg = "";
318  if (is_array($v["values"])) {
319  foreach ($v["values"] as $ka => $va) {
320  if ($va != "/no change/") $chg.= "{" . $ka . ":" . str_replace("\n", "-", $va) . '}';
321  }
322  }
323  fputs($flog, sprintf("IMPORT DOC %s : [title:%s] [id:%d] [action:%s] [changes:%s] [message:%s] [specmsg:%s] %s\n", $v["err"] ? "KO" : "OK", $v["title"], $v["id"], $v["action"], $chg, str_replace("\n", "-", $v["msg"]) , ($v["err"] ? ('[error:' . str_replace("\n", "-", $v["err"]) . ']') : "") , (isset($v['specmsg']) ? str_replace("\n", "-", $v['specmsg']) : '')));
324  if ($v['action'] !== 'ignored') {
325  if ($v["err"]) {
326  $counterr++;
327  } else {
328  $countok++;
329  }
330  }
331  }
332  fputs($flog, sprintf("IMPORT COUNT OK : %d\n", $countok));
333  fputs($flog, sprintf("IMPORT COUNT KO : %d\n", $counterr));
334  fputs($flog, sprintf("IMPORT END OK : %s\n", Doc::getTimeDate(0, true)));
335  fclose($flog);
336  }
337  }
338  }
339 
340  public static function isdoc($var)
341  {
342  return (($var["action"] == "added") || ($var["action"] == "updated"));
343  }
344 
345  public static function isprof($var)
346  {
347  return (($var["action"] == "modprofil"));
348  }
349 }
Layout is a template generator.
writeHtmlCr(Layout &$lay)
global $action
$archive
setVerifyAttributeAccess($verifyAttributeAccess)
addWarningMsg($msg)
Definition: Lib.Common.php:95
getSysMimeFile($f, $fn="")
getTarExtractDir(Action &$action, $tar)
Definition: import_tar.php:34
$file
n___($message, $message_plural, $num, $context="")
Definition: Lib.Common.php:55
$log
Definition: wsh.php:33
savePoint($point)
getLayoutFile($app, $layfile)
Definition: Lib.Common.php:258
hasfdlpointcsv($dir)
if($updateExistingTable) $point
Definition: updateclass.php:88
set($tag, $val)
rollbackPoint($point)
commitPoint($point)
static getTimeDate($hourdelta=0, $second=false)
Definition: Class.Doc.php:8826
extractTar($tar, $untardir, $mime="")
if($file) if($subject==""&&$file) if($subject=="") $err
setCsvOptions($csvSeparator= ';', $csvEnclosure= '"', $csvLinebreak = '\n')
importDocuments(Action &$action, $file, $onlyAnalyze=false, $archive=false)
import_directory(&$action, $ldir, $dirid=0, $famid=7, $dfldid=2, $onlycsv=false, $analyze=false, $csvLinebreak= '\n')
Definition: import_tar.php:64
← centre documentaire © anakeen