<commit_before><?php
/**
 * seo_RedirectionService
 * @package modules.seo
 */
class seo_RedirectionService extends f_persistentdocument_DocumentService
{
	/**
	 * @var seo_RedirectionService
	 */
	private static $instance;

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

	/**
	 * @return seo_persistentdocument_redirection
	 */
	public function getNewDocumentInstance()
	{
		return $this->getNewDocumentInstanceByModelName('modules_seo/redirection');
	}

	/**
	 * Create a query based on 'modules_seo/redirection' model.
	 * Return document that are instance of modules_seo/redirection,
	 * including potential children.
	 * @return f_persistentdocument_criteria_Query
	 */
	public function createQuery()
	{
		return $this->pp->createQuery('modules_seo/redirection');
	}
	
	/**
	 * Create a query based on 'modules_seo/redirection' model.
	 * Only documents that are strictly instance of modules_seo/redirection
	 * (not children) will be retrieved
	 * @return f_persistentdocument_criteria_Query
	 */
	public function createStrictQuery()
	{
		return $this->pp->createQuery('modules_seo/redirection', false);
	}
	
	public function compileAll()
	{
		try 
		{
			$content = "# Rules for SEO redirections.\n" . $this->generateHtAccessRules();
			$apacheService = ApacheService::getInstance();
			$apacheService->generateSpecificConfFileForModule('seo', $content);
		} 
		catch (Exception $e) 
		{
			Framework::exception($e);
		}
	}
	
	/**
	 * @param website_persistentdocument_website $website
	 * @param string lang
	 * @param string $oldPath
	 * @return seo_persistentdocument_redirection or null
	 */
	public function getByOldpath($website, $lang, $oldPath)
	{
		return $this->createQuery()
			->add(Restrictions::eq('oldUrl', $oldPath))
			->add(Restrictions::eq('website', $website))
			->add(Restrictions::eq('websiteLang', $lang))
			->findUnique();
	}
	
	/**
	 * @return void
	 */
	private function generateHtAccessRules()
	{
		// Write redirections file.
		$data = array();
		$website = null;
		$rqc = RequestContext::getInstance();
		foreach ($this->createQuery()->add(Restrictions::published())->find() as $redirection)
		{
			if ($redirection instanceof seo_persistentdocument_redirection)
			{
				$website = $redirection->getWebsite();
				$lang = $redirection->getWebsiteLang();
				if (!$website->isLangAvailable($lang)) {continue;}
				$domain = preg_quote($website->getDomainForLang($lang));
				$data[] = 'RewriteCond %{SERVER_NAME} ^(www\.)?(' .$domain.')$ [NC]';
				
				$oldUrl = $redirection->getOldUrl();
				if ($oldUrl[0] == '/'){$oldUrl = substr($oldUrl, 1);}
				$newUrl = $redirection->getNewUrl();
				
				// For pages with variables, we need:
				// * a specific condition to check the query string.
				// * a '?' in trhe rewrite rule after the new URL to remove the query string.
				if (strpos($oldUrl, '?') !== false)
				{
					list($oldUrl, $queryString) = explode('?', $oldUrl);
					$data[] = 'RewriteCond %{QUERY_STRING} ^'.preg_quote($queryString).'$';
					$data[] = 'RewriteRule ^' . preg_quote($oldUrl) . '$ ' . $newUrl . ' [L,R=301]';
				}
				else 
				{
					$data[] = 'RewriteRule ^' . preg_quote($oldUrl) . '$ ' . $newUrl . ' [L,R=301]';
				}
			}
		}
		return join("\n", $data);
	}
	
