ILIAS  trunk Revision v12.0_alpha-399-g579a087ced2
ILIAS\News\Persistence\NewsRepository Class Reference

News Repository provides basic CRUD operations and optimized database access for news operations with batch loading and optimized queries. More...

+ Collaboration diagram for ILIAS\News\Persistence\NewsRepository:

Public Member Functions

 __construct (protected readonly \ilDBInterface $db, protected readonly Factory $factory)
 
 findById (int $news_id)
 
 findByIds (array $news_ids)
 
 loadLazyItems (array $news_ids, array $group_context_types)
 
 findByContextsBatch (array $contexts, NewsCriteria $criteria)
 
 findByContextsBatchLazy (array $contexts, NewsCriteria $criteria)
 
 countByContextsBatch (array $contexts)
 

Private Member Functions

 buildFindQuery (?array $news_ids=null)
 
 buildBatchQuery (array $obj_ids, NewsCriteria $criteria, bool $only_id=false)
 

Static Private Member Functions

static parseTimePeriod (string|int $time_period)
 

Detailed Description

News Repository provides basic CRUD operations and optimized database access for news operations with batch loading and optimized queries.

Definition at line 35 of file NewsRepository.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\News\Persistence\NewsRepository::__construct ( protected readonly \ilDBInterface  $db,
protected readonly Factory  $factory 
)

Definition at line 37 of file NewsRepository.php.

40 {
41 }

Member Function Documentation

◆ buildBatchQuery()

ILIAS\News\Persistence\NewsRepository::buildBatchQuery ( array  $obj_ids,
NewsCriteria  $criteria,
bool  $only_id = false 
)
private

Definition at line 207 of file NewsRepository.php.

207 : array
208 {
209 $values = [];
210 $types = [];
211 $joins = '';
212
213 if ($only_id) {
214 $columns = ['il_news_item.id'];
215 } else {
216 $columns = [
217 'il_news_item.*',
218 'COALESCE((SELECT ref_id FROM object_reference WHERE object_reference.obj_id = il_news_item.context_obj_id LIMIT 1), 0) AS ref_id'
219 ];
220 }
221
222 if ($criteria->isIncludeReadStatus()) {
223 if ($criteria->getReadUserId() === null) {
224 throw new \InvalidArgumentException("Read user id is required for read status");
225 }
226
227 $columns[] = 'il_news_read.user_id AS user_read';
228 $joins .= 'LEFT JOIN il_news_read ON il_news_item.id = il_news_read.news_id AND il_news_read.user_id = %s ';
229
230 $values[] = $criteria->getReadUserId();
231 $types[] = ilDBConstants::T_INTEGER;
232 }
233
234 $query = "SELECT " . join(', ', $columns) . " FROM il_news_item {$joins} WHERE "
235 . $this->db->in('context_obj_id', $obj_ids, false, ilDBConstants::T_INTEGER);
236
237 if ($criteria->getPeriod() > 0) {
238 $query .= " AND creation_date >= %s";
239 $values[] = self::parseTimePeriod($criteria->getPeriod());
241 }
242
243 if ($criteria->getStartDate()) {
244 $query .= " AND creation_date >= %s";
245 $values[] = $criteria->getStartDate()->format('Y-m-d H:i:s');
247 }
248
249 if ($criteria->isNoAutoGenerated()) {
250 $query .= " AND priority = 1 AND content_type = 'text'";
251 }
252
253 if ($criteria->getMinPriority() !== null || $criteria->getMaxPriority() !== null) {
254 $operator = $criteria->getMinPriority() !== null ? '>=' : '<=';
255 $query .= " AND n.priority {$operator} %s";
256 $values[] = $criteria->getMinPriority();
257 $types[] = ilDBConstants::T_INTEGER;
258 }
259
260 if ($criteria->isOnlyPublic()) {
261 $query .= " AND visibility = '" . NEWS_PUBLIC . "'";
262 }
263
264 $query .= " ORDER BY creation_date DESC";
265
266 return [$query, $types, $values];
267 }
static parseTimePeriod(string|int $time_period)
const NEWS_PUBLIC

References ILIAS\News\Data\NewsCriteria\getMaxPriority(), ILIAS\News\Data\NewsCriteria\getMinPriority(), ILIAS\News\Data\NewsCriteria\getPeriod(), ILIAS\News\Data\NewsCriteria\getReadUserId(), ILIAS\News\Data\NewsCriteria\getStartDate(), ILIAS\News\Data\NewsCriteria\isIncludeReadStatus(), ILIAS\News\Data\NewsCriteria\isNoAutoGenerated(), ILIAS\News\Data\NewsCriteria\isOnlyPublic(), NEWS_PUBLIC, ILIAS\News\Persistence\NewsRepository\parseTimePeriod(), ilDBConstants\T_INTEGER, and ilDBConstants\T_TIMESTAMP.

