<commit_before><?php

namespace WP_CLI;

/**
 * Base class for WP-CLI commands that deal with translations
 *
 * @package wp-cli
 */
abstract class CommandWithTranslation extends \WP_CLI_Command {

	protected $obj_type;

	protected $obj_fields = array(
		'language',
		'english_name',
		'native_name',
		'status',
		'update',
		'updated',
		);

	/**
	 * List all available languages.
	 *
	 * [--field=<field>]
	 * : Display the value of a single field
	 *
	 * [--<field>=<value>]
	 * : Filter results by key=value pairs.
	 *
	 * [--fields=<fields>]
	 * : Limit the output to specific fields.
	 *
	 * [--format=<format>]
	 * : Accepted values: table, csv, json. Default: table
	 *
	 * ## AVAILABLE FIELDS
	 *
	 * These fields will be displayed by default for each translation:
	 *
	 * * language
	 * * english_name
	 * * native_name
	 * * status
	 * * update
	 * * updated
	 *
	 * These fields are optionally available:
	 *
	 * * version
	 * * package
	 *
	 * ## EXAMPLES
	 *
	 *     # List language,english_name,status fields of available languages.
	 *     $ wp core language list --fields=language,english_name,status
	 *     +----------------+-------------------------+-------------+
	 *     | language       | english_name            | status      |
	 *     +----------------+-------------------------+-------------+
	 *     | ar             | Arabic                  | uninstalled |
	 *     | ary            | Moroccan Arabic         | uninstalled |
	 *     | az             | Azerbaijani             | uninstalled |
	 *
	 * @subcommand list
	 */
	public function list_( $args, $assoc_args ) {

		$translations = $this->get_all_languages();
		$available = $this->get_installed_languages();

		$updates = $this->get_translation_updates();

		$current_locale = get_locale();
		$translations = array_map( function( $translation ) use ( $available, $current_locale, $updates ) {
			$translation['status'] = ( in_array( $translation['language'], $available ) ) ? 'installed' : 'uninstalled';
			if ( $current_locale == $translation['language'] ) {
				$translation['status'] = 'active';
			}

			$update = wp_list_filter( $updates, array(
				'language' => $translation['language']
			) );
			if ( $update ) {
				$translation['update'] = 'available';
			} else {
				$translation['update'] = 'none';
			}

			return $translation;
		}, $translations );

		foreach( $translations as $key => $translation ) {

			$fields = array_keys( $translation );
			foreach( $fields as $field ) {
				if ( isset( $assoc_args[ $field ] ) && $assoc_args[ $field ] != $translation[ $field ] ) {
					unset( $translations[ $key ] );
				}
			}
		}

		$formatter = $this->get_formatter( $assoc_args );
		$formatter->display_items( $translations );

	}

	/**
	 * Callback to sort array by a 'language' key.
	 */
	protected function sort_translations_callback( $a, $b ) {
		return strnatcasecmp( $a['language'], $b['language'] );
	}

	/**
	 * Install a given language.
	 *
	 * Downloads the language pack from WordPress.org.
	 *
	 * <language>
	 * : Language code to install.
	 *
	 * [--activate]
	 * : If set, the language will be activated immediately after install.
	 *
	 * ## EXAMPLES
	 *
	 *     # Install the Japanese language.
	 *     $ wp core language install ja
	 *     Success: Language installed.
	 *
	 * @subcommand install
	 */
	public function install( $args, $assoc_args ) {

		list( $language_code ) = $args;

		$available = $this->get_installed_languages();

		if ( in_array( $language_code, $available ) ) {
			\WP_CLI::warning( "Language already installed." );
			exit;
		}

		$response = $this->download_language_pack( $language_code );
		if ( ! is_wp_error( $response ) ) {
			\WP_CLI::success( "Language installed." );

			if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'activate' ) ) {
				$this->activate( array( $language_code ), array() );
			}
		} else {
			\WP_CLI::error( $response );
		}

	}

	/**
	 * Update installed languages.
	 *
	 * Updates installed languages for core, plugins and themes.
	 *
	 * [--dry-run]
	 * : Preview which translations would be updated.
	 *
	 * ## EXAMPLES
	 *
	 *     $ wp core language update
	 *     Updating 'Japanese' translation for Akismet 3.1.11...
	 *     Downloading translation from https://downloads.wordpress.org/translation/plugin/akismet/3.1.11/ja.zip...<commit_msg>Don't exit in lang install if lang is already installed<commit_after><?php

namespace WP_CLI;

/**
 * Base class for WP-CLI commands that deal with translations
 *
 * @package wp-cli
 */
