Skip to content

Commit a004ba7

Browse files
committed
Merge branch '6.2' into 62-experimental-upgrade
2 parents 221bfb6 + 8b43d47 commit a004ba7

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace wcf\data;
4+
5+
use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
6+
7+
/**
8+
* Trait for dbo collections with message embedded objects.
9+
*
10+
* @author Marcel Werk
11+
* @copyright 2001-2025 WoltLab GmbH
12+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13+
* @since 6.2
14+
*/
15+
trait TCollectionEmbeddedObjects
16+
{
17+
private bool $embeddedObjectsLoaded = false;
18+
19+
public function loadEmbeddedObjects(string $messageObjectType): void
20+
{
21+
if ($this->embeddedObjectsLoaded) {
22+
return;
23+
}
24+
25+
$this->embeddedObjectsLoaded = true;
26+
27+
$objectIDs = $this->getEmbeddedObjectIDs();
28+
if ($objectIDs === []) {
29+
return;
30+
}
31+
32+
MessageEmbeddedObjectManager::getInstance()->loadObjects(
33+
$messageObjectType,
34+
$objectIDs
35+
);
36+
}
37+
38+
/**
39+
* @return int[]
40+
*/
41+
private function getEmbeddedObjectIDs(): array
42+
{
43+
\assert($this instanceof DatabaseObjectCollection);
44+
45+
return \array_map(
46+
static fn($object) => $object->getObjectID(),
47+
\array_filter($this->getObjects(), fn($object) => $object->hasEmbeddedObjects === 1)
48+
);
49+
}
50+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace wcf\data;
4+
5+
use wcf\data\user\UserProfile;
6+
use wcf\system\cache\runtime\UserProfileRuntimeCache;
7+
8+
/**
9+
* Trait for dbo collections with user profiles.
10+
*
11+
* @author Marcel Werk
12+
* @copyright 2001-2025 WoltLab GmbH
13+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14+
* @since 6.2
15+
*/
16+
trait TCollectionUserProfiles
17+
{
18+
private bool $userProfilesLoaded = false;
19+
20+
public function getUserProfile(
21+
DatabaseObject $object,
22+
string $userIdProperty = 'userID',
23+
string $usernameProperty = 'username'
24+
): UserProfile {
25+
$this->loadUserProfiles($userIdProperty);
26+
27+
if ($object->{$userIdProperty}) {
28+
return UserProfileRuntimeCache::getInstance()->getObject($object->{$userIdProperty});
29+
} else {
30+
return UserProfile::getGuestUserProfile($object->{$usernameProperty});
31+
}
32+
}
33+
34+
private function loadUserProfiles(string $userIdProperty): void
35+
{
36+
if ($this->userProfilesLoaded) {
37+
return;
38+
}
39+
40+
\assert($this instanceof DatabaseObjectCollection);
41+
42+
$this->userProfilesLoaded = true;
43+
44+
$userIDs = [];
45+
foreach ($this->getObjects() as $object) {
46+
if ($object->{$userIdProperty}) {
47+
$userIDs[] = $object->{$userIdProperty};
48+
}
49+
}
50+
51+
if ($userIDs !== []) {
52+
UserProfileRuntimeCache::getInstance()->cacheObjectIDs($userIDs);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)