MO::import_from_reader

Advertisement

Syntax Syntax

MO::import_from_reader( POMO_FileReader $reader )

Parameters Parameters

$reader

(Required)

Return Return

(bool) True if the import was successful, otherwise false.

Source Source

File: wp-includes/pomo/mo.php

		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_lenghts_addr/{$endian}translations_lenghts_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_lenghts_addr'] );

			// Read originals' indices.
			$originals_lengths_length = $header['translations_lenghts_addr'] - $header['originals_lenghts_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_lenghts_length = $header['hash_addr'] - $header['translations_lenghts_addr'];
			if ( $translations_lenghts_length != $header['total'] * 8 ) {
				return false;
			}

			$translations = $reader->read( $translations_lenghts_length );
			if ( $reader->strlen( $translations ) != $translations_lenghts_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;
		}

Advertisement

Advertisement

Leave a Reply