Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
importDocuments.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * importation of documents
8  *
9  * @author Anakeen
10  * @version $Id: freedom_import.php,v 1.9 2008/11/13 16:49:16 eric Exp $
11  * @package FDL
12  * @subpackage WSH
13  */
14 /**
15  */
16 
17 global $appl, $action;
18 
19 include_once ("FDL/import_file.php");
20 include_once ("FREEDOM/freedom_import.php");
21 
22 $usage = new ApiUsage();
23 $usage->setDefinitionText("Import documents from description file");
24 $filename = $usage->addRequiredParameter("file", "the description file path");
25 $analyze = $usage->addOptionalParameter("analyze", "analyze only", array(
26  "yes",
27  "no"
28 ) , "no");
29 $archive = $usage->addOptionalParameter("archive", "description file is an standard archive (not xml)", array(
30  "yes",
31  "no"
32 ) , "no");
33 $logfile = $usage->addOptionalParameter("log", "log file output");
34 $policy = $usage->addOptionalParameter("policy", "policy import - \n\t\t[update] to auto update same document (the default), \n\t\t[add] to always create new document, \n\t\t[keep] to do nothing if same document already present", array(
35  "update",
36  "add",
37  "keep"
38 ));
39 $htmlmode = $usage->addOptionalParameter("htmlmode", "analyze report mode in html", array(
40  "yes",
41  "no"
42 ) , "yes");
43 $reinit = $usage->addOptionalParameter("reinitattr", "reset attribute before import family update (deprecated)", array(
44  "yes",
45  "no"
46 ));
47 $reset = $usage->addOptionalParameter("reset", "reset options", function ($values, $argName, ApiUsage $apiusage)
48 {
49  $opt = array(
50  "default",
51  "parameters",
52  "attributes",
53  "structure",
54  "properties",
55  "enums"
56  );
57  if ($values === ApiUsage::GET_USAGE) return sprintf(" [%s] ", implode('|', $opt));
58 
59  $error = $apiusage->matchValues($values, $opt);
60  if ($error) {
61  $apiusage->exitError(sprintf("Error: wrong value for argument 'reset' : %s", $error));
62  }
63  return '';
64 });
65 $to = $usage->addOptionalParameter("to", "email address to send report");
66 $dirid = $usage->addOptionalParameter("dir", "folder where imported documents are put");
67 
68 $strict = $usage->addOptionalParameter("strict", "don't import if one error detected", array(
69  "yes",
70  "no"
71 ) , "yes");
72 
73 $csvSeparator = $usage->addOptionalParameter("csv-separator", "character to delimiter fields - generaly a comma", function ($values, $argName, ApiUsage $apiusage)
74 {
75  if ($values === ApiUsage::GET_USAGE) {
76  return sprintf(' use single character or "auto"');
77  }
78  if (!is_string($values)) {
79  return sprintf("must be a character [%s] ", print_r($values, true));
80  }
81  if ($values != "auto") {
82  if (mb_strlen($values) > 1) {
83  return sprintf("must be a only one character [%s] ", $values);
84  }
85  }
86  return '';
87 }
88 , ";");
89 
90 $csvEnclosure = $usage->addOptionalParameter("csv-enclosure", "character to enclose fields - generaly double-quote", function ($values, $argName, ApiUsage $apiusage)
91 {
92  if ($values === ApiUsage::GET_USAGE) {
93  return sprintf(' use single character or "auto"');
94  }
95  if (!is_string($values)) {
96  return sprintf("must be a character [%s] ", print_r($values, true));
97  }
98  if ($values != "auto") {
99  if (mb_strlen($values) > 1) {
100  return sprintf("must be a only one character [%s] ", $values);
101  }
102  }
103  return '';
104 }
105 , "");
106 
107 $csvLinebreak = $usage->addOptionalParameter("csv-linebreak", "character sequence to be import like CRLF", null, '\n');
108 
109 $usage->verify();
110 
111 if ($reinit == "yes") {
112  $reset[] = "attributes";
113  $action->log->deprecated("importDocuments :reinitattr option is deprecated, use --reset=attributes");
114 }
115 if (!file_exists($filename)) {
116  $action->ExitError(sprintf(_("import file %s not found") , $filename));
117 }
118 if (!is_file($filename)) {
119  $action->exitError(sprintf(_("import file '%s' is not a valid file") , $filename));
120 }
121 if ($logfile) {
122  if (file_exists($logfile) && (!is_writable($logfile))) {
123  $action->ExitError(sprintf(_("log file %s not writable") , $logfile));
124  }
125  if (!file_exists($logfile)) {
126  $f = @fopen($logfile, 'a');
127  if ($f === false) {
128  $action->ExitError(sprintf(_("log file %s not writable") , $logfile));
129  }
130  fclose($f);
131  }
132 }
133 setHttpVar('analyze', ($analyze == "yes") ? 'Y' : 'N');
134 setHttpVar('htmlmode', ($htmlmode == "yes") ? 'Y' : 'N');
135 $archive = ($archive == "yes");
136 
137 if ($dirid) {
138  $dir = new_doc("", $dirid);
139  if (!$dir->isAlive()) {
140  $action->exitError(sprintf("folder %s not found (dir option)", $dirid));
141  }
142  $dirid = $dir->id;
143  SetHttpVar("dirid", $dirid);
144 }
146 if ($strict == 'no') $oImport->setStrict(false);
148 $oImport->setPolicy($policy);
149 $oImport->setReset($reset);
150 $oImport->setVerifyAttributeAccess(false);
151 if ($dirid) $oImport->setTargetDirectory($dirid);
152 $cr = $oImport->importDocuments($action, $filename, $analyze != "no", $archive == "yes");
153 
154 $filetmp = false;
155 if ((!$logfile) && $to) {
156  $logfile = tempnam(getTmpDir() , 'logimport');
157  $filetmp = true;
158 }
159 if ($logfile) {
160  if ($htmlmode == "yes") {
161  $oImport->writeHTMLImportLog($logfile);
162  } else {
163  $oImport->writeImportLog($logfile);
164  }
165 }
166 // mode HTML
167 if ($to) {
168  include_once ("FDL/sendmail.php");
169 
170  $message = new \Dcp\Mail\Message();
171 
172  $message->setBody(new \Dcp\Mail\Body(file_get_contents($logfile) , (($htmlmode == 'yes') ? 'text/html' : 'text/plain')));
173 
174  $from = getMailAddr($action->user->id);
175  if ($from == "") $from = getParam('SMTP_FROM');
176  if ($from == "") $from = $action->user->login . '@' . php_uname('n');
177 
178  $subject = sprintf(_("result of import %s") , basename(GetHttpVars("file")));
179  $err = sendmail($to, $from, $cc = '', $bcc = '', $subject, $message);
180  if ($err) error_log("import sending mail: Error:$err");
181  if ($filetmp) unlink($logfile);
182 }
183 $err = $oImport->getErrorMessage();
184 if ($err) $action->ExitError($err);
getMailAddr($userid, $full=false)
Definition: Lib.Common.php:133
SetHttpVar($name, $def)
Definition: Lib.Http.php:150
sendmail($to, $from, $cc, $bcc, $subject, &$mimemail, $multipart=null)
Definition: sendmail.php:26
$archive
exitError($error= '')
$csvEnclosure
$message
if((!$logfile)&&$to) if($logfile) if($to) $err
$htmlmode
const GET_USAGE
$csvLinebreak
global $action
$subject
$filename
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
$csvSeparator
$bcc
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
$cc
$dir
Definition: resizeimg.php:144
$logfile
$from
static matchValues($value, $restrictions)
global $appl
if($dirid) $oImport
$analyze
Verify arguments for wsh programs.
← centre documentaire © anakeen