ILIAS  release_8 Revision v8.23
class.ilCOPageImporter.php
Go to the documentation of this file.
1 <?php
2 
24 {
26  protected ilLogger $log;
27  protected ilCOPageDataSet $ds;
28  // Names of active plugins with own importers for additional data
29  protected array $importer_plugins = array();
30 
31  public function init(): void
32  {
33  global $DIC;
35  $component_repository = $DIC["component.repository"];
36 
37  $this->ds = new ilCOPageDataSet();
38  $this->ds->setDSPrefix("ds");
39  $this->config = $this->getImport()->getConfig("Services/COPage");
40 
41  $this->log = ilLoggerFactory::getLogger('copg');
42 
43  // collect all page component plugins that have their own exporter
44  foreach ($component_repository->getPluginSlotById("pgcp")->getActivePlugins() as $plugin) {
45  $plugin_name = $plugin->getName();
46  if ($plugin->supportsExport()) {
47  require_once('Customizing/global/plugins/Services/COPage/PageComponent/'
48  . $plugin_name . '/classes/class.il' . $plugin_name . 'Importer.php');
49 
50  $this->importer_plugins[] = $plugin_name;
51  }
52  }
53  }
54 
55  public function importXmlRepresentation(
56  string $a_entity,
57  string $a_id,
58  string $a_xml,
59  ilImportMapping $a_mapping
60  ): void {
61  $this->log->debug("entity: " . $a_entity . ", id: " . $a_id);
62 
63  if ($a_entity == "pgtp") {
64  $parser = new ilDataSetImportParser(
65  $a_entity,
66  $this->getSchemaVersion(),
67  $a_xml,
68  $this->ds,
69  $a_mapping
70  );
71  }
72 
73  if ($a_entity == "pg") {
74  $pg_id = $a_mapping->getMapping("Services/COPage", "pg", $a_id);
75 
76  $this->log->debug("mapping id: " . $pg_id);
77 
78  if ($pg_id != "") {
79  $id = explode(":", $pg_id);
80  if (count($id) == 2) {
81  while (substr($a_xml, 0, 11) == "<PageObject") {
82  $l1 = strpos($a_xml, ">");
83 
84  $page_tag = "<?xml version='1.0'?> " . substr($a_xml, 0, $l1 + 1) . "</PageObject>";
85  $page_data = simplexml_load_string($page_tag);
86  $lstr = $page_data['Language'];
87  $p = strpos($a_xml, "</PageObject>") + 13;
88  $next_xml = "<PageObject>" . substr($a_xml, $l1 + 1, $p - $l1 - 1);
89 
90  if ($this->config->getForceLanguage() != "") {
91  $lstr = $this->config->getForceLanguage();
92  }
93  if ($lstr == "") {
94  $lstr = "-";
95  }
96  // see bug #0019049
97  $next_xml = str_replace("&amp;", "&", $next_xml);
98  if ($this->config->getUpdateIfExists() && ilPageObject::_exists($id[0], $id[1], $lstr)) {
99  $page = ilPageObjectFactory::getInstance($id[0], $id[1], 0, $lstr);
100  $page->setImportMode(true);
101  $page->setXMLContent($next_xml);
102  $page->updateFromXML();
103  $this->extractPluginProperties($page);
104  } else {
105  if (ilPageObject::_exists($id[0], (int) $id[1], "-", true)) {
106  return;
107  }
108  $new_page = ilPageObjectFactory::getInstance($id[0]);
109  $new_page->setImportMode(true);
110  $new_page->setId($id[1]);
111  if ($lstr != "" && $lstr != "-") {
112  $new_page->setLanguage($lstr);
113  }
114  $this->log->debug(">>> CREATE PAGE " . $id[0] . ":" . $id[1]);
115  $new_page->setXMLContent($next_xml);
116  $new_page->setActive(true);
117  // array_key_exists does NOT work on simplexml!
118  if (isset($page_data["Active"])) {
119  $new_page->setActive((string) $page_data["Active"]);
120  }
121  $new_page->setActivationStart($page_data["ActivationStart"]);
122  $new_page->setActivationEnd($page_data["ActivationEnd"]);
123  $new_page->setShowActivationInfo((string) $page_data["ShowActivationInfo"]);
124  $new_page->createFromXML();
125  $this->extractPluginProperties($new_page);
126  }
127 
128  $a_xml = substr($a_xml, $p);
129  if ($lstr == "") {
130  $lstr = "-";
131  }
132  $a_mapping->addMapping("Services/COPage", "pgl", $a_id . ":" . $lstr, $pg_id . ":" . $lstr);
133  }
134  }
135  }
136  }
137  $this->log->debug("done");
138  }
139 
140  public function finalProcessing(
141  ilImportMapping $a_mapping
142  ): void {
143  $this->log->debug("start");
144  $pages = $a_mapping->getMappingsOfEntity("Services/COPage", "pgl");
145  $media_objects = $a_mapping->getMappingsOfEntity("Services/MediaObjects", "mob");
146  $file_objects = $a_mapping->getMappingsOfEntity("Modules/File", "file");
147 
148  $ref_mapping = $a_mapping->getMappingsOfEntity('Services/Container', 'refs');
149 
150  foreach ($pages as $p) {
151  $id = explode(":", $p);
152  if (count($id) == 3) {
153  if (ilPageObject::_exists($id[0], $id[1], $id[2], true)) {
155  $new_page = ilPageObjectFactory::getInstance($id[0], $id[1], 0, $id[2]);
156  $new_page->buildDom();
157  $med = $new_page->resolveMediaAliases($media_objects, $this->config->getReuseOriginallyExportedMedia());
158  $fil = $new_page->resolveFileItems($file_objects);
159  $res = $new_page->resolveResources($ref_mapping);
160  $il = false;
161  if (!$this->config->getSkipInternalLinkResolve()) {
162  $il = $new_page->resolveIntLinks();
163  $this->log->debug("resolve internal link for page " . $id[0] . "-" . $id[1] . "-" . $id[2]);
164  }
165  $plug = $this->replacePluginProperties($new_page);
166  if ($med || $fil || $il || $plug || $res) {
167  $new_page->update(false, true);
168  }
169  }
170  }
171  }
172  $this->log->debug("end");
173  }
174 
176  ilImportMapping $a_mapping
177  ): void {
178  }
179 
187  protected function extractPluginProperties(
188  ilPageObject $a_page
189  ): void {
190  if (empty($this->importer_plugins)) {
191  return;
192  }
193 
194  $a_page->buildDom();
195  $domdoc = $a_page->getDomDoc();
196  $xpath = new DOMXPath($domdoc);
197  $nodes = $xpath->query("//PageContent[child::Plugged]");
198 
200  foreach ($nodes as $pcnode) {
201  // page content id (unique in the page)
202  $pc_id = $pcnode->getAttribute('PCID');
203  $plnode = $pcnode->childNodes->item(0);
204  $plugin_name = $plnode->getAttribute('PluginName');
205  $plugin_version = $plnode->getAttribute('PluginVersion');
206 
207  // additional data will be imported
208  if (in_array($plugin_name, $this->importer_plugins)) {
209  // get the id of the mapped plugged page content
210  $id = $a_page->getParentType()
211  . ':' . $a_page->getId()
212  . ':' . $a_page->getLanguage()
213  . ':' . $pc_id;
214 
215  $properties = array();
217  foreach ($plnode->childNodes as $child) {
218  $properties[$child->getAttribute('Name')] = $child->nodeValue;
219  }
220 
221  // statical provision of content to the pluged importer classes
224  }
225  }
226  }
227 
236  public function replacePluginProperties(
237  ilPageObject $a_page
238  ): bool {
239  if (empty($this->importer_plugins)) {
240  return false;
241  }
242 
243  $a_page->buildDom();
244  $domdoc = $a_page->getDomDoc();
245  $xpath = new DOMXPath($domdoc);
246  $nodes = $xpath->query("//PageContent[child::Plugged]");
247 
248  $modified = false;
249 
251  foreach ($nodes as $pcnode) {
252  // page content id (unique in the page)
253  $pc_id = $pcnode->getAttribute('PCID');
254  $plnode = $pcnode->childNodes->item(0);
255  $plugin_name = $plnode->getAttribute('PluginName');
256 
257  // get the id of the mapped plugged page content
258  $id = $a_page->getParentType()
259  . ':' . $a_page->getId()
260  . ':' . $a_page->getLanguage()
261  . ':' . $pc_id;
262 
265 
266  // update the version if modified by the plugin importer
267  if (isset($plugin_version)) {
268  $plnode->setAttribute('PluginVersion', $plugin_version);
269  $modified = true;
270  }
271 
272  // update the properties if modified by the plugin importer
273  if (is_array($properties)) {
275  foreach ($plnode->childNodes as $child) {
276  $plnode->removeChild($child);
277  }
278  foreach ($properties as $name => $value) {
279  $child = new DOMElement('PluggedProperty', $value);
280  $plnode->appendChild($child);
281  $child->setAttribute('Name', $name);
282  }
283  $modified = true;
284  }
285  }
286 
287  return $modified;
288  }
289 }
$res
Definition: ltiservices.php:69
buildDom(bool $a_force=false)
static getLogger(string $a_component_id)
Get component logger.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static setPCProperties(string $a_id, array $a_properties)
Set the properties of a plugged page content This method is used by ilCOPageExporter to provide the p...
finalProcessing(ilImportMapping $a_mapping)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getPCProperties(string $a_id)
Get the properties of a plugged page content.
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
Manifest parser for ILIAS standard export files.
static setPCVersion(string $a_id, string $a_version)
Set the version of a plugged page content This method is used by ilCOPageExporter to provide the vers...
global $DIC
Definition: feed.php:28
if($format !==null) $name
Definition: metadata.php:247
getMapping(string $a_comp, string $a_entity, string $a_old_id)
getMappingsOfEntity(string $a_comp, string $a_entity)
importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping)
static _exists(string $a_parent_type, int $a_id, string $a_lang="", bool $a_no_cache=false)
Checks whether page exists.
static getPCVersion(string $a_id)
Get the version of a plugged page content.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
afterContainerImportProcessing(ilImportMapping $a_mapping)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static getInstance(string $a_parent_type, int $a_id=0, int $a_old_nr=0, string $a_lang="-")
Get page object instance.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...