MO::import_from_reader()publicWP 1.0

Method of the class: MO{}

No Hooks.

Return

true|false. True if the import was successful, otherwise false.

Usage

$MO = new MO();
$MO->import_from_reader( $reader );
$reader(POMO_FileReader) (required)
-

MO::import_from_reader() code WP 6.6.2

public function import_from_reader( $reader ) {
	$endian_string = MO::get_byteorder( $reader->readint32() );
	if ( false === $endian_string ) {
		return false;
	}
	$reader->setEndian( $endian_string );

	$endian = ( 'big' === $endian_string ) ? 'N' : 'V';

	$header = $reader->read( 24 );
	if ( $reader->strlen( $header ) !== 24 ) {
		return false;
	}

	// Parse header.
	$header = unpack( "{$endian}revision/{$endian}total/{$endian}originals_lengths_addr/{$endian}translations_lengths_addr/{$endian}hash_length/{$endian}hash_addr", $header );
	if ( ! is_array( $header ) ) {
		return false;
	}

	// Support revision 0 of MO format specs, only.
	if ( 0 !== $header['revision'] ) {
		return false;
	}

	// Seek to data blocks.
	$reader->seekto( $header['originals_lengths_addr'] );

	// Read originals' indices.
	$originals_lengths_length = $header['translations_lengths_addr'] - $header['originals_lengths_addr'];
	if ( $originals_lengths_length !== $header['total'] * 8 ) {
		return false;
	}

	$originals = $reader->read( $originals_lengths_length );
	if ( $reader->strlen( $originals ) !== $originals_lengths_length ) {
		return false;
	}

	// Read translations' indices.
	$translations_lengths_length = $header['hash_addr'] - $header['translations_lengths_addr'];
	if ( $translations_lengths_length !== $header['total'] * 8 ) {
		return false;
	}

	$translations = $reader->read( $translations_lengths_length );
	if ( $reader->strlen( $translations ) !== $translations_lengths_length ) {
		return false;
	}

	// Transform raw data into set of indices.
	$originals    = $reader->str_split( $originals, 8 );
	$translations = $reader->str_split( $translations, 8 );

	// Skip hash table.
	$strings_addr = $header['hash_addr'] + $header['hash_length'] * 4;

	$reader->seekto( $strings_addr );

	$strings = $reader->read_all();
	$reader->close();

	for ( $i = 0; $i < $header['total']; $i++ ) {
		$o = unpack( "{$endian}length/{$endian}pos", $originals[ $i ] );
		$t = unpack( "{$endian}length/{$endian}pos", $translations[ $i ] );
		if ( ! $o || ! $t ) {
			return false;
		}

		// Adjust offset due to reading strings to separate space before.
		$o['pos'] -= $strings_addr;
		$t['pos'] -= $strings_addr;

		$original    = $reader->substr( $strings, $o['pos'], $o['length'] );
		$translation = $reader->substr( $strings, $t['pos'], $t['length'] );

		if ( '' === $original ) {
			$this->set_headers( $this->make_headers( $translation ) );
		} else {
			$entry                          = &$this->make_entry( $original, $translation );
			$this->entries[ $entry->key() ] = &$entry;
		}
	}
	return true;
}