abstract class CommandWithTranslation extends \WP_CLI_Command {

	protected $obj_type;

	protected $obj_fields = array(
		'language',
		'english_name',
		'native_name',
		'status',
		'update',
		'updated',
		);

	/**
	 * List all available languages.
	 *
	 * [--field=<field>]
	 * : Display the value of a single field
	 *
	 * [--<field>=<value>]
	 * : Filter results by key=value pairs.
	 *
	 * [--fields=<fields>]
	 * : Limit the output to specific fields.
	 *
	 * [--format=<format>]
	 * : Accepted values: table, csv, json. Default: table
	 *
	 * ## AVAILABLE FIELDS
	 *
	 * These fields will be displayed by default for each translation:
	 *
	 * * language
	 * * english_name
	 * * native_name
	 * * status
	 * * update
	 * * updated
	 *
	 * These fields are optionally available:
	 *
	 * * version
	 * * package
	 *
	 * ## EXAMPLES
	 *
	 *     # List language,english_name,status fields of available languages.
	 *     $ wp core language list --fields=language,english_name,status
	 *     +----------------+-------------------------+-------------+
	 *     | language       | english_name            | status      |
	 *     +----------------+-------------------------+-------------+
	 *     | ar             | Arabic                  | uninstalled |
	 *     | ary            | Moroccan Arabic         | uninstalled |
	 *     | az             | Azerbaijani             | uninstalled |
	 *
	 * @subcommand list
	 */
	public function list_( $args, $assoc_args ) {

		$translations = $this->get_all_languages();
		$available = $this->get_installed_languages();

		$updates = $this->get_translation_updates();

		$current_locale = get_locale();
		$translations = array_map( function( $translation ) use ( $available, $current_locale, $updates ) {
			$translation['status'] = ( in_array( $translation['language'], $available ) ) ? 'installed' : 'uninstalled';
			if ( $current_locale == $translation['language'] ) {
				$translation['status'] = 'active';
			}

			$update = wp_list_filter( $updates, array(
				'language' => $translation['language']
			) );
			if ( $update ) {
				$translation['update'] = 'available';
			} else {
				$translation['update'] = 'none';
			}

			return $translation;
		}, $translations );

		foreach( $translations as $key => $translation ) {

			$fields = array_keys( $translation );
			foreach( $fields as $field ) {
				if ( isset( $assoc_args[ $field ] ) && $assoc_args[ $field ] != $translation[ $field ] ) {
					unset( $translations[ $key ] );
				}
			}
		}

		$formatter = $this->get_formatter( $assoc_args );
		$formatter->display_items( $translations );

	}

	/**
	 * Callback to sort array by a 'language' key.
	 */
	protected function sort_translations_callback( $a, $b ) {
		return strnatcasecmp( $a['language'], $b['language'] );
	}

	/**
	 * Install a given language.
	 *
	 * Downloads the language pack from WordPress.org.
	 *
	 * <language>
	 * : Language code to install.
	 *
	 * [--activate]
	 * : If set, the language will be activated immediately after install.
	 *
	 * ## EXAMPLES
	 *
	 *     # Install the Japanese language.
	 *     $ wp core language install ja
	 *     Success: Language installed.
	 *
	 * @subcommand install
	 */
	public function install( $args, $assoc_args ) {

		list( $language_code ) = $args;

		$available = $this->get_installed_languages();

		if ( in_array( $language_code, $available ) ) {
			\WP_CLI::warning( "Language '{$language_code}' already installed." );
			\WP_CLI::success( "Language already installed." );
		} else {
			$response = $this->download_language_pack( $language_code );

			if ( is_wp_error( $response ) ) {
				\WP_CLI::error( $response );
			} else {
				\WP_CLI::success( "Language installed." );
			}
		}

		if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'activate' ) ) {
			$this->activate( array( $language_code ), array() );
		}

	}

	/**
	 * Update installed languages.
	 *
	 * Updates installed languages for core, plugins and themes.
	 *
	 * [--dry-run]
	 * : Preview which translations would be updated.
	 *
	 * ## EXAMPLES
	 *
	 *     $ wp core language update
	 *     Updating 'Japanese' translation for Akismet 3.1.11...
	 *     Downloading translation from https://downloads.wordpress.org/translation/plugin/akismet/3.1.11/ja.zip...
{'fraction_non_alphanumeric': 0.16765578635014836, 'fraction_numerical': 0.0018260671079662178, 'mean_word_length': 2.582583810302535, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6944232', 'n_tokens_mistral': 3062, 'n_tokens_neox': 2628, 'n_words': 655}