<commit_before><?php
namespace wcf\data\language;
use wcf\data\language\category\LanguageCategory;
use wcf\data\language\category\LanguageCategoryEditor;
use wcf\data\language\item\LanguageItemEditor;
use wcf\data\language\item\LanguageItemList;
use wcf\data\DatabaseObjectEditor;
use wcf\data\IEditableCachedObject;
use wcf\system\cache\builder\LanguageCacheBuilder;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\SystemException;
use wcf\system\io\AtomicWriter;
use wcf\system\language\LanguageFactory;
use wcf\system\Regex;
use wcf\system\WCF;
use wcf\util\DirectoryUtil;
use wcf\util\FileUtil;
use wcf\util\StringUtil;
use wcf\util\XML;

/**
 * Provides functions to edit languages.
 * 
 * @author	<NAME>
 * @copyright	2001-2015 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	com.woltlab.wcf
 * @subpackage	data.language
 * @category	Community Framework
 */
class LanguageEditor extends DatabaseObjectEditor implements IEditableCachedObject {
	/**
	 * @see	\wcf\data\DatabaseObjectDecorator::$baseClass
	 */
	protected static $baseClass = 'wcf\data\language\Language';
	
	/**
	 * @see	\wcf\data\DatabaseObjectEditor::delete()
	 */
	public function delete() {
		parent::delete();
		
		self::deleteLanguageFiles($this->languageID);
	}
	
	/**
	 * Updates the language files for the given category.
	 * 
	 * @param	\wcf\data\language\category\LanguageCategory	$languageCategory
	 */
	public function updateCategory(LanguageCategory $languageCategory) {
		$this->writeLanguageFiles(array($languageCategory->languageCategoryID));
	}
	
