<commit_before><?php
/**
 * @author <NAME> <<EMAIL>>
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\console\controllers;

use Yii;
use yii\console\Controller;
use yii\console\Exception;
use yii\helpers\FileHelper;

/**
 * This command extracts messages to be translated from source files.
 * The extracted messages are saved as PHP message source files
 * under the specified directory.
 *
 * Usage:
 * 1. Create a configuration file using the 'message/config' command:
 *    yii message/config /path/to/myapp/messages/config.php
 * 2. Edit the created config file, adjusting it for your web application needs.
 * 3. Run the 'message/extract' extract, using created config:
 *    yii message /path/to/myapp/messages/config.php
 *
 * @author <NAME> <<EMAIL>>
 * @since 2.0
 */
class MessageController extends Controller
{
	/**
	 * @var string controller default action ID.
	 */
	public $defaultAction = 'extract';


	/**
	 * Creates a configuration file for the "extract" command.
	 *
	 * The generated configuration file contains detailed instructions on
	 * how to customize it to fit for your needs. After customization,
	 * you may use this configuration file with the "extract" command.
	 *
	 * @param string $filePath output file name.
	 * @throws Exception on failure.
	 */
	public function actionConfig($filePath)
	{
		if (file_exists($filePath)) {
			if (!$this->confirm("File '{$filePath}' already exists. Do you wish to overwrite it?")) {
				return;
			}
		}
		copy(Yii::getAlias('@yii/views/messageConfig.php'), $filePath);
		echo "Configuration file template created at '{$filePath}'.\n\n";
	}

	/**
	 * Extracts messages to be translated from source code.
	 *
	 * This command will search through source code files and extract
	 * messages that need to be translated in different languages.
	 *
	 * @param string $configFile the path of the configuration file.
	 * You may use the "yii message/config" command to generate
	 * this file and then customize it for your needs.
	 * @throws Exception on failure.
	 */
	public function actionExtract($configFile)
	{
		if (!is_file($configFile)) {
			throw new Exception("The configuration file does not exist: $configFile");
		}

		$config = array_merge([
			'translator' => 'Yii::t',
			'overwrite' => false,
			'removeUnused' => false,
			'sort' => false,
		], require($configFile));

		if (!isset($config['sourcePath'], $config['messagePath'], $config['languages'])) {
			throw new Exception('The configuration file must specify "sourcePath", "messagePath" and "languages".');
		}
		if (!is_dir($config['sourcePath'])) {
			throw new Exception("The source path {$config['sourcePath']} is not a valid directory.");
		}
		if (!is_dir($config['messagePath'])) {
			throw new Exception("The message path {$config['messagePath']} is not a valid directory.");
		}
		if (empty($config['languages'])) {
			throw new Exception("Languages cannot be empty.");
		}

		$files = FileHelper::findFiles(realpath($config['sourcePath']), $config);

		$messages = [];
		foreach ($files as $file) {
			$messages = array_merge_recursive($messages, $this->extractMessages($file, $config['translator']));
		}

		foreach ($config['languages'] as $language) {
			$dir = $config['messagePath'] . DIRECTORY_SEPARATOR . $language;
			if (!is_dir($dir)) {
				@mkdir($dir);
			}
			foreach ($messages as $category => $msgs) {
				$msgs = array_values(array_unique($msgs));
				$this->generateMessageFile($msgs, $dir . DIRECTORY_SEPARATOR . $category . '.php',
					$config['overwrite'],
					$config['removeUnused'],
					$config['sort']);
			}
		}
	}

