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