ILIAS  trunk Revision v12.0_alpha-413-g215742c0453
ILIAS\News\Data\NewsCollection Class Reference

Optimized News Collection with memory-efficient data structures to support large news feeds. More...

+ Inheritance diagram for ILIAS\News\Data\NewsCollection:
+ Collaboration diagram for ILIAS\News\Data\NewsCollection:

Public Member Functions

 __construct (array $news_items=[])
 
 addNewsItems (array $news_items)
 Add multiple news items efficiently. More...
 
 addNewsItem (NewsItem $item)
 Add a single news item with indexing. More...
 
 getNewsItems ()
 
 getNewsForContext (int $context_obj_id, string $context_obj_type)
 
 getNewsByType (string $obj_type)
 
 setUserReadStatus (int $user_id, array $read_news_ids)
 
 isReadByUser (int $user_id, int $news_id)
 
 getUserReadStatus (int $user_id)
 
 groupFiles ()
 
 groupForums (bool $group_posting_sequence)
 
 getGroupingFor (NewsItem $item)
 Returns the grouping for a given news item. More...
 
 getAggregatedNews (bool $aggregate_files=false, bool $aggregate_forums=false, bool $group_posting_sequence=false)
 Get news items in a format compatible with the legacy rendering implementation. More...
 
 jsonSerialize ()
 
 getIterator ()
 
 count ()
 
 isEmpty ()
 
 first ()
 
 last ()
 
 contains (int $news_id)
 
 getById (int $news_id)
 
 getPageFor (int $news_id)
 
 pick (int $offset)
 
 pluck (string $key, bool $wrap=false)
 
 toArray ()
 
 merge (NewsCollection $other)
 Merge with another collection and returns it as a new collection. More...
 
 limit (?int $limit)
 Limit the number of news items and returns it as a new collection. More...
 
 exclude (array $news_ids)
 Returns a new collection with only the news items that are not in the provided list. More...
 
 load (array $news_ids=[])
 

Protected Attributes

array $news_items = []
 
array $context_map = []
 
array $type_map = []
 
array $user_read_status = []
 
array $grouped_items_map = []
 

Detailed Description

Optimized News Collection with memory-efficient data structures to support large news feeds.

It's designed for context-based filtering and fast_lookups.

Definition at line 29 of file NewsCollection.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\News\Data\NewsCollection::__construct ( array  $news_items = [])

Definition at line 45 of file NewsCollection.php.

46 {
48 }
addNewsItems(array $news_items)
Add multiple news items efficiently.

References ILIAS\News\Data\NewsCollection\addNewsItems().

+ Here is the call graph for this function:

Member Function Documentation

◆ addNewsItem()

ILIAS\News\Data\NewsCollection::addNewsItem ( NewsItem  $item)

Add a single news item with indexing.

Definition at line 64 of file NewsCollection.php.

64 : static
65 {
66 $id = $item->getId();
67 $this->news_items[$id] = $item;
68
69 // Build context index for fast context-based lookups
70 $context_key = $item->getContextObjId() . '_' . $item->getContextObjType();
71 $this->context_map[$context_key][] = $id;
72
73 // Build type index for fast type-based filtering
74 $this->type_map[$item->getContextObjType()][] = $id;
75
76 return $this;
77 }
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23

References $id, ILIAS\News\Data\NewsItem\getContextObjId(), ILIAS\News\Data\NewsItem\getContextObjType(), and ILIAS\News\Data\NewsItem\getId().

Referenced by ILIAS\News\Data\LazyNewsCollection\addNewsItems(), and ILIAS\News\Data\NewsCollection\addNewsItems().

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

◆ addNewsItems()

ILIAS\News\Data\NewsCollection::addNewsItems ( array  $news_items)

Add multiple news items efficiently.

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 53 of file NewsCollection.php.

53 : static
54 {
55 foreach ($news_items as $item) {
56 $this->addNewsItem($item);
57 }
58 return $this;
59 }
addNewsItem(NewsItem $item)
Add a single news item with indexing.

References ILIAS\News\Data\NewsCollection\$news_items, and ILIAS\News\Data\NewsCollection\addNewsItem().

