/wp-includes/pomo/mo.php

  1. <?php 
  2. /** 
  3. * Class for working with MO files 
  4. * 
  5. * @version $Id: mo.php 1157 2015-11-20 04:30:11Z dd32 $ 
  6. * @package pomo 
  7. * @subpackage mo 
  8. */ 
  9.  
  10. require_once dirname(__FILE__) . '/translations.php'; 
  11. require_once dirname(__FILE__) . '/streams.php'; 
  12.  
  13. if ( ! class_exists( 'MO', false ) ): 
  14. class MO extends Gettext_Translations { 
  15.  
  16. var $_nplurals = 2; 
  17.  
  18. /** 
  19. * Loaded MO file. 
  20. * 
  21. * @var string 
  22. */ 
  23. private $filename = ''; 
  24.  
  25. /** 
  26. * Returns the loaded MO file. 
  27. * 
  28. * @return string The loaded MO file. 
  29. */ 
  30. public function get_filename() { 
  31. return $this->filename; 
  32.  
  33. /** 
  34. * Fills up with the entries from MO file $filename 
  35. * 
  36. * @param string $filename MO file to load 
  37. */ 
  38. function import_from_file($filename) { 
  39. $reader = new POMO_FileReader( $filename ); 
  40.  
  41. if ( ! $reader->is_resource() ) { 
  42. return false; 
  43.  
  44. $this->filename = (string) $filename; 
  45.  
  46. return $this->import_from_reader( $reader ); 
  47.  
  48. /** 
  49. * @param string $filename 
  50. * @return bool 
  51. */ 
  52. function export_to_file($filename) { 
  53. $fh = fopen($filename, 'wb'); 
  54. if ( !$fh ) return false; 
  55. $res = $this->export_to_file_handle( $fh ); 
  56. fclose($fh); 
  57. return $res; 
  58.  
  59. /** 
  60. * @return string|false 
  61. */ 
  62. function export() { 
  63. $tmp_fh = fopen("php://temp", 'r+'); 
  64. if ( !$tmp_fh ) return false; 
  65. $this->export_to_file_handle( $tmp_fh ); 
  66. rewind( $tmp_fh ); 
  67. return stream_get_contents( $tmp_fh ); 
  68.  
  69. /** 
  70. * @param Translation_Entry $entry 
  71. * @return bool 
  72. */ 
  73. function is_entry_good_for_export( $entry ) { 
  74. if ( empty( $entry->translations ) ) { 
  75. return false; 
  76.  
  77. if ( !array_filter( $entry->translations ) ) { 
  78. return false; 
  79.  
  80. return true; 
  81.  
  82. /** 
  83. * @param resource $fh 
  84. * @return true 
  85. */ 
  86. function export_to_file_handle($fh) { 
  87. $entries = array_filter( $this->entries, array( $this, 'is_entry_good_for_export' ) ); 
  88. ksort($entries); 
  89. $magic = 0x950412de; 
  90. $revision = 0; 
  91. $total = count($entries) + 1; // all the headers are one entry 
  92. $originals_lenghts_addr = 28; 
  93. $translations_lenghts_addr = $originals_lenghts_addr + 8 * $total; 
  94. $size_of_hash = 0; 
  95. $hash_addr = $translations_lenghts_addr + 8 * $total; 
  96. $current_addr = $hash_addr; 
  97. fwrite($fh, pack('V*', $magic, $revision, $total, $originals_lenghts_addr,  
  98. $translations_lenghts_addr, $size_of_hash, $hash_addr)); 
  99. fseek($fh, $originals_lenghts_addr); 
  100.  
  101. // headers' msgid is an empty string 
  102. fwrite($fh, pack('VV', 0, $current_addr)); 
  103. $current_addr++; 
  104. $originals_table = chr(0); 
  105.  
  106. $reader = new POMO_Reader(); 
  107.  
  108. foreach($entries as $entry) { 
  109. $originals_table .= $this->export_original($entry) . chr(0); 
  110. $length = $reader->strlen($this->export_original($entry)); 
  111. fwrite($fh, pack('VV', $length, $current_addr)); 
  112. $current_addr += $length + 1; // account for the NULL byte after 
  113.  
  114. $exported_headers = $this->export_headers(); 
  115. fwrite($fh, pack('VV', $reader->strlen($exported_headers), $current_addr)); 
  116. $current_addr += strlen($exported_headers) + 1; 
  117. $translations_table = $exported_headers . chr(0); 
  118.  
  119. foreach($entries as $entry) { 
  120. $translations_table .= $this->export_translations($entry) . chr(0); 
  121. $length = $reader->strlen($this->export_translations($entry)); 
  122. fwrite($fh, pack('VV', $length, $current_addr)); 
  123. $current_addr += $length + 1; 
  124.  
  125. fwrite($fh, $originals_table); 
  126. fwrite($fh, $translations_table); 
  127. return true; 
  128.  
  129. /** 
  130. * @param Translation_Entry $entry 
  131. * @return string 
  132. */ 
  133. function export_original($entry) { 
  134. //TODO: warnings for control characters 
  135. $exported = $entry->singular; 
  136. if ($entry->is_plural) $exported .= chr(0).$entry->plural; 
  137. if ($entry->context) $exported = $entry->context . chr(4) . $exported; 
  138. return $exported; 
  139.  
  140. /** 
  141. * @param Translation_Entry $entry 
  142. * @return string 
  143. */ 
  144. function export_translations($entry) { 
  145. //TODO: warnings for control characters 
  146. return $entry->is_plural ? implode(chr(0), $entry->translations) : $entry->translations[0]; 
  147.  
  148. /** 
  149. * @return string 
  150. */ 
  151. function export_headers() { 
  152. $exported = ''; 
  153. foreach($this->headers as $header => $value) { 
  154. $exported.= "$header: $value\n"; 
  155. return $exported; 
  156.  
  157. /** 
  158. * @param int $magic 
  159. * @return string|false 
  160. */ 
  161. function get_byteorder($magic) { 
  162. // The magic is 0x950412de 
  163.  
  164. // bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565 
  165. $magic_little = (int) - 1794895138; 
  166. $magic_little_64 = (int) 2500072158; 
  167. // 0xde120495 
  168. $magic_big = ((int) - 569244523) & 0xFFFFFFFF; 
  169. if ($magic_little == $magic || $magic_little_64 == $magic) { 
  170. return 'little'; 
  171. } else if ($magic_big == $magic) { 
  172. return 'big'; 
  173. } else { 
  174. return false; 
  175.  
  176. /** 
  177. * @param POMO_FileReader $reader 
  178. */ 
  179. function import_from_reader($reader) { 
  180. $endian_string = MO::get_byteorder($reader->readint32()); 
  181. if (false === $endian_string) { 
  182. return false; 
  183. $reader->setEndian($endian_string); 
  184.  
  185. $endian = ('big' == $endian_string)? 'N' : 'V'; 
  186.  
  187. $header = $reader->read(24); 
  188. if ($reader->strlen($header) != 24) 
  189. return false; 
  190.  
  191. // parse header 
  192. $header = unpack("{$endian}revision/{$endian}total/{$endian}originals_lenghts_addr/{$endian}translations_lenghts_addr/{$endian}hash_length/{$endian}hash_addr", $header); 
  193. if (!is_array($header)) 
  194. return false; 
  195.  
  196. // support revision 0 of MO format specs, only 
  197. if ( $header['revision'] != 0 ) { 
  198. return false; 
  199.  
  200. // seek to data blocks 
  201. $reader->seekto( $header['originals_lenghts_addr'] ); 
  202.  
  203. // read originals' indices 
  204. $originals_lengths_length = $header['translations_lenghts_addr'] - $header['originals_lenghts_addr']; 
  205. if ( $originals_lengths_length != $header['total'] * 8 ) { 
  206. return false; 
  207.  
  208. $originals = $reader->read($originals_lengths_length); 
  209. if ( $reader->strlen( $originals ) != $originals_lengths_length ) { 
  210. return false; 
  211.  
  212. // read translations' indices 
  213. $translations_lenghts_length = $header['hash_addr'] - $header['translations_lenghts_addr']; 
  214. if ( $translations_lenghts_length != $header['total'] * 8 ) { 
  215. return false; 
  216.  
  217. $translations = $reader->read($translations_lenghts_length); 
  218. if ( $reader->strlen( $translations ) != $translations_lenghts_length ) { 
  219. return false; 
  220.  
  221. // transform raw data into set of indices 
  222. $originals = $reader->str_split( $originals, 8 ); 
  223. $translations = $reader->str_split( $translations, 8 ); 
  224.  
  225. // skip hash table 
  226. $strings_addr = $header['hash_addr'] + $header['hash_length'] * 4; 
  227.  
  228. $reader->seekto($strings_addr); 
  229.  
  230. $strings = $reader->read_all(); 
  231. $reader->close(); 
  232.  
  233. for ( $i = 0; $i < $header['total']; $i++ ) { 
  234. $o = unpack( "{$endian}length/{$endian}pos", $originals[$i] ); 
  235. $t = unpack( "{$endian}length/{$endian}pos", $translations[$i] ); 
  236. if ( !$o || !$t ) return false; 
  237.  
  238. // adjust offset due to reading strings to separate space before 
  239. $o['pos'] -= $strings_addr; 
  240. $t['pos'] -= $strings_addr; 
  241.  
  242. $original = $reader->substr( $strings, $o['pos'], $o['length'] ); 
  243. $translation = $reader->substr( $strings, $t['pos'], $t['length'] ); 
  244.  
  245. if ('' === $original) { 
  246. $this->set_headers($this->make_headers($translation)); 
  247. } else { 
  248. $entry = &$this->make_entry($original, $translation); 
  249. $this->entries[$entry->key()] = &$entry; 
  250. return true; 
  251.  
  252. /** 
  253. * Build a Translation_Entry from original string and translation strings,  
  254. * found in a MO file 
  255. * 
  256. * @static 
  257. * @param string $original original string to translate from MO file. Might contain 
  258. * 0x04 as context separator or 0x00 as singular/plural separator 
  259. * @param string $translation translation string from MO file. Might contain 
  260. * 0x00 as a plural translations separator 
  261. */ 
  262. function &make_entry($original, $translation) { 
  263. $entry = new Translation_Entry(); 
  264. // look for context 
  265. $parts = explode(chr(4), $original); 
  266. if (isset($parts[1])) { 
  267. $original = $parts[1]; 
  268. $entry->context = $parts[0]; 
  269. // look for plural original 
  270. $parts = explode(chr(0), $original); 
  271. $entry->singular = $parts[0]; 
  272. if (isset($parts[1])) { 
  273. $entry->is_plural = true; 
  274. $entry->plural = $parts[1]; 
  275. // plural translations are also separated by \0 
  276. $entry->translations = explode(chr(0), $translation); 
  277. return $entry; 
  278.  
  279. /** 
  280. * @param int $count 
  281. * @return string 
  282. */ 
  283. function select_plural_form($count) { 
  284. return $this->gettext_select_plural_form($count); 
  285.  
  286. /** 
  287. * @return int 
  288. */ 
  289. function get_plural_forms_count() { 
  290. return $this->_nplurals; 
  291. endif; 
.