	/**
	 * @param seo_persistentdocument_redirection $document
	 * @param Integer $parentNodeId Parent node ID where to save the document (optionnal => can be null !).
	 * @return void
	 */
	protected function preSave($document, $parentNodeId)
	{
		if ($document->getLabel() == null)<commit_msg>[FIX #68188] Redirection vers une ancre -> caractère '#' reste encodé<commit_after><?php
/**
 * seo_RedirectionService
 * @package modules.seo
 */
class seo_RedirectionService extends f_persistentdocument_DocumentService
{
	/**
	 * @var seo_RedirectionService
	 */
	private static $instance;

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

	/**
	 * @return seo_persistentdocument_redirection
	 */
	public function getNewDocumentInstance()
	{
		return $this->getNewDocumentInstanceByModelName('modules_seo/redirection');
	}

	/**
	 * Create a query based on 'modules_seo/redirection' model.
	 * Return document that are instance of modules_seo/redirection,
	 * including potential children.
	 * @return f_persistentdocument_criteria_Query
	 */
	public function createQuery()
	{
		return $this->pp->createQuery('modules_seo/redirection');
	}
	
	/**
	 * Create a query based on 'modules_seo/redirection' model.
	 * Only documents that are strictly instance of modules_seo/redirection
	 * (not children) will be retrieved
	 * @return f_persistentdocument_criteria_Query
	 */
	public function createStrictQuery()
	{
		return $this->pp->createQuery('modules_seo/redirection', false);
	}
	
	public function compileAll()
	{
		try 
		{
			$content = "# Rules for SEO redirections.\n" . $this->generateHtAccessRules();
			$apacheService = ApacheService::getInstance();
			$apacheService->generateSpecificConfFileForModule('seo', $content);
		} 
		catch (Exception $e) 
		{
			Framework::exception($e);
		}
	}
	
	/**
	 * @param website_persistentdocument_website $website
	 * @param string lang
	 * @param string $oldPath
	 * @return seo_persistentdocument_redirection or null
	 */
	public function getByOldpath($website, $lang, $oldPath)
	{
		return $this->createQuery()
			->add(Restrictions::eq('oldUrl', $oldPath))
			->add(Restrictions::eq('website', $website))
			->add(Restrictions::eq('websiteLang', $lang))
			->findUnique();
	}
	
	/**
	 * @return void
	 */
	private function generateHtAccessRules()
	{
		// Write redirections file.
		$data = array();
		$website = null;
		$rqc = RequestContext::getInstance();
		foreach ($this->createQuery()->add(Restrictions::published())->find() as $redirection)
		{
			if ($redirection instanceof seo_persistentdocument_redirection)
			{
				$website = $redirection->getWebsite();
				$lang = $redirection->getWebsiteLang();
				if (!$website->isLangAvailable($lang)) {continue;}
				$domain = preg_quote($website->getDomainForLang($lang));
				$data[] = 'RewriteCond %{SERVER_NAME} ^(www\.)?(' .$domain.')$ [NC]';
				
				$oldUrl = $redirection->getOldUrl();
				if ($oldUrl[0] == '/'){$oldUrl = substr($oldUrl, 1);}
				$newUrl = $redirection->getNewUrl();
				
				// For pages with variables, we need:
				// * a specific condition to check the query string.
				// * a '?' in the rewrite rule after the new URL to remove the query string.
				if (strpos($oldUrl, '?') !== false)
				{
					list($oldUrl, $queryString) = explode('?', $oldUrl);
					$data[] = 'RewriteCond %{QUERY_STRING} ^'.preg_quote($queryString).'$';
					$data[] = 'RewriteRule ^' . preg_quote($oldUrl) . '$ ' . $newUrl . ' [L,NE,R=301]';
				}
				else 
				{
					$data[] = 'RewriteRule ^' . preg_quote($oldUrl) . '$ ' . $newUrl . ' [L,NE,R=301]';
				}
			}
		}
		return join("\n", $data);
	}
	
	/**
	 * @param seo_persistentdocument_redirection $document
	 * @param Integer $parentNodeId Parent node ID where to save the document (optionnal => can be null !).
	 * @return void
	 */
	protected function preSave($document, $parentNodeId)
	{
		if ($document->getLabel() == null)
{'fraction_non_alphanumeric': 0.15530860891685633, 'fraction_numerical': 0.0028116213683223993, 'mean_word_length': 4.0, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7137951', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2355, 'n_words': 519}