<commit_before><?php
namespace wcf\util;
use wcf\data\user\group\UserGroup;
use wcf\system\application\ApplicationHandler;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\html\input\HtmlInputProcessor;
use wcf\system\Regex;
use wcf\system\WCF;

/**
 * Contains message-related functions.
 * 
 * @author	<NAME>
 * @copyright	2001-2019 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\Util
 */
class MessageUtil {
	/**
	 * Strips session links, html entities and \r\n from the given text.
	 * 
	 * @param	string		$text
	 * @return	string
	 */
	public static function stripCrap($text) {
		// strip session links, security tokens and access tokens
		$text = Regex::compile('(?<=\?|&)([st]=[a-f0-9]{40}|at=\d+-[a-f0-9]{40})')->replace($text, '');
		
		// convert html entities (utf-8)
		$text = Regex::compile('&#(3[2-9]|[4-9][0-9]|\d{3,5});')->replace($text, function ($matches) {
			return StringUtil::getCharacter(intval($matches[1]));
		});
		
		// unify new lines
		$text = StringUtil::unifyNewlines($text);
		
		// remove control characters
		$text = preg_replace('~[\x00-\x08\x0B-\x1F\x7F]~', '', $text);
		
		return $text;
	}
	
	/**
	 * Returns the mentioned users in the given text.
	 * 
	 * @param       HtmlInputProcessor      $htmlInputProcessor     html input processor instance
	 * @return      string[]                mentioned usernames
	 */<commit_msg>Use of UserIDs for the search for mentions<commit_after><?php
namespace wcf\util;
use wcf\data\user\group\UserGroup;
use wcf\system\application\ApplicationHandler;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\html\input\HtmlInputProcessor;
use wcf\system\Regex;
use wcf\system\WCF;

/**
 * Contains message-related functions.
 * 
 * @author	<NAME>
 * @copyright	2001-2019 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\Util
 */
class MessageUtil {
	/**
	 * Strips session links, html entities and \r\n from the given text.
	 * 
	 * @param	string		$text
	 * @return	string
	 */
	public static function stripCrap($text) {
		// strip session links, security tokens and access tokens
		$text = Regex::compile('(?<=\?|&)([st]=[a-f0-9]{40}|at=\d+-[a-f0-9]{40})')->replace($text, '');
		
		// convert html entities (utf-8)
		$text = Regex::compile('&#(3[2-9]|[4-9][0-9]|\d{3,5});')->replace($text, function ($matches) {
			return StringUtil::getCharacter(intval($matches[1]));
		});
		
		// unify new lines
		$text = StringUtil::unifyNewlines($text);
		
		// remove control characters
		$text = preg_replace('~[\x00-\x08\x0B-\x1F\x7F]~', '', $text);
		
		return $text;
	}
	
	/**
	 * Returns the mentioned users in the given text.
	 *
	 * @param       HtmlInputProcessor      $htmlInputProcessor     html input processor instance
	 * @return      int[]                   ids of the mentioned users
	 * @since       5.3
	 */
	public static function getMentionedUserIDs(HtmlInputProcessor $htmlInputProcessor) {
		$userIDs = [];
		$groups = [];
		
		$elements = $htmlInputProcessor->getHtmlInputNodeProcessor()->getDocument()->getElementsByTagName('woltlab-metacode');
		/** @var \DOMElement $element */
		foreach ($elements as $element) {
			$type = $element->getAttribute('data-name');
			if ($type !== 'user' && $type !== 'group') {
				continue;
			}
			
			if (DOMUtil::hasParent($element, 'woltlab-quote')) {
				// ignore mentions within quotes
				continue;
			}
			
			$attributes = $htmlInputProcessor->getHtmlInputNodeProcessor()->parseAttributes(
				$element->getAttribute('data-attributes')
			);
			
			if ($type === 'user') {
				if (!empty($attributes[0])) {
					$userIDs[] = $attributes[0];
				}
			}
			else if ($type === 'group' && WCF::getSession()->getPermission('user.message.canMentionGroups')) {
				if (!empty($attributes[0])) {
					$group = UserGroup::getGroupByID($attributes[0]);
					if ($group !== null && $group->canBeMentioned() && !isset($groups[$group->groupID])) {
						$groups[$group->groupID] = $group;
					}
				}
			}
		}
		
		$userIDs = array_unique($userIDs);
		if (!empty($groups)) {
			$conditions = new PreparedStatementConditionBuilder();
			$conditions->add('groupID IN (?)', [array_keys($groups)]);
			if (!empty($userIDs)) $conditions->add('userID NOT IN (?)', [$userIDs]);
			
			$sql = "SELECT  userID
				FROM    wcf".WCF_N."_user_to_group
				".$conditions;
			$statement = WCF::getDB()->prepareStatement($sql);
			$statement->execute($conditions->getParameters());
			while ($userID = $statement->fetchColumn()) {
				$userIDs[] = $userID;
			}
		}
		
		return $userIDs;
	}
	
	/**
	 * Returns the mentioned users in the given text.
	 * 
	 * @param       HtmlInputProcessor      $htmlInputProcessor     html input processor instance
	 * @return      string[]                mentioned usernames
	 * @deprecated  5.3
	 */
{'fraction_non_alphanumeric': 0.1687424668541583, 'fraction_numerical': 0.015267175572519083, 'mean_word_length': 3.522252497729337, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, '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': '6956265', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1581, 'n_words': 337}