ILIAS  trunk Revision v12.0_alpha-399-g579a087ced2
ILIAS\News\Data\LazyNewsCollection Class Reference

This class is a special implementation of a NewsCollection that is designed to load the complete NewsItems only when needed. More...

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

Public Member Functions

 __construct (array $news_ids=[], protected ?\Closure $fetch_callback=null)
 
 withFetchCallback (\Closure $callback)
 
 addNewsItems (array $news_items)
 Add multiple news items efficiently. More...
 
 load (array $news_ids=[])
 This method loads the provided NewsItems from the database. More...
 
 groupFiles ()
 
 groupForums (bool $group_posting_sequence)
 
 contains (int $news_id)
 This method returns true if the NewsItem exists in this collection. More...
 
 has (int $news_id)
 This method returns true if the NewsItem exists in this collection and has been loaded. More...
 
 getById (int $news_id)
 This method returns the NewsItem with the given id or null if it does not exist. More...
 
 pick (int $offset)
 This method returns the NewsItem of the given offset of the collection. More...
 
 pluck (string $key, bool $wrap=false)
 INFO: This method will load all NewsItems into the collection. More...
 
 toArray ()
 INFO: This method will load all NewsItems into the collection. More...
 
 getAggregatedNews (bool $aggregate_files=false, bool $aggregate_forums=false, bool $group_posting_sequence=false)
 INFO: This method will load all NewsItems into the collection. More...
 
 merge (NewsCollection $other)
 Merge with another collection 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...
 
 limit (?int $limit)
 Limit the number of news items and returns it as a new collection. More...
 
- Public Member Functions inherited from ILIAS\News\Data\NewsCollection
 __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=[])
 

Private Member Functions

 regroup (array $items)
 

Private Attributes

bool $group_files = false
 
int $group_forums = 0
 

Additional Inherited Members

- Protected Attributes inherited from ILIAS\News\Data\NewsCollection
array $news_items = []
 
array $context_map = []
 
array $type_map = []
 
array $user_read_status = []
 
array $grouped_items_map = []
 

Detailed Description

This class is a special implementation of a NewsCollection that is designed to load the complete NewsItems only when needed.

This lazy loading has the advantage that less main memory is required, since only the IDs of the NewsItems are initially stored in the collection. It also prevents large database queries from being made when only some of the data records are needed.

This class should be used with caution and should not be used across the board. If used incorrectly, lazy loading can lead to a deterioration in performance due to the N+1 problem.

Definition at line 32 of file LazyNewsCollection.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\News\Data\LazyNewsCollection::__construct ( array  $news_ids = [],
protected ?\Closure  $fetch_callback = null 
)

Definition at line 37 of file LazyNewsCollection.php.

40 {
42
43 foreach ($news_ids as $news_id) {
44 $this->news_items[$news_id] = null;
45 }
46 }
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc

References ILIAS\GlobalScreen\Provider\__construct().

+ Here is the call graph for this function:

Member Function Documentation

◆ addNewsItems()

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

Add multiple news items efficiently.

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 58 of file LazyNewsCollection.php.

58 : static
59 {
60 foreach ($news_items as $id => $item) {
61 if ($item !== null) {
62 $this->addNewsItem($item);
63 } else {
64 $this->news_items[$id] = null;
65 }
66
67 }
68 return $this;
69 }
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
addNewsItem(NewsItem $item)
Add a single news item with indexing.

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

Referenced by ILIAS\News\Data\LazyNewsCollection\load().

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

◆ contains()

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

This method returns true if the NewsItem exists in this collection.

It does not provide any information about whether the NewsItem has been loaded or not.

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 159 of file LazyNewsCollection.php.

159 : bool
160 {
161 return array_key_exists($news_id, $this->news_items);
162 }

◆ exclude()

ILIAS\News\Data\LazyNewsCollection::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 from ILIAS\News\Data\NewsCollection.

Definition at line 242 of file LazyNewsCollection.php.

242 : static
243 {
244 return parent::exclude($news_ids)->withFetchCallback($this->fetch_callback);
245 }

◆ getAggregatedNews()

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

INFO: This method will load all NewsItems into the collection.

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 228 of file LazyNewsCollection.php.

232 : array {
233 $this->load();
234 return parent::getAggregatedNews($aggregate_files, $aggregate_forums, $group_posting_sequence);
235 }
load(array $news_ids=[])
This method loads the provided NewsItems from the database.

◆ getById()

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

This method returns the NewsItem with the given id or null if it does not exist.

If the NewsItem was not loaded, it will be loaded from the database.

WARNING: this may cause N+1 queries when using this method in a loop. Use load instead, to batch load NewsItems.

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 179 of file LazyNewsCollection.php.

179 : ?NewsItem
180 {
181 if (!$this->has($news_id)) {
182 $this->load([$news_id]);
183 }
184
185 return $this->news_items[$news_id] ?? null;
186 }
has(int $news_id)
This method returns true if the NewsItem exists in this collection and has been loaded.

References ILIAS\News\Data\LazyNewsCollection\has(), and ILIAS\News\Data\LazyNewsCollection\load().

Referenced by ILIAS\News\Data\LazyNewsCollection\pick().

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

◆ groupFiles()

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

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 116 of file LazyNewsCollection.php.

116 : static
117 {
118 $this->group_files = true;
119 return $this;
120 }

◆ groupForums()

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

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 122 of file LazyNewsCollection.php.

122 : static
123 {
124 $this->group_forums = $group_posting_sequence ? 2 : 1;
125 return $this;
126 }

◆ has()

ILIAS\News\Data\LazyNewsCollection::has ( int  $news_id)

This method returns true if the NewsItem exists in this collection and has been loaded.

Definition at line 167 of file LazyNewsCollection.php.

167 : bool
168 {
169 return isset($this->news_items[$news_id]);
170 }

Referenced by ILIAS\News\Data\LazyNewsCollection\getById().

+ Here is the caller graph for this function:

◆ limit()

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

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

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 247 of file LazyNewsCollection.php.

247 : static
248 {
249 return parent::limit($limit)->withFetchCallback($this->fetch_callback);
250 }

◆ load()

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

This method loads the provided NewsItems from the database.

If an empty array is provided, it will load all missing NewsItems. It will also load the dependencies of the NewsItems if grouping is enabled.

Parameters
int[]$news_ids

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 77 of file LazyNewsCollection.php.

77 : static
78 {
79 if ($this->fetch_callback === null) {
80 throw new \RuntimeException('No fetch callback provided');
81 }
82
83 if (empty($news_ids)) {
84 $news_ids = array_keys(array_filter($this->news_items, fn($item) => $item === null));
85 } else {
86 $news_ids = array_intersect($news_ids, array_keys($this->news_items));
87 }
88
89 if (empty($news_ids)) {
90 return $this;
91 }
92
93 // Check if grouping was requested
94 $context_types = [];
95 if ($this->group_files === true) {
96 $context_types[] = 'file';
97 }
98 if ($this->group_forums > 0) {
99 $context_types[] = 'frm';
100 }
101
102 // Load items from a repository
103 $items = call_user_func($this->fetch_callback, $news_ids, $context_types);
104 $this->addNewsItems($items);
105
106 // Perform re-grouping if necessary
107 $this->regroup($items);
108
109 return $this;
110 }
addNewsItems(array $news_items)
Add multiple news items efficiently.

References ILIAS\News\Data\LazyNewsCollection\addNewsItems(), and ILIAS\News\Data\LazyNewsCollection\regroup().

Referenced by ILIAS\News\Data\LazyNewsCollection\getById(), ILIAS\News\Data\LazyNewsCollection\pluck(), and ILIAS\News\Data\LazyNewsCollection\toArray().

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

◆ merge()

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

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

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 237 of file LazyNewsCollection.php.

237 : static
238 {
239 return parent::merge($other)->withFetchCallback($this->fetch_callback);
240 }

◆ pick()

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

This method returns the NewsItem of the given offset of the collection.

If the NewsItem was not loaded, it will be loaded from the database.

WARNING: this may cause N+1 queries when using this method in a loop. Use load instead, to batch load NewsItems.

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 195 of file LazyNewsCollection.php.

195 : ?NewsItem
196 {
197 $index = max(0, $offset);
198 $news_id = array_keys($this->news_items)[$index] ?? null;
199
200 return $news_id !== null ? $this->getById($news_id) : null;
201 }
getById(int $news_id)
This method returns the NewsItem with the given id or null if it does not exist.

References ILIAS\News\Data\LazyNewsCollection\getById().

+ Here is the call graph for this function:

◆ pluck()

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

INFO: This method will load all NewsItems into the collection.

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 206 of file LazyNewsCollection.php.

206 : array
207 {
208 if ($key === 'id') {
209 return $wrap ? array_map(fn($item) => [$item], array_keys($this->news_items)) : array_keys($this->news_items);
210 }
211
212 $this->load();
213 return parent::pluck($key, $wrap);
214 }

References ILIAS\News\Data\LazyNewsCollection\load().

+ Here is the call graph for this function:

◆ regroup()

ILIAS\News\Data\LazyNewsCollection::regroup ( array  $items)
private

Definition at line 128 of file LazyNewsCollection.php.

128 : void
129 {
130 // Check if re-grouping is required
131 $needs_file_grouping = false;
132 $needs_forum_grouping = false;
133 foreach ($items as $item) {
134 if ($this->group_files === true && $item->getContextObjType() === 'file') {
135 $needs_file_grouping = true;
136 }
137 if ($this->group_forums > 0 && $item->getContextObjType() === 'frm') {
138 $needs_forum_grouping = true;
139 }
140 }
141
142 // Perform re-grouping
143 if ($needs_file_grouping) {
144 parent::groupFiles();
145 }
146 if ($needs_forum_grouping) {
147 parent::groupForums($this->group_forums === 2);
148 }
149 }

Referenced by ILIAS\News\Data\LazyNewsCollection\load().

+ Here is the caller graph for this function:

◆ toArray()

ILIAS\News\Data\LazyNewsCollection::toArray ( )

INFO: This method will load all NewsItems into the collection.

Reimplemented from ILIAS\News\Data\NewsCollection.

Definition at line 219 of file LazyNewsCollection.php.

219 : array
220 {
221 $this->load();
222 return parent::toArray();
223 }

References ILIAS\News\Data\LazyNewsCollection\load().

+ Here is the call graph for this function:

◆ withFetchCallback()

ILIAS\News\Data\LazyNewsCollection::withFetchCallback ( \Closure  $callback)
Parameters

Closure(int[], string[]): NewsItem[] $callback

Definition at line 51 of file LazyNewsCollection.php.

51 : static
52 {
53 $clone = clone $this;
54 $clone->fetch_callback = $callback;
55 return $clone;
56 }

Field Documentation

◆ $group_files

bool ILIAS\News\Data\LazyNewsCollection::$group_files = false
private

Definition at line 34 of file LazyNewsCollection.php.

◆ $group_forums

int ILIAS\News\Data\LazyNewsCollection::$group_forums = 0
private

Definition at line 35 of file LazyNewsCollection.php.


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