mo.php 9.3 KB

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