ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
ILIAS\News\Domain\NewsCollectionService Class Reference

News Collection Service orchestrates all news-related operations and provides a high-level API for the news service. More...

+ Collaboration diagram for ILIAS\News\Domain\NewsCollectionService:

Public Member Functions

 __construct (private readonly NewsRepository $repository, private readonly NewsCache $cache, private readonly UserContextResolver $user_context_resolver, private readonly \ilObjectDataCache $object_data, private readonly \ilRbacSystem $rbac)
 
 getNewsForUser (\ilObjUser $user, NewsCriteria $criteria, bool $lazy=false)
 
 getNewsForContext (NewsContext $context, NewsCriteria $criteria, int $user_id, bool $lazy=false)
 
 getNewsForContainer (int $ref_id, int $context_obj_id, string $context_type, NewsCriteria $criteria, int $user_id, bool $lazy=false)
 
 invalidateCache (int $user_id)
 

Private Member Functions

 getNewsForContexts (array $contexts, NewsCriteria $criteria, int $user_id, bool $lazy)
 
 fetchContextData (array $contexts)
 
 filterByAccess (array $contexts, NewsCriteria $criteria, int $user_id)
 
 applyFinalProcessing (NewsCollection $collection, NewsCriteria $criteria)
 Apply the last steps of the news collection processing pipeline: Exclude, Limit. More...
 

Detailed Description

News Collection Service orchestrates all news-related operations and provides a high-level API for the news service.

Definition at line 34 of file NewsCollectionService.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\News\Domain\NewsCollectionService::__construct ( private readonly NewsRepository  $repository,
private readonly NewsCache  $cache,
private readonly UserContextResolver  $user_context_resolver,
private readonly \ilObjectDataCache  $object_data,
private readonly \ilRbacSystem  $rbac 
)

Definition at line 36 of file NewsCollectionService.php.

42 {
43 }

Member Function Documentation

◆ applyFinalProcessing()

ILIAS\News\Domain\NewsCollectionService::applyFinalProcessing ( NewsCollection  $collection,
NewsCriteria  $criteria 
)
private

Apply the last steps of the news collection processing pipeline: Exclude, Limit.

Definition at line 231 of file NewsCollectionService.php.

232 {
233 return $collection->exclude($criteria->getExcludedNewsIds())->limit($criteria->getLimit());
234 }
Optimized News Collection with memory-efficient data structures to support large news feeds.
exclude(array $news_ids)
Returns a new collection with only the news items that are not in the provided list.

References ILIAS\News\Data\NewsCollection\exclude(), ILIAS\News\Data\NewsCriteria\getExcludedNewsIds(), and ILIAS\News\Data\NewsCriteria\getLimit().

Referenced by ILIAS\News\Domain\NewsCollectionService\getNewsForUser().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fetchContextData()

ILIAS\News\Domain\NewsCollectionService::fetchContextData ( array  $contexts)
private
Parameters
NewsContext[]$contexts
Returns
NewsContext[]

Definition at line 165 of file NewsCollectionService.php.

165 : array
166 {
167 // Batch loads object_data and object_references using preloading
168 $obj_ids = array_filter(array_map(fn($context) => $context->getObjId(), $contexts));
169 $this->object_data->preloadObjectCache($obj_ids);
170
171 for ($i = 0; $i < count($contexts); $i++) {
172 $context = $contexts[$i];
173
174 if ($context->getObjId() === null) {
175 $context->setObjId($this->object_data->lookupObjId($context->getRefId()));
176 }
177
178 if ($context->getObjType() === null) {
179 $context->setObjType($this->object_data->lookupType($context->getObjId()));
180 }
181
182 $contexts[$i] = $context;
183 }
184
185 return $contexts;
186 }

◆ filterByAccess()

ILIAS\News\Domain\NewsCollectionService::filterByAccess ( array  $contexts,
NewsCriteria  $criteria,
int  $user_id 
)
private
Parameters
NewsContext[]$contexts
Returns
NewsContext[]

Definition at line 192 of file NewsCollectionService.php.

