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