<commit_before><?php
namespace wcf\system\worker;
use wcf\data\like\Like;
use wcf\data\user\avatar\UserAvatar;
use wcf\data\user\avatar\UserAvatarEditor;
use wcf\data\user\avatar\UserAvatarList;
use wcf\data\user\User;
use wcf\data\user\UserEditor;
use wcf\data\user\UserList;
use wcf\data\user\UserProfileAction;
use wcf\system\bbcode\BBCodeHandler;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\SystemException;
use wcf\system\html\input\HtmlInputProcessor;
use wcf\system\image\ImageHandler;
use wcf\system\user\activity\point\UserActivityPointHandler;
use wcf\system\WCF;

/**
 * Worker implementation for updating users.
 * 
 * @author	<NAME>
 * @copyright	2001-2018 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\System\Worker
 * 
 * @method	UserList	getObjectList()
 */
class UserRebuildDataWorker extends AbstractRebuildDataWorker {
	/**
	 * @inheritDoc
	 */
	protected $objectListClassName = UserList::class;
	
	/**
	 * @inheritDoc
	 */
	protected $limit = 50;
	
	/**
	 * @inheritDoc
	 */
	protected function initObjectList() {
		parent::initObjectList();
		
		$this->objectList->sqlSelects = 'user_option_value.userOption' . User::getUserOptionID('aboutMe') . ' AS aboutMe';
		$this->objectList->sqlJoins = "LEFT JOIN wcf".WCF_N."_user_option_value user_option_value ON (user_option_value.userID = user_table.userID)";
		$this->objectList->sqlOrderBy = 'user_table.userID';
	}
	
