Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
ods2csv.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Convert OpenDocument Spreadsheet to csv (semicolon)
9  *
10  * @author Anakeen 2000
11  * @version $Id: ods2csv.php,v 1.8 2008/12/02 15:22:32 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 define("SEPCHAR", ';');
19 define("ALTSEPCHAR", ' --- ');
20 
21 $inrow = false;
22 $incell = false;
23 $nrow = 0;
24 $ncol = 0;
25 $rows = array();
27 function startElementOds($parser, $name, $attrs)
28 {
29  global $rows, $nrow, $inrow, $incell, $ncol, $colrepeat, $celldata;
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  if ($attrs["TABLE:NUMBER-COLUMNS-REPEATED"]) {
50  $colrepeat = intval($attrs["TABLE:NUMBER-COLUMNS-REPEATED"]);
51  }
52  }
53  if ($name == "TEXT:P") {
54  if (strlen($rows[$nrow][$ncol]) > 0) $rows[$nrow][$ncol].= '\n';
55  }
56  if ($name == "TEXT:S" && $incell) {
57  $count = intval($attrs["TEXT:C"]);
58  if ($count < 1) {
59  $count = 1;
60  }
61  $celldata .= str_repeat(" ", $count);
62  }
63 }
64 
65 function endElementOds($parser, $name)
66 {
67  global $rows, $nrow, $inrow, $incell, $ncol, $colrepeat, $celldata;
68  if ($name == "TABLE:TABLE-ROW") {
69  // Remove trailing empty cells
70  $i = $ncol - 1;
71  while ($i >= 0) {
72  if (strlen($rows[$nrow][$i]) > 0) {
73  break;
74  }
75  $i--;
76  }
77  array_splice($rows[$nrow], $i + 1);
78  $inrow = false;
79  }
80 
81  if ($name == "TABLE:TABLE-CELL") {
82  $incell = false;
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 xmlcontent2csv($xmlcontent, &$fcsv)
110 {
111  global $rows;
112  $xml_parser = xml_parser_create();
113  // Utilisons la gestion de casse, de maniere a etre surs de trouver la balise dans $map_array
114  xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
115  xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 0);
116  xml_set_element_handler($xml_parser, "startElementOds", "endElementOds");
117  xml_set_character_data_handler($xml_parser, "characterDataOds");
118 
119  if (!xml_parse($xml_parser, $xmlcontent)) {
120  return (sprintf("error XML : %s line %d", xml_error_string(xml_get_error_code($xml_parser)) , xml_get_current_line_number($xml_parser)));
121  }
122 
123  xml_parser_free($xml_parser);
124  //print_r($rows);
125  foreach ($rows as $k => $row) {
126  $fcsv.= implode(SEPCHAR, $row) . "\n";
127  }
128 }
129 
131 {
132  if (!file_exists($odsfile)) return "file $odsfile not found";
133  $cibledir = uniqid(getTmpDir() . "/ods");
134 
135  $cmd = sprintf("unzip -j %s content.xml -d %s >/dev/null", escapeshellarg($odsfile) , escapeshellarg($cibledir));
136  system($cmd);
137 
138  $contentxml = $cibledir . "/content.xml";
139  if (file_exists($contentxml)) {
140  $content = file_get_contents($contentxml);
141  unlink($contentxml);
142  }
143 
144  rmdir($cibledir);
145 }
146 
147 $odsfile = GetHttpVars("odsfile"); // file ods (input)
148 $csvfile = GetHttpVars("csvfile"); // file xml (output)
149 if ($odsfile == "") {
150  print "odsfile needed :usage --odsfile=<ods file> [--csvfile=<csv file output>]\n";
151  return;
152 }
153 
155 if ($err == "") {
157  if ($err == "") {
158  if ($csvfile) {
159  $n = file_put_contents($csvfile, $csv);
160  if ($n > 0) print sprintf(_("csv file <%s> wroted") . "\n", $csvfile);
161  else $err = sprintf(_("cannot write %s") , $csvfile);
162  } else print $csv;
163  }
164 }
165 if ($err != "") print "ERROR:$err\n";
166 ?>
← centre documentaire © anakeen - published under CC License - Dynacase