Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / web / public_php / webtt / app / vendors / SheetParser.php
blob066468c7f80fdf16d534dda0cc2be951f259ad7c
1 <?php
2 /*
3 Ryzom Core Web-Based Translation Tool
4 Copyright (C) 2011 Piotr Kaczmarek <p.kaczmarek@openlink.pl>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 <?php
21 class SheetParser
23 var $debug = false;
25 function parseLine($str)
27 $arr = str_getcsv($str, "\t");
28 return $arr;
31 function parseFile($file)
33 $parsedEnt = array();
34 $newEnt = false;
35 $prevStringLine = false;
36 $entities = array();
37 $diffFile = false;
39 $file = mb_convert_encoding($file, 'UTF-8', 'UTF-16');
40 // $file = $this->removeBOM($file);
41 // $file = $this->removeComments($file);
42 $lines = explode("\n", $file);
43 if ($this->debug)
45 echo "<pre>\n\n";
47 $line_no=1;
49 /* var_dump(setlocale(LC_ALL,NULL));
50 var_dump(setlocale(LC_ALL,'pl_PL.UTF-8'));*/
51 // Need to set UTF-8 locale to get str_getcsv to work with UTF-8 cyryllic
52 setlocale(LC_ALL,'pl_PL.UTF-8');
54 foreach ($lines as $line)
56 if ($this->debug)
58 echo "\n\t#################### LINE NUMBER " . $line_no . "\n\n";
61 // var_dump($line);
62 $line = rtrim($line,"\r\n");
64 $parsedLine = $this->parseLine($line);
66 if (!$line || mb_strpos($line, "DIFF NOT") === 0 || mb_strpos($line, "REMOVE THE") === 0)
67 continue;
69 if ($line_no == 1)
71 $parsedEnt["type"] = "sheet_description";
72 if ($parsedLine[0] == "DIFF_CMD")
73 $diffFile = true;
74 $parsedEnt["sheet_id_column"] = $parsedLine[2];
76 else
78 if ($diffFile)
80 $parsedEnt["diff"] = $parsedEnt["command"] = rtrim($parsedLine[0]);
81 $parsedEnt["hash_value"] = $parsedLine[1];
82 $parsedEnt["identifier"] = $parsedLine[2];
84 else
86 $parsedEnt["hash_value"] = $parsedLine[0];
87 $parsedEnt["identifier"] = $parsedLine[1];
89 $parsedEnt["type"] = "sheet";
92 if ($diffFile)
93 $columns = array_slice($parsedLine, 3);
94 else
95 $columns = array_slice($parsedLine, 2);
97 if (!isset($columnsCount))
98 $columnsCount = count($columns);
100 $parsedEnt["columns"] = $columns;
102 if ($this->debug)
104 echo "%%%% parsedEnt %%%%%\n";
105 var_dump($parsedEnt);
108 /* if ($parsedLine["type"] == "internal_index")
109 $parsedEnt["internal_index"] = $parsedLine["index"];
111 if ($parsedLine["type"] == "diff")
113 $parsedEnt["diff"] = $parsedLine["command"];
114 $parsedEnt["index"] = $parsedLine["index"];
117 $newEnt = true;
119 if ($newEnt)
121 if ($this->debug && 0)
123 echo "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
124 echo "\t%%%% newEnt %%%%%%%%% newEnt %%%%%%%%% newEnt %%%%%%%%% newEnt %%%%%\n";
125 echo "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n";
126 var_dump($parsedEnt);
128 /* if (!isset($parsedEnt["diff"]) && !isset($parsedEnt["index"]))
129 $parsedEnt["index"] = $parsedEnt["internal_index"];*/
131 $entities[] = $parsedEnt;
132 $parsedEnt =array();
133 $newEnt = false;
135 $line_no++;
138 if ($this->debug && 0)
140 echo "<pre>";
141 var_dump($entities);
142 echo "</pre>\n";
144 return $entities;
147 function CRLF($s)
149 $s = str_replace("\r\n", "\n", $s);
150 $s = str_replace("\n", "\r\n", $s);
151 return $s;
154 function addBOM($str)
156 if(($bom = substr($str, 0,3)) != pack("CCC",0xef,0xbb,0xbf))
157 return pack("CCC",0xef,0xbb,0xbf) . $str;
158 else
159 return $str;
162 function buildFile($entities)
164 $content = '';
165 foreach ($entities as $ent)
167 if ($ent['type'] == 'sheet_description')
169 $_columns = $ent['columns'];
170 $_sheet_id_column = $ent['sheet_id_column'];
172 if (isset($ent['diff']))
174 $content .= 'DIFF_CMD' . "\t" . '*HASH_VALUE' . "\t" . $_sheet_id_column . "\t";
175 foreach ($ent['columns'] as $value)
177 $content .= $value . "\t";
179 $content = mb_substr($content, 0, -1);
180 $content .= "\n";
182 continue;
185 if (isset($ent['command']))
186 $content .= $ent['command'] . "\t";
187 if (isset($ent['hash_value']))
188 $content .= $ent['hash_value'] . "\t";
189 else
190 $content .= '_0000000000000000' . "\t";
191 $content .= $ent['identifier'] . "\t";
192 foreach ($ent['columns'] as $value)
194 $content .= $value . "\t";
196 $content = mb_substr($content, 0, -1);
197 $content .= "\n";
199 return mb_convert_encoding($this->addBOM($this->CRLF($content)), 'UTF-16LE', 'UTF-8');
200 // return mb_convert_encoding($this->CRLF($content), 'UTF-16LE', 'UTF-8');