PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wp-includes/pomo/mo.php

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