Commit | Line | Data |
---|---|---|
e3667539 MW |
1 | <?php |
2 | namespace wcf\system\worker; | |
3 | use wcf\data\like\Like; | |
2772d4eb MW |
4 | use wcf\data\user\avatar\UserAvatar; |
5 | use wcf\data\user\avatar\UserAvatarEditor; | |
6 | use wcf\data\user\avatar\UserAvatarList; | |
8a413435 | 7 | use wcf\data\user\User; |
e3667539 | 8 | use wcf\data\user\UserEditor; |
157054c9 | 9 | use wcf\data\user\UserList; |
e3667539 MW |
10 | use wcf\data\user\UserProfileAction; |
11 | use wcf\system\database\util\PreparedStatementConditionBuilder; | |
c5c9e424 | 12 | use wcf\system\exception\SystemException; |
b8c48208 | 13 | use wcf\system\html\input\HtmlInputProcessor; |
2772d4eb | 14 | use wcf\system\image\ImageHandler; |
e3667539 MW |
15 | use wcf\system\user\activity\point\UserActivityPointHandler; |
16 | use wcf\system\WCF; | |
17 | ||
18 | /** | |
19 | * Worker implementation for updating users. | |
20 | * | |
21 | * @author Marcel Werk | |
cea1798f | 22 | * @copyright 2001-2017 WoltLab GmbH |
e3667539 | 23 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
e71525e4 | 24 | * @package WoltLabSuite\Core\System\Worker |
bbd89532 MS |
25 | * |
26 | * @method UserList getObjectList() | |
e3667539 MW |
27 | */ |
28 | class UserRebuildDataWorker extends AbstractRebuildDataWorker { | |
29 | /** | |
0fcfe5f6 | 30 | * @inheritDoc |
e3667539 | 31 | */ |
157054c9 | 32 | protected $objectListClassName = UserList::class; |
e3667539 MW |
33 | |
34 | /** | |
0fcfe5f6 | 35 | * @inheritDoc |
e3667539 MW |
36 | */ |
37 | protected $limit = 50; | |
38 | ||
39 | /** | |
0fcfe5f6 | 40 | * @inheritDoc |
e3667539 MW |
41 | */ |
42 | protected function initObjectList() { | |
43 | parent::initObjectList(); | |
44 | ||
8a413435 AE |
45 | $this->objectList->sqlSelects = 'user_option_value.userOption' . User::getUserOptionID('aboutMe') . ' AS aboutMe'; |
46 | $this->objectList->sqlJoins = "LEFT JOIN wcf".WCF_N."_user_option_value user_option_value ON (user_option_value.userID = user_table.userID)"; | |
e3667539 MW |
47 | $this->objectList->sqlOrderBy = 'user_table.userID'; |
48 | } | |
49 | ||
50 | /** | |
0fcfe5f6 | 51 | * @inheritDoc |
e3667539 MW |
52 | */ |
53 | public function execute() { | |
54 | parent::execute(); | |
55 | ||
058cbd6a | 56 | $users = $userIDs = []; |
e3667539 MW |
57 | foreach ($this->getObjectList() as $user) { |
58 | $users[] = new UserEditor($user); | |
59 | $userIDs[] = $user->userID; | |
60 | } | |
61 | ||
62 | // update user ranks | |
63 | if (!empty($users)) { | |
64 | $action = new UserProfileAction($users, 'updateUserOnlineMarking'); | |
65 | $action->executeAction(); | |
66 | } | |
67 | ||
68 | if (!empty($userIDs)) { | |
69 | // update activity points | |
70 | UserActivityPointHandler::getInstance()->updateUsers($userIDs); | |
71 | ||
72 | // update like counter | |
73 | if (MODULE_LIKE) { | |
74 | $conditionBuilder = new PreparedStatementConditionBuilder(); | |
058cbd6a | 75 | $conditionBuilder->add('user_table.userID IN (?)', [$userIDs]); |
e3667539 MW |
76 | $sql = "UPDATE wcf".WCF_N."_user user_table |
77 | SET likesReceived = ( | |
78 | SELECT COUNT(*) | |
79 | FROM wcf".WCF_N."_like | |
80 | WHERE objectUserID = user_table.userID | |
1e5b71d4 | 81 | AND likeValue = ".Like::LIKE." |
e3667539 MW |
82 | ) |
83 | ".$conditionBuilder; | |
84 | $statement = WCF::getDB()->prepareStatement($sql); | |
85 | $statement->execute($conditionBuilder->getParameters()); | |
86 | } | |
2772d4eb | 87 | |
85f59bb3 JR |
88 | // update trophy points |
89 | if (MODULE_TROPHY) { | |
90 | $conditionBuilder = new PreparedStatementConditionBuilder(); | |
91 | $conditionBuilder->add('user_table.userID IN (?)', [$userIDs]); | |
92 | $sql = "UPDATE wcf".WCF_N."_user user_table | |
93 | SET trophyPoints = ( | |
94 | SELECT COUNT(*) | |
95 | FROM wcf".WCF_N."_user_trophy user_trophy | |
96 | LEFT JOIN wcf".WCF_N."_trophy trophy ON user_trophy.trophyID = trophy.trophyID | |
97 | LEFT JOIN wcf".WCF_N."_category trophy_category ON trophy.categoryID = trophy_category.categoryID | |
98 | WHERE user_trophy.userID = user_table.userID | |
99 | AND trophy.isDisabled = 0 | |
100 | AND trophy_category.isDisabled = 0 | |
101 | ) | |
102 | ".$conditionBuilder; | |
103 | $statement = WCF::getDB()->prepareStatement($sql); | |
104 | $statement->execute($conditionBuilder->getParameters()); | |
105 | } | |
106 | ||
8a413435 AE |
107 | // update signatures and about me |
108 | $sql = "UPDATE wcf".WCF_N."_user_option_value | |
109 | SET userOption" . User::getUserOptionID('aboutMe') . " = ? | |
110 | WHERE userID = ?"; | |
111 | $statement = WCF::getDB()->prepareStatement($sql); | |
112 | ||
b8c48208 AE |
113 | $htmlInputProcessor = new HtmlInputProcessor(); |
114 | WCF::getDB()->beginTransaction(); | |
115 | /** @var UserEditor $user */ | |
116 | foreach ($users as $user) { | |
117 | if (!$user->signatureEnableHtml) { | |
118 | $htmlInputProcessor->process($user->signature, 'com.woltlab.wcf.user.signature', $user->userID, true); | |
119 | ||
120 | $user->update([ | |
121 | 'signature' => $htmlInputProcessor->getHtml(), | |
122 | 'signatureEnableHtml' => 1 | |
123 | ]); | |
8a413435 AE |
124 | |
125 | if ($user->aboutMe) { | |
126 | $htmlInputProcessor->process($user->aboutMe, 'com.woltlab.wcf.user.aboutMe', $user->userID, true); | |
1c22c458 AE |
127 | $html = $htmlInputProcessor->getHtml(); |
128 | // MySQL's TEXT type allows for 65,535 bytes, hence we need to count | |
129 | // the bytes rather than the actual amount of characters | |
130 | if (strlen($html) > 65535) { | |
131 | // content does not fit the available space, and any | |
132 | // attempts to truncate it will yield awkward results | |
133 | $html = ''; | |
134 | } | |
135 | ||
8a413435 | 136 | $statement->execute([ |
1c22c458 | 137 | $html, |
8a413435 AE |
138 | $user->userID |
139 | ]); | |
140 | } | |
b8c48208 AE |
141 | } |
142 | } | |
143 | WCF::getDB()->commitTransaction(); | |
144 | ||
bbabc452 | 145 | // update old/imported avatars |
2772d4eb MW |
146 | $avatarList = new UserAvatarList(); |
147 | $avatarList->getConditionBuilder()->add('user_avatar.userID IN (?)', [$userIDs]); | |
148 | $avatarList->getConditionBuilder()->add('(user_avatar.width <> ? OR user_avatar.height <> ?)', [UserAvatar::AVATAR_SIZE, UserAvatar::AVATAR_SIZE]); | |
149 | $avatarList->readObjects(); | |
150 | foreach ($avatarList as $avatar) { | |
1fe8a30f AE |
151 | $editor = new UserAvatarEditor($avatar); |
152 | if (!file_exists($avatar->getLocation()) || @getimagesize($avatar->getLocation()) === false) { | |
153 | // delete avatars that are missing or broken | |
154 | $editor->delete(); | |
155 | continue; | |
156 | } | |
157 | ||
2772d4eb MW |
158 | $width = $avatar->width; |
159 | $height = $avatar->height; | |
160 | if ($width != $height) { | |
bbabc452 | 161 | // make avatar quadratic |
2772d4eb MW |
162 | $width = $height = min($width, $height, UserAvatar::AVATAR_SIZE); |
163 | $adapter = ImageHandler::getInstance()->getAdapter(); | |
c5c9e424 AE |
164 | |
165 | try { | |
166 | $adapter->loadFile($avatar->getLocation()); | |
167 | } | |
168 | catch (SystemException $e) { | |
169 | // broken image | |
170 | $editor->delete(); | |
171 | continue; | |
172 | } | |
173 | ||
2772d4eb MW |
174 | $thumbnail = $adapter->createThumbnail($width, $height, false); |
175 | $adapter->writeImage($thumbnail, $avatar->getLocation()); | |
176 | } | |
177 | ||
bbabc452 MW |
178 | if ($width != UserAvatar::AVATAR_SIZE || $height != UserAvatar::AVATAR_SIZE) { |
179 | // resize avatar | |
3461e167 | 180 | $adapter = ImageHandler::getInstance()->getAdapter(); |
c5c9e424 AE |
181 | |
182 | try { | |
183 | $adapter->loadFile($avatar->getLocation()); | |
184 | } | |
185 | catch (SystemException $e) { | |
186 | // broken image | |
187 | $editor->delete(); | |
188 | continue; | |
189 | } | |
190 | ||
3461e167 MW |
191 | $adapter->resize(0, 0, $width, $height, UserAvatar::AVATAR_SIZE, UserAvatar::AVATAR_SIZE); |
192 | $adapter->writeImage($adapter->getImage(), $avatar->getLocation()); | |
193 | $width = $height = UserAvatar::AVATAR_SIZE; | |
2772d4eb MW |
194 | } |
195 | ||
2772d4eb | 196 | $editor->update([ |
3461e167 MW |
197 | 'width' => $width, |
198 | 'height' => $height | |
2772d4eb MW |
199 | ]); |
200 | } | |
e3667539 MW |
201 | } |
202 | } | |
203 | } |