192 : array
193 {
194 if ($criteria->isOnlyPublic()) {
195 return $contexts;
196 }
197
198 // Remove contexts without news items or outside the criteria
199 $contexts = $this->repository->filterContext($contexts, $criteria);
200
201 // Preload rbac cache
202 $this->rbac->preloadRbacPaCache(array_map(fn($context) => $context->getRefId(), $contexts), $user_id);
203
204 // Order contexts by level to keep tree hierarchy
205 usort($contexts, fn($a, $b) => $a->getLevel() <=> $b->getLevel());
206 $filtered = [];
207 $ac_result = [];
208
209 foreach ($contexts as $context) {
210 // Filter object and skip access check if the parent object was denied
211 if (isset($ac_result[$context->getParentRefId()]) && !$ac_result[$context->getParentRefId()]) {
212 continue;
213 }
214
215 $ac_result[$context->getRefId()] = $this->rbac->checkAccess(
216 'read',
217 $context->getRefId(),
218 $context->getObjType(),
219 );
220
221 if ($ac_result[$context->getRefId()]) {
222 $filtered[] = $context;
223 }
224 }
225 return $filtered;
226 }
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples

References Vendor\Package\$a, Vendor\Package\$b, $user_id, ILIAS\News\Data\NewsCriteria\isOnlyPublic(), ILIAS\Repository\rbac(), and ILIAS\UI\examples\Deck\repository().

+ Here is the call graph for this function:

◆ getNewsForContainer()

ILIAS\News\Domain\NewsCollectionService::getNewsForContainer ( int  $ref_id,
int  $context_obj_id,
string  $context_type,
NewsCriteria  $criteria,
int  $user_id,
bool  $lazy = false 
)

Definition at line 94 of file NewsCollectionService.php.

102 if (in_array($context_type, ['grp', 'crs'])) {
103 // see #31471, #30687, and ilMembershipNotification
104 if (!\ilContainer::_lookupContainerSetting($context_obj_id, 'cont_use_news', '1')
105 || (
106 !\ilContainer::_lookupContainerSetting($context_obj_id, 'cont_use_news', '1')
107 && !\ilContainer::_lookupContainerSetting($context_obj_id, 'news_timeline')
108 )) {
109 return new NewsCollection();
110 }
111
112 if (\ilBlockSetting::_lookup('news', 'hide_news_per_date', 0, $context_obj_id)) {
113 $hide_date = \ilBlockSetting::_lookup('news', 'hide_news_date', 0, $context_obj_id);
114 if (!empty($hide_date)) {
115 $criteria = $criteria->withStartDate(new \DateTimeImmutable($hide_date));
116 }
117 }
118 }
119
120 $context = new NewsContext($ref_id, $context_obj_id, $context_type);
121 return $this->applyFinalProcessing($this->getNewsForContexts([$context], $criteria, $user_id, $lazy), $criteria);
122 }
withStartDate(?DateTimeImmutable $start_date)
applyFinalProcessing(NewsCollection $collection, NewsCriteria $criteria)
Apply the last steps of the news collection processing pipeline: Exclude, Limit.
getNewsForContexts(array $contexts, NewsCriteria $criteria, int $user_id, bool $lazy)
static _lookup(string $a_type, string $a_setting, int $a_user=0, int $a_block_id=0)
Lookup setting from database.
static _lookupContainerSetting(int $a_id, string $a_keyword, ?string $a_default_value=null)
$ref_id
Definition: ltiauth.php:66

References ilBlockSetting\_lookup(), ilContainer\_lookupContainerSetting(), and ILIAS\News\Data\NewsCriteria\withStartDate().

+ Here is the call graph for this function:

◆ getNewsForContext()

ILIAS\News\Domain\NewsCollectionService::getNewsForContext ( NewsContext  $context,
NewsCriteria  $criteria,
int  $user_id,
bool  $lazy = false 
)

Definition at line 85 of file NewsCollectionService.php.

91 return $this->applyFinalProcessing($this->getNewsForContexts([$context], $criteria, $user_id, $lazy), $criteria);
92 }

◆ getNewsForContexts()

ILIAS\News\Domain\NewsCollectionService::getNewsForContexts ( array  $contexts,
NewsCriteria  $criteria,
int  $user_id,
bool  $lazy 
)
private
Parameters
NewsContext[]$contexts

Definition at line 132 of file NewsCollectionService.php.

