Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Api/ods2csv.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Convert OpenDocument Spreadsheet to csv (semicolon)
8  *
9  * @author Anakeen
10  * @version $Id: ods2csv.php,v 1.8 2008/12/02 15:22:32 eric Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 define("SEPCHAR", ';');
17 define("ALTSEPCHAR", ' --- ');
18 
19 $inrow = false;
20 $incell = false;
21 $nrow = 0;
22 $ncol = 0;
23 $rows = array();
25 $celldata = '';
26 $cellattrs = array();
27 function startElementOds($parser, $name, $attrs)
28 {
30  if ($name == "TABLE:TABLE-ROW") {
31  $inrow = true;
32  if (isset($rows[$nrow])) {
33  // fill empty cells
34  $idx = 0;
35  foreach ($rows[$nrow] as $k => $v) {
36  if (!isset($rows[$nrow][$idx])) $rows[$nrow][$idx] = '';
37  $idx++;
38  }
39  ksort($rows[$nrow], SORT_NUMERIC);
40  }
41  $nrow++;
42  $ncol = 0;
43  $rows[$nrow] = array();
44  }
45 
46  if ($name == "TABLE:TABLE-CELL") {
47  $incell = true;
48  $celldata = "";
49  $cellattrs = $attrs;
50  if (isset($attrs["TABLE:NUMBER-COLUMNS-REPEATED"])) {
51  $colrepeat = intval($attrs["TABLE:NUMBER-COLUMNS-REPEATED"]);
52  }
53  }
54  if ($name == "TEXT:P") {
55  if ((isset($rows[$nrow][$ncol])) && strlen($rows[$nrow][$ncol]) > 0) $rows[$nrow][$ncol].= '\n';
56  }
57 }
58 
59 function endElementOds($parser, $name)
60 {
62  if ($name == "TABLE:TABLE-ROW") {
63  // Remove trailing empty cells
64  $i = $ncol - 1;
65  while ($i >= 0) {
66  if (strlen($rows[$nrow][$i]) > 0) {
67  break;
68  }
69  $i--;
70  }
71  array_splice($rows[$nrow], $i + 1);
72  $inrow = false;
73  }
74  if ($name == "TEXT:S") {
75  $celldata.= ' ';
76  }
77  if ($name == "TABLE:TABLE-CELL") {
78  $incell = false;
79 
80  if ($celldata === '') {
81  $celldata = getOfficeTypedValue($cellattrs);
82  }
83 
84  $rows[$nrow][$ncol] = $celldata;
85 
86  if ($colrepeat > 1) {
87  $rval = $rows[$nrow][$ncol];
88  for ($i = 1; $i < $colrepeat; $i++) {
89  $ncol++;
90  $rows[$nrow][$ncol] = $rval;
91  }
92  }
93  //$ncol+=intval($attrs["TABLE:NUMBER-COLUMNS-REPEATED"]);
94  $ncol++;
95  $colrepeat = 0;
96  }
97 }
98 
99 function characterDataOds($parser, $data)
100 {
101  global $rows, $nrow, $inrow, $incell, $ncol, $celldata;
102  if ($inrow && $incell) {
103  $celldata.= preg_replace('/\s/u', ' ', preg_replace('/^\s*[\r\n]\s*$/ms', '', str_replace(SEPCHAR, ALTSEPCHAR, $data)));
104  }
105  // print $data. "- ";
106 
107 }
108 
109 function getOfficeTypedValue($attrs)
110 {
111  $value = '';
112  /* Get value from property OFFICE:<type>-VALUE */
113  if (isset($attrs['OFFICE:VALUE-TYPE'])) {
114  $type = strtoupper($attrs['OFFICE:VALUE-TYPE']);
115  $propName = 'OFFICE:' . $type . '-VALUE';
116  if (isset($attrs[$propName])) {
117  $value = (string)$attrs[$propName];
118  }
119  }
120  /* Get value from property OFFICE:VALUE */
121  if ($value == '' && isset($attrs['OFFICE:VALUE'])) {
122  $value = (string)$attrs['OFFICE:VALUE'];
123  }
124  return $value;
125 }
126 
127 function xmlcontent2csv($xmlcontent, &$fcsv)
128 {
129  global $rows;
130  $xml_parser = xml_parser_create();
131  // Utilisons la gestion de casse, de maniere a etre surs de trouver la balise dans $map_array
132  xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
133  xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 0);
134  xml_set_element_handler($xml_parser, "startElementOds", "endElementOds");
135  xml_set_character_data_handler($xml_parser, "characterDataOds");
136 
137  if (!xml_parse($xml_parser, $xmlcontent)) {
138  return (sprintf("error XML : %s line %d", xml_error_string(xml_get_error_code($xml_parser)) , xml_get_current_line_number($xml_parser)));
139  }
140 
141  xml_parser_free($xml_parser);
142  //print_r($rows);
143  foreach ($rows as $k => $row) {
144  $fcsv.= implode(SEPCHAR, $row) . "\n";
145  }
146  return "";
147 }
148 
149 function ods2content($odsfile, &$content)
150 {
151  if (!file_exists($odsfile)) return "file $odsfile not found";
152  $cibledir = uniqid(getTmpDir() . "/ods");
153 
154  $cmd = sprintf("unzip -j %s content.xml -d %s >/dev/null", escapeshellarg($odsfile) , escapeshellarg($cibledir));
155  system($cmd);
156 
157  $contentxml = $cibledir . "/content.xml";
158  if (file_exists($contentxml)) {
159  $content = file_get_contents($contentxml);
160  unlink($contentxml);
161  }
162 
163  rmdir($cibledir);
164  return "";
165 }
166 
167 $usage = new ApiUsage();
168 
169 $usage->setDefinitionText("Convert OpenDocument Spreadsheet to csv (semicolon)");
170 $odsfile = $usage->addRequiredParameter("odsfile", "ods file (input)"); // file ods (input)
171 $csvfile = $usage->addOptionalParameter("csvfile", "xml file (output)"); // file xml (output)
172 $usage->verify();
173 
174 $err = ods2content($odsfile, $content);
175 if ($err == "") {
176  $err = xmlcontent2csv($content, $csv);
177  if ($err == "") {
178  if ($csvfile) {
179  $n = file_put_contents($csvfile, $csv);
180  if ($n > 0) print sprintf(_("csv file <%s> wroted") . "\n", $csvfile);
181  else $err = sprintf(_("cannot write %s") , $csvfile);
182  } else print $csv;
183  }
184 }
185 if ($err != "") print "ERROR:$err\n";
$csv
Definition: checkVault.php:41
ods2content($odsfile, &$content)
$incell
Definition: Api/ods2csv.php:20
const ALTSEPCHAR
Definition: Api/ods2csv.php:17
$celldata
Definition: Api/ods2csv.php:25
$usage
$nrow
Definition: Api/ods2csv.php:21
$colrepeat
Definition: Api/ods2csv.php:24
xmlcontent2csv($xmlcontent, &$fcsv)
$inrow
Definition: Api/ods2csv.php:19
$csvfile
$ncol
Definition: Api/ods2csv.php:22
foreach($argv as $arg) $cmd
$rows
Definition: Api/ods2csv.php:23
print
Definition: checklist.php:49
$err
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
$odsfile
getOfficeTypedValue($attrs)
const SEPCHAR
Definition: Api/ods2csv.php:16
endElementOds($parser, $name)
Definition: Api/ods2csv.php:59
characterDataOds($parser, $data)
Definition: Api/ods2csv.php:99
startElementOds($parser, $name, $attrs)
Definition: Api/ods2csv.php:27
$value
Verify arguments for wsh programs.
$data
$cellattrs
Definition: Api/ods2csv.php:26
← centre documentaire © anakeen