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

/wp-includes/pomo/mo.php

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