<?php


/**
 * Improved MO file reader for Wordpress.
 *
 * @author Thomas Urban <thomas.urban@toxa.de>
 */


require_once dirname(__FILE__) . '/translations.php';


class MO extends Translations
{

	protected $nplurals = 2;



	public function __construct( $headers = array(), $hash = array() )
	{

		$this->headers = $headers;
		$this->entries = $hash;

		if ( $this->headers['Plural-Forms'] )
		{

			$temp = strtok( $this->headers['Plural-Forms'], ';' );
			if ( preg_match( '/^nplurals=(\d+)$/i', trim( $temp ), $matches ) )
				$this->nplurals = intval( $matches[1] );

		}
	}


	/**
	 * Reads provided MO file.
	 *
	 * @param string $filename name of MO file to read
	 * @return MOX
	 */

	public function import_from_file( $filename )
	{

		/*
		 * check cache for existing record first
		 */

		// currently defunct due to internal WP_Object_Cache not saving anything
		$set = wp_cache_get( $filename, 'l10n' );
		if ( is_array( $set ) )
		{

			list( $this->headers, $this->entries ) = $set;

			return true;

		}



		/**
		 * read header from file
		 */

		$file = fopen( $filename, 'r' );
		if ( !$file )
			return false;

		$header = fread( $file, 28 );
		if ( strlen( $header ) != 28 )
			return false;

		// detect endianess
		$endian = unpack( 'Nendian', substr( $header, 0, 4 ) );
		if ( $endian['endian'] == intval( hexdec( '950412de' ) ) )
			$endian = 'N';
		else if ( $endian['endian'] == intval( hexdec( 'de120495' ) ) )
			$endian = 'V';
		else
			return false;

		// parse header
		$header = unpack( "{$endian}Hrevision/{$endian}Hcount/{$endian}HposOriginals/{$endian}HposTranslations/{$endian}HsizeHash/{$endian}HposHash", substr( $header, 4 ) );
		if ( !is_array( $header ) )
			return false;

		extract( $header );

		// support revision 0 of MO format specs, only
		if ( $Hrevision != 0 )
			return false;



		/*
		 * read index tables on originals and translations
		 */

		// seek to data blocks
		fseek( $file, $HposOriginals, SEEK_SET );

		// read originals' indices
		$HsizeOriginals = $HposTranslations - $HposOriginals;
		if ( $HsizeOriginals != $Hcount * 8 )
			return false;

		$originals = fread( $file, $HsizeOriginals );
		if ( strlen( $originals ) != $HsizeOriginals )
			return false;

		// read translations' indices
		$HsizeTranslations = $HposHash - $HposTranslations;
		if ( $HsizeTranslations != $Hcount * 8 )
			return false;

		$translations = fread( $file, $HsizeTranslations );
		if ( strlen( $translations ) != $HsizeTranslations )
			return false;

		// transform raw data into set of indices
		$originals    = str_split( $originals, 8 );
		$translations = str_split( $translations, 8 );



		/*
		 * read set of strings to separate string
		 */

		// skip hash table
		$HposStrings = $HposHash + $HsizeHash * 4;

		fseek( $file, $HposStrings, SEEK_SET );

		// read strings expected in rest of file
		$strings = '';
		while ( !feof( $file ) )
			$strings .= fread( $file, 4096 );

		fclose( $file );



		// collect hash records
		$hash = $header = array();

		for ( $i = 0; $i < $Hcount; $i++ )
		{

			// parse index records on original and related translation
			$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'] -= $HposStrings;
			$t['pos'] -= $HposStrings;

			// extract original and translations
			$original    = substr( $strings, $o['pos'], $o['length'] );
			$translation = substr( $strings, $t['pos'], $t['length'] );



			if ( $original === '' )
			{
				// got header --> store separately

				$header = array();

				foreach ( explode( "\n", $translation ) as $line )
				{

					$sep = strpos( $line, ':' );
					if ( $sep !== false )
						$header[trim(substr( $line, 0, $sep ))] = trim( substr( $line, $sep + 1 ));

				}
			}
			else
			{

				// detect context in original
				$sep = strpos( $original, "\04" );
				if ( $sep !== false )
				{
					$context  = substr( $original, 0, $sep );
					$original = substr( $original, $sep + 1 );
				}
				else
					$context  = null;


				$original     = explode( "\00", $original );
				$translation  = explode( "\00", $translation );

				$singularFrom = array_shift( $original );
				$singularTo   = array_shift( $translation );

				if ( count( $original ) && ( count( $original ) == count( $translation ) ) )
					$plurals = array_combine( $original, $translation );
				else
					$plurals = array();


				$record = array(
								'context'     => $context,
								'singular'    => $singularFrom,
								'translation' => $singularTo,
								'plurals'     => $plurals,
								);

				$key = is_null( $context ) ? $singularFrom
										   : "$context\04$singularFrom";


				$hash[$key] = $record;

			}
		}


		$this->headers = $header;
		$this->entries = $hash;



		/*
		 * write result to cache
		 */

		// currently defunct due to internal WP_Object_Cache not saving anything
		wp_cache_set( $filename, array( $header, $hash ), 'l10n' );



		return true;

	}