	/**
	 * Extracts messages from a file
	 *
	 * @param string $fileName name of the file to extract messages from
	 * @param string $translator name of the function used to translate messages
	 * @return array
	 */
	protected function extractMessages($fileName, $translator)
	{
		echo "Extracting messages from $fileName...\n";
		$subject = file_get_contents($fileName);<commit_msg>Fixes #1332: generate subdirectories when needed during message extraction process.<commit_after><?php
/**
 * @author <NAME> <<EMAIL>>
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\console\controllers;

use Yii;
use yii\console\Controller;
use yii\console\Exception;
use yii\helpers\FileHelper;

/**
 * This command extracts messages to be translated from source files.
 * The extracted messages are saved as PHP message source files
 * under the specified directory.
 *
 * Usage:
 * 1. Create a configuration file using the 'message/config' command:
 *    yii message/config /path/to/myapp/messages/config.php
 * 2. Edit the created config file, adjusting it for your web application needs.
 * 3. Run the 'message/extract' extract, using created config:
 *    yii message /path/to/myapp/messages/config.php
 *
 * @author <NAME> <<EMAIL>>
 * @since 2.0
 */
class MessageController extends Controller
{
	/**
	 * @var string controller default action ID.
	 */
	public $defaultAction = 'extract';


	/**
	 * Creates a configuration file for the "extract" command.
	 *
	 * The generated configuration file contains detailed instructions on
	 * how to customize it to fit for your needs. After customization,
	 * you may use this configuration file with the "extract" command.
	 *
	 * @param string $filePath output file name.
	 * @throws Exception on failure.
	 */
	public function actionConfig($filePath)
	{
		if (file_exists($filePath)) {
			if (!$this->confirm("File '{$filePath}' already exists. Do you wish to overwrite it?")) {
				return;
			}
		}
		copy(Yii::getAlias('@yii/views/messageConfig.php'), $filePath);
		echo "Configuration file template created at '{$filePath}'.\n\n";
	}

	/**
	 * Extracts messages to be translated from source code.
	 *
	 * This command will search through source code files and extract
	 * messages that need to be translated in different languages.
	 *
	 * @param string $configFile the path of the configuration file.
	 * You may use the "yii message/config" command to generate
	 * this file and then customize it for your needs.
	 * @throws Exception on failure.
	 */
	public function actionExtract($configFile)
	{
		if (!is_file($configFile)) {
			throw new Exception("The configuration file does not exist: $configFile");
		}

		$config = array_merge([
			'translator' => 'Yii::t',
			'overwrite' => false,
			'removeUnused' => false,
			'sort' => false,
		], require($configFile));

		if (!isset($config['sourcePath'], $config['messagePath'], $config['languages'])) {
			throw new Exception('The configuration file must specify "sourcePath", "messagePath" and "languages".');
		}
		if (!is_dir($config['sourcePath'])) {
			throw new Exception("The source path {$config['sourcePath']} is not a valid directory.");
		}
		if (!is_dir($config['messagePath'])) {
			throw new Exception("The message path {$config['messagePath']} is not a valid directory.");
		}
		if (empty($config['languages'])) {
			throw new Exception("Languages cannot be empty.");
		}

		$files = FileHelper::findFiles(realpath($config['sourcePath']), $config);

		$messages = [];
		foreach ($files as $file) {
			$messages = array_merge_recursive($messages, $this->extractMessages($file, $config['translator']));
		}

		foreach ($config['languages'] as $language) {
			$dir = $config['messagePath'] . DIRECTORY_SEPARATOR . $language;
			if (!is_dir($dir)) {
				@mkdir($dir);
			}
			foreach ($messages as $category => $msgs) {
				$file = str_replace("\\", '/', "$dir/$category.php");
				$path = dirname($file);
				if (!is_dir($path)) {
					mkdir($path, 0755, true);
				}
				$msgs = array_values(array_unique($msgs));
				$this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort']);
			}
		}
	}

	/**
	 * Extracts messages from a file
	 *
	 * @param string $fileName name of the file to extract messages from
	 * @param string $translator name of the function used to translate messages
	 * @return array
	 */
	protected function extractMessages($fileName, $translator)
	{
		echo "Extracting messages from $fileName...\n";
		$subject = file_get_contents($fileName);
{'fraction_non_alphanumeric': 0.13835418671799807, 'fraction_numerical': 0.0031280076997112606, 'mean_word_length': 4.053495440729483, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '519919', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2389, 'n_words': 791}