<commit_before><?php

class uixul_PropertyGridBindingService extends BaseService
{
	/**
	 * Singleton
	 * @var uixul_PropertyGridBindingService
	 */
	private static $instance = null;

	/**
	 * @return uixul_PropertyGridBindingService
	 */
	public static function getInstance()
	{
		if (is_null(self::$instance))
		{
			self::$instance = self::getServiceClassInstance(get_class());
		}
		return self::$instance;
	}

	/**
	 * @param string $moduleName
	 * @param string $blockName
	 * @return string
	 */
	public function getBinding($moduleName, $blockName)
	{
		if (headers_sent() === false)
		{
			header('Content-type: text/xml');
		}

		$configDocument = $this->getConfig($moduleName, $blockName);

		$binding = $this->buildPropertyGridBinding($configDocument);
		$xpath = $this->getBindingXPath($binding);
		$bindingNodes = $xpath->query('//xbl:binding[@extends]');
		foreach ($bindingNodes as $bindingNode)
		{
			$extend = uixul_lib_BindingObject::getUrl($bindingNode->getAttribute("extends"));
			$bindingNode->setAttribute("extends", $extend);
		}

		$binding->formatOutput = true;
		$tr = new f_util_TagReplacer();
		$tr->setReplacement('HttpHost', Framework::getUIBaseUrl());
		$tr->setReplacement('IconsBase', MediaHelper::getIconBaseUrl());
		return $tr->run($binding->saveXML(), true);
	}

	private function getConfig($moduleName, $blockName)
	{
		$bi = block_BlockService::getInstance()->getBlockInfo($blockName);
		
		$configDocument = f_util_DOMUtils::newDocument();
		$configPaths = FileResolver::getInstance()->setPackageName('modules_'.$moduleName)
						->setDirectory('config')
						->getPaths('blocks.xml');

		if (f_util_ArrayUtils::isNotEmpty($configPaths))
		{
			foreach (array_reverse($configPaths) as $configPath)
			{
				Framework::fatal($configPath);
				$document = f_util_DOMUtils::fromPath($configPath);
				$blockElem = $document->findUnique('//block[@type="'. $blockName .'"]');
				if ($blockElem !== null)
				{
					$this->appendConfig($configDocument, $blockElem, $bi);
				}
			}
		}
		
		if ($bi->getInjectedBy())
		{
			$injectedType = $bi->getInjectedBy();
			list( ,$moduleName, ) = explode('_', $injectedType);
			$configPaths = FileResolver::getInstance()->setPackageName('modules_' . $moduleName)
						->setDirectory('config')
						->getPaths('blocks.xml');
			if (f_util_ArrayUtils::isNotEmpty($configPaths))
			{
				foreach (array_reverse($configPaths) as $configPath)
				{
					Framework::fatal($configPath);
					
					$document = f_util_DOMUtils::fromPath($configPath);
					$blockElem = $document->findUnique('//block[@type="'. $injectedType .'"]');
					if ($blockElem !== null)
					{
						$this->appendConfig($configDocument, $blockElem, $bi);
					}	
				}	
			}
		}
		
		$this->appendMetaParameter($configDocument, $bi);

		return $configDocument;
	}
	
	/**
	 * 
	 * @param f_util_DOMDocument $configDocument
	 * @param DOMElement $blockElem
	 * @param block_BlockInfo $blockInfo
	 */
	private function appendConfig($configDocument, $blockElem, $blockInfo)
	{
		$document = $blockElem->ownerDocument;
		if ($configDocument->documentElement !== null)
		{
			// Import parameters
			$parameters = $document->find("parameters/parameter", $blockElem);
			if ($parameters->length > 0)
			{
				$parametersElem = $configDocument->createIfNotExists("parameters", $configDocument->documentElement);
				foreach ($parameters as $parameter)
				{
					$newParamElem = $configDocument->importNode($parameter, true);
					$oldParamElem = $configDocument->findUnique("parameter[@name = '".$newParamElem->getAttribute("name")."']", $parametersElem);
					if ($oldParamElem !== null)
					{
						$parametersElem->replaceChild($newParamElem, $oldParamElem);	
					}
					else
					{
						$parametersElem->appendChild($newParamElem);
					}
				}
			}
			
			// Import XUL
			$javascript = $document->findUnique("xul/javascript", $blockElem);
			if ($javascript !== null)
			{
				$configJsElem = $configDocument->createIfNotExists("xul/javascript", $configDocument->documentElement);
				foreach ($javascript->childNodes as $jsChildNode)
				{
					if ($jsChildNode->nodeType == XML_ELEMENT_NODE)
					{
						$jsElem = $configDocument->importNode($jsChildNode, true);
						$jsXPath = $jsElem->tagName;
						if ($jsElem->hasAttribute("name"))
						{
							$jsXPath .= "[@name = '".$jsElem->getAttribute("name")."']";
						}
						$jsElemFromConfig = $configDocument->findUnique($jsXPath, $configJsElem);
						if ($jsElemFromConfig === null)
						{
							$configJsElem->appendChild($jsElem);
						}
						else
						{
							$configJsElem->replaceChild($jsElem, $jsElemFromConfig);
						}
					}
				}
			}
		}
		else
		{
			$configDocument->appendChild($configDocument->importNode($blockElem, true));
		}

		if (!$configDocument->documentElement->hasAttribute('icon'))
		{
			$configDocument->documentElement->setAttribute('icon', 'document');
		}
	}
	
