ILIAS  trunk Revision v12.0_alpha-413-g215742c0453
NewsCollectionService.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30
36{
37 public function __construct(
38 private readonly NewsRepository $repository,
39 private readonly NewsCache $cache,
40 private readonly UserContextResolver $user_context_resolver,
41 private readonly \ilObjectDataCache $object_data,
42 private readonly \ilAccessHandler $access,
43 ) {
44 }
45
46 public function getNewsForUser(\ilObjUser $user, NewsCriteria $criteria, bool $lazy = false): NewsCollection
47 {
48 // 1. Try user cache first
49 $cached_news = $this->cache->getNewsForUser($user->getId(), $criteria);
50 if ($cached_news !== null) {
51 // Transform the lazy collection to a normal collection if needed
52 if (!$lazy) {
53 $news_collection = new NewsCollection($this->repository->findByIds($cached_news->pluck('id')));
54 } else {
55 $news_collection = $cached_news->withFetchCallback(
56 fn(...$args) => $this->repository->loadLazyItems(...$args)
57 );
58 }
59
60 // Apply request-specific filtering [DPL 5]
61 return $this->applyFinalProcessing($news_collection, $criteria);
62 }
63
64 // 2. Add missing criteria and validate it
65 if ($criteria->isIncludeReadStatus() && $criteria->getReadUserId() === null) {
66 $criteria = $criteria->withReadUserId($user->getId());
67 }
68 $criteria->validate();
69
70 // 3. Get user accessible contexts [DPL 1]
71 $user_contexts = $this->user_context_resolver->getAccessibleContexts($user, $criteria);
72 if (empty($user_contexts)) {
73 return new NewsCollection();
74 }
75
76 // 4. Query news for resolved contexts [DPL 2-4]
77 $news_collection = $this->getNewsForContexts($user_contexts, $criteria, $lazy);
78
79 // 5. Store in cache
80 $this->cache->storeNewsForUser($user->getId(), $criteria, $news_collection);
81
82 // 6. Apply request-specific filtering [DPL 5]
83 return $this->applyFinalProcessing($news_collection, $criteria);
84 }
85
86 public function getNewsForContext(NewsContext $context, NewsCriteria $criteria, bool $lazy = false): NewsCollection
87 {
88 return $this->applyFinalProcessing($this->getNewsForContexts([$context], $criteria, $lazy), $criteria);
89 }
90
91 public function invalidateCache(int $user_id): void
92 {
93 $this->cache->invalidateNewsForUser($user_id, new NewsCriteria());
94 }
95
99 private function getNewsForContexts(array $contexts, NewsCriteria $criteria, bool $lazy): NewsCollection
100 {
101 // 1. Try context cache first (L1)
102 $cached = $this->cache->getAggregatedContexts($contexts);
103 $hits = $cached['hit'];
104
105 if (!empty($cached['missing'])) {
106 // 2. Batch load missing context object information [DPL 2]
107 $remaining = $this->fetchContextData($cached['missing']);
108
109 // 3. Perform aggregation [DPL 3]
110 if (!$criteria->isPreventNesting()) {
111 $aggregated = (new NewsAggregator())->aggregate($remaining);
112 $this->cache->storeAggregatedContexts($remaining, $aggregated);
113 $hits = array_merge($hits, $aggregated);
114 } else {
115 $hits = array_merge($hits, $remaining);
116 }
117 }
118
119 // 4. Perform access checks [DPL 3]
120 $aggregated = $this->filterByAccess($hits, $criteria);
121
122 // 5. Batch load news from the database [DPL 4]
123 return $lazy
124 ? $this->repository->findByContextsBatchLazy($aggregated, $criteria)
125 : $this->repository->findByContextsBatch($aggregated, $criteria);
126 }
127
132 private function fetchContextData(array $contexts): array
133 {
134 // Batch loads object_data and object_references using preloading
135 $obj_ids = array_filter(array_map(fn($context) => $context->getObjId(), $contexts));
136 $this->object_data->preloadObjectCache($obj_ids);
137
138 for ($i = 0; $i < count($contexts); $i++) {
139 $context = $contexts[$i];
140
141 if ($context->getObjId() === null) {
142 $context->setObjId($this->object_data->lookupObjId($context->getRefId()));
143 }
144
145 if ($context->getObjType() === null) {
146 $context->setObjType($this->object_data->lookupType($context->getObjId()));
147 }
148
149 $contexts[$i] = $context;
150 }
151
152 return $contexts;
153 }
154
159 private function filterByAccess(array $contexts, NewsCriteria $criteria): array
160 {
161 if ($criteria->isOnlyPublic()) {
162 return $contexts;
163 }
164
165 // Preload activation cache which is used in access handler
166 \ilObjectActivation::preloadData(array_map(fn($context) => $context->getRefId(), $contexts));
167
168 $filtered = [];
169 foreach ($contexts as $context) {
170 if ($this->access->checkAccess('read', '', $context->getRefId())) {
171 $filtered[] = $context;
172 }
173 }
174 return $filtered;
175 }
176
180 private function applyFinalProcessing(NewsCollection $collection, NewsCriteria $criteria): NewsCollection
181 {
182 return $collection->exclude($criteria->getExcludedNewsIds())->limit($criteria->getLimit());
183 }
184}
News Aggregator aggregates related contexts for a news context using a layer-wise Batching BFS to agg...
This class is a special implementation of a NewsCollection that is designed to load the complete News...
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.
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...
applyFinalProcessing(NewsCollection $collection, NewsCriteria $criteria)
Apply the last steps of the news collection processing pipeline: Exclude, Limit.
getNewsForContexts(array $contexts, NewsCriteria $criteria, bool $lazy)
filterByAccess(array $contexts, NewsCriteria $criteria)
getNewsForContext(NewsContext $context, NewsCriteria $criteria, bool $lazy=false)
getNewsForUser(\ilObjUser $user, NewsCriteria $criteria, bool $lazy=false)
__construct(private readonly NewsRepository $repository, private readonly NewsCache $cache, private readonly UserContextResolver $user_context_resolver, private readonly \ilObjectDataCache $object_data, private readonly \ilAccessHandler $access,)
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...
User class.
static preloadData(array $ref_ids)
Preload data to internal cache.
class ilObjectDataCache
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
$context
Definition: webdav.php:31