ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilWikiPage.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once("./Services/COPage/classes/class.ilPageObject.php");
5 include_once("./Modules/Wiki/classes/class.ilWikiUtil.php");
14 class ilWikiPage extends ilPageObject
15 {
16  protected $blocked = false;
17 
23  function __construct($a_id = 0, $a_old_nr = 0)
24  {
25  parent::__construct("wpg", $a_id, $a_old_nr);
26  }
27 
33  function setTitle($a_title)
34  {
35  $this->title = ilWikiUtil::makeDbTitle($a_title);
36  }
37 
43  function getTitle()
44  {
45  return $this->title;
46  }
47 
53  function setWikiId($a_wikiid)
54  {
55  $this->setParentId($a_wikiid);
56  }
57 
63  function getWikiId()
64  {
65  return $this->getParentId();
66  }
67 
73  function setWikiRefId($a_wiki_ref_id)
74  {
75  $this->parent_ref_id = $a_wiki_ref_id;
76  }
77 
83  function getWikiRefId()
84  {
85  return $this->parent_ref_id;
86  }
87 
93  public function setBlocked($a_val)
94  {
95  $this->blocked = $a_val;
96  }
97 
103  public function getBlocked()
104  {
105  return $this->blocked;
106  }
107 
111  function create($a_prevent_page_creation = false)
112  {
113  global $ilDB;
114 
115  $id = $ilDB->nextId("il_wiki_page");
116  $this->setId($id);
117  $query = "INSERT INTO il_wiki_page (".
118  "id".
119  ", title".
120  ", wiki_id".
121  ", blocked".
122  " ) VALUES (".
123  $ilDB->quote($this->getId(), "integer")
124  .",".$ilDB->quote($this->getTitle(), "text")
125  .",".$ilDB->quote($this->getWikiId(), "integer")
126  .",".$ilDB->quote($this->getBlocked(), "integer")
127  .")";
128  $ilDB->manipulate($query);
129 
130  // create page object
131  if (!$a_prevent_page_creation)
132  {
133  parent::create();
134  $this->saveInternalLinks($this->getXMLContent());
135 
136  include_once "./Services/Notification/classes/class.ilNotification.php";
138  }
139 
140  $this->updateNews();
141  }
142 
149  function update($a_validate = true, $a_no_history = false)
150  {
151  global $ilDB;
152 
153  // update wiki page data
154  $query = "UPDATE il_wiki_page SET ".
155  " title = ".$ilDB->quote($this->getTitle(), "text").
156  ",wiki_id = ".$ilDB->quote($this->getWikiId(), "integer").
157  ",blocked = ".$ilDB->quote($this->getBlocked(), "integer").
158  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
159  $ilDB->manipulate($query);
160  parent::update($a_validate, $a_no_history);
161 
162  include_once "./Services/Notification/classes/class.ilNotification.php";
164 
165  $this->updateNews(true);
166 
167  return true;
168  }
169 
173  function read()
174  {
175  global $ilDB;
176 
177  $query = "SELECT * FROM il_wiki_page WHERE id = ".
178  $ilDB->quote($this->getId(), "integer");
179  $set = $ilDB->query($query);
180  $rec = $ilDB->fetchAssoc($set);
181 
182  $this->setTitle($rec["title"]);
183  $this->setWikiId($rec["wiki_id"]);
184  $this->setBlocked($rec["blocked"]);
185 
186  // get co page
187  parent::read();
188  }
189 
190 
196  function delete()
197  {
198  global $ilDB;
199 
200  // get other pages that link to this page
201  $linking_pages = ilWikiPage::getLinksToPage($this->getWikiId(),
202  $this->getId());
203 
204  // delete internal links information to this page
205  include_once("./Services/COPage/classes/class.ilInternalLink.php");
207 
208  include_once "./Services/Notification/classes/class.ilNotification.php";
210 
211  // remove all notifications
212  include_once "./Services/Notification/classes/class.ilNotification.php";
214 
215  // delete record of table il_wiki_data
216  $query = "DELETE FROM il_wiki_page".
217  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
218  $ilDB->manipulate($query);
219 
220  // delete co page
221  parent::delete();
222 
223  // make links of other pages to this page a missing link
224  foreach($linking_pages as $lp)
225  {
226  $ilDB->manipulateF("DELETE FROM il_wiki_missing_page ".
227  " WHERE wiki_id = %s AND source_id = %s AND target_name = %s ",
228  array("integer", "integer", "text"),
229  array($this->getWikiId(), $lp["id"], $this->getTitle()));
230  $ilDB->manipulateF("INSERT INTO il_wiki_missing_page ".
231  "(wiki_id, source_id, target_name) VALUES ".
232  "(%s,%s,%s)",
233  array("integer", "integer", "text"),
234  array($this->getWikiId(), $lp["id"], $this->getTitle()));
235  }
236 
237  return true;
238  }
239 
245  static function deleteAllPagesOfWiki($a_wiki_id)
246  {
247  global $ilDB;
248 
249  // delete record of table il_wiki_data
250  $query = "SELECT * FROM il_wiki_page".
251  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer");
252  $set = $ilDB->query($query);
253 
254  while($rec = $ilDB->fetchAssoc($set))
255  {
256  $wiki_page = new ilWikiPage($rec["id"]);
257  $wiki_page->delete();
258 
259 
260  }
261  }
262 
266  static function exists($a_wiki_id, $a_title)
267  {
268  global $ilDB;
269 
270  $a_title = ilWikiUtil::makeDbTitle($a_title);
271 
272  $query = "SELECT id FROM il_wiki_page".
273  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
274  " AND title = ".$ilDB->quote($a_title, "text");
275  $set = $ilDB->query($query);
276  if($rec = $ilDB->fetchAssoc($set))
277  {
278  return true;
279  }
280 
281  return false;
282  }
283 
287  static function getIdForPageTitle($a_wiki_id, $a_title)
288  {
289  global $ilDB;
290 
291  $a_title = ilWikiUtil::makeDbTitle($a_title);
292 
293  $query = "SELECT id FROM il_wiki_page".
294  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
295  " AND title = ".$ilDB->quote($a_title, "text");
296  $set = $ilDB->query($query);
297  if($rec = $ilDB->fetchAssoc($set))
298  {
299  return $rec["id"];
300  }
301 
302  return false;
303  }
304 
308  static function getPageIdForTitle($a_wiki_id, $a_title)
309  {
310  global $ilDB;
311 
312  $a_title = ilWikiUtil::makeDbTitle($a_title);
313 
314  $query = "SELECT * FROM il_wiki_page".
315  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
316  " AND title = ".$ilDB->quote($a_title, "text");
317  $set = $ilDB->query($query);
318  if($rec = $ilDB->fetchAssoc($set))
319  {
320  return $rec["id"];
321  }
322 
323  return false;
324  }
325 
329  static function lookupTitle($a_page_id)
330  {
331  global $ilDB;
332 
333  $query = "SELECT * FROM il_wiki_page".
334  " WHERE id = ".$ilDB->quote($a_page_id, "integer");
335  $set = $ilDB->query($query);
336  if($rec = $ilDB->fetchAssoc($set))
337  {
338  return $rec["title"];
339  }
340 
341  return false;
342  }
343 
347  static function lookupWikiId($a_page_id)
348  {
349  global $ilDB;
350 
351  $query = "SELECT wiki_id FROM il_wiki_page".
352  " WHERE id = ".$ilDB->quote($a_page_id, "integer");
353  $set = $ilDB->query($query);
354  if ($rec = $ilDB->fetchAssoc($set))
355  {
356  return $rec["wiki_id"];
357  }
358 
359  return false;
360  }
361 
367  static function getAllPages($a_wiki_id)
368  {
369  global $ilDB;
370 
371  $pages = parent::getAllPages("wpg", $a_wiki_id);
372 
373  $query = "SELECT * FROM il_wiki_page".
374  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
375  " ORDER BY title";
376  $set = $ilDB->query($query);
377 
378  $pg = array();
379  while($rec = $ilDB->fetchAssoc($set))
380  {
381  if (isset($pages[$rec["id"]]))
382  {
383  $pg[$rec["id"]] = $pages[$rec["id"]];
384  $pg[$rec["id"]]["title"] = $rec["title"];
385  }
386  }
387 
388  return $pg;
389  }
390 
394  static function getLinksToPage($a_wiki_id, $a_page_id)
395  {
396  global $ilDB;
397 
398  include_once("./Services/COPage/classes/class.ilInternalLink.php");
399  $sources = ilInternalLink::_getSourcesOfTarget("wpg", $a_page_id, 0);
400 
401  $ids = array();
402  foreach ($sources as $source)
403  {
404  if ($source["type"] == "wpg:pg")
405  {
406  $ids[] = $source["id"];
407  }
408  }
409  // get wiki page record
410  $query = "SELECT * FROM il_wiki_page wp, page_object p".
411  " WHERE ".$ilDB->in("wp.id", $ids, false, "integer").
412  " AND wp.id = p.page_id AND p.parent_type = ".$ilDB->quote("wpg", "text").
413  " AND wp.wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
414  " ORDER BY title";
415  $set = $ilDB->query($query);
416 
417  $pages = array();
418  while ($rec = $ilDB->fetchAssoc($set))
419  {
420  $pages[] = array_merge($rec, array("user" => $rec["last_change_user"],
421  "date" => $rec["last_change"]));
422  }
423 
424  return $pages;
425  }
426 
432  static function getOrphanedPages($a_wiki_id)
433  {
434  global $ilDB;
435 
436  $pages = ilWikiPage::getAllPages($a_wiki_id);
437 
438  include_once("./Services/COPage/classes/class.ilInternalLink.php");
439 
440  $orphaned = array();
441  foreach ($pages as $k => $page)
442  {
443  $sources = ilInternalLink::_getSourcesOfTarget("wpg", $page["id"], 0);
444 
445  $ids = array();
446  foreach ($sources as $source)
447  {
448  if ($source["type"] == "wpg:pg")
449  {
450  $ids[] = $source["id"];
451  }
452  }
453  // delete record of table il_wiki_data
454  $query = "SELECT count(*) AS cnt FROM il_wiki_page".
455  " WHERE ".$ilDB->in("id", $ids, false, "integer").
456  " AND wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
457  " ORDER BY title";
458  $set = $ilDB->query($query);
459  $rec = $ilDB->fetchAssoc($set);
460  if ($rec["cnt"] == 0 &&
461  ilObjWiki::_lookupStartPage($a_wiki_id) != $page["title"])
462  {
463  $orphaned[] = $page;
464  }
465  }
466 
467  return $orphaned;
468  }
469 
475  static function _wikiPageExists($a_wiki_id, $a_title)
476  {
477  global $ilDB;
478 
479  $a_title = ilWikiUtil::makeDbTitle($a_title);
480 
481  $query = "SELECT id FROM il_wiki_page".
482  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
483  " AND title = ".$ilDB->quote($a_title, "text");
484  $set = $ilDB->query($query);
485 
486  $pages = array();
487  if ($rec = $ilDB->fetchAssoc($set))
488  {
489  return true;
490  }
491 
492  return false;
493  }
494 
500  static function getParentObjectContributors($a_wiki_id)
501  {
502  global $ilDB;
503 
504  $contributors = parent::getParentObjectContributors("wpg", $a_wiki_id);
505 
506  return $contributors;
507  }
508 
514  static function getPageContributors($a_page_id)
515  {
516  global $ilDB;
517 
518  $contributors = parent::getPageContributors("wpg", $a_page_id);
519 
520  return $contributors;
521  }
522 
523 
529  function saveInternalLinks($a_xml)
530  {
531  global $ilDB;
532 
533 
534  // *** STEP 1: Standard Processing ***
535 
537 
538 
539  // *** STEP 2: Other Pages -> This Page ***
540 
541  // Check, whether ANOTHER page links to this page as a "missing" page
542  // (this is the case, when this page is created newly)
543  $set = $ilDB->queryF("SELECT * FROM il_wiki_missing_page WHERE ".
544  " wiki_id = %s AND target_name = %s",
545  array("integer", "text"),
546  array($this->getWikiId(), ilWikiUtil::makeDbTitle($this->getTitle())));
547  while ($anmiss = $ilDB->fetchAssoc($set)) // insert internal links instead
548  {
549 //echo "adding link";
550  ilInternalLink::_saveLink("wpg:pg", $anmiss["source_id"], "wpg",
551  $this->getId(), 0);
552  }
553 //exit;
554  // now remove the missing page entries
555  $ilDB->manipulateF("DELETE FROM il_wiki_missing_page WHERE ".
556  " wiki_id = %s AND target_name = %s",
557  array("integer", "text"),
558  array($this->getWikiId(), $this->getTitle()));
559 
560 
561  // *** STEP 3: This Page -> Other Pages ***
562 
563  // remove the exising "missing page" links for THIS page (they will be re-inserted below)
564  $ilDB->manipulateF("DELETE FROM il_wiki_missing_page WHERE ".
565  " wiki_id = %s AND source_id = %s",
566  array("integer", "integer"),
567  array($this->getWikiId(), $this->getId()));
568 
569  // collect the wiki links of the page
570  include_once("./Modules/Wiki/classes/class.ilWikiUtil.php");
571  $int_wiki_links = ilWikiUtil::collectInternalLinks($a_xml, $this->getWikiId(), true);
572 
573  foreach($int_wiki_links as $wlink)
574  {
575  $page_id = ilWikiPage::_getPageIdForWikiTitle($this->getWikiId(), $wlink);
576 
577  if ($page_id > 0) // save internal link for existing page
578  {
579  ilInternalLink::_saveLink("wpg:pg", $this->getId(), "wpg",
580  $page_id, 0);
581  }
582  else // save missing link for non-existing page
583  {
584  $ilDB->manipulateF("DELETE FROM il_wiki_missing_page WHERE".
585  " wiki_id = %s AND source_id = %s AND target_name = %s",
586  array("integer", "integer", "text"),
587  array($this->getWikiId(), $this->getId(), $wlink));
588  $ilDB->manipulateF("INSERT INTO il_wiki_missing_page (wiki_id, source_id, target_name)".
589  " VALUES (%s,%s,%s)",
590  array("integer", "integer", "text"),
591  array($this->getWikiId(), $this->getId(), $wlink));
592  }
593  }
594  }
595 
599  static function _getPageIdForWikiTitle($a_wiki_id, $a_title)
600  {
601  global $ilDB;
602 
603  $query = "SELECT id FROM il_wiki_page".
604  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
605  " AND title = ".$ilDB->quote($a_title, "text");
606  $set = $ilDB->query($query);
607  if($rec = $ilDB->fetchAssoc($set))
608  {
609  return $rec["id"];
610  }
611 
612  return false;
613  }
614 
620  static function getPopularPages($a_wiki_id)
621  {
622  global $ilDB;
623 
624  $query = "SELECT wp.*, po.view_cnt as cnt FROM il_wiki_page wp, page_object po".
625  " WHERE wp.wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
626  " AND wp.id = po.page_id ".
627  " AND po.parent_type = ".$ilDB->quote("wpg", "text")." ".
628  " ORDER BY po.view_cnt";
629  $set = $ilDB->query($query);
630 
631  $pages = array();
632  while($rec = $ilDB->fetchAssoc($set))
633  {
634  $pages[] = $rec;
635  }
636 
637  return $pages;
638  }
639 
645  static function countPages($a_wiki_id)
646  {
647  global $ilDB;
648 
649  // delete record of table il_wiki_data
650  $query = "SELECT count(*) as cnt FROM il_wiki_page".
651  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer");
652  $s = $ilDB->query($query);
653  $r = $ilDB->fetchAssoc($s);
654 
655  return $r["cnt"];
656  }
657 
663  static function getRandomPage($a_wiki_id)
664  {
665  global $ilDB;
666 
667  $cnt = ilWikiPage::countPages($a_wiki_id);
668 
669  if ($cnt < 1)
670  {
671  return "";
672  }
673 
674  $rand = rand(1, $cnt);
675 
676  // delete record of table il_wiki_data
677  $ilDB->setLimit(1, $rand);
678  $query = "SELECT title FROM il_wiki_page".
679  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer");
680  $s = $ilDB->query($query);
681  $r = $ilDB->fetchAssoc($s);
682 
683  return $r["title"];
684  }
685 
691  static function getNewPages($a_wiki_id)
692  {
693  global $ilDB;
694 
695  $pages = parent::getNewPages("wpg", $a_wiki_id);
696 
697  foreach($pages as $k => $page)
698  {
699  $pages[$k]["title"] = ilWikiPage::lookupTitle($page["id"]);
700  }
701 
702  return $pages;
703  }
704 
705 
712  public static function lookupObjIdByPage($a_page_id)
713  {
714  global $ilDB;
715 
716  $query = "SELECT wiki_id FROM il_wiki_page".
717  " WHERE id = ".$ilDB->quote($a_page_id, "integer");
718  $set = $ilDB->query($query);
719  if($rec = $ilDB->fetchAssoc($set))
720  {
721  return $rec["wiki_id"];
722  }
723 
724  return false;
725  }
726 
730  function rename($a_new_name)
731  {
732  if (!ilWikiPage::exists($this->getWikiId(), $a_new_name))
733  {
734  include_once("./Services/COPage/classes/class.ilInternalLink.php");
735  $sources = ilInternalLink::_getSourcesOfTarget("wpg", $this->getId(), 0);
736  foreach ($sources as $s)
737  {
738  if ($s["type"] == "wpg:pg")
739  {
740  $wpage = new ilWikiPage($s["id"]);
741 
742  $col = ilWikiUtil::processInternalLinks($wpage->getXmlContent(), 0,
744  $new_content = $wpage->getXmlContent();
745  foreach ($col as $c)
746  {
747  if (ilWikiUtil::makeDbTitle($c["nt"]->mTextform) ==
749  {
750  $new_content =
751  str_replace("[[".$c["nt"]->mTextform."]]",
752  "[[".$a_new_name."]]", $new_content);
753  if ($c["text"] != "")
754  {
755  $new_content =
756  str_replace("[[".$c["text"]."]]",
757  "[[".$a_new_name."]]", $new_content);
758  }
759  $add = ($c["text"] != "")
760  ? "|".$c["text"]
761  : "";
762  $new_content =
763  str_replace("[[".$c["nt"]->mTextform.$add."]]",
764  "[[".$a_new_name.$add."]]", $new_content);
765 //echo "<br>[[".$c["nt"]->mTextform.$add."]] -> "."[[".$a_new_name.$add."]]";
766  }
767  }
768 
769  $wpage->setXmlContent($new_content);
770  $wpage->update();
771  }
772  }
773 
774  include_once("./Modules/Wiki/classes/class.ilObjWiki.php");
775  if (ilObjWiki::_lookupStartPage($this->getWikiId()) == $this->getTitle())
776  {
777  ilObjWiki::writeStartPage($this->getWikiId(), $a_new_name);
778  }
779 
780  $this->setTitle($a_new_name);
781 
782  $this->update();
783  }
784  }
785 
786 
790  function updateNews($a_update = false)
791  {
792  global $ilUser;
793 
794  $news_set = new ilSetting("news");
795  $default_visibility = ($news_set->get("default_visibility") != "")
796  ? $news_set->get("default_visibility")
797  : "users";
798 
799  include_once("./Services/News/classes/class.ilNewsItem.php");
800  if (!$a_update)
801  {
802  $news_item = new ilNewsItem();
803  $news_item->setContext(
804  $this->getWikiId(), "wiki",
805  $this->getId(), "wpg");
806  $news_item->setPriority(NEWS_NOTICE);
807  $news_item->setTitle($this->getTitle());
808  $news_item->setContentTextIsLangVar(true);
809  $news_item->setContent("wiki_news_page_created");
810  $news_item->setUserId($ilUser->getId());
811  $news_item->setVisibility($default_visibility);
812  $news_item->create();
813  }
814  else
815  {
816  // get last news item of the day (if existing)
818  $this->getWikiId(), "wiki",
819  $this->getId(), "wpg", true);
820 
821  if ($news_id > 0)
822  {
823  $news_item = new ilNewsItem($news_id);
824  $news_item->setContent("wiki_news_page_changed");
825  $news_item->setUserId($ilUser->getId());
826  $news_item->setTitle($this->getTitle());
827  $news_item->setContentTextIsLangVar(true);
828  $news_item->update(true);
829  }
830  else
831  {
832  $news_item = new ilNewsItem();
833  $news_item->setContext(
834  $this->getWikiId(), "wiki",
835  $this->getId(), "wpg");
836  $news_item->setPriority(NEWS_NOTICE);
837  $news_item->setTitle($this->getTitle());
838  $news_item->setContentTextIsLangVar(true);
839  $news_item->setContent("wiki_news_page_changed");
840  $news_item->setUserId($ilUser->getId());
841  $news_item->setVisibility($default_visibility);
842  $news_item->create();
843  }
844  }
845  }
846 
850  function getNewsContent()
851  {
852  return "12.1.1: Test User, Max";
853  }
854 
855 
856 }
857 ?>