133 {
134 // 1. Try context cache first (L1)
135 $cached = $this->cache->getAggregatedContexts($contexts);
136 $hits = $cached['hit'];
137
138 if (!empty($cached['missing'])) {
139 // 2. Batch load missing context object information [DPL 2]
140 $remaining = $this->fetchContextData($cached['missing']);
141
142 // 3. Perform aggregation [DPL 3]
143 if (!$criteria->isPreventNesting()) {
144 $aggregated = (new NewsAggregator())->aggregate($remaining);
145 $this->cache->storeAggregatedContexts($remaining, $aggregated);
146 $hits = array_merge($hits, $aggregated);
147 } else {
148 $hits = array_merge($hits, $remaining);
149 }
150 }
151
152 // 4. Perform access checks [DPL 3]
153 $aggregated = $this->filterByAccess($hits, $criteria, $user_id);
154
155 // 5. Batch load news from the database [DPL 4]
156 return $lazy
157 ? $this->repository->findByContextsBatchLazy($aggregated, $criteria)
158 : $this->repository->findByContextsBatch($aggregated, $criteria);
159 }
filterByAccess(array $contexts, NewsCriteria $criteria, int $user_id)

References $user_id, ILIAS\News\Data\NewsCriteria\isPreventNesting(), and ILIAS\UI\examples\Deck\repository().

Referenced by ILIAS\News\Domain\NewsCollectionService\getNewsForUser().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getNewsForUser()

ILIAS\News\Domain\NewsCollectionService::getNewsForUser ( \ilObjUser  $user,
NewsCriteria  $criteria,
bool  $lazy = false 
)

Definition at line 45 of file NewsCollectionService.php.

46 {
47 // 1. Try user cache first
48 $cached_news = $this->cache->getNewsForUser($user->getId(), $criteria);
49 if ($cached_news !== null) {
50 // Transform the lazy collection to a normal collection if needed
51 if (!$lazy) {
52 $news_collection = new NewsCollection($this->repository->findByIds($cached_news->pluck('id')));
53 } else {
54 $news_collection = $cached_news->withFetchCallback(
55 fn(...$args) => $this->repository->loadLazyItems(...$args)
56 );
57 }
58
59 // Apply request-specific filtering [DPL 5]
60 return $this->applyFinalProcessing($news_collection, $criteria);
61 }
62
63 // 2. Add missing criteria and validate it
64 if ($criteria->isIncludeReadStatus() && $criteria->getReadUserId() === null) {
65 $criteria = $criteria->withReadUserId($user->getId());
66 }
67 $criteria->validate();
68
69 // 3. Get user accessible contexts [DPL 1]
70 $user_contexts = $this->user_context_resolver->getAccessibleContexts($user, $criteria);
71 if (empty($user_contexts)) {
72 return new NewsCollection();
73 }
74
75 // 4. Query news for resolved contexts [DPL 2-4]
76 $news_collection = $this->getNewsForContexts($user_contexts, $criteria, $user->getId(), $lazy);
77
78 // 5. Store in cache
79 $this->cache->storeNewsForUser($user->getId(), $criteria, $news_collection);
80
81 // 6. Apply request-specific filtering [DPL 5]
82 return $this->applyFinalProcessing($news_collection, $criteria);
83 }
validate()
Validate criteria parameters.
withReadUserId(?int $read_user_id)

References ILIAS\News\Domain\NewsCollectionService\applyFinalProcessing(), ilObject\getId(), ILIAS\News\Domain\NewsCollectionService\getNewsForContexts(), ILIAS\News\Data\NewsCriteria\getReadUserId(), ILIAS\News\Data\NewsCriteria\isIncludeReadStatus(), ILIAS\UI\examples\Deck\repository(), ILIAS\News\Data\NewsCriteria\validate(), and ILIAS\News\Data\NewsCriteria\withReadUserId().

+ Here is the call graph for this function:

◆ invalidateCache()

ILIAS\News\Domain\NewsCollectionService::invalidateCache ( int  $user_id)

Definition at line 124 of file NewsCollectionService.php.

124 : void
125 {
126 $this->cache->invalidateNewsForUser($user_id, new NewsCriteria());
127 }
News Criteria DTO for querying news items supports caching, JSON serialization, and validation.

References $user_id.


The documentation for this class was generated from the following file: