PageRenderTime 36ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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