wppaste
WordPress

MO::import_from_reader( POMO_FileReader $reader ): bool

Source
wp-includes/pomo/mo.php:222

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$readerPOMO_FileReader

Return value

bool
True if the import was successful, otherwise false.

Performance profile

How much work a call to MO::import_from_reader() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
10–135

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 189.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost MO::import_from_reader() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$endian_string === false10->readint32(), ::MO()
$endian_string !== false26–27->readint32(), ::MO(), ->setEndian(), ->read(), ->strlen()
$endian_string !== false45–49->readint32(), ::MO(), ->setEndian(), ->read(), ->strlen(), unpack()
$endian_string !== false && is_array($header) && $originals_lengths_length !== false60–61->readint32(), ::MO(), ->setEndian(), ->read(), ->strlen(), unpack(), ->seekto()
$endian_string !== false && is_array($header) && $originals_lengths_length === false69–77->readint32(), ::MO(), ->setEndian(), ->read(), ->strlen(), unpack(), ->seekto(), ->read(), ->strlen()
$endian_string !== false && is_array($header) && $originals_lengths_length === false85–86->readint32(), ::MO(), ->setEndian(), ->read(), ->strlen(), unpack(), ->seekto(), ->read(), ->strlen(), ->read(), ->strlen()
$endian_string !== false && is_array($header) && $originals_lengths_length === false && $translations_lengths_length === false && !$i112–113->readint32(), ::MO(), ->setEndian(), ->read(), ->strlen(), unpack(), ->seekto(), ->read(), ->strlen(), ->read(), ->strlen(), ->str_split(), ->str_split(), ->seekto(), ->read_all(), ->close()
$endian_string !== false && is_array($header) && $originals_lengths_length === false && $translations_lengths_length === false && $i133–135->readint32(), ::MO(), ->setEndian(), ->read(), ->strlen(), unpack(), ->seekto(), ->read(), ->strlen(), ->read(), ->strlen(), ->str_split(), ->str_split(), ->seekto(), ->read_all(), ->close(), unpack(), unpack()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 189 instructions, 10–135 executed per call, 13 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 4

Used by · 1

Source code

		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 );

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/pomo/mo.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.