Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
import_file.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Import documents
8  *
9  * @author Anakeen
10  * @version $Id: import_file.php,v 1.149 2008/11/14 12:40:07 eric Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 
17 include_once ("FDL/Class.DocFam.php");
18 include_once ("FDL/Class.DocSearch.php");
19 include_once ("FDL/Class.Dir.php");
20 include_once ("FDL/Class.QueryDir.php");
21 include_once ("FDL/Lib.Attr.php");
22 include_once ("FDL/Class.DocAttrLDAP.php");
23 require_once ("WHAT/Lib.FileMime.php");
24 
25 define("ALTSEPCHAR", ' --- ');
26 define("SEPCHAR", ';');
27 
29 {
31  if ($dirid == 0) {
32  $dirid = GetHttpVars("dirid", 0); // directory to place imported doc
33 
34  }
35  $analyze = (GetHttpVars("analyze", "N") == "Y"); // just analyze
36  $policy = GetHttpVars("policy", "update");
37  $reinit = GetHttpVars("reinitattr");
38  $reset = GetHttpVars("reset");
39  $comma = GetHttpVars("comma", SEPCHAR);
40 
42  $if->setImportDirectory($dirid);
43  $if->analyzeOnly($analyze);
44  $if->setPolicy($policy);
45  $if->reinitAttribute($reinit == "yes");
46  $if->reset($reset);
47  $if->setComma($comma);
48  return $if->import();
49 }
50 /**
51  * Add a document from csv import file
52  * @param string $dbaccess database specification
53  * @param array $data data information conform to {@link Doc::GetImportAttributes()}
54  * @param int $dirid default folder id to add new document
55  * @param bool $analyze true is want just analyze import file (not really import)
56  * @param string $ldir path where to search imported files
57  * @param string $policy add|update|keep policy use if similar document
58  * @param array $tkey attribute key to search similar documents
59  * @param array $prevalues default values for new documents
60  * @param array $torder array to describe CSV column attributes
61  * @global double Http var : Y if want double title document
62  * @return array properties of document added (or analyzed to be added)
63  */
64 function csvAddDoc($dbaccess, $data, $dirid = 0, $analyze = false, $ldir = '', $policy = "add", $tkey = array(
65  "title"
66 ) , $prevalues = array() , $torder = array())
67 {
68 
69  $o = new importSingleDocument();
70  if ($tkey) $o->setKey($tkey);
71  if ($torder) $o->setOrder($torder);
72  $o->analyzeOnly($analyze);
73  $o->setPolicy($policy);
74  $o->setTargetDirectory($dirid);
75  $o->setFilePath($ldir);
76  if ($prevalues) $o->setPreValues($prevalues);
77  return $o->import($data)->getImportResult();
78 }
79 
80 function AddImportLog($msg)
81 {
82  global $action;
83  if ($action->lay) {
84  $tmsg = $action->lay->GetBlockData("MSG");
85  $tmsg[] = array(
86  "msg" => $msg
87  );
88  $action->lay->SetBlockData("MSG", $tmsg);
89  } else {
90  print "\n$msg";
91  }
92 }
93 /**
94  * @param array $orderdata
95  * @return array
96  */
97 function getOrder(array $orderdata)
98 {
99  return array_map("strtolower", array_map("trim", array_slice($orderdata, 4)));
100 }
101 
103 {
104  global $importedFiles;
105 
106  $err = '';
107  $path = preg_replace(':/+:', '/', $path);
108  // return same if already imported (case of multi links)
109  if (isset($importedFiles[$path])) {
110  $vid = $importedFiles[$path];
111  return "";
112  }
113  // $mime=mime_content_type($absfile);
114  $mime = getSysMimeFile($path);
115  if (!$analyze) {
117  $err = $vf->Store($path, false, $vid);
118  }
119  if ($err != "") {
120 
121  AddWarningMsg($err);
122  return $err;
123  } else {
124  $base = basename($path);
125  $importedFiles[$path] = "$mime|$vid|$base";
126  $vid = "$mime|$vid|$base";
127 
128  return "";
129  }
130  return false;
131 }
133 {
134  if (preg_match('/\.ods$/', $filename)) return true;
135  $sys = trim(shell_exec(sprintf("file -bi %s", escapeshellarg($filename))));
136  if ($sys == "application/x-zip") return true;
137  if ($sys == "application/vnd.oasis.opendocument.spreadsheet") return true;
138  return false;
139 }
140 /**
141  * convert ods file in csv file
142  * the csv file must be delete by caller after using it
143  * @return strint the path to the csv file
144  */
145 function ods2csv($odsfile)
146 {
147  $csvfile = uniqid(getTmpDir() . "/csv") . "csv";
148  $wsh = getWshCmd();
149  $cmd = sprintf("%s --api=ods2csv --odsfile=%s --csvfile=%s >/dev/null", getWshCmd() , escapeshellarg($odsfile) , escapeshellarg($csvfile));
150  $err = system($cmd, $out);
151  if ($err === false) return false;
152  return $csvfile;
153 }
154 ?>
seemsODS($filename)
global $action
newFreeVaultFile($dbaccess)
Definition: Lib.Util.php:17
$ldir
Definition: resizeimg.php:145
add_import_file(Action &$action, $fimport, $dirid=0)
Definition: import_file.php:28
$filename
getSysMimeFile($f, $fn="")
const SEPCHAR
Definition: import_file.php:26
getOrder(array $orderdata)
Definition: import_file.php:97
$csvfile
foreach($argv as $arg) $cmd
getWshCmd($nice=false, $userid=0, $sudo=false)
Definition: Lib.Common.php:594
$path
Definition: dav.php:39
AddVaultFile($dbaccess, $path, $analyze, &$vid)
print
Definition: checklist.php:49
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
$odsfile
if($dbaccess=="") $o
ods2csv($odsfile)
setMaxExecutionTimeTo($limit)
Definition: Lib.Common.php:121
$vf
Definition: geticon.php:28
$dbaccess
Definition: checkVault.php:17
csvAddDoc($dbaccess, $data, $dirid=0, $analyze=false, $ldir= '', $policy="add", $tkey=array("title"), $prevalues=array(), $torder=array())
Definition: import_file.php:64
if($file) if($subject==""&&$file) if($subject=="") $err
AddImportLog($msg)
Definition: import_file.php:80
$analyze
$fimport
Definition: csv2sql.php:21
$data
← centre documentaire © anakeen