Referenced by ILIAS\News\Data\NewsCollection\__construct().

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

◆ contains()

ILIAS\News\Data\NewsCollection::contains ( int  $news_id)

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 344 of file NewsCollection.php.

344 : bool
345 {
346 return isset($this->news_items[$news_id]);
347 }

◆ count()

ILIAS\News\Data\NewsCollection::count ( )

Definition at line 324 of file NewsCollection.php.

324 : int
325 {
326 return count($this->news_items);
327 }

◆ exclude()

ILIAS\News\Data\NewsCollection::exclude ( array  $news_ids)

Returns a new collection with only the news items that are not in the provided list.

Parameters
int[]$news_ids

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 421 of file NewsCollection.php.

421 : static
422 {
423 if (empty($news_ids)) {
424 return $this;
425 }
426
427 $filtered = new static();
428 $filtered->addNewsItems(array_filter(
429 $this->news_items,
430 fn($item) => !in_array($item->getId(), $news_ids)
431 ));
432 return $filtered;
433 }

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

+ Here is the caller graph for this function:

◆ first()

ILIAS\News\Data\NewsCollection::first ( )

Definition at line 334 of file NewsCollection.php.

334 : ?NewsItem
335 {
336 return reset($this->news_items) ?: null;
337 }
News Item DTO for transfer of news items.
Definition: NewsItem.php:29

◆ getAggregatedNews()

ILIAS\News\Data\NewsCollection::getAggregatedNews ( bool  $aggregate_files = false,
bool  $aggregate_forums = false,
bool  $group_posting_sequence = false 
)

Get news items in a format compatible with the legacy rendering implementation.

This should never be introduced in new code and will be removed in the future.

Deprecated:
Returns
array<int, array<string, mixed>>

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 224 of file NewsCollection.php.

228 : array {
229 $items = [];
230 $file_aggregation_map = [];
231 $forum_aggregation_map = [];
232 $last_forum = 0;
233
234 foreach ($this->news_items as $item) {
235 $entry = [
236 'id' => $item->getId(),
237 'priority' => $item->getPriority(),
238 'title' => $item->getTitle(),
239 'content' => $item->getContent(),
240 'context_obj_id' => $item->getContextObjId(),
241 'context_obj_type' => $item->getContextObjType(),
242 'context_sub_obj_id' => $item->getContextSubObjId(),
243 'context_sub_obj_type' => $item->getContextSubObjType(),
244 'content_type' => $item->getContentType(),
245 'creation_date' => $item->getCreationDate()->format('Y-m-d H:i:s'),
246 'user_id' => $item->getUserId(),
247 'visibility' => $item->getVisibility(),
248 'content_long' => $item->getContentLong(),
249 'content_is_lang_var' => $item->isContentIsLangVar(),
250 'mob_id' => $item->getMobId(),
251 'playtime' => $item->getPlaytime(),
252 'start_date' => null, //it seems like this is not used anymore
253 'end_date' => null, //it seems like this is not used anymore
254 'content_text_is_lang_var' => $item->isContentTextIsLangVar(),
255 'mob_cnt_download' => $item->getMobCntDownload(),
256 'mob_cnt_play' => $item->getMobCntPlay(),
257 'content_html' => $item->isContentHtml(),
258 'update_user_id' => $item->getUpdateUserId(),
259 'user_read' => (int) $this->isReadByUser($item->getUserId(), $item->getId()),
260 'ref_id' => $item->getContextRefId()
261 ];
262
263 if ($aggregate_files && $item->getContextObjType() === 'file') {
264 if (isset($file_aggregation_map[$item->getContextObjId()])) {
265 // If this file already has an aggregation entry, add it there and prevent adding it to the main list
266 $idx = $file_aggregation_map[$item->getContextObjId()];
267 $items[$idx]['aggregation'][$item->getId()] = $entry;
268 continue;
269 }
270
271 // If this is the first news for this file, set the aggregation array
272 $entry['aggregation'] = [];
273 $entry['agg_ref_id'] = $item->getContextRefId();
274 $file_aggregation_map[$item->getContextObjId()] = $item->getId();
275
276 }
277
278 if ($aggregate_forums) {
279 // If we are grouping by sequence, we need to reset the entry in the aggregation map when switching
280 if ($group_posting_sequence && $last_forum !== 0 && $last_forum !== $item->getContextObjType()) {
281 $forum_aggregation_map[$last_forum] = null;
282 }
283
284 if ($item->getContextObjType() === 'frm') {
285 $entry['no_context_title'] = true;
286
287 if (isset($forum_aggregation_map[$item->getContextObjId()])) {
288 // If this form already has an aggregation entry, add it there and prevent adding it to the main list
289 $idx = $forum_aggregation_map[$item->getContextObjId()];
290 $items[$idx]['aggregation'][$item->getId()] = $entry;
291 continue;
292 }
293
294 // If this is the first news for this forum, set the aggregation array
295 $entry['agg_ref_id'] = $item->getContextRefId();
296 $entry['content'] = '';
297 $entry['content_long'] = '';
298
299 $forum_aggregation_map[$item->getContextObjId()] = $item->getId();
300 $last_forum = $item->getContextObjType();
301
302 }
303 }
304
305 $items[$item->getId()] = $entry;
306 }
307 return $items;
308 }
isReadByUser(int $user_id, int $news_id)

References ILIAS\Repository\int(), and ILIAS\News\Data\NewsCollection\isReadByUser().

+ Here is the call graph for this function:

◆ getById()

ILIAS\News\Data\NewsCollection::getById ( int  $news_id)

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 349 of file NewsCollection.php.

349 : ?NewsItem
350 {
351 return $this->news_items[$news_id] ?? null;
352 }

◆ getGroupingFor()

ILIAS\News\Data\NewsCollection::getGroupingFor ( NewsItem  $item)

Returns the grouping for a given news item.

It will return an array with the grouped items if the provided item is the first in the group.

Returns
array{parent: NewsItem, aggregation: NewsItem[], agg_ref_id: int, no_context_title: bool}|null

Definition at line 189 of file NewsCollection.php.

189 : ?array
190 {
191 if (!isset($this->grouped_items_map[$item->getContextObjId()])) {
192 return null;
193 }
194
195 $aggregation = $this->grouped_items_map[$item->getContextObjId()];
196 if ($aggregation['first'] !== $item->getId()) {
197 return null;
198 }
199
200 if ($item->getContextObjType() === 'frm') {
201 $item = $item->withContent('')->withContentLong('');
202 }
203
204 return [
205 'parent' => $item,
206 'aggregation' => [$item, ...array_map(fn($id) => $this->news_items[$id], $aggregation['aggregation'])],
207 'agg_ref_id' => $item->getContextRefId(),
208 'no_context_title' => $item->getContextObjType() === 'frm'
209 ];
210 }
withContent(string $content)
Definition: NewsItem.php:180

References $id, ILIAS\News\Data\NewsItem\getContextObjId(), ILIAS\News\Data\NewsItem\getContextObjType(), ILIAS\News\Data\NewsItem\getContextRefId(), ILIAS\News\Data\NewsItem\getId(), and ILIAS\News\Data\NewsItem\withContent().

+ Here is the call graph for this function:

◆ getIterator()

ILIAS\News\Data\NewsCollection::getIterator ( )

Definition at line 319 of file NewsCollection.php.

319 : ArrayIterator
320 {
321 return new ArrayIterator($this->news_items);
322 }

◆ getNewsByType()

ILIAS\News\Data\NewsCollection::getNewsByType ( string  $obj_type)

Definition at line 98 of file NewsCollection.php.

98 : array
99 {
100 if (!isset($this->type_map[$obj_type])) {
101 return [];
102 }
103
104 return array_map(
105 fn($id) => $this->news_items[$id],
106 $this->type_map[$obj_type]
107 );
108 }

References $id.

◆ getNewsForContext()

ILIAS\News\Data\NewsCollection::getNewsForContext ( int  $context_obj_id,
string  $context_obj_type 
)

Definition at line 84 of file NewsCollection.php.

84 : array
85 {
86 $context_key = $context_obj_id . '_' . $context_obj_type;
87
88 if (!isset($this->context_map[$context_key])) {
89 return [];
90 }
91
92 return array_map(
93 fn($id) => $this->news_items[$id],
94 $this->context_map[$context_key]
95 );
96 }

References $id.

Referenced by ilNewsForContextBlockGUI\loadNewsData().

+ Here is the caller graph for this function:

◆ getNewsItems()

ILIAS\News\Data\NewsCollection::getNewsItems ( )

Definition at line 79 of file NewsCollection.php.

79 : array
80 {
81 return $this->news_items;
82 }

References ILIAS\News\Data\NewsCollection\$news_items.

Referenced by ILIAS\News\Data\NewsCollection\merge().

+ Here is the caller graph for this function:

◆ getPageFor()

ILIAS\News\Data\NewsCollection::getPageFor ( int  $news_id)

Definition at line 354 of file NewsCollection.php.

354 : int
355 {
356 $pages = array_keys($this->news_items);
357 return (int) array_search($news_id, $pages);
358 }

◆ getUserReadStatus()

ILIAS\News\Data\NewsCollection::getUserReadStatus ( int  $user_id)
Returns
array<int, bool>

Definition at line 127 of file NewsCollection.php.

127 : array
128 {
129 $result = [];
130 foreach (array_keys($this->news_items) as $news_id) {
131 $result[$news_id] = $this->isReadByUser($user_id, $news_id);
132 }
133 return $result;
134 }

References ILIAS\News\Data\NewsCollection\isReadByUser().

Referenced by ILIAS\News\Persistence\NewsCache\storeNewsForUser().

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

◆ groupFiles()

ILIAS\News\Data\NewsCollection::groupFiles ( )

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 140 of file NewsCollection.php.

140 : static
141 {
142 foreach (array_filter($this->news_items) as $item) {
143 if ($item->getContextObjType() === 'file') {
144 if (isset($this->grouped_items_map[$item->getContextObjId()])) {
145 $this->grouped_items_map[$item->getContextObjId()]['aggregation'][] = $item->getId();
146 } else {
147 $this->grouped_items_map[$item->getContextObjId()] = [
148 'first' => $item->getId(),
149 'aggregation' => []
150 ];
151 }
152 }
153 }
154 return $this;
155 }

Referenced by ilNewsForContextBlockGUI\loadNewsData().

+ Here is the caller graph for this function:

◆ groupForums()

ILIAS\News\Data\NewsCollection::groupForums ( bool  $group_posting_sequence)

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 157 of file NewsCollection.php.

157 : static
158 {
159 $last_forum = 0;
160
161 foreach (array_filter($this->news_items) as $item) {
162 // If we are grouping by sequence, we need to reset the entry in the aggregation map when switching
163 if ($group_posting_sequence && $last_forum !== $item->getContextObjType() && $last_forum !== 0) {
164 $this->grouped_items_map[$last_forum] = null;
165 }
166
167 if ($item->getContextObjType() === 'frm') {
168 if (isset($this->grouped_items_map[$item->getContextObjId()])) {
169 $this->grouped_items_map[$item->getContextObjId()]['aggregation'][] = $item->getId();
170 } else {
171 $this->grouped_items_map[$item->getContextObjId()] = [
172 'first' => $item->getId(),
173 'aggregation' => []
174 ];
175 $last_forum = $item->getContextObjId();
176 }
177 }
178 }
179
180 return $this;
181 }

◆ isEmpty()

ILIAS\News\Data\NewsCollection::isEmpty ( )

Definition at line 329 of file NewsCollection.php.

329 : bool
330 {
331 return empty($this->news_items);
332 }

◆ isReadByUser()

ILIAS\News\Data\NewsCollection::isReadByUser ( int  $user_id,
int  $news_id 
)

Definition at line 119 of file NewsCollection.php.

119 : bool
120 {
121 return isset($this->user_read_status[$user_id][$news_id]);
122 }

References $user_id.

Referenced by ILIAS\News\Data\NewsCollection\getAggregatedNews(), and ILIAS\News\Data\NewsCollection\getUserReadStatus().

