ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
NewsCollectionService.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29
35{
36 public function __construct(
37 private readonly NewsRepository $repository,
38 private readonly NewsCache $cache,
39 private readonly UserContextResolver $user_context_resolver,
40 private readonly \ilObjectDataCache $object_data,
41 private readonly \ilRbacSystem $rbac
42 ) {
43 }
44
45 public function getNewsForUser(\ilObjUser $user, NewsCriteria $criteria, bool $lazy = false): NewsCollection
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 }
84
85 public function getNewsForContext(
86 NewsContext $context,
87 NewsCriteria $criteria,
88 int $user_id,
89 bool $lazy = false
91 return $this->applyFinalProcessing($this->getNewsForContexts([$context], $criteria, $user_id, $lazy), $criteria);
92 }
93
94 public function getNewsForContainer(
95 int $ref_id,
96 int $context_obj_id,
97 string $context_type,
98 NewsCriteria $criteria,
99 int $user_id,
100 bool $lazy = false
101 ): NewsCollection {
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 }
123
124 public function invalidateCache(int $user_id): void
125 {
126 $this->cache->invalidateNewsForUser($user_id, new NewsCriteria());
127 }
128
132 private function getNewsForContexts(array $contexts, NewsCriteria $criteria, int $user_id, bool $lazy): NewsCollection
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 }
160
165 private function fetchContextData(array $contexts): 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 }
187
192 private function filterByAccess(array $contexts, NewsCriteria $criteria, int $user_id): 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 }
227
231 private function applyFinalProcessing(NewsCollection $collection, NewsCriteria $criteria): NewsCollection
232 {
233 return $collection->exclude($criteria->getExcludedNewsIds())->limit($criteria->getLimit());
234 }
235}
News Aggregator aggregates related contexts for a news context using a layer-wise Batching BFS to agg...
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.
News Context DTO represents a context where news items can be associated with.
Definition: NewsContext.php:29
News Criteria DTO for querying news items supports caching, JSON serialization, and validation.
withStartDate(?DateTimeImmutable $start_date)
validate()
Validate criteria parameters.
withReadUserId(?int $read_user_id)
News Collection Service orchestrates all news-related operations and provides a high-level API for th...
__construct(private readonly NewsRepository $repository, private readonly NewsCache $cache, private readonly UserContextResolver $user_context_resolver, private readonly \ilObjectDataCache $object_data, private readonly \ilRbacSystem $rbac)
applyFinalProcessing(NewsCollection $collection, NewsCriteria $criteria)
Apply the last steps of the news collection processing pipeline: Exclude, Limit.
getNewsForContext(NewsContext $context, NewsCriteria $criteria, int $user_id, bool $lazy=false)
filterByAccess(array $contexts, NewsCriteria $criteria, int $user_id)
getNewsForUser(\ilObjUser $user, NewsCriteria $criteria, bool $lazy=false)
getNewsForContainer(int $ref_id, int $context_obj_id, string $context_type, NewsCriteria $criteria, int $user_id, bool $lazy=false)
getNewsForContexts(array $contexts, NewsCriteria $criteria, int $user_id, bool $lazy)
User Context Resolver resolves which contexts a user can access for news operations.
Multi-Level News Cache Implementation:
Definition: NewsCache.php:36
News Repository provides basic CRUD operations and optimized database access for news operations with...
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)
User class.
class ilObjectDataCache
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:66
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
if(!file_exists('../ilias.ini.php'))