PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/code/ryzom/tools/server/www/webtt/app/vendors/PhraseParser.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 307 lines | 235 code | 26 blank | 46 comment | 67 complexity | 48ca23e6449f66f558a027e3986f163e MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Ryzom Core Web-Based Translation Tool
  4. Copyright (C) 2011 Piotr Kaczmarek <p.kaczmarek@openlink.pl>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. ?>
  17. <?php
  18. class PhraseParser
  19. {
  20. var $debug = false;
  21. function removeComments($str)
  22. {
  23. //As a pertinent note, there's an issue with this function where parsing any string longer than 94326 characters long will silently return null. So be careful where you use it at.
  24. //http://pl.php.net/manual/en/function.preg-replace.php#98843
  25. ini_set('pcre.backtrack_limit', 10000000);
  26. //$returnString = preg_replace('!/\*.*?\*/!s', '', $str); // /* .*? */ s
  27. // added [^/] because there was //******* in translation file
  28. $returnString = preg_replace('![^/]/\*.*?\*/!s', '', $str); // /* .*? */ s
  29. // PHP 5.2.0
  30. // if (PREG_NO_ERROR !== preg_last_error())
  31. if ($returnString === null)
  32. {
  33. $returnStr = $str;
  34. var_dump("PREG ERROR");
  35. // exception
  36. }
  37. return $returnString;
  38. }
  39. function removeBOM($str)
  40. {
  41. if(($bom = substr($str, 0,3)) == pack("CCC",0xef,0xbb,0xbf))
  42. {
  43. $bla = substr($str, 3);
  44. return $bla;
  45. }
  46. else
  47. {
  48. return $str;
  49. }
  50. }
  51. function addBOM($str)
  52. {
  53. if(($bom = substr($str, 0,3)) != pack("CCC",0xef,0xbb,0xbf))
  54. return pack("CCC",0xef,0xbb,0xbf) . $str;
  55. else
  56. return $str;
  57. }
  58. function parseLine($str)
  59. {
  60. $arr = array();
  61. if (mb_substr($str, 0, 2) == "//")
  62. {
  63. if (mb_substr($str, 0, 7) == "// DIFF")
  64. {
  65. $tmp = explode(" ", $str);
  66. $type = 'diff';
  67. $command = mb_strtolower($tmp[2]);
  68. $args = isset($tmp[3]) ? $tmp[3] : null;
  69. if ($command == "add" || $command == "changed")
  70. {
  71. if (isset($args))
  72. $index = intval($args);
  73. else
  74. $index = null;
  75. $command = mb_substr($str, 3);
  76. }
  77. else
  78. unset($type);
  79. }
  80. else if (mb_substr($str, 0, 8) == "// INDEX")
  81. {
  82. list($j, $type, $index) = explode(" ", $str);
  83. $type = "internal_index";
  84. }
  85. else if (mb_substr($str, 0, 13) == "// HASH_VALUE")
  86. {
  87. list($j, $type, $hash_value) = explode(" ", $str);
  88. $type = "hash_value";
  89. }
  90. /* if (!isset($type))
  91. {
  92. var_dump(isset($type));
  93. debug_print_backtrace();
  94. }
  95. var_dump($type);*/
  96. if (isset($type))
  97. {
  98. $type = mb_strtolower($type);
  99. $arr = compact("type","command","index","hash_value");
  100. }
  101. }
  102. else if (!(mb_substr($str, 0, 2) == "//") && mb_strlen($str))
  103. {
  104. $type = "string";
  105. $string = "";
  106. $identifier = "";
  107. $arguments = "";
  108. $body = "";
  109. if (preg_match('|^([0-9A-Za-z@_]*) \((.*?)\).*$|', $str, $matches))
  110. {
  111. $identifier = $matches[1];
  112. $arguments = $matches[2];
  113. }
  114. else if ($str == "{")
  115. $body = "begin";
  116. else if ($str == "}")
  117. $body = "end";
  118. else
  119. {
  120. $string = $str;
  121. }
  122. $arr = compact("type", "identifier", "arguments", "string", "body");
  123. }
  124. /* echo "<pre>################################\n";
  125. var_dump($str);
  126. var_dump($arr);
  127. echo "</pre>\n";*/
  128. return $arr;
  129. }
  130. function addEnt(&$parsedEnt, &$entities, &$newEnt)
  131. {
  132. if ($this->debug)
  133. {
  134. echo "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  135. echo "\t%%%% newEnt %%%%%%%%% newEnt %%%%%%%%% newEnt %%%%%%%%% newEnt %%%%%\n";
  136. echo "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n";
  137. var_dump($parsedEnt);
  138. }
  139. if (!isset($parsedEnt["diff"]) && !isset($parsedEnt["index"]))
  140. $parsedEnt["index"] = $parsedEnt["internal_index"];
  141. $parsedEnt["type"] = "phrase";
  142. $entities[] = $parsedEnt;
  143. $parsedEnt =array();
  144. $newEnt = false;
  145. }
  146. function parseFile($file)
  147. {
  148. $parsedEnt = array();
  149. $newEnt = false;
  150. $prevStringLine = false;
  151. $entities = array();
  152. $file = $this->removeBOM($file);
  153. // var_dump($file);
  154. $file = $this->removeComments($file);
  155. // var_dump($file);
  156. $lines = explode("\n", $file);
  157. if ($this->debug)
  158. {
  159. echo "<pre>\n\n";
  160. }
  161. $line_no=1;
  162. foreach ($lines as $line)
  163. {
  164. if ($this->debug)
  165. {
  166. echo "\n\t#################### LINE NUMBER " . $line_no++ . "\n\n";
  167. var_dump($line);
  168. }
  169. $line = rtrim($line, "\r\n");
  170. $parsedLine = $this->parseLine($line);
  171. if ($this->debug)
  172. {
  173. echo "%%%% parsedLine\n";
  174. var_dump($parsedLine);
  175. echo "\n";
  176. echo "%%%% prevStringLine\n";
  177. var_dump($prevStringLine);
  178. echo "\n";
  179. }
  180. if (!$parsedLine)
  181. continue;
  182. // if line start with diff (diff files) or hash_value (translated files) and before was line with translation, then we start new ent
  183. if ($prevStringLine && (
  184. ($parsedLine["type"] == "diff" && $parsedEnt) || ($parsedLine["type"] == "hash_value" && $parsedEnt)
  185. ))
  186. {
  187. /* echo "%%%% prevStringLine %%%%%\n";
  188. var_dump($parsedEnt);*/
  189. $newEnt = true;
  190. }
  191. if ($newEnt)
  192. {
  193. $this->addEnt($parsedEnt, $entities, $newEnt);
  194. }
  195. if ($parsedLine["type"] == "internal_index")
  196. $parsedEnt["internal_index"] = $parsedLine["index"];
  197. if ($parsedLine["type"] == "string")
  198. {
  199. $prevStringLine = true;
  200. if ($parsedLine["body"] == "begin" || $parsedLine["body"] == "end")
  201. continue;
  202. if ($this->debug)
  203. {
  204. echo "%%%% parsedEnt before %%%%%\n";
  205. var_dump($parsedEnt);
  206. // echo "%%%% parsedLine %%%%%\n";
  207. // var_dump($parsedLine);
  208. }
  209. if (!$parsedLine['identifier'])
  210. {
  211. if ($this->debug) echo "%%%% parsedEnt ZLACZENIE \n";
  212. if ($this->debug && !isset($parsedEnt['string']))
  213. {
  214. echo "!isset parsedEnt['string']\n";
  215. var_dump($line);
  216. var_dump($parsedEnt);
  217. var_dump($parsedLine);
  218. }
  219. $parsedEnt['string'] .= $parsedLine['string'] . "\n";
  220. }
  221. else
  222. {
  223. if ($this->debug) echo "DODANIE \n";
  224. $parsedEnt += $parsedLine;
  225. // $parsedEnt['string'] .= "\n";
  226. }
  227. if ($this->debug)
  228. {
  229. echo "%%%% parsedEnt after %%%%%\n";
  230. var_dump($parsedEnt);
  231. }
  232. }
  233. else
  234. $prevStringLine = false;
  235. if ($parsedLine["type"] == "diff")
  236. {
  237. $parsedEnt["diff"] = $parsedEnt["command"] = $parsedLine["command"];
  238. $parsedEnt["index"] = $parsedLine["index"];
  239. }
  240. }
  241. if ($parsedEnt)
  242. {
  243. $this->addEnt($parsedEnt, $entities, $newEnt);
  244. }
  245. if ($this->debug)
  246. {
  247. echo "<pre>";
  248. var_dump($entities);
  249. echo "</pre>\n";
  250. }
  251. return $entities;
  252. }
  253. function CRLF($s)
  254. {
  255. $s = str_replace("\r\n", "\n", $s);
  256. $s = str_replace("\n", "\r\n", $s);
  257. return $s;
  258. }
  259. function buildFile($entities)
  260. {
  261. $content = '// DIFF_VERSION 2' . "\n";
  262. foreach ($entities as $ent)
  263. {
  264. if (isset($ent['command']))
  265. $content .= '// ' . $ent['command'] . "\n";
  266. if (isset($ent['hash_value']))
  267. $content .= '// HASH_VALUE ' . $ent['hash_value'];
  268. /* if (isset($ent['command']))
  269. $content .= '// INDEX ' . $ent['internal_index'] . "\n";
  270. else
  271. $content .= '// INDEX ' . $ent['index'] . "\n";*/
  272. $content .= $ent['identifier'] . ' (' . $ent['arguments'] . ')' . "\n" . '{' . "\n" . $ent['string'] . "\n" . '}' . "\n";
  273. $content .= "\n";
  274. }
  275. return $this->addBOM($this->CRLF($content));
  276. }
  277. }
  278. ?>