+ Here is the caller graph for this function:

◆ jsonSerialize()

ILIAS\News\Data\NewsCollection::jsonSerialize ( )

Definition at line 314 of file NewsCollection.php.

314 : array
315 {
316 return array_values($this->news_items);
317 }

◆ last()

ILIAS\News\Data\NewsCollection::last ( )

Definition at line 339 of file NewsCollection.php.

339 : ?NewsItem
340 {
341 return end($this->news_items) ?: null;
342 }

◆ limit()

ILIAS\News\Data\NewsCollection::limit ( ?int  $limit)

Limit the number of news items and returns it as a new collection.

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 403 of file NewsCollection.php.

403 : static
404 {
405 if ($limit === null || $limit >= count($this->news_items)) {
406 return $this;
407 }
408
409 $limited = new static();
410 $items = array_slice($this->news_items, 0, $limit, true);
411 $limited->addNewsItems($items);
412
413 return $limited;
414 }

◆ load()

ILIAS\News\Data\NewsCollection::load ( array  $news_ids = [])

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 435 of file NewsCollection.php.

435 : static
436 {
437 return $this;
438 }

◆ merge()

ILIAS\News\Data\NewsCollection::merge ( NewsCollection  $other)

Merge with another collection and returns it as a new collection.

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 384 of file NewsCollection.php.

384 : static
385 {
386 $merged = new static();
387 $merged->addNewsItems($this->news_items);
388 $merged->addNewsItems($other->getNewsItems());
389
390 // Merge user read status
391 foreach ($other->user_read_status as $user_id => $read_ids) {
392 $merged->user_read_status[$user_id] = isset($this->user_read_status[$user_id])
393 ? array_merge($this->user_read_status[$user_id], $read_ids)
394 : $read_ids;
395 }
396
397 return $merged;
398 }

References $user_id, and ILIAS\News\Data\NewsCollection\getNewsItems().

+ Here is the call graph for this function:

◆ pick()

ILIAS\News\Data\NewsCollection::pick ( int  $offset)

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 360 of file NewsCollection.php.

360 : ?NewsItem
361 {
362 $index = max(0, $offset);
363 return array_values($this->news_items)[$index] ?? null;
364 }

◆ pluck()

ILIAS\News\Data\NewsCollection::pluck ( string  $key,
bool  $wrap = false 
)

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 366 of file NewsCollection.php.

366 : array
367 {
368 $arr = array_column($this->toArray(), $key);
369 return $wrap ? array_map(fn($item) => [$item], $arr) : $arr;
370 }

Referenced by ilNewsForContextBlockGUI\initData().

+ Here is the caller graph for this function:

◆ setUserReadStatus()

ILIAS\News\Data\NewsCollection::setUserReadStatus ( int  $user_id,
array  $read_news_ids 
)
Parameters
array<int,bool>$read_news_ids

Definition at line 113 of file NewsCollection.php.

113 : static
114 {
115 $this->user_read_status[$user_id] = array_filter($read_news_ids);
116 return $this;
117 }

References $user_id.

◆ toArray()

ILIAS\News\Data\NewsCollection::toArray ( )
Returns
array<int, array>

Reimplemented in ILIAS\News\Data\LazyNewsCollection.

Definition at line 375 of file NewsCollection.php.

375 : array
376 {
377 return array_map(fn($item) => $item->toArray(), $this->news_items);
378 }

Field Documentation

◆ $context_map

array ILIAS\News\Data\NewsCollection::$context_map = []
protected

Definition at line 35 of file NewsCollection.php.

◆ $grouped_items_map

array ILIAS\News\Data\NewsCollection::$grouped_items_map = []
protected

Definition at line 43 of file NewsCollection.php.

◆ $news_items

array ILIAS\News\Data\NewsCollection::$news_items = []
protected

◆ $type_map

array ILIAS\News\Data\NewsCollection::$type_map = []
protected

Definition at line 38 of file NewsCollection.php.

◆ $user_read_status

array ILIAS\News\Data\NewsCollection::$user_read_status = []
protected

Definition at line 41 of file NewsCollection.php.


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