	/**
	 * @param f_util_DOMDocument $configDocument
	 * @param block_BlockInfo $blockInfo
	 */
	private function appendMetaParameter($configDocument, $blockInfo)
	{
		if ($blockInfo && $blockInfo->hasMeta() && 
			!$configDocument->exists("parameters/parameter[@name = 'enablemetas']", $configDocument->documentElement))
		{
			$parametersElem = $configDocument->findUnique("parameters", $configDocument->documentElement);
			if ($parametersElem === null)
			{
				$parametersElem = $configDocument->createElement("parameters");
				$configDocument->documentElement->appendChild($parametersElem);
			}
			$parameterElem = $configDocument->createElement("parameter");
			// WARN: synchronize with block_BlockService::completeBlockInfoWithMetas()
			$parameterElem->setAttribute("name", "enablemetas");
			$parameterElem->setAttribute("type", "Boolean");
			$parameterElem->setAttribute("default-value", "true");
			$parameterElem->setAttribute("labeli18n", 'modules.website.bo.blocks.Enablemetas');
			$parameterElem->setAttribute("shorthelpi18n", 'modules.website.bo.blocks.Enablemetas-help');
			$parametersElem->appendChild($parameterElem);
		}			
	}
	<commit_msg>[NEW] use-cache auto parameter on property grid<commit_after><?php

class uixul_PropertyGridBindingService extends BaseService
{
	/**
	 * Singleton
	 * @var uixul_PropertyGridBindingService
	 */
	private static $instance = null;

	/**
	 * @return uixul_PropertyGridBindingService
	 */
	public static function getInstance()
	{
		if (is_null(self::$instance))
		{
			self::$instance = self::getServiceClassInstance(get_class());
		}
		return self::$instance;
	}

	/**
	 * @param string $moduleName
	 * @param string $blockName
	 * @return string
	 */
	public function getBinding($moduleName, $blockName)
	{
		if (headers_sent() === false)
		{
			header('Content-type: text/xml');
		}

		$configDocument = $this->getConfig($moduleName, $blockName);

		$binding = $this->buildPropertyGridBinding($configDocument);
		$xpath = $this->getBindingXPath($binding);
		$bindingNodes = $xpath->query('//xbl:binding[@extends]');
		foreach ($bindingNodes as $bindingNode)
		{
			$extend = uixul_lib_BindingObject::getUrl($bindingNode->getAttribute("extends"));
			$bindingNode->setAttribute("extends", $extend);
		}

		$binding->formatOutput = true;
		$tr = new f_util_TagReplacer();
		$tr->setReplacement('HttpHost', Framework::getUIBaseUrl());
		$tr->setReplacement('IconsBase', MediaHelper::getIconBaseUrl());
		return $tr->run($binding->saveXML(), true);
	}

	private function getConfig($moduleName, $blockName)
	{
		$bi = block_BlockService::getInstance()->getBlockInfo($blockName);
		
		$configDocument = f_util_DOMUtils::newDocument();
		$configPaths = FileResolver::getInstance()->setPackageName('modules_'.$moduleName)
						->setDirectory('config')
						->getPaths('blocks.xml');

		if (f_util_ArrayUtils::isNotEmpty($configPaths))
		{
			foreach (array_reverse($configPaths) as $configPath)
			{
				Framework::fatal($configPath);
				$document = f_util_DOMUtils::fromPath($configPath);
				$blockElem = $document->findUnique('//block[@type="'. $blockName .'"]');
				if ($blockElem !== null)
				{
					$this->appendConfig($configDocument, $blockElem, $bi);
				}
			}
		}
		
		if ($bi->getInjectedBy())
		{
			$injectedType = $bi->getInjectedBy();
			list( ,$moduleName, ) = explode('_', $injectedType);
			$configPaths = FileResolver::getInstance()->setPackageName('modules_' . $moduleName)
						->setDirectory('config')
						->getPaths('blocks.xml');
			if (f_util_ArrayUtils::isNotEmpty($configPaths))
			{
				foreach (array_reverse($configPaths) as $configPath)
				{
					Framework::fatal($configPath);
					
					$document = f_util_DOMUtils::fromPath($configPath);
					$blockElem = $document->findUnique('//block[@type="'. $injectedType .'"]');
					if ($blockElem !== null)
					{
						$this->appendConfig($configDocument, $blockElem, $bi);
					}	
				}	
			}
		}
		
		$this->appendMetaParameter($configDocument, $bi);
		
		$this->appendUseCache($configDocument, $bi);

		return $configDocument;
	}
	