	/**
	 * Retrieves translation of single entry.
	 *
	 * The provided entry is constructed manually and thus providing proper
	 * key for lookup. The method returns a matching entry from internal pool
	 * of translations.
	 *
	 * NOTE! Passing by reference is required as long as parent class is using
	 *       it (obviously due to keeping things compatible with PHP4).
	 *
	 * @param Translate_Entry $entry entry to look up
	 * @return Translate_Entry resulting entry
	 */

	public function translate_entry( &$entry )
	{

		if ( !isset( $this->entries[$entry->key()] ) )
			return false;

		if ( is_array( $this->entries[$entry->key()] ) )
		{
			// convert entry to instance of Translate_Entry on demand

			// use internally managed record
			$args = $this->entries[$entry->key()];

			// add structures required by Translation_Entry
			$args['translations'] = array( $args['translation'] );
			if ( count( $args['plurals'] ) )
				$args['translations'] = array_merge( $args['translations'], array_values( $args['plurals'] ) );

			if ( count( $args['plurals'] ) )
				$args['plural'] = array_shift( array_keys( $args['plurals'] ) );

			// temporarily transform entry
			return new Translation_Entry( $args );

		}

		if ( $this->entries[$entry->key()] instanceof Translation_Entry )
			return $this->entries[$entry->key()];

		return false;

	}


	public function translate( $singular, $context = null )
	{

		$key = $this->key( $singular, $context );

		if ( isset( $this->entries[$key] ) )
			if ( is_string( $this->entries[$key]['translation'] ) )
				return $this->entries[$key]['translation'];

		return $singular;

	}


	public function translate_plural( $singular, $plural, $count, $context = null )
	{

		$key = $this->key( $singular, $context );

		$translated = $this->entries[$key];
		if ( is_array( $translated ) )
		{

			$index              = $this->select_plural_form( $count );
			$total_plural_forms = $this->get_plural_forms_count();

			if ( ( $index >= 0 ) && ( $index < $total_plural_forms ) )
			{

				if ( ( $index == 0 ) && is_string( $translated['translation'] ) )
					return $translated['translation'];

				if ( count( $translated['plurals'] ) == $total_plural_forms - 1 )
				{
					$plurals = array_values( $translated['plurals'] );
					return $plurals[$index-1];
				}
			}


			return ( $count == 1 ) ? $singular : $plural;

		}


		// keep class compatible with old-style entry class
		return parent::translate_plural( $singular, $plural, $count, $context );

	}


	public function key( $singular, $context = null )
	{
		return is_null( $context ) ? $singular : "$context\04$singular";
	}


	public function get_plural_forms_count()
	{
		return $this->nplurals;
	}


	public function select_plural_form( $count )
	{
		return $this->gettext_select_plural_form( $count );
	}
}


?>