ILIAS  release_4-3 Revision
 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 
113  public function setRating($a_val)
114  {
115  $this->rating = (bool)$a_val;
116  }
117 
123  public function getRating()
124  {
125  return $this->rating;
126  }
127 
131  function create($a_prevent_page_creation = false)
132  {
133  global $ilDB;
134 
135  $id = $ilDB->nextId("il_wiki_page");
136  $this->setId($id);
137  $query = "INSERT INTO il_wiki_page (".
138  "id".
139  ", title".
140  ", wiki_id".
141  ", blocked".
142  ", rating".
143  " ) VALUES (".
144  $ilDB->quote($this->getId(), "integer")
145  .",".$ilDB->quote($this->getTitle(), "text")
146  .",".$ilDB->quote((int) $this->getWikiId(), "integer")
147  .",".$ilDB->quote((int) $this->getBlocked(), "integer")
148  .",".$ilDB->quote((int) $this->getRating(), "integer")
149  .")";
150  $ilDB->manipulate($query);
151 
152  // create page object
153  if (!$a_prevent_page_creation)
154  {
155  parent::create();
156  $this->saveInternalLinks($this->getXMLContent());
157 
158  include_once "./Services/Notification/classes/class.ilNotification.php";
160  }
161 
162  $this->updateNews();
163  }
164 
171  function update($a_validate = true, $a_no_history = false)
172  {
173  global $ilDB;
174 
175  // update wiki page data
176  $query = "UPDATE il_wiki_page SET ".
177  " title = ".$ilDB->quote($this->getTitle(), "text").
178  ",wiki_id = ".$ilDB->quote((int) $this->getWikiId(), "integer").
179  ",blocked = ".$ilDB->quote((int) $this->getBlocked(), "integer").
180  ",rating = ".$ilDB->quote((int) $this->getRating(), "integer").
181  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
182  $ilDB->manipulate($query);
183  parent::update($a_validate, $a_no_history);
184 
185  include_once "./Services/Notification/classes/class.ilNotification.php";
187 
188  $this->updateNews(true);
189 
190  return true;
191  }
192 
196  function read()
197  {
198  global $ilDB;
199 
200  $query = "SELECT * FROM il_wiki_page WHERE id = ".
201  $ilDB->quote($this->getId(), "integer");
202  $set = $ilDB->query($query);
203  $rec = $ilDB->fetchAssoc($set);
204 
205  $this->setTitle($rec["title"]);
206  $this->setWikiId($rec["wiki_id"]);
207  $this->setBlocked($rec["blocked"]);
208  $this->setRating($rec["rating"]);
209 
210  // get co page
211  parent::read();
212  }
213 
214 
220  function delete()
221  {
222  global $ilDB;
223 
224  // get other pages that link to this page
225  $linking_pages = ilWikiPage::getLinksToPage($this->getWikiId(),
226  $this->getId());
227 
228  // delete internal links information to this page
229  include_once("./Services/COPage/classes/class.ilInternalLink.php");
231 
232  include_once "./Services/Notification/classes/class.ilNotification.php";
234 
235  // remove all notifications
236  include_once "./Services/Notification/classes/class.ilNotification.php";
238 
239  // delete record of table il_wiki_data
240  $query = "DELETE FROM il_wiki_page".
241  " WHERE id = ".$ilDB->quote($this->getId(), "integer");
242  $ilDB->manipulate($query);
243 
244  // delete co page
245  parent::delete();
246 
247  // make links of other pages to this page a missing link
248  foreach($linking_pages as $lp)
249  {
250  $ilDB->manipulateF("DELETE FROM il_wiki_missing_page ".
251  " WHERE wiki_id = %s AND source_id = %s AND target_name = %s ",
252  array("integer", "integer", "text"),
253  array($this->getWikiId(), $lp["id"], $this->getTitle()));
254  $ilDB->manipulateF("INSERT INTO il_wiki_missing_page ".
255  "(wiki_id, source_id, target_name) VALUES ".
256  "(%s,%s,%s)",
257  array("integer", "integer", "text"),
258  array($this->getWikiId(), $lp["id"], $this->getTitle()));
259  }
260 
261  return true;
262  }
263 
269  static function deleteAllPagesOfWiki($a_wiki_id)
270  {
271  global $ilDB;
272 
273  // delete record of table il_wiki_data
274  $query = "SELECT * FROM il_wiki_page".
275  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer");
276  $set = $ilDB->query($query);
277 
278  while($rec = $ilDB->fetchAssoc($set))
279  {
280  $wiki_page = new ilWikiPage($rec["id"]);
281  $wiki_page->delete();
282 
283 
284  }
285  }
286 
290  static function exists($a_wiki_id, $a_title)
291  {
292  global $ilDB;
293 
294  $a_title = ilWikiUtil::makeDbTitle($a_title);
295 
296  $query = "SELECT id FROM il_wiki_page".
297  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
298  " AND title = ".$ilDB->quote($a_title, "text");
299  $set = $ilDB->query($query);
300  if($rec = $ilDB->fetchAssoc($set))
301  {
302  return true;
303  }
304 
305  return false;
306  }
307 
311  static function getIdForPageTitle($a_wiki_id, $a_title)
312  {
313  global $ilDB;
314 
315  $a_title = ilWikiUtil::makeDbTitle($a_title);
316 
317  $query = "SELECT id FROM il_wiki_page".
318  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
319  " AND title = ".$ilDB->quote($a_title, "text");
320  $set = $ilDB->query($query);
321  if($rec = $ilDB->fetchAssoc($set))
322  {
323  return $rec["id"];
324  }
325 
326  return false;
327  }
328 
332  static function getPageIdForTitle($a_wiki_id, $a_title)
333  {
334  global $ilDB;
335 
336  $a_title = ilWikiUtil::makeDbTitle($a_title);
337 
338  $query = "SELECT * FROM il_wiki_page".
339  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
340  " AND title = ".$ilDB->quote($a_title, "text");
341  $set = $ilDB->query($query);
342  if($rec = $ilDB->fetchAssoc($set))
343  {
344  return $rec["id"];
345  }
346 
347  return false;
348  }
349 
353  static function lookupTitle($a_page_id)
354  {
355  global $ilDB;
356 
357  $query = "SELECT * FROM il_wiki_page".
358  " WHERE id = ".$ilDB->quote($a_page_id, "integer");
359  $set = $ilDB->query($query);
360  if($rec = $ilDB->fetchAssoc($set))
361  {
362  return $rec["title"];
363  }
364 
365  return false;
366  }
367 
371  static function lookupWikiId($a_page_id)
372  {
373  global $ilDB;
374 
375  $query = "SELECT wiki_id FROM il_wiki_page".
376  " WHERE id = ".$ilDB->quote($a_page_id, "integer");
377  $set = $ilDB->query($query);
378  if ($rec = $ilDB->fetchAssoc($set))
379  {
380  return $rec["wiki_id"];
381  }
382 
383  return false;
384  }
385 
391  static function getAllPages($a_wiki_id)
392  {
393  global $ilDB;
394 
395  $pages = parent::getAllPages("wpg", $a_wiki_id);
396 
397  $query = "SELECT * FROM il_wiki_page".
398  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
399  " ORDER BY title";
400  $set = $ilDB->query($query);
401 
402  $pg = array();
403  while($rec = $ilDB->fetchAssoc($set))
404  {
405  if (isset($pages[$rec["id"]]))
406  {
407  $pg[$rec["id"]] = $pages[$rec["id"]];
408  $pg[$rec["id"]]["title"] = $rec["title"];
409  }
410  }
411 
412  return $pg;
413  }
414 
418  static function getLinksToPage($a_wiki_id, $a_page_id)
419  {
420  global $ilDB;
421 
422  include_once("./Services/COPage/classes/class.ilInternalLink.php");
423  $sources = ilInternalLink::_getSourcesOfTarget("wpg", $a_page_id, 0);
424 
425  $ids = array();
426  foreach ($sources as $source)
427  {
428  if ($source["type"] == "wpg:pg")
429  {
430  $ids[] = $source["id"];
431  }
432  }
433  // get wiki page record
434  $query = "SELECT * FROM il_wiki_page wp, page_object p".
435  " WHERE ".$ilDB->in("wp.id", $ids, false, "integer").
436  " AND wp.id = p.page_id AND p.parent_type = ".$ilDB->quote("wpg", "text").
437  " AND wp.wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
438  " ORDER BY title";
439  $set = $ilDB->query($query);
440 
441  $pages = array();
442  while ($rec = $ilDB->fetchAssoc($set))
443  {
444  $pages[] = array_merge($rec, array("user" => $rec["last_change_user"],
445  "date" => $rec["last_change"]));
446  }
447 
448  return $pages;
449  }
450 
456  static function getOrphanedPages($a_wiki_id)
457  {
458  global $ilDB;
459 
460  $pages = ilWikiPage::getAllPages($a_wiki_id);
461 
462  include_once("./Services/COPage/classes/class.ilInternalLink.php");
463 
464  $orphaned = array();
465  foreach ($pages as $k => $page)
466  {
467  $sources = ilInternalLink::_getSourcesOfTarget("wpg", $page["id"], 0);
468 
469  $ids = array();
470  foreach ($sources as $source)
471  {
472  if ($source["type"] == "wpg:pg")
473  {
474  $ids[] = $source["id"];
475  }
476  }
477  $query = "SELECT count(*) cnt FROM il_wiki_page".
478  " WHERE ".$ilDB->in("id", $ids, false, "integer").
479  " AND wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
480  " GROUP BY wiki_id";
481  $set = $ilDB->query($query);
482  $rec = $ilDB->fetchAssoc($set);
483  if ($rec["cnt"] == 0 &&
484  ilObjWiki::_lookupStartPage($a_wiki_id) != $page["title"])
485  {
486  $orphaned[] = $page;
487  }
488  }
489 
490  return $orphaned;
491  }
492 
498  static function _wikiPageExists($a_wiki_id, $a_title)
499  {
500  global $ilDB;
501 
502  $a_title = ilWikiUtil::makeDbTitle($a_title);
503 
504  $query = "SELECT id FROM il_wiki_page".
505  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
506  " AND title = ".$ilDB->quote($a_title, "text");
507  $set = $ilDB->query($query);
508 
509  $pages = array();
510  if ($rec = $ilDB->fetchAssoc($set))
511  {
512  return true;
513  }
514 
515  return false;
516  }
517 
523  static function getParentObjectContributors($a_wiki_id)
524  {
525  global $ilDB;
526 
527  $contributors = parent::getParentObjectContributors("wpg", $a_wiki_id);
528 
529  return $contributors;
530  }
531 
537  static function getPageContributors($a_page_id)
538  {
539  global $ilDB;
540 
541  $contributors = parent::getPageContributors("wpg", $a_page_id);
542 
543  return $contributors;
544  }
545 
546 
552  function saveInternalLinks($a_xml)
553  {
554  global $ilDB;
555 
556 
557  // *** STEP 1: Standard Processing ***
558 
560 
561 
562  // *** STEP 2: Other Pages -> This Page ***
563 
564  // Check, whether ANOTHER page links to this page as a "missing" page
565  // (this is the case, when this page is created newly)
566  $set = $ilDB->queryF("SELECT * FROM il_wiki_missing_page WHERE ".
567  " wiki_id = %s AND target_name = %s",
568  array("integer", "text"),
569  array($this->getWikiId(), ilWikiUtil::makeDbTitle($this->getTitle())));
570  while ($anmiss = $ilDB->fetchAssoc($set)) // insert internal links instead
571  {
572 //echo "adding link";
573  ilInternalLink::_saveLink("wpg:pg", $anmiss["source_id"], "wpg",
574  $this->getId(), 0);
575  }
576 //exit;
577  // now remove the missing page entries
578  $ilDB->manipulateF("DELETE FROM il_wiki_missing_page WHERE ".
579  " wiki_id = %s AND target_name = %s",
580  array("integer", "text"),
581  array($this->getWikiId(), $this->getTitle()));
582 
583 
584  // *** STEP 3: This Page -> Other Pages ***
585 
586  // remove the exising "missing page" links for THIS page (they will be re-inserted below)
587  $ilDB->manipulateF("DELETE FROM il_wiki_missing_page WHERE ".
588  " wiki_id = %s AND source_id = %s",
589  array("integer", "integer"),
590  array($this->getWikiId(), $this->getId()));
591 
592  // collect the wiki links of the page
593  include_once("./Modules/Wiki/classes/class.ilWikiUtil.php");
594  $int_wiki_links = ilWikiUtil::collectInternalLinks($a_xml, $this->getWikiId(), true);
595 
596  foreach($int_wiki_links as $wlink)
597  {
598  $page_id = ilWikiPage::_getPageIdForWikiTitle($this->getWikiId(), $wlink);
599 
600  if ($page_id > 0) // save internal link for existing page
601  {
602  ilInternalLink::_saveLink("wpg:pg", $this->getId(), "wpg",
603  $page_id, 0);
604  }
605  else // save missing link for non-existing page
606  {
607  $ilDB->manipulateF("DELETE FROM il_wiki_missing_page WHERE".
608  " wiki_id = %s AND source_id = %s AND target_name = %s",
609  array("integer", "integer", "text"),
610  array($this->getWikiId(), $this->getId(), $wlink));
611  $ilDB->manipulateF("INSERT INTO il_wiki_missing_page (wiki_id, source_id, target_name)".
612  " VALUES (%s,%s,%s)",
613  array("integer", "integer", "text"),
614  array($this->getWikiId(), $this->getId(), $wlink));
615  }
616  }
617  }
618 
622  static function _getPageIdForWikiTitle($a_wiki_id, $a_title)
623  {
624  global $ilDB;
625 
626  $query = "SELECT id FROM il_wiki_page".
627  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
628  " AND title = ".$ilDB->quote($a_title, "text");
629  $set = $ilDB->query($query);
630  if($rec = $ilDB->fetchAssoc($set))
631  {
632  return $rec["id"];
633  }
634 
635  return false;
636  }
637 
643  static function getPopularPages($a_wiki_id)
644  {
645  global $ilDB;
646 
647  $query = "SELECT wp.*, po.view_cnt as cnt FROM il_wiki_page wp, page_object po".
648  " WHERE wp.wiki_id = ".$ilDB->quote($a_wiki_id, "integer").
649  " AND wp.id = po.page_id ".
650  " AND po.parent_type = ".$ilDB->quote("wpg", "text")." ".
651  " ORDER BY po.view_cnt";
652  $set = $ilDB->query($query);
653 
654  $pages = array();
655  while($rec = $ilDB->fetchAssoc($set))
656  {
657  $pages[] = $rec;
658  }
659 
660  return $pages;
661  }
662 
668  static function countPages($a_wiki_id)
669  {
670  global $ilDB;
671 
672  // delete record of table il_wiki_data
673  $query = "SELECT count(*) as cnt FROM il_wiki_page".
674  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer");
675  $s = $ilDB->query($query);
676  $r = $ilDB->fetchAssoc($s);
677 
678  return $r["cnt"];
679  }
680 
686  static function getRandomPage($a_wiki_id)
687  {
688  global $ilDB;
689 
690  $cnt = ilWikiPage::countPages($a_wiki_id);
691 
692  if ($cnt < 1)
693  {
694  return "";
695  }
696 
697  $rand = rand(1, $cnt);
698 
699  // delete record of table il_wiki_data
700  $ilDB->setLimit(1, $rand);
701  $query = "SELECT title FROM il_wiki_page".
702  " WHERE wiki_id = ".$ilDB->quote($a_wiki_id, "integer");
703  $s = $ilDB->query($query);
704  $r = $ilDB->fetchAssoc($s);
705 
706  return $r["title"];
707  }
708 
714  static function getNewPages($a_wiki_id)
715  {
716  global $ilDB;
717 
718  $pages = parent::getNewPages("wpg", $a_wiki_id);
719 
720  foreach($pages as $k => $page)
721  {
722  $pages[$k]["title"] = ilWikiPage::lookupTitle($page["id"]);
723  }
724 
725  return $pages;
726  }
727 
728 
735  public static function lookupObjIdByPage($a_page_id)
736  {
737  global $ilDB;
738 
739  $query = "SELECT wiki_id FROM il_wiki_page".
740  " WHERE id = ".$ilDB->quote($a_page_id, "integer");
741  $set = $ilDB->query($query);
742  if($rec = $ilDB->fetchAssoc($set))
743  {
744  return $rec["wiki_id"];
745  }
746 
747  return false;
748  }
749 
753  function rename($a_new_name)
754  {
755  global $ilDB;
756 
757  // replace unallowed characters
758  $a_new_name = str_replace(array("<", ">"), '', $a_new_name);
759 
760  // replace multiple whitespace characters by one single space
761  $a_new_name = trim(preg_replace('!\s+!', ' ', $a_new_name));
762 
763  $page_title = ilWikiUtil::makeDbTitle($a_new_name);
764  $pg_id = ilWikiPage::_getPageIdForWikiTitle($this->getWikiId(), $page_title);
765 
766  $xml_new_name = str_replace("&", "&amp;", $a_new_name);
767 
768  if ($pg_id == 0 || $pg_id == $this->getId())
769  {
770  include_once("./Services/COPage/classes/class.ilInternalLink.php");
771  $sources = ilInternalLink::_getSourcesOfTarget("wpg", $this->getId(), 0);
772 
773  foreach ($sources as $s)
774  {
775  if ($s["type"] == "wpg:pg" && ilPageObject::_exists("wpg", $s["id"]))
776  {
777  $wpage = new ilWikiPage($s["id"]);
778 
779  $col = ilWikiUtil::processInternalLinks($wpage->getXmlContent(), 0,
781  $new_content = $wpage->getXmlContent();
782  foreach ($col as $c)
783  {
784 
785  // this complicated procedure is needed due to the fact
786  // that depending on the collation e = é is true
787  // in the (mysql) database
788  // see bug http://www.ilias.de/mantis/view.php?id=11227
789  $t1 = ilWikiUtil::makeDbTitle($c["nt"]->mTextform);
790  $t2 = ilWikiUtil::makeDbTitle($this->getTitle());
791 
792  // this one replaces C2A0 (&nbsp;) by a usual space
793  // otherwise the comparision will fail, since you
794  // get these characters from tiny if more than one
795  // space is repeated in a string. This may not be
796  // 100% but we do not store $t1 anywhere and only
797  // modify it for the comparison
798  $t1 = preg_replace('/\xC2\xA0/', ' ', $t1);
799  $t2 = preg_replace('/\xC2\xA0/', ' ', $t2);
800 
801  $set = $ilDB->query($q = "SELECT ".$ilDB->quote($t1, "text")." = ".$ilDB->quote($t2, "text")." isequal");
802  $rec = $ilDB->fetchAssoc($set);
803 
804  if ($rec["isequal"])
805  {
806  $new_content =
807  str_replace("[[".$c["nt"]->mTextform."]]",
808  "[[".$xml_new_name."]]", $new_content);
809  if ($c["text"] != "")
810  {
811  $new_content =
812  str_replace("[[".$c["text"]."]]",
813  "[[".$xml_new_name."]]", $new_content);
814  }
815  $add = ($c["text"] != "")
816  ? "|".$c["text"]
817  : "";
818  $new_content =
819  str_replace("[[".$c["nt"]->mTextform.$add."]]",
820  "[[".$xml_new_name.$add."]]", $new_content);
821  }
822  }
823  $wpage->setXmlContent($new_content);
824 //echo htmlentities($new_content);
825  $wpage->update();
826  }
827  }
828 
829  include_once("./Modules/Wiki/classes/class.ilObjWiki.php");
830  if (ilObjWiki::_lookupStartPage($this->getWikiId()) == $this->getTitle())
831  {
832  ilObjWiki::writeStartPage($this->getWikiId(), $a_new_name);
833  }
834 
835  $this->setTitle($a_new_name);
836 
837  $this->update();
838  }
839 
840  return $a_new_name;
841  }
842 
843 
847  function updateNews($a_update = false)
848  {
849  global $ilUser;
850 
851  $news_set = new ilSetting("news");
852  $default_visibility = ($news_set->get("default_visibility") != "")
853  ? $news_set->get("default_visibility")
854  : "users";
855 
856  include_once("./Services/News/classes/class.ilNewsItem.php");
857  if (!$a_update)
858  {
859  $news_item = new ilNewsItem();
860  $news_item->setContext(
861  $this->getWikiId(), "wiki",
862  $this->getId(), "wpg");
863  $news_item->setPriority(NEWS_NOTICE);
864  $news_item->setTitle($this->getTitle());
865  $news_item->setContentTextIsLangVar(true);
866  $news_item->setContent("wiki_news_page_created");
867  $news_item->setUserId($ilUser->getId());
868  $news_item->setVisibility($default_visibility);
869  $news_item->create();
870  }
871  else
872  {
873  // get last news item of the day (if existing)
875  $this->getWikiId(), "wiki",
876  $this->getId(), "wpg", true);
877 
878  if ($news_id > 0)
879  {
880  $news_item = new ilNewsItem($news_id);
881  $news_item->setContent("wiki_news_page_changed");
882  $news_item->setUserId($ilUser->getId());
883  $news_item->setTitle($this->getTitle());
884  $news_item->setContentTextIsLangVar(true);
885  $news_item->update(true);
886  }
887  else
888  {
889  $news_item = new ilNewsItem();
890  $news_item->setContext(
891  $this->getWikiId(), "wiki",
892  $this->getId(), "wpg");
893  $news_item->setPriority(NEWS_NOTICE);
894  $news_item->setTitle($this->getTitle());
895  $news_item->setContentTextIsLangVar(true);
896  $news_item->setContent("wiki_news_page_changed");
897  $news_item->setUserId($ilUser->getId());
898  $news_item->setVisibility($default_visibility);
899  $news_item->create();
900  }
901  }
902  }
903 
907  function getNewsContent()
908  {
909  return "12.1.1: Test User, Max";
910  }
911 
912 
913 }
914 ?>