	/**
	 * 
	 * @param f_util_DOMDocument $configDocument
	 * @param DOMElement $blockElem
	 * @param block_BlockInfo $blockInfo
	 */
	private function appendConfig($configDocument, $blockElem, $blockInfo)
	{
		$document = $blockElem->ownerDocument;
		if ($configDocument->documentElement !== null)
		{
			// Import parameters
			$parameters = $document->find("parameters/parameter", $blockElem);
			if ($parameters->length > 0)
			{
				$parametersElem = $configDocument->createIfNotExists("parameters", $configDocument->documentElement);
				foreach ($parameters as $parameter)
				{
					$newParamElem = $configDocument->importNode($parameter, true);
					$oldParamElem = $configDocument->findUnique("parameter[@name = '".$newParamElem->getAttribute("name")."']", $parametersElem);
					if ($oldParamElem !== null)
					{
						$parametersElem->replaceChild($newParamElem, $oldParamElem);	
					}
					else
					{
						$parametersElem->appendChild($newParamElem);
					}
				}
			}
			
			// Import XUL
			$javascript = $document->findUnique("xul/javascript", $blockElem);
			if ($javascript !== null)
			{
				$configJsElem = $configDocument->createIfNotExists("xul/javascript", $configDocument->documentElement);
				foreach ($javascript->childNodes as $jsChildNode)
				{
					if ($jsChildNode->nodeType == XML_ELEMENT_NODE)
					{
						$jsElem = $configDocument->importNode($jsChildNode, true);
						$jsXPath = $jsElem->tagName;
						if ($jsElem->hasAttribute("name"))
						{
							$jsXPath .= "[@name = '".$jsElem->getAttribute("name")."']";
						}
						$jsElemFromConfig = $configDocument->findUnique($jsXPath, $configJsElem);
						if ($jsElemFromConfig === null)
						{
							$configJsElem->appendChild($jsElem);
						}
						else
						{
							$configJsElem->replaceChild($jsElem, $jsElemFromConfig);
						}
					}
				}
			}
		}
		else
		{
			$configDocument->appendChild($configDocument->importNode($blockElem, true));
		}

		if (!$configDocument->documentElement->hasAttribute('icon'))
		{
			$configDocument->documentElement->setAttribute('icon', 'document');
		}
	}
	
	/**
	 * @param f_util_DOMDocument $configDocument
	 * @param block_BlockInfo $blockInfo
	 */
	private function appendMetaParameter($configDocument, $blockInfo)
	{
		if ($blockInfo && $blockInfo->hasMeta() && 
			!$configDocument->exists("parameters/parameter[@name = 'enablemetas']", $configDocument->documentElement))
		{
			$parametersElem = $configDocument->findUnique("parameters", $configDocument->documentElement);
			if ($parametersElem === null)
			{
				$parametersElem = $configDocument->createElement("parameters");
				$configDocument->documentElement->appendChild($parametersElem);
			}
			$parameterElem = $configDocument->createElement("parameter");
			// WARN: synchronize with block_BlockService::completeBlockInfoWithMetas()
			$parameterElem->setAttribute("name", "enablemetas");
			$parameterElem->setAttribute("type", "Boolean");
			$parameterElem->setAttribute("default-value", "true");
			$parameterElem->setAttribute("labeli18n", 'modules.website.bo.blocks.Enablemetas');
			$parameterElem->setAttribute("shorthelpi18n", 'modules.website.bo.blocks.Enablemetas-help');
			$parametersElem->appendChild($parameterElem);
		}			
	}
	
	/**
	 * @param f_util_DOMDocument $configDocument
	 * @param block_BlockInfo $blockInfo
	 */
	private function appendUseCache($configDocument, $blockInfo)
	{
		if ($blockInfo && $configDocument->documentElement->getAttribute("cache") != "false")
		{
			$parametersElem = $configDocument->findUnique("parameters", $configDocument->documentElement);
			if ($parametersElem === null)
			{
				$parametersElem = $configDocument->createElement("parameters");
				$configDocument->documentElement->appendChild($parametersElem);
			}
			$parameterElem = $configDocument->createElement("parameter");
			// WARN: synchronize with block_BlockService::completeBlockInfoWithMetas()
			$parameterElem->setAttribute("name", "cusecache");
			$parameterElem->setAttribute("type", "Boolean");
			$parameterElem->setAttribute("default-value", "true");
			$parameterElem->setAttribute("labeli18n", 'modules.website.bo.blocks.Usecache');
			$parameterElem->setAttribute("shorthelpi18n", 'modules.website.bo.blocks.Usecache');
			$parametersElem->appendChild($parameterElem);
		}			
	}
	
{'fraction_non_alphanumeric': 0.16283438533213104, 'fraction_numerical': 0.0010519987977156598, 'mean_word_length': 4.506412908564336, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 158, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '2488352', 'n_tokens_mistral': 4830, 'n_tokens_neox': 3976, 'n_words': 588}