Referenced by ILIAS\News\Persistence\NewsRepository\findByContextsBatch(), and ILIAS\News\Persistence\NewsRepository\findByContextsBatchLazy().

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

◆ buildFindQuery()

ILIAS\News\Persistence\NewsRepository::buildFindQuery ( ?array  $news_ids = null)
private

Definition at line 109 of file NewsRepository.php.

109 : string
110 {
111 $query = "
112 SELECT il_news_item.*,
113 COALESCE(
114 (SELECT ref_id FROM object_reference WHERE object_reference.obj_id = il_news_item.context_obj_id LIMIT 1),
115 0
116 ) AS ref_id
117 FROM il_news_item ";
118
119 if ($news_ids !== null) {
120 $query .= "WHERE " . $this->db->in('id', $news_ids, false, \ilDBConstants::T_INTEGER);
121 }
122
123 return $query;
124 }

References ilDBConstants\T_INTEGER.

Referenced by ILIAS\News\Persistence\NewsRepository\findByIds(), and ILIAS\News\Persistence\NewsRepository\loadLazyItems().

+ Here is the caller graph for this function:

◆ countByContextsBatch()

ILIAS\News\Persistence\NewsRepository::countByContextsBatch ( array  $contexts)
Parameters
NewsContext[]$contexts
Returns
array{0: NewsContext, 1: int}[]

Definition at line 185 of file NewsRepository.php.

185 : array
186 {
187 $context_map = [];
188 foreach ($contexts as $context) {
189 $context_map[$context->getObjId()] = $context;
190 }
191
192 $in_clause = $this->db->in('context_obj_id', array_keys($context_map), false, ilDBConstants::T_INTEGER);
193 $query = "SELECT context_obj_id, count(context_obj_id) as count FROM il_news_item WHERE {$in_clause} GROUP BY context_obj_id";
194 $result = $this->db->query($query);
195
196 $count = [];
197 foreach ($this->db->fetchAll($result) as $row) {
198 $count[] = [
199 $context_map[$row['context_obj_id']],
200 $row['count']
201 ];
202 }
203
204 return $count;
205 }
$context
Definition: webdav.php:31

References $context, and ilDBConstants\T_INTEGER.

◆ findByContextsBatch()

ILIAS\News\Persistence\NewsRepository::findByContextsBatch ( array  $contexts,
NewsCriteria  $criteria 
)
Parameters
NewsContext[]$contexts

Definition at line 129 of file NewsRepository.php.

129 : NewsCollection
130 {
131 if (empty($contexts)) {
132 return new NewsCollection();
133 }
134
135 $obj_ids = array_map(fn($context) => $context->getObjId(), $contexts);
136 $result = $this->db->queryF(...$this->buildBatchQuery($obj_ids, $criteria));
137
138 $items = [];
139 $user_read = [];
140
141 while ($row = $this->db->fetchAssoc($result)) {
142 $items[] = $this->factory->newsItem($row);
143 $user_read[$row['id']] = isset($row['user_read']) && $row['user_read'] !== 0;
144 }
145
146 $collection = new NewsCollection($items);
147 if ($criteria->isIncludeReadStatus()) {
148 $collection->setUserReadStatus($criteria->getReadUserId(), $user_read);
149 }
150
151 return $collection;
152 }
factory()
buildBatchQuery(array $obj_ids, NewsCriteria $criteria, bool $only_id=false)

References $context, ILIAS\News\Persistence\NewsRepository\buildBatchQuery(), factory(), ILIAS\News\Data\NewsCriteria\getReadUserId(), and ILIAS\News\Data\NewsCriteria\isIncludeReadStatus().

+ Here is the call graph for this function:

◆ findByContextsBatchLazy()

ILIAS\News\Persistence\NewsRepository::findByContextsBatchLazy ( array  $contexts,
NewsCriteria  $criteria 
)
Parameters
NewsContext[]$contexts

Definition at line 157 of file NewsRepository.php.

