PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/pomo/mo.php

https://github.com/davodey/WordPress
PHP | 260 lines | 180 code | 41 blank | 39 comment | 38 complexity | 12c13ed28a4da2b4bf8d43fb97b0951d MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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. foreach($entries as $entry) {
  67. $originals_table .= $this->export_original($entry) . chr(0);
  68. $length = strlen($this->export_original($entry));
  69. fwrite($fh, pack('VV', $length, $current_addr));
  70. $current_addr += $length + 1; // account for the NULL byte after
  71. }
  72. $exported_headers = $this->export_headers();
  73. fwrite($fh, pack('VV', strlen($exported_headers), $current_addr));
  74. $current_addr += strlen($exported_headers) + 1;
  75. $translations_table = $exported_headers . chr(0);
  76. foreach($entries as $entry) {
  77. $translations_table .= $this->export_translations($entry) . chr(0);
  78. $length = strlen($this->export_translations($entry));
  79. fwrite($fh, pack('VV', $length, $current_addr));
  80. $current_addr += $length + 1;
  81. }
  82. fwrite($fh, $originals_table);
  83. fwrite($fh, $translations_table);
  84. return true;
  85. }
  86. function export_original($entry) {
  87. //TODO: warnings for control characters
  88. $exported = $entry->singular;
  89. if ($entry->is_plural) $exported .= chr(0).$entry->plural;
  90. if (!is_null($entry->context)) $exported = $entry->context . chr(4) . $exported;
  91. return $exported;
  92. }
  93. function export_translations($entry) {
  94. //TODO: warnings for control characters
  95. return implode(chr(0), $entry->translations);
  96. }
  97. function export_headers() {
  98. $exported = '';
  99. foreach($this->headers as $header => $value) {
  100. $exported.= "$header: $value\n";
  101. }
  102. return $exported;
  103. }
  104. function get_byteorder($magic) {
  105. // The magic is 0x950412de
  106. // bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565
  107. $magic_little = (int) - 1794895138;
  108. $magic_little_64 = (int) 2500072158;
  109. // 0xde120495
  110. $magic_big = ((int) - 569244523) & 0xFFFFFFFF;
  111. if ($magic_little == $magic || $magic_little_64 == $magic) {
  112. return 'little';
  113. } else if ($magic_big == $magic) {
  114. return 'big';
  115. } else {
  116. return false;
  117. }
  118. }
  119. function import_from_reader($reader) {
  120. $endian_string = MO::get_byteorder($reader->readint32());
  121. if (false === $endian_string) {
  122. return false;
  123. }
  124. $reader->setEndian($endian_string);
  125. $endian = ('big' == $endian_string)? 'N' : 'V';
  126. $header = $reader->read(24);
  127. if ($reader->strlen($header) != 24)
  128. return false;
  129. // parse header
  130. $header = unpack("{$endian}revision/{$endian}total/{$endian}originals_lenghts_addr/{$endian}translations_lenghts_addr/{$endian}hash_length/{$endian}hash_addr", $header);
  131. if (!is_array($header))
  132. return false;
  133. // support revision 0 of MO format specs, only
  134. if ( $header['revision'] != 0 ) {
  135. return false;
  136. }
  137. // seek to data blocks
  138. $reader->seekto( $header['originals_lenghts_addr'] );
  139. // read originals' indices
  140. $originals_lengths_length = $header['translations_lenghts_addr'] - $header['originals_lenghts_addr'];
  141. if ( $originals_lengths_length != $header['total'] * 8 ) {
  142. return false;
  143. }
  144. $originals = $reader->read($originals_lengths_length);
  145. if ( $reader->strlen( $originals ) != $originals_lengths_length ) {
  146. return false;
  147. }
  148. // read translations' indices
  149. $translations_lenghts_length = $header['hash_addr'] - $header['translations_lenghts_addr'];
  150. if ( $translations_lenghts_length != $header['total'] * 8 ) {
  151. return false;
  152. }
  153. $translations = $reader->read($translations_lenghts_length);
  154. if ( $reader->strlen( $translations ) != $translations_lenghts_length ) {
  155. return false;
  156. }
  157. // transform raw data into set of indices
  158. $originals = $reader->str_split( $originals, 8 );
  159. $translations = $reader->str_split( $translations, 8 );
  160. // skip hash table
  161. $strings_addr = $header['hash_addr'] + $header['hash_length'] * 4;
  162. $reader->seekto($strings_addr);
  163. $strings = $reader->read_all();
  164. $reader->close();
  165. for ( $i = 0; $i < $header['total']; $i++ ) {
  166. $o = unpack( "{$endian}length/{$endian}pos", $originals[$i] );
  167. $t = unpack( "{$endian}length/{$endian}pos", $translations[$i] );
  168. if ( !$o || !$t ) return false;
  169. // adjust offset due to reading strings to separate space before
  170. $o['pos'] -= $strings_addr;
  171. $t['pos'] -= $strings_addr;
  172. $original = $reader->substr( $strings, $o['pos'], $o['length'] );
  173. $translation = $reader->substr( $strings, $t['pos'], $t['length'] );
  174. if ('' === $original) {
  175. $this->set_headers($this->make_headers($translation));
  176. } else {
  177. $entry = &$this->make_entry($original, $translation);
  178. $this->entries[$entry->key()] = &$entry;
  179. }
  180. }
  181. return true;
  182. }
  183. /**
  184. * Build a Translation_Entry from original string and translation strings,
  185. * found in a MO file
  186. *
  187. * @static
  188. * @param string $original original string to translate from MO file. Might contain
  189. * 0x04 as context separator or 0x00 as singular/plural separator
  190. * @param string $translation translation string from MO file. Might contain
  191. * 0x00 as a plural translations separator
  192. */
  193. function &make_entry($original, $translation) {
  194. $entry = new Translation_Entry();
  195. // look for context
  196. $parts = explode(chr(4), $original);
  197. if (isset($parts[1])) {
  198. $original = $parts[1];
  199. $entry->context = $parts[0];
  200. }
  201. // look for plural original
  202. $parts = explode(chr(0), $original);
  203. $entry->singular = $parts[0];
  204. if (isset($parts[1])) {
  205. $entry->is_plural = true;
  206. $entry->plural = $parts[1];
  207. }
  208. // plural translations are also separated by \0
  209. $entry->translations = explode(chr(0), $translation);
  210. return $entry;
  211. }
  212. function select_plural_form($count) {
  213. return $this->gettext_select_plural_form($count);
  214. }
  215. function get_plural_forms_count() {
  216. return $this->_nplurals;
  217. }
  218. }
  219. endif;