| 1 |
<?php |
| 2 |
// Backward compatibility for WP versions older than 3.3, in which the method MO::export does not exist |
| 3 |
// FIXME to be removed once WP 3.2.1 and older versions is no more supported |
| 4 |
|
| 5 |
function pll_mo_export($mo) { |
| 6 |
$tmp_fh = fopen("php://temp", 'r+'); |
| 7 |
if ( !$tmp_fh ) return false; |
| 8 |
pll_mo_export_to_file_handle( $mo, $tmp_fh ); |
| 9 |
rewind( $tmp_fh ); |
| 10 |
return stream_get_contents( $tmp_fh ); |
| 11 |
} |
| 12 |
|
| 13 |
function pll_mo_export_to_file_handle($mo, $fh) { |
| 14 |
$entries = array_filter($mo->entries, create_function('$e', 'return !empty($e->translations);')); |
| 15 |
ksort($entries); |
| 16 |
$magic = 0x950412de; |
| 17 |
$revision = 0; |
| 18 |
$total = count($entries) + 1; // all the headers are one entry |
| 19 |
$originals_lenghts_addr = 28; |
| 20 |
$translations_lenghts_addr = $originals_lenghts_addr + 8 * $total; |
| 21 |
$size_of_hash = 0; |
| 22 |
$hash_addr = $translations_lenghts_addr + 8 * $total; |
| 23 |
$current_addr = $hash_addr; |
| 24 |
fwrite($fh, pack('V*', $magic, $revision, $total, $originals_lenghts_addr, |
| 25 |
$translations_lenghts_addr, $size_of_hash, $hash_addr)); |
| 26 |
fseek($fh, $originals_lenghts_addr); |
| 27 |
|
| 28 |
// headers' msgid is an empty string |
| 29 |
fwrite($fh, pack('VV', 0, $current_addr)); |
| 30 |
$current_addr++; |
| 31 |
$originals_table = chr(0); |
| 32 |
|
| 33 |
foreach($entries as $entry) { |
| 34 |
$originals_table .= $mo->export_original($entry) . chr(0); |
| 35 |
$length = strlen($mo->export_original($entry)); |
| 36 |
fwrite($fh, pack('VV', $length, $current_addr)); |
| 37 |
$current_addr += $length + 1; // account for the NULL byte after |
| 38 |
} |
| 39 |
|
| 40 |
$exported_headers = $mo->export_headers(); |
| 41 |
fwrite($fh, pack('VV', strlen($exported_headers), $current_addr)); |
| 42 |
$current_addr += strlen($exported_headers) + 1; |
| 43 |
$translations_table = $exported_headers . chr(0); |
| 44 |
|
| 45 |
foreach($entries as $entry) { |
| 46 |
$translations_table .= $mo->export_translations($entry) . chr(0); |
| 47 |
$length = strlen($mo->export_translations($entry)); |
| 48 |
fwrite($fh, pack('VV', $length, $current_addr)); |
| 49 |
$current_addr += $length + 1; |
| 50 |
} |
| 51 |
|
| 52 |
fwrite($fh, $originals_table); |
| 53 |
fwrite($fh, $translations_table); |
| 54 |
return true; |
| 55 |
} |
| 56 |
?> |
| 57 |
|