	/**
	 * @inheritDoc
	 */
	public function execute() {
		parent::execute();
		
		$users = $userIDs = [];
		foreach ($this->getObjectList() as $user) {
			$users[] = new UserEditor($user);
			$userIDs[] = $user->userID;
		}
		
		// update user ranks
		if (!empty($users)) {
			$action = new UserProfileAction($users, 'updateUserOnlineMarking');
			$action->executeAction();
		}
		
		if (!empty($userIDs)) {
			// update activity points
			UserActivityPointHandler::getInstance()->updateUsers($userIDs);
			
			// update article counter
			$conditionBuilder = new PreparedStatementConditionBuilder();
			$conditionBuilder->add('user_table.userID IN (?)', [$userIDs]);
			$sql = "UPDATE  wcf".WCF_N."_user user_table
				SET     articles = (
						SELECT  COUNT(*)
						FROM    wcf".WCF_N."_article
						WHERE   userID = user_table.userID
					)
				".$conditionBuilder;
			$statement = WCF::getDB()->prepareStatement($sql);
			$statement->execute($conditionBuilder->getParameters());
			
			// update like counter
			if (MODULE_LIKE) {
				$sql = "UPDATE	wcf".WCF_N."_user user_table
					SET	likesReceived = (
							SELECT	COUNT(*)
							FROM	wcf".WCF_N."_like
							WHERE	objectUserID = user_table.userID
								AND likeValue = ".Like::LIKE."
						)
					".$conditionBuilder;
				$statement = WCF::getDB()->prepareStatement($sql);
				$statement->execute($conditionBuilder->getParameters());
			}
			
			// update trophy points
			if (MODULE_TROPHY) {
				$sql = "UPDATE	wcf".WCF_N."_user user_table
					SET	trophyPoints = (
							SELECT		COUNT(*)
							FROM		wcf".WCF_N."_user_trophy user_trophy
							LEFT JOIN	wcf".WCF_N."_trophy trophy ON user_trophy.trophyID = trophy.trophyID
							LEFT JOIN	wcf".WCF_N."_category trophy_category ON trophy.categoryID = trophy_category.categoryID
							WHERE		user_trophy.userID = user_table.userID
									AND trophy.isDisabled = 0
									AND trophy_category.isDisabled = 0
						)
					".$conditionBuilder;
				$statement = WCF::getDB()->prepareStatement($sql);<commit_msg>Rebuild reaction stats on user rebuilding See #2508<commit_after><?php
namespace wcf\system\worker;
use wcf\data\reaction\type\ReactionType;
use wcf\data\reaction\type\ReactionTypeCache;
use wcf\data\user\avatar\UserAvatar;
use wcf\data\user\avatar\UserAvatarEditor;
use wcf\data\user\avatar\UserAvatarList;
use wcf\data\user\User;
use wcf\data\user\UserEditor;
use wcf\data\user\UserList;
use wcf\data\user\UserProfileAction;
use wcf\system\bbcode\BBCodeHandler;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\SystemException;
use wcf\system\html\input\HtmlInputProcessor;
use wcf\system\image\ImageHandler;
use wcf\system\user\activity\point\UserActivityPointHandler;
use wcf\system\WCF;

/**
 * Worker implementation for updating users.
 * 
 * @author	<NAME>
 * @copyright	2001-2018 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\System\Worker
 * 
 * @method	UserList	getObjectList()
 */
class UserRebuildDataWorker extends AbstractRebuildDataWorker {
	/**
	 * @inheritDoc
	 */
	protected $objectListClassName = UserList::class;
	
	/**
	 * @inheritDoc
	 */
	protected $limit = 50;
	
	/**
	 * @inheritDoc
	 */
	protected function initObjectList() {
		parent::initObjectList();
		
		$this->objectList->sqlSelects = 'user_option_value.userOption' . User::getUserOptionID('aboutMe') . ' AS aboutMe';
		$this->objectList->sqlJoins = "LEFT JOIN wcf".WCF_N."_user_option_value user_option_value ON (user_option_value.userID = user_table.userID)";
		$this->objectList->sqlOrderBy = 'user_table.userID';
	}
	
	/**
	 * @inheritDoc
	 */
	public function execute() {
		parent::execute();
		
		$users = $userIDs = [];
		foreach ($this->getObjectList() as $user) {
			$users[] = new UserEditor($user);
			$userIDs[] = $user->userID;
		}
		
		// update user ranks
		if (!empty($users)) {
			$action = new UserProfileAction($users, 'updateUserOnlineMarking');
			$action->executeAction();
		}
		
		if (!empty($userIDs)) {
			// update activity points
			UserActivityPointHandler::getInstance()->updateUsers($userIDs);
			
			// update article counter
			$conditionBuilder = new PreparedStatementConditionBuilder();
			$conditionBuilder->add('user_table.userID IN (?)', [$userIDs]);
			$sql = "UPDATE  wcf".WCF_N."_user user_table
				SET     articles = (
						SELECT  COUNT(*)
						FROM    wcf".WCF_N."_article
						WHERE   userID = user_table.userID
					)
				".$conditionBuilder;
			$statement = WCF::getDB()->prepareStatement($sql);
			$statement->execute($conditionBuilder->getParameters());
			
			// update like counter
			if (MODULE_LIKE) {
				$positiveReactionTypes = $negativeReactionTypes = $neutralReactionTypes = [];
				
				foreach (ReactionTypeCache::getInstance()->getEnabledReactionTypes() as $reactionType) {
					switch ($reactionType->type) {
						case ReactionType::REACTION_TYPE_POSITIVE: 
							$positiveReactionTypes[] = $reactionType->reactionTypeID;
							break;
						
						case ReactionType::REACTION_TYPE_NEGATIVE: 
							$negativeReactionTypes[] = $reactionType->reactionTypeID;
							break;
							
						case ReactionType::REACTION_TYPE_NEUTRAL:
							$neutralReactionTypes[] = $reactionType->reactionTypeID;
							break;
							
						default: 
							throw new \LogicException('Unreachable');
					}
				}
				
				
				$sql = "UPDATE	wcf".WCF_N."_user user_table SET";
					
				if (!empty($positiveReactionTypes)) {
					$sql .= " likesReceived = (
							SELECT	COUNT(*)
							FROM	wcf".WCF_N."_like
							WHERE	objectUserID = user_table.userID
								AND reactionTypeID IN (". implode(',', $positiveReactionTypes) .")
						), positiveReactionsReceived = (
							SELECT	COUNT(*)
							FROM	wcf".WCF_N."_like
							WHERE	objectUserID = user_table.userID
								AND reactionTypeID IN (". implode(',', $positiveReactionTypes) ."))";
				}
				else {
					$sql .= " likesReceived = 0, positiveReactionsReceived = 0";
				}
				
				if (!empty($negativeReactionTypes)) {
					$sql .= ", negativeReactionsReceived = (
							SELECT	COUNT(*)
							FROM	wcf".WCF_N."_like
							WHERE	objectUserID = user_table.userID
								AND reactionTypeID IN (". implode(',', $negativeReactionTypes) ."))";
				}
				else {
					$sql .= ", negativeReactionsReceived = 0";
				}
					
				if (!empty($neutralReactionTypes)) {
					$sql .= ", neutralReactionsReceived = (
							SELECT	COUNT(*)
							FROM	wcf".WCF_N."_like
							WHERE	objectUserID = user_table.userID
								AND reactionTypeID IN (". implode(',', $neutralReactionTypes) ."))";
				}
				else {
					$sql .= ", neutralReactionsReceived = 0";
				}
					
				$sql .= " ".$conditionBuilder;
				$statement = WCF::getDB()->prepareStatement($sql);
				$statement->execute($conditionBuilder->getParameters());
			}
			
			// update trophy points
			if (MODULE_TROPHY) {
				$sql = "UPDATE	wcf".WCF_N."_user user_table
					SET	trophyPoints = (
							SELECT		COUNT(*)
							FROM		wcf".WCF_N."_user_trophy user_trophy
							LEFT JOIN	wcf".WCF_N."_trophy trophy ON user_trophy.trophyID = trophy.trophyID
							LEFT JOIN	wcf".WCF_N."_category trophy_category ON trophy.categoryID = trophy_category.categoryID
							WHERE		user_trophy.userID = user_table.userID
									AND trophy.isDisabled = 0
									AND trophy_category.isDisabled = 0
						)
					".$conditionBuilder;
				$statement = WCF::getDB()->prepareStatement($sql);
{'fraction_non_alphanumeric': 0.13278561119129567, 'fraction_numerical': 0.003552792272676807, 'mean_word_length': 3.4571994062345373, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 48, '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': '1892370', 'n_tokens_mistral': 3653, 'n_tokens_neox': 2840, 'n_words': 527}