ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilWikiPage.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2005 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once("./Services/COPage/classes/class.ilPageObject.php");
25 
34 class ilWikiPage extends ilPageObject
35 {
41  function __construct($a_id = 0, $a_old_nr = 0)
42  {
43  parent::__construct("wpg", $a_id, $a_old_nr);
44  }
45 
51  function setTitle($a_title)
52  {
53  $this->title = ilWikiUtil::makeDbTitle($a_title);
54  }
55 
61  function getTitle()
62  {
63  return $this->title;
64  }
65 
71  function setWikiId($a_wikiid)
72  {
73  $this->setParentId($a_wikiid);
74  }
75 
81  function getWikiId()
82  {
83  return $this->getParentId();
84  }
85 
89  function create()
90  {
91  global $ilDB;
92 
93  $query = "INSERT INTO il_wiki_page (".
94  "title".
95  ", wiki_id".
96  " ) VALUES (".
97  $ilDB->quote($this->getTitle())
98  .",".$ilDB->quote($this->getWikiId())
99  .")";
100  $ilDB->query($query);
101 
102  $id = $ilDB->getLastInsertId();
103  $this->setId($id);
104 
105  // create page object
106  parent::create();
107 
108  $this->saveInternalLinks($this->getXMLContent());
109  }
110 
117  function update($a_validate = true, $a_no_history = false)
118  {
119  global $ilDB;
120 
121  // update wiki page data
122  $query = "UPDATE il_wiki_page SET ".
123  " title = ".$ilDB->quote($this->getTitle()).
124  ",wiki_id = ".$ilDB->quote($this->getWikiId()).
125  " WHERE id = ".$ilDB->quote($this->getId());
126  $ilDB->query($query);
127  parent::update($a_validate, $a_no_history);
128 
129  return true;
130  }
131 
135  function read()
136  {
137  global $ilDB;
138 
139  $query = "SELECT * FROM il_wiki_page WHERE id = ".
140  $ilDB->quote($this->getId());
141  $set = $ilDB->query($query);
142  $rec = $set->fetchRow(DB_FETCHMODE_ASSOC);
143 
144  $this->setTitle($rec["title"]);
145  $this->setWikiId($rec["wiki_id"]);
146 
147  // get co page
148  parent::read();
149  }
150 
151 
157  function delete()
158  {
159  global $ilDB;
160 
161  // get other pages that link to this page
162  $linking_pages = ilWikiPage::getLinksToPage($this->getWikiId(),
163  $this->getId());
164 
165  // delete internal links information to this page
166  include_once("./Services/COPage/classes/class.ilInternalLink.php");
168 
169  // delete comments and notes of this page
170  // (we keep them first)
171 
172  // delete record of table il_wiki_data
173  $query = "DELETE FROM il_wiki_page".
174  " WHERE id = ".$ilDB->quote($this->getId());
175 
176  $ilDB->query($query);
177 
178  // delete co page
179  parent::delete();
180 
181  // make links of other pages to this page a missing link
182  foreach($linking_pages as $lp)
183  {
184  $st = $ilDB->prepareManip("REPLACE INTO il_wiki_missing_page ".
185  "(wiki_id, source_id, target_name) VALUES ".
186  "(?,?,?)", array("integer", "integer", "text"));
187  $ilDB->execute($st, array($this->getWikiId(), $lp["id"],
188  $this->getTitle()));
189  }
190 
191  return true;
192  }
193 
199  static function deleteAllPagesOfWiki($a_wiki_id)
200  {
201  global $ilDB;
202 
203  // delete record of table il_wiki_data
204  $query = "SELECT * FROM il_wiki_page".
205  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id);
206  $set = $ilDB->query($query);
207 
208  while($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
209  {
210  $wiki_page = new ilWikiPage($rec["id"]);
211  $wiki_page->delete();
212  }
213  }
214 
218  static function exists($a_wiki_id, $a_title)
219  {
220  global $ilDB;
221 
222  $a_title = ilWikiUtil::makeDbTitle($a_title);
223 
224  $query = "SELECT * FROM il_wiki_page".
225  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id).
226  " AND title = ".$ilDB->quote($a_title);
227  $set = $ilDB->query($query);
228  if($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
229  {
230  return true;
231  }
232 
233  return false;
234  }
235 
239  static function getPageIdForTitle($a_wiki_id, $a_title)
240  {
241  global $ilDB;
242 
243  $a_title = ilWikiUtil::makeDbTitle($a_title);
244 
245  $query = "SELECT * FROM il_wiki_page".
246  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id).
247  " AND title = ".$ilDB->quote($a_title);
248  $set = $ilDB->query($query);
249  if($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
250  {
251  return $rec["id"];
252  }
253 
254  return false;
255  }
256 
260  static function lookupTitle($a_page_id)
261  {
262  global $ilDB;
263 
264  $query = "SELECT * FROM il_wiki_page".
265  " WHERE id = ".$ilDB->quote($a_page_id);
266  $set = $ilDB->query($query);
267  if($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
268  {
269  return $rec["title"];
270  }
271 
272  return false;
273  }
274 
280  static function getAllPages($a_wiki_id)
281  {
282  global $ilDB;
283 
284  $pages = parent::getAllPages("wpg", $a_wiki_id);
285 
286  $query = "SELECT * FROM il_wiki_page".
287  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id).
288  " ORDER BY title";
289  $set = $ilDB->query($query);
290 
291  while($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
292  {
293  if (isset($pages[$rec["id"]]))
294  {
295  $pages[$rec["id"]]["title"] = $rec["title"];
296  }
297  }
298 
299  return $pages;
300  }
301 
305  static function getLinksToPage($a_wiki_id, $a_page_id)
306  {
307  global $ilDB;
308 
309  include_once("./Services/COPage/classes/class.ilInternalLink.php");
310  $sources = ilInternalLink::_getSourcesOfTarget("wpg", $a_page_id, 0);
311 
312  $ids = array();
313  foreach ($sources as $source)
314  {
315  if ($source["type"] == "wpg:pg")
316  {
317  $ids[] = $source["id"];
318  }
319  }
320  // get wiki page record
321  $query = "SELECT * FROM il_wiki_page wp, page_object p".
322  " WHERE wp.id IN (".implode(",",ilUtil::quoteArray($ids)).")".
323  " AND wp.id = p.page_id AND p.parent_type = 'wpg'".
324  " AND wp.wiki_id = ".$ilDB->quote($a_wiki_id).
325  " ORDER BY title";
326  $set = $ilDB->query($query);
327 
328  $pages = array();
329  while ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
330  {
331  $pages[] = array_merge($rec, array("user" => $rec["last_change_user"],
332  "date" => $rec["last_change"]));
333  }
334 
335  return $pages;
336  }
337 
343  static function getOrphanedPages($a_wiki_id)
344  {
345  global $ilDB;
346 
347  $pages = ilWikiPage::getAllPages($a_wiki_id);
348 
349  include_once("./Services/COPage/classes/class.ilInternalLink.php");
350 
351  $orphaned = array();
352  foreach ($pages as $k => $page)
353  {
354  $sources = ilInternalLink::_getSourcesOfTarget("wpg", $page["id"], 0);
355 
356  $ids = array();
357  foreach ($sources as $source)
358  {
359  if ($source["type"] == "wpg:pg")
360  {
361  $ids[] = $source["id"];
362  }
363  }
364  // delete record of table il_wiki_data
365  $query = "SELECT count(*) AS cnt FROM il_wiki_page".
366  " WHERE id IN (".implode(",",ilUtil::quoteArray($ids)).")".
367  " AND wiki_id = ".$ilDB->quote($a_wiki_id).
368  " ORDER BY title";
369  $set = $ilDB->query($query);
370  $rec = $set->fetchRow(DB_FETCHMODE_ASSOC);
371  if ($rec["cnt"] == 0 &&
372  ilObjWiki::_lookupStartPage($a_wiki_id) != $page["title"])
373  {
374  $orphaned[] = $page;
375  }
376  }
377 
378  return $orphaned;
379  }
380 
386  static function _wikiPageExists($a_wiki_id, $a_title)
387  {
388  global $ilDB;
389 
390  $a_title = ilWikiUtil::makeDbTitle($a_title);
391 
392  // delete record of table il_wiki_data
393  $query = "SELECT * FROM il_wiki_page".
394  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id).
395  " AND title = ".$ilDB->quote($a_title);
396  $set = $ilDB->query($query);
397 
398  $pages = array();
399  if ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
400  {
401  return true;
402  }
403 
404  return false;
405  }
406 
412  static function getParentObjectContributors($a_wiki_id)
413  {
414  global $ilDB;
415 
416  $contributors = parent::getParentObjectContributors("wpg", $a_wiki_id);
417 
418  return $contributors;
419  }
420 
426  static function getPageContributors($a_page_id)
427  {
428  global $ilDB;
429 
430  $contributors = parent::getPageContributors("wpg", $a_page_id);
431 
432  return $contributors;
433  }
434 
435 
441  function saveInternalLinks($a_xml)
442  {
443  global $ilDB;
444 
445 
446  // *** STEP 1: Standard Processing ***
447 
449 
450 
451  // *** STEP 2: Other Pages -> This Page ***
452 
453  // Check, whether ANOTHER page links to this page as a "missing" page
454  // (this is the case, when this page is created newly)
455  $stmt = $ilDB->prepare("SELECT * FROM il_wiki_missing_page WHERE ".
456  " wiki_id = ? AND target_name = ?", array("integer", "text"));
457  $set = $ilDB->execute($stmt, array($this->getWikiId(), $this->getTitle()));
458  while ($anmiss = $ilDB->fetchAssoc($set)) // insert internal links instead
459  {
460  ilInternalLink::_saveLink("wpg:pg", $anmiss["source_id"], "wpg",
461  $this->getId(), 0);
462  }
463 
464  // now remove the missing page entries
465  $stmt = $ilDB->prepareManip("DELETE FROM il_wiki_missing_page WHERE ".
466  " wiki_id = ? AND target_name = ?", array("integer", "text"));
467  $ilDB->execute($stmt, array($this->getWikiId(), $this->getTitle()));
468 
469 
470  // *** STEP 3: This Page -> Other Pages ***
471 
472  // remove the exising "missing page" links for THIS page (they will be re-inserted below)
473  $stmt = $ilDB->prepareManip("DELETE FROM il_wiki_missing_page WHERE ".
474  " wiki_id = ? AND source_id = ?", array("integer", "integer"));
475  $ilDB->execute($stmt, array($this->getWikiId(), $this->getId()));
476 
477  // collect the wiki links of the page
478  include_once("./Modules/Wiki/classes/class.ilWikiUtil.php");
479  $int_wiki_links = ilWikiUtil::collectInternalLinks($a_xml, $this->getWikiId(), true);
480 
481  foreach($int_wiki_links as $wlink)
482  {
483  $page_id = ilWikiPage::_getPageIdForWikiTitle($this->getWikiId(), $wlink);
484 
485  if ($page_id > 0) // save internal link for existing page
486  {
487  ilInternalLink::_saveLink("wpg:pg", $this->getId(), "wpg",
488  $page_id, 0);
489  }
490  else // save missing link for non-existing page
491  {
492  $stmt = $ilDB->prepareManip("REPLACE INTO il_wiki_missing_page (wiki_id, source_id, target_name)".
493  " VALUES (?,?,?)", array("integer", "integer", "text"));
494  $ilDB->execute($stmt, array($this->getWikiId(), $this->getId(), $wlink));
495  }
496  }
497  }
498 
502  static function _getPageIdForWikiTitle($a_wiki_id, $a_title)
503  {
504  global $ilDB;
505 
506  $query = "SELECT * FROM il_wiki_page".
507  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id).
508  " AND title = ".$ilDB->quote($a_title);
509  $set = $ilDB->query($query);
510  if($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
511  {
512  return $rec["id"];
513  }
514 
515  return false;
516  }
517 
523  static function getPopularPages($a_wiki_id)
524  {
525  global $ilDB;
526 
527  $query = "SELECT wp.*, po.view_cnt as cnt FROM il_wiki_page as wp, page_object as po".
528  " WHERE wp.wiki_id = ".$ilDB->quote($a_wiki_id).
529  " AND wp.id = po.page_id ".
530  " AND po.parent_type = 'wpg' ".
531  " ORDER BY po.view_cnt";
532  $set = $ilDB->query($query);
533 
534  $pages = array();
535  while($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
536  {
537  $pages[] = $rec;
538  }
539 
540  return $pages;
541  }
542 
548  static function countPages($a_wiki_id)
549  {
550  global $ilDB;
551 
552  // delete record of table il_wiki_data
553  $query = "SELECT count(*) as cnt FROM il_wiki_page".
554  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id);
555  $s = $ilDB->query($query);
556  $r = $s->fetchRow(DB_FETCHMODE_ASSOC);
557 
558  return $r["cnt"];
559  }
560 
566  static function getRandomPage($a_wiki_id)
567  {
568  global $ilDB;
569 
570  $cnt = ilWikiPage::countPages($a_wiki_id);
571 
572  if ($cnt < 1)
573  {
574  return "";
575  }
576 
577  $rand = rand(1, $cnt);
578 
579  // delete record of table il_wiki_data
580  $query = "SELECT title FROM il_wiki_page".
581  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id).
582  " LIMIT $rand, 1";
583  $s = $ilDB->query($query);
584  $r = $s->fetchRow(DB_FETCHMODE_ASSOC);
585 
586  return $r["title"];
587  }
588 
594  static function getNewPages($a_wiki_id)
595  {
596  global $ilDB;
597 
598  $pages = parent::getNewPages("wpg", $a_wiki_id);
599 
600  foreach($pages as $k => $page)
601  {
602  $pages[$k]["title"] = ilWikiPage::lookupTitle($page["id"]);
603  }
604 
605  return $pages;
606  }
607 
608 
609 } // END class.ilWikiPage
610 ?>