ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
PageDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Wiki\Page;
22 
24 
29 {
31  protected \ilDBInterface $db;
32 
33  public function __construct(
34  InternalDataService $data,
35  \ilDBInterface $db
36  ) {
37  $this->data = $data;
38  $this->db = $db;
39  }
40 
41  public function delete($id, $lang = "-"): void
42  {
43  $and = in_array($lang, ["", "-"])
44  ? ""
45  : " AND lang = " . $this->db->quote($lang, "text");
46  $query = "DELETE FROM il_wiki_page" .
47  " WHERE id = " . $this->db->quote($id, "integer") .
48  $and;
49  $this->db->manipulate($query);
50  }
51 
52  protected function getPageFromRecord(array $rec): Page
53  {
54  return $this->data->page(
55  (int) $rec["id"],
56  (int) $rec["wiki_id"],
57  $rec["title"],
58  $rec["lang"],
59  (bool) $rec["blocked"],
60  (bool) $rec["rating"],
61  (bool) $rec["hide_adv_md"]
62  );
63  }
64 
65  protected function getPageInfoFromRecord(array $rec): PageInfo
66  {
67  return $this->data->pageInfo(
68  (int) $rec["id"],
69  $rec["lang"] ?? "",
70  $rec["title"],
71  (int) ($rec["last_change_user"] ?? 0),
72  $rec["last_change"] ?? "",
73  (int) ($rec["create_user"] ?? 0),
74  $rec["created"] ?? "",
75  (int) ($rec["cnt"] ?? 0),
76  (int) ($rec["nr"] ?? 0)
77  );
78  }
79 
83  public function getWikiPages(int $wiki_id, string $lang = "-"): \Iterator
84  {
85  $set = $this->db->queryF(
86  "SELECT * FROM il_wiki_page " .
87  " WHERE lang = %s AND wiki_id = %s ORDER BY title",
88  ["string", "integer"],
89  [$lang, $wiki_id]
90  );
91  while ($rec = $this->db->fetchAssoc($set)) {
92  yield $this->getPageFromRecord($rec);
93  }
94  }
95 
99  public function getMasterPagesWithoutTranslation(int $wiki_id, string $trans): \Iterator
100  {
101  $set = $this->db->queryF(
102  "SELECT w1.* FROM il_wiki_page w1 LEFT JOIN il_wiki_page w2 " .
103  " ON w1.id = w2.id AND w2.lang = %s " .
104  " WHERE w1.lang = %s AND w1.wiki_id = %s AND w2.id IS NULL ORDER BY w1.title",
105  ["string", "string", "integer"],
106  [$trans, "-", $wiki_id]
107  );
108  while ($rec = $this->db->fetchAssoc($set)) {
109  yield $this->getPageFromRecord($rec);
110  }
111  }
112 
117  public function getAllPagesInfo(int $wiki_id): \Iterator
118  {
119  $set = $this->db->queryF(
120  "SELECT w.id, p.last_change_user, p.last_change, w.title " . "FROM page_object p JOIN il_wiki_page w " .
121  " ON (w.wiki_id = %s AND p.parent_type = %s AND p.page_id = w.id AND w.lang = %s) " .
122  " JOIN ( select page_id, max(last_change) mlc FROM page_object " .
123  " WHERE parent_type='wpg' group by page_id) mp " .
124  " ON (mp.page_id = p.page_id AND mp.mlc = p.last_change)",
125  ["integer", "string", "string"],
126  [$wiki_id, "wpg", "-"]
127  );
128  $ids = [];
129  while ($rec = $this->db->fetchAssoc($set)) {
130  // note: the query may get multiple entries for a page id, if multiple
131  // languages share the same max(last_change). We only return one.
132  if (isset($ids[(int) $rec["id"]])) {
133  continue;
134  }
135  $ids[(int) $rec["id"]] = (int) $rec["id"];
136  yield $this->getPageInfoFromRecord($rec);
137  }
138  }
139 
140  public function getInfoOfSelected($wiki_id, array $ids, $lang = "-"): \Iterator
141  {
142  $query = "SELECT wp.id, p.last_change_user, p.last_change, wp.title, wp.lang " .
143  " FROM il_wiki_page wp JOIN page_object p " .
144  " ON (wp.id = p.page_id AND wp.lang = p.lang) " .
145  " WHERE " . $this->db->in("wp.id", $ids, false, "integer") .
146  " AND p.parent_type = " . $this->db->quote("wpg", "text") .
147  " AND wp.wiki_id = " . $this->db->quote($wiki_id, "integer") .
148  " AND wp.lang = " . $this->db->quote($lang, "text") .
149  " ORDER BY title";
150  $set = $this->db->query($query);
151 
152  while ($rec = $this->db->fetchAssoc($set)) {
153  yield $this->getPageInfoFromRecord($rec);
154  }
155  }
156 
161  public function getRecentChanges(
162  int $wiki_id,
163  int $period = 30
164  ): \Iterator {
165  $limit_ts = date('Y-m-d H:i:s', time() - ($period * 24 * 60 * 60));
166  $q1 = "SELECT w.id, p.last_change_user, p.last_change, w.title, w.lang, 0 nr FROM page_object p " .
167  " JOIN il_wiki_page w ON (w.id = p.page_id AND p.parent_type = %s AND w.lang = p.lang AND w.wiki_id = %s) " .
168  " WHERE p.last_change >= " . $this->db->quote($limit_ts, "timestamp");
169  $q2 = "SELECT w.id, p.user_id last_change_user, p.hdate last_change, w.title, w.lang, p.nr FROM page_history p " .
170  " JOIN il_wiki_page w ON (w.id = p.page_id AND p.parent_type = %s AND w.lang = p.lang AND w.wiki_id = %s) " .
171  " WHERE p.hdate >= " . $this->db->quote($limit_ts, "timestamp");
172  $q = $q1 . " UNION " . $q2 . " ORDER BY last_change DESC ";
173  $set = $this->db->queryF(
174  $q,
175  ["string", "integer", "string", "integer"],
176  ["wpg", $wiki_id,"wpg", $wiki_id]
177  );
178  while ($rec = $this->db->fetchAssoc($set)) {
179  yield $this->getPageInfoFromRecord($rec);
180  }
181  }
182 
186  public function getNewPages(int $wiki_id): \Iterator
187  {
188  $set = $this->db->queryF(
189  "SELECT w.id, p.created, p.create_user, w.title, w.lang FROM page_object p " .
190  " JOIN il_wiki_page w " .
191  " ON w.id = p.page_id AND p.parent_type = %s AND w.lang = p.lang AND w.wiki_id = %s " .
192  " ORDER BY created DESC",
193  ["string", "integer"],
194  ["wpg", $wiki_id]
195  );
196  while ($rec = $this->db->fetchAssoc($set)) {
197  yield $this->getPageInfoFromRecord($rec);
198  }
199  }
200 
204  public function getPopularPages(
205  int $a_wiki_id
206  ): \Iterator {
207 
208  $query = "SELECT wp.id, wp.title, wp.lang, po.view_cnt as cnt FROM il_wiki_page wp JOIN page_object po" .
209  " ON (wp.id = po.page_id AND wp.lang = po.lang) " .
210  " WHERE wp.wiki_id = " . $this->db->quote($a_wiki_id, "integer") .
211  " AND po.parent_type = " . $this->db->quote("wpg", "text") . " " .
212  " ORDER BY po.view_cnt";
213  $set = $this->db->query($query);
214 
215  while ($rec = $this->db->fetchAssoc($set)) {
216  yield $this->getPageInfoFromRecord($rec);
217  }
218  }
219 
223  public function getLanguages(int $wpg_id): array
224  {
225  $set = $this->db->queryF(
226  "SELECT DISTINCT lang FROM " .
227  " il_wiki_page WHERE id = %s AND lang <> '-' ",
228  ["integer"],
229  [$wpg_id]
230  );
231  $langs = [];
232  while ($rec = $this->db->fetchAssoc($set)) {
233  $langs[] = $rec["lang"];
234  }
235  return $langs;
236  }
237 
238  public function doesAtLeastOnePageExist(int $wiki_id, array $ids): bool
239  {
240  // cross check existence of sources in il_wiki_page
241  $query = "SELECT count(*) cnt FROM il_wiki_page" .
242  " WHERE " . $this->db->in("id", $ids, false, "integer") .
243  " AND wiki_id = " . $this->db->quote($wiki_id, "integer") .
244  " GROUP BY wiki_id";
245  $set = $this->db->query($query);
246  $rec = $this->db->fetchAssoc($set);
247  return ((int) $rec["cnt"]) > 0;
248  }
249 
250  public function getPageIdForTitle(
251  int $wiki_id,
252  string $title,
253  string $lang = "-"
254  ): ?int {
255  if ($lang === "") {
256  $lang = "-";
257  }
258  $title = \ilWikiUtil::makeDbTitle($title);
259 
260  $query = "SELECT w.id FROM il_wiki_page w " .
261  " JOIN page_object p ON (w.id = p.page_id AND w.lang = p.lang) " .
262  " WHERE w.wiki_id = " . $this->db->quote($wiki_id, "integer") .
263  " AND w.title = " . $this->db->quote($title, "text") .
264  " AND w.lang = " . $this->db->quote($lang, "text");
265  $set = $this->db->query($query);
266  if ($rec = $this->db->fetchAssoc($set)) {
267  return (int) $rec["id"];
268  }
269 
270  return null;
271  }
272 
273  public function existsByTitle(
274  int $wiki_id,
275  string $title,
276  string $lang = "-"
277  ): bool {
278  $id = $this->getPageIdForTitle($wiki_id, $title, $lang);
279  if (is_null($id)) {
280  return false;
281  }
282  return $this->exists($id, $lang);
283  }
284 
285  public function exists(
286  int $id,
287  string $lang = "-"
288  ): bool {
289  if ($lang === "") {
290  $lang = "-";
291  }
292  $query = "SELECT w.id FROM il_wiki_page w " .
293  " JOIN page_object p ON (w.id = p.page_id AND w.lang = p.lang) " .
294  " WHERE w.id = " . $this->db->quote($id, "integer") .
295  " AND w.lang = " . $this->db->quote($lang, "text");
296  $set = $this->db->query($query);
297  if ($rec = $this->db->fetchAssoc($set)) {
298  return true;
299  }
300  return false;
301  }
302 
303  public function getTitle(
304  int $id,
305  string $lang = "-"
306  ): string {
307  if ($lang === "") {
308  $lang = "-";
309  }
310  $query = "SELECT title FROM il_wiki_page " .
311  " WHERE id = " . $this->db->quote($id, "integer") .
312  " AND lang = " . $this->db->quote($lang, "text");
313  $set = $this->db->query($query);
314  if ($rec = $this->db->fetchAssoc($set)) {
315  return $rec["title"];
316  }
317  return "";
318  }
319 
320  public function getWikiIdByPageId(
321  int $id
322  ): ?int {
323  $query = "SELECT wiki_id FROM il_wiki_page" .
324  " WHERE id = " . $this->db->quote($id, "integer") .
325  " AND lang = " . $this->db->quote('-', "text");
326  $set = $this->db->query($query);
327  if ($rec = $this->db->fetchAssoc($set)) {
328  return (int) $rec["wiki_id"];
329  }
330  return null;
331  }
332 
333 }
existsByTitle(int $wiki_id, string $title, string $lang="-")
getTitle(int $id, string $lang="-")
getWikiPages(int $wiki_id, string $lang="-")
quote($value, string $type)
Wiki page info.
Definition: PageInfo.php:26
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct(InternalDataService $data, \ilDBInterface $db)
getRecentChanges(int $wiki_id, int $period=30)
Queries last change and user per page regardless of language.
$lang
Definition: xapiexit.php:25
static makeDbTitle(string $a_par)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$q
Definition: shib_logout.php:21
exists(int $id, string $lang="-")
getPageIdForTitle(int $wiki_id, string $title, string $lang="-")
getAllPagesInfo(int $wiki_id)
Queries last change and user per page regardless of language.
getInfoOfSelected($wiki_id, array $ids, $lang="-")
Wiki page.
Definition: Page.php:26
doesAtLeastOnePageExist(int $wiki_id, array $ids)
getMasterPagesWithoutTranslation(int $wiki_id, string $trans)