ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
LinkManager.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Wiki\Links;
22 
30 
35 {
36  protected int $wiki_id;
37  protected PageManager $pm;
38  protected \ilLogger $log;
42 
43  public function __construct(
44  InternalDataService $data_service,
45  MissingPageDBRepository $missing_page_repo,
46  InternalDomainService $domain,
47  int $ref_id
48  ) {
49  $this->missing_page_repo = $missing_page_repo;
50  $this->data_service = $data_service;
51  $this->domain = $domain;
52  $this->log = $this->domain->log();
53  $this->pm = $domain->page()->page($ref_id);
54  $this->wiki_id = $this->pm->getWikiId();
55  }
56 
57  public function saveInternalLinksForPage(
58  \DOMDocument $domdoc,
59  int $page_id,
60  string $title,
61  string $lang
62  ): void {
63 
64  $wiki_id = $this->wiki_id;
65 
66  // Check, whether ANOTHER page links to this page as a "missing" page
67  // (this is the case, when this page is created newly)
68  foreach($this->missing_page_repo->getSourcesOfMissingTarget($wiki_id, $title, $lang) as $i) { // insert internal links instead
69  //echo "adding link";
71  "wpg:pg",
72  $i,
73  "wpg",
74  $page_id,
75  0,
76  $lang
77  );
78  }
79 
80  // now remove the missing page entries for our $title (since it exists now)
81  $this->missing_page_repo->deleteForTarget($wiki_id, $title, $lang);
82 
83  // remove the existing "missing page" links for THIS page (they will be re-inserted below)
84  $this->missing_page_repo->deleteForSourceId($wiki_id, $page_id, $lang);
85 
86  // collect the wiki links of the page
87  $xml = $domdoc->saveXML();
88  $int_wiki_links = \ilWikiUtil::collectInternalLinks($xml, $wiki_id, true);
89  foreach ($int_wiki_links as $wlink) {
90  $target_page_id = (int) $this->pm->getPageIdForTitle($wlink, $lang);
91 
92  if ($target_page_id > 0) { // save internal link for existing page
94  "wpg:pg",
95  $page_id,
96  "wpg",
97  $target_page_id,
98  0,
99  $lang
100  );
101  } else { // save missing link for non-existing page
102  $this->missing_page_repo->save(
103  $wiki_id,
104  $page_id,
105  $wlink,
106  $lang
107  );
108  }
109  }
110  }
111 
115  public function getLinksToPage(
116  int $a_page_id,
117  string $lang = "-"
118  ): \Iterator {
119 
120  $wiki_id = $this->wiki_id;
121  if ($lang === "") {
122  $lang = "-";
123  }
124  $sources = \ilInternalLink::_getSourcesOfTarget("wpg", $a_page_id, 0);
125  $ids = array();
126  foreach ($sources as $source) {
127  if ($source["type"] === "wpg:pg" && $source["lang"] === $lang) {
128  $ids[] = (int) $source["id"];
129  }
130  }
131 
132  // get wiki page record
133  foreach ($this->pm->getInfoOfSelected($ids, $lang) as $p) {
134  yield $p;
135  }
136  }
137 
138 }
$ref_id
Definition: ltiauth.php:65
static collectInternalLinks(string $s, int $a_wiki_id, bool $a_collect_non_ex=false, string $mode=IL_WIKI_MODE_COLLECT)
Collect internal wiki links of a string.
$lang
Definition: xapiexit.php:25