	/**
	 * Write the languages files.
	 * 
	 * @param	array<integer>		$languageCategoryIDs
	 */
	protected function writeLanguageFiles(array $languageCategoryIDs) {
		$conditions = new PreparedStatementConditionBuilder();
		$conditions->add("languageID = ?", array($this->languageID));
		$conditions->add("languageCategoryID IN (?)", array($languageCategoryIDs));
		
		// get language items
		$sql = "SELECT	languageItem, languageItemValue, languageCustomItemValue,
				languageUseCustomValue, languageCategoryID
			FROM	wcf".WCF_N."_language_item
			".$conditions;
		$statement = WCF::getDB()->prepareStatement($sql);
		$statement->execute($conditions->getParameters());
		$items = array();
		while ($row = $statement->fetchArray()) {
			$languageCategoryID = $row['languageCategoryID'];
			if (!isset($items[$languageCategoryID])) {
				$items[$languageCategoryID] = array();
			}
			
			$items[$languageCategoryID][$row['languageItem']] = ($row['languageUseCustomValue']) ? $row['languageCustomItemValue'] : $row['languageItemValue'];
		}
		
		foreach ($items as $languageCategoryID => $languageItems) {
			$category = LanguageFactory::getInstance()->getCategoryByID($languageCategoryID);
			if ($category === null) {
				continue;
			}
			
			$filename = WCF_DIR.'language/'.$this->languageID.'_'.$category->languageCategory.'.php';
			$writer = new AtomicWriter($filename);
			
			$writer->write("<?php\n/**\n* WoltLab Community Framework\n* language: ".$this->languageCode."\n* encoding: UTF-8\n* category: ".$category->languageCategory."\n* generated at ".gmdate("r")."\n* \n* DO NOT EDIT THIS FILE\n*/\n");
			foreach ($languageItems as $languageItem => $languageItemValue) {
				$writer->write("\$this->items['".$languageItem."'] = '".str_replace(array("\\", "'"), array("\\\\", "\'"), $languageItemValue)."';\n");
				
				// compile dynamic language variables
				if ($category->languageCategory != 'wcf.global' && strpos($languageItemValue, '{') !== false) {
					$writer->write("\$this->dynamicItems['".$languageItem."'] = '");
					
					$output = LanguageFactory::getInstance()->getScriptingCompiler()->compileString($languageItem, $languageItemValue);
					$writer->write(str_replace("'", "\'", $output['template']));
					
					$writer->write("';\n");
				}
			}
			<commit_msg>Fixed issues with curly braces in i18n phrases<commit_after><?php
namespace wcf\data\language;
use wcf\data\language\category\LanguageCategory;
use wcf\data\language\category\LanguageCategoryEditor;
use wcf\data\language\item\LanguageItemEditor;
use wcf\data\language\item\LanguageItemList;
use wcf\data\DatabaseObjectEditor;
use wcf\data\IEditableCachedObject;
use wcf\system\cache\builder\LanguageCacheBuilder;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\SystemException;
use wcf\system\io\AtomicWriter;
use wcf\system\language\LanguageFactory;
use wcf\system\Regex;
use wcf\system\WCF;
use wcf\util\DirectoryUtil;
use wcf\util\FileUtil;
use wcf\util\StringUtil;
use wcf\util\XML;

/**
 * Provides functions to edit languages.
 * 
 * @author	<NAME>
 * @copyright	2001-2015 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	com.woltlab.wcf
 * @subpackage	data.language
 * @category	Community Framework
 */
class LanguageEditor extends DatabaseObjectEditor implements IEditableCachedObject {
	/**
	 * @see	\wcf\data\DatabaseObjectDecorator::$baseClass
	 */
	protected static $baseClass = 'wcf\data\language\Language';
	
	/**
	 * @see	\wcf\data\DatabaseObjectEditor::delete()
	 */
	public function delete() {
		parent::delete();
		
		self::deleteLanguageFiles($this->languageID);
	}
	
	/**
	 * Updates the language files for the given category.
	 * 
	 * @param	\wcf\data\language\category\LanguageCategory	$languageCategory
	 */
	public function updateCategory(LanguageCategory $languageCategory) {
		$this->writeLanguageFiles(array($languageCategory->languageCategoryID));
	}
	
	/**
	 * Write the languages files.
	 * 
	 * @param	array<integer>		$languageCategoryIDs
	 */
	protected function writeLanguageFiles(array $languageCategoryIDs) {
		$conditions = new PreparedStatementConditionBuilder();
		$conditions->add("languageID = ?", array($this->languageID));
		$conditions->add("languageCategoryID IN (?)", array($languageCategoryIDs));
		
		// get language items
		$sql = "SELECT	languageItem, languageItemValue, languageCustomItemValue,
				languageUseCustomValue, languageCategoryID
			FROM	wcf".WCF_N."_language_item
			".$conditions;
		$statement = WCF::getDB()->prepareStatement($sql);
		$statement->execute($conditions->getParameters());
		$items = array();
		while ($row = $statement->fetchArray()) {
			$languageCategoryID = $row['languageCategoryID'];
			if (!isset($items[$languageCategoryID])) {
				$items[$languageCategoryID] = array();
			}
			
			$items[$languageCategoryID][$row['languageItem']] = ($row['languageUseCustomValue']) ? $row['languageCustomItemValue'] : $row['languageItemValue'];
		}
		
		foreach ($items as $languageCategoryID => $languageItems) {
			$category = LanguageFactory::getInstance()->getCategoryByID($languageCategoryID);
			if ($category === null) {
				continue;
			}
			
			$filename = WCF_DIR.'language/'.$this->languageID.'_'.$category->languageCategory.'.php';
			$writer = new AtomicWriter($filename);
			
			$writer->write("<?php\n/**\n* WoltLab Community Framework\n* language: ".$this->languageCode."\n* encoding: UTF-8\n* category: ".$category->languageCategory."\n* generated at ".gmdate("r")."\n* \n* DO NOT EDIT THIS FILE\n*/\n");
			foreach ($languageItems as $languageItem => $languageItemValue) {
				$writer->write("\$this->items['".$languageItem."'] = '".str_replace(array("\\", "'"), array("\\\\", "\'"), $languageItemValue)."';\n");
				
				// compile dynamic language variables
				if ($category->languageCategory != 'wcf.global' && strpos($languageItemValue, '{') !== false) {
					try {
						$output = LanguageFactory::getInstance()->getScriptingCompiler()->compileString($languageItem, $languageItemValue);
						$writer->write("\$this->dynamicItems['".$languageItem."'] = '");
						$writer->write(str_replace("'", "\'", $output['template']));
						$writer->write("';\n");
					}
					catch (SystemException $e) {} // ignore compiler errors
				}
			}
			
{'fraction_non_alphanumeric': 0.16261892839258887, 'fraction_numerical': 0.0025037556334501754, 'mean_word_length': 4.909023668639053, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4507677', 'n_tokens_mistral': 2654, 'n_tokens_neox': 2352, 'n_words': 428}