157 : LazyNewsCollection
158 {
159 if (empty($contexts)) {
160 return new LazyNewsCollection();
161 }
162
163 $obj_ids = array_map(fn($context) => $context->getObjId(), $contexts);
164 $result = $this->db->queryF(...$this->buildBatchQuery($obj_ids, $criteria, true));
165
166 $items = [];
167 $user_read = [];
168 while ($row = $this->db->fetchAssoc($result)) {
169 $items[] = $row['id'];
170 $user_read[$row['id']] = isset($row['user_read']) && $row['user_read'] !== 0;
171 }
172
173 $collection = new LazyNewsCollection($items, fn(...$args) => $this->loadLazyItems(...$args));
174 if ($criteria->isIncludeReadStatus()) {
175 $collection->setUserReadStatus($criteria->getReadUserId(), $user_read);
176 }
177
178 return $collection;
179 }
loadLazyItems(array $news_ids, array $group_context_types)

References $context, ILIAS\News\Persistence\NewsRepository\buildBatchQuery(), ILIAS\News\Data\NewsCriteria\getReadUserId(), ILIAS\News\Data\NewsCriteria\isIncludeReadStatus(), and ILIAS\News\Persistence\NewsRepository\loadLazyItems().

+ Here is the call graph for this function:

◆ findById()

ILIAS\News\Persistence\NewsRepository::findById ( int  $news_id)

Definition at line 43 of file NewsRepository.php.

43 : ?NewsItem
44 {
45 $query = "SELECT * FROM il_news_item WHERE id = %s";
46 $result = $this->db->queryF($query, [\ilDBConstants::T_INTEGER], [$news_id]);
47
48 return $result->numRows()
49 ? $this->factory->newsItem($this->db->fetchAssoc($result))
50 : null;
51 }

References factory(), and ilDBConstants\T_INTEGER.

+ Here is the call graph for this function:

◆ findByIds()

ILIAS\News\Persistence\NewsRepository::findByIds ( array  $news_ids)
Parameters
int[]$news_ids
Returns
NewsItem[]

Definition at line 57 of file NewsRepository.php.

57 : array
58 {
59 if (empty($news_ids)) {
60 return [];
61 }
62
63 $result = $this->db->query($this->buildFindQuery($news_ids));
64 return array_map(fn($row) => $this->factory->newsItem($row), $this->db->fetchAll($result));
65 }

References ILIAS\News\Persistence\NewsRepository\buildFindQuery(), and factory().

+ Here is the call graph for this function:

◆ loadLazyItems()

ILIAS\News\Persistence\NewsRepository::loadLazyItems ( array  $news_ids,
array  $group_context_types 
)
Parameters
int[]$news_ids
string[]$group_context_types
Returns
NewsItem[]

Definition at line 73 of file NewsRepository.php.

73 : array
74 {
75 if (empty($news_ids)) {
76 return [];
77 }
78
79 $result = $this->db->query($this->buildFindQuery($news_ids));
80 $news_items = [];
81 $additional_obj_ids = [];
82
83 foreach ($this->db->fetchAll($result) as $row) {
84 $news_item = $this->factory->newsItem($row);
85
86 if (in_array($news_item->getContextObjType(), $group_context_types)) {
87 $additional_obj_ids[] = $news_item->getContextObjId();
88 }
89
90 $news_items[] = $news_item;
91 }
92
93 if (empty($additional_obj_ids)) {
94 return $news_items;
95 }
96
97 // Fetch all additional items with same context_obj_id for grouping
98 $query = $this->buildFindQuery()
99 . " WHERE " . $this->db->in('context_obj_id', $additional_obj_ids, false, \ilDBConstants::T_INTEGER)
100 . " AND " . $this->db->in('id', $news_ids, true, \ilDBConstants::T_INTEGER);
101 $result = $this->db->query($query);
102
103 return array_merge(
104 $news_items,
105 array_map(fn($row) => $this->factory->newsItem($row), $this->db->fetchAll($result))
106 );
107 }

References ILIAS\News\Persistence\NewsRepository\buildFindQuery(), factory(), and ilDBConstants\T_INTEGER.

Referenced by ILIAS\News\Persistence\NewsRepository\findByContextsBatchLazy().

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

◆ parseTimePeriod()

static ILIAS\News\Persistence\NewsRepository::parseTimePeriod ( string|int  $time_period)
staticprivate

Definition at line 269 of file NewsRepository.php.

269 : string
270 {
271 // time period is a number of days
272 if (is_numeric($time_period) && $time_period > 0) {
273 return date('Y-m-d H:i:s', time() - ($time_period * 24 * 60 * 60));
274 }
275
276 // time period is datetime (string)
277 if (preg_match("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/", $time_period)) {
278 return $time_period;
279 }
280
281 return '';
282 }

Referenced by ILIAS\News\Persistence\NewsRepository\buildBatchQuery().

+ Here is the caller graph for this function:

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