<commit_before><?php
namespace wcf\system;
use wcf\util\StringUtil;

/**
 * Handles meta tags.
 * 
 * @author	<NAME>
 * @copyright	2001-2014 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	com.woltlab.wcf
 * @subpackage	system.message
 * @category	Community Framework
 */
class MetaTagHandler extends SingletonFactory implements \Countable, \Iterator {
	/**
	 * current iterator index
	 * @var	integer
	 */
	protected $index = 0;
	
	/**
	 * list of index to object relation
	 * @var	array<integer>
	 */
	protected $indexToObject = null;
	
	/**
	 * regex object
	 * @var	\wcf\system\Regex;
	 */
	protected $regex = null;
	
	/**
	 * list of meta tags
	 * @var	array
	 */
	protected $objects = array();
	
	/**
	 * @see	\wcf\system\SingletonFactory::init()
	 */
	protected function init() {
		$this->regex = new Regex('^https?://');
		
		// set default tags
		$this->addTag('description', 'description', WCF::getLanguage()->get(META_DESCRIPTION));
		$this->addTag('keywords', 'keywords', WCF::getLanguage()->get(META_KEYWORDS));
		$this->addTag('og:site_name', 'og:site_name', WCF::getLanguage()->get(PAGE_TITLE), true);
	}
	
	/**
	 * Adds or replaces a meta tag.
	 * 
	 * @param	string		$identifier
	 * @param	string		$name
	 * @param	string		$value
	 * @param	boolean		$isProperty
	 */
	public function addTag($identifier, $name, $value, $isProperty = false) {
		if (!$this->regex->match($value)) {
			$value = StringUtil::encodeHTML($value);
		}
		
		if (!isset($this->objects[$identifier])) {
			$this->indexToObject[] = $identifier;
		}
		
		$this->objects[$identifier] = array(
			'isProperty' => $isProperty,
			'name' => $name,
			'value' => $value
		);
		
		// replace description if Open Graph Protocol tag was given
		if ($name == 'og:description') {
			$this->objects['description']['value'] = $value;
		}		
	}
	
	/**
	 * Removes a meta tag.
	 * 
	 * @param	string		$identifier
	 */
	public function removeTag($identifier) {
		if (isset($this->objects[$identifier])) {
			unset($this->objects[$identifier]);
			
			$this->indexToObject = array_keys($this->objects);
		}
	}
	
	/**
	 * @see	\Countable::count()
	 */
	public function count() {
		return count($this->objects);
	}
	
	/**
	 * @see	\Iterator::current()
	 */
	public function current() {
		$tag = $this->objects[$this->indexToObject[$this->index]];
		
		return '<meta ' . ($tag['isProperty'] ? 'property' : 'name') . '="' . $tag['name'] . '" content="' . $tag['value'] . '" />';
	}
	
	/**
	 * CAUTION: This methods does not return the current iterator index,
	 * rather than the object key which maps to that index.
	 * 
	 * @see	\Iterator::key()
	 */
	public function key() {
		return $this->indexToObject[$this->index];
	}
	
	/**
	 * @see	\Iterator::next()
	 */
	public function next() {
		++$this->index;
	}
	
	/**
	 * @see	\Iterator::rewind()
	 */
	public function rewind() {
		$this->index = 0;
	}
	
	/**
	 * @see	\Iterator::valid()
	 */
	public function valid() {
		return isset($this->indexToObject[$this->index]);
	}
}
<commit_msg>Fixed broken/missing encoding of meta tag values<commit_after><?php
namespace wcf\system;
use wcf\util\StringUtil;

/**
 * Handles meta tags.
 * 
 * @author	<NAME>
 * @copyright	2001-2014 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	com.woltlab.wcf
 * @subpackage	system.message
 * @category	Community Framework
 */
class MetaTagHandler extends SingletonFactory implements \Countable, \Iterator {
	/**
	 * current iterator index
	 * @var	integer
	 */
	protected $index = 0;
	
	/**
	 * list of index to object relation
	 * @var	array<integer>
	 */
	protected $indexToObject = null;
	
	/**
	 * list of meta tags
	 * @var	array
	 */
	protected $objects = array();
	
	/**
	 * @see	\wcf\system\SingletonFactory::init()
	 */
	protected function init() {
		// set default tags
		$this->addTag('description', 'description', WCF::getLanguage()->get(META_DESCRIPTION));
		$this->addTag('keywords', 'keywords', WCF::getLanguage()->get(META_KEYWORDS));
		$this->addTag('og:site_name', 'og:site_name', WCF::getLanguage()->get(PAGE_TITLE), true);
	}
	
	/**
	 * Adds or replaces a meta tag.
	 * 
	 * @param	string		$identifier
	 * @param	string		$name
	 * @param	string		$value
	 * @param	boolean		$isProperty
	 */
	public function addTag($identifier, $name, $value, $isProperty = false) {
		if (!isset($this->objects[$identifier])) {
			$this->indexToObject[] = $identifier;
		}
		
		$this->objects[$identifier] = array(
			'isProperty' => $isProperty,
			'name' => $name,
			'value' => $value
		);
		
		// replace description if Open Graph Protocol tag was given
		if ($name == 'og:description') {
			$this->objects['description']['value'] = $value;
		}		
	}
	
	/**
	 * Removes a meta tag.
	 * 
	 * @param	string		$identifier
	 */
	public function removeTag($identifier) {
		if (isset($this->objects[$identifier])) {
			unset($this->objects[$identifier]);
			
			$this->indexToObject = array_keys($this->objects);
		}
	}
	
	/**
	 * @see	\Countable::count()
	 */
	public function count() {
		return count($this->objects);
	}
	
	/**
	 * @see	\Iterator::current()
	 */
	public function current() {
		$tag = $this->objects[$this->indexToObject[$this->index]];
		
		return '<meta ' . ($tag['isProperty'] ? 'property' : 'name') . '="' . $tag['name'] . '" content="' . StringUtil::encodeHTML($tag['value']) . '" />';
	}
	
	/**
	 * CAUTION: This methods does not return the current iterator index,
	 * rather than the object key which maps to that index.
	 * 
	 * @see	\Iterator::key()
	 */
	public function key() {
		return $this->indexToObject[$this->index];
	}
	
	/**
	 * @see	\Iterator::next()
	 */
	public function next() {
		++$this->index;
	}
	
	/**
	 * @see	\Iterator::rewind()
	 */
	public function rewind() {
		$this->index = 0;
	}
	
	/**
	 * @see	\Iterator::valid()
	 */
	public function valid() {
		return isset($this->indexToObject[$this->index]);
	}
}
{'fraction_non_alphanumeric': 0.18453075512585432, 'fraction_numerical': 0.003333888981496916, 'mean_word_length': 3.4280442804428044, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 68, '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': '5031087', 'n_tokens_mistral': 2226, 'n_tokens_neox': 1950, 'n_words': 452}