ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCOPageImporter.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/Export/classes/class.ilXmlImporter.php");
5 
14 {
18  protected $log;
19 
23  protected $ds;
24 
29  protected $importer_plugins = array();
30 
34  public function init()
35  {
36  global $DIC;
38  $ilPluginAdmin = $DIC['ilPluginAdmin'];
39 
40  include_once("./Services/COPage/classes/class.ilCOPageDataSet.php");
41  $this->ds = new ilCOPageDataSet();
42  $this->ds->setDSPrefix("ds");
43  $this->config = $this->getImport()->getConfig("Services/COPage");
44 
45  $this->log = ilLoggerFactory::getLogger('copg');
46 
47  // collect all page component plugins that have their own exporter
48  require_once('Services/COPage/classes/class.ilPageComponentPluginImporter.php');
49  foreach (ilPluginAdmin::getActivePluginsForSlot(IL_COMP_SERVICE, "COPage", "pgcp") as $plugin_name) {
50  if ($ilPluginAdmin->supportsExport(IL_COMP_SERVICE, "COPage", "pgcp", $plugin_name)) {
51  require_once('Customizing/global/plugins/Services/COPage/PageComponent/'
52  . $plugin_name . '/classes/class.il' . $plugin_name . 'Importer.php');
53 
54  $this->importer_plugins[] = $plugin_name;
55  }
56  }
57  }
58 
59 
66  public function importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
67  {
68  $this->log->debug("entity: " . $a_entity . ", id: " . $a_id);
69 
70  if ($a_entity == "pgtp") {
71  include_once("./Services/DataSet/classes/class.ilDataSetImportParser.php");
72  $parser = new ilDataSetImportParser(
73  $a_entity,
74  $this->getSchemaVersion(),
75  $a_xml,
76  $this->ds,
77  $a_mapping
78  );
79  }
80 
81  if ($a_entity == "pg") {
82  $pg_id = $a_mapping->getMapping("Services/COPage", "pg", $a_id);
83 
84  $this->log->debug("mapping id: " . $pg_id);
85 
86  if ($pg_id != "") {
87  $id = explode(":", $pg_id);
88  if (count($id) == 2) {
89  include_once("./Services/COPage/classes/class.ilPageObjectFactory.php");
90 
91  while (substr($a_xml, 0, 11) == "<PageObject") {
92  $l1 = strpos($a_xml, ">");
93 
94  $page_tag = "<?xml version='1.0'?> " . substr($a_xml, 0, $l1 + 1) . "</PageObject>";
95  $page_data = simplexml_load_string($page_tag);
96  $lstr = $page_data['Language'];
97  $p = strpos($a_xml, "</PageObject>") + 13;
98  $next_xml = "<PageObject>" . substr($a_xml, $l1 + 1, $p - $l1 - 1);
99 
100  if ($this->config->getForceLanguage() != "") {
101  $lstr = $this->config->getForceLanguage();
102  }
103  if ($lstr == "") {
104  $lstr = "-";
105  }
106  // see bug #0019049
107  $next_xml = str_replace("&amp;", "&", $next_xml);
108  if ($this->config->getUpdateIfExists() && ilPageObject::_exists($id[0], $id[1], $lstr)) {
109  $page = ilPageObjectFactory::getInstance($id[0], $id[1], 0, $lstr);
110  $page->setImportMode(true);
111  $page->setXMLContent($next_xml);
112  $page->updateFromXML();
113  $this->extractPluginProperties($page);
114  } else {
115  if (ilPageObject::_exists($id[0], (int) $id[1], "-", true)) {
116  return;
117  }
118  $new_page = ilPageObjectFactory::getInstance($id[0]);
119  $new_page->setImportMode(true);
120  $new_page->setId($id[1]);
121  if ($lstr != "" && $lstr != "-") {
122  $new_page->setLanguage($lstr);
123  }
124  $this->log->debug(">>> CREATE PAGE " . $id[0] . ":" . $id[1]);
125  $new_page->setXMLContent($next_xml);
126  $new_page->setActive(true);
127  // array_key_exists does NOT work on simplexml!
128  if (isset($page_data["Active"])) {
129  $new_page->setActive($page_data["Active"]);
130  }
131  $new_page->setActivationStart($page_data["ActivationStart"]);
132  $new_page->setActivationEnd($page_data["ActivationEnd"]);
133  $new_page->setShowActivationInfo($page_data["ShowActivationInfo"]);
134  $new_page->createFromXML();
135  $this->extractPluginProperties($new_page);
136  }
137 
138  $a_xml = substr($a_xml, $p);
139  if ($lstr == "") {
140  $lstr = "-";
141  }
142  $a_mapping->addMapping("Services/COPage", "pgl", $a_id . ":" . $lstr, $pg_id . ":" . $lstr);
143  }
144  }
145  }
146  }
147  $this->log->debug("done");
148  }
149 
155  public function finalProcessing($a_mapping)
156  {
157  $this->log->debug("start");
158  $pages = $a_mapping->getMappingsOfEntity("Services/COPage", "pgl");
159  $media_objects = $a_mapping->getMappingsOfEntity("Services/MediaObjects", "mob");
160  $file_objects = $a_mapping->getMappingsOfEntity("Modules/File", "file");
161 
162  $ref_mapping = $a_mapping->getMappingsOfEntity('Services/Container', 'refs');
163 
164  //if (count($media_objects) > 0 || count($file_objects) > 0)
165  //{
166  foreach ($pages as $p) {
167  $id = explode(":", $p);
168  if (count($id) == 3) {
169  include_once("./Services/COPage/classes/class.ilPageObject.php");
170  if (ilPageObject::_exists($id[0], $id[1], $id[2], true)) {
171  include_once("./Services/COPage/classes/class.ilPageObjectFactory.php");
172 
174  $new_page = ilPageObjectFactory::getInstance($id[0], $id[1], 0, $id[2]);
175  $new_page->buildDom();
176  $med = $new_page->resolveMediaAliases($media_objects, $this->config->getReuseOriginallyExportedMedia());
177  $fil = $new_page->resolveFileItems($file_objects);
178  $res = $new_page->resolveResources($ref_mapping);
179  $il = false;
180  if (!$this->config->getSkipInternalLinkResolve()) {
181  $il = $new_page->resolveIntLinks();
182  $this->log->debug("resolve internal link for page " . $id[0] . "-" . $id[1] . "-" . $id[2]);
183  }
184  $plug = $this->replacePluginProperties($new_page);
185  if ($med || $fil || $il || $plug || $res) {
186  $new_page->update(false, true);
187  }
188  }
189  }
190  }
191  //}
192  $this->log->debug("end");
193  }
194 
204  protected function extractPluginProperties($a_page)
205  {
206  if (empty($this->importer_plugins)) {
207  return;
208  }
209 
210  $a_page->buildDom();
211  $domdoc = $a_page->getDomDoc();
212  $xpath = new DOMXPath($domdoc);
213  $nodes = $xpath->query("//PageContent[child::Plugged]");
214 
216  foreach ($nodes as $pcnode) {
217  // page content id (unique in the page)
218  $pc_id = $pcnode->getAttribute('PCID');
219  $plnode = $pcnode->childNodes->item(0);
220  $plugin_name = $plnode->getAttribute('PluginName');
221  $plugin_version = $plnode->getAttribute('PluginVersion');
222 
223  // additional data will be imported
224  if (in_array($plugin_name, $this->importer_plugins)) {
225  // get the id of the mapped plugged page content
226  $id = $a_page->getParentType()
227  . ':' . $a_page->getId()
228  . ':' . $a_page->getLanguage()
229  . ':' . $pc_id;
230 
231  $properties = array();
233  foreach ($plnode->childNodes as $child) {
234  $properties[$child->getAttribute('Name')] = $child->nodeValue;
235  }
236 
237  // statical provision of content to the pluged importer classes
238  ilPageComponentPluginImporter::setPCVersion($id, $plugin_version);
240  }
241  }
242  }
243 
255  public function replacePluginProperties($a_page)
256  {
257  if (empty($this->importer_plugins)) {
258  return false;
259  }
260 
261  $a_page->buildDom();
262  $domdoc = $a_page->getDomDoc();
263  $xpath = new DOMXPath($domdoc);
264  $nodes = $xpath->query("//PageContent[child::Plugged]");
265 
266  $modified = false;
267 
269  foreach ($nodes as $pcnode) {
270  // page content id (unique in the page)
271  $pc_id = $pcnode->getAttribute('PCID');
272  $plnode = $pcnode->childNodes->item(0);
273  $plugin_name = $plnode->getAttribute('PluginName');
274 
275  // get the id of the mapped plugged page content
276  $id = $a_page->getParentType()
277  . ':' . $a_page->getId()
278  . ':' . $a_page->getLanguage()
279  . ':' . $pc_id;
280 
281  $plugin_version = ilPageComponentPluginImporter::getPCVersion($id);
283 
284  // update the version if modified by the plugin importer
285  if (isset($plugin_version)) {
286  $plnode->setAttribute('PluginVersion', $plugin_version);
287  $modified = true;
288  }
289 
290  // update the properties if modified by the plugin importer
291  if (is_array($properties)) {
293  foreach ($plnode->childNodes as $child) {
294  $plnode->removeChild($child);
295  }
296  foreach ($properties as $name => $value) {
297  $child = new DOMElement('PluggedProperty', $value);
298  $plnode->appendChild($child);
299  $child->setAttribute('Name', $name);
300  }
301  $modified = true;
302  }
303  }
304 
305  return $modified;
306  }
307 }
static _exists($a_parent_type, $a_id, $a_lang="", $a_no_cache=false)
Checks whether page exists.
getSchemaVersion()
Get schema version.
static getActivePluginsForSlot($a_ctype, $a_cname, $a_slot_id)
Get all active plugins for a slot.
static getPCVersion($a_id)
Get the version of a plugged page content.
COPage Data set class.
Manifest parser for ILIAS standard export files.
static setPCProperties($a_id, $a_properties)
Set the properties of a plugged page content This method is used by ilCOPageExporter to provide the p...
if($format !==null) $name
Definition: metadata.php:230
static getPCProperties($a_id)
Get the properties of a plugged page content.
foreach($_POST as $key=> $value) $res
Importer class for pages.
getImport()
Get import.
global $DIC
Definition: goto.php:24
finalProcessing($a_mapping)
Final processing.
importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
Import XML.
static getInstance($a_parent_type, $a_id=0, $a_old_nr=0, $a_lang="-")
Get page object instance.
static setPCVersion($a_id, $a_version)
Set the version of a plugged page content This method is used by ilCOPageExporter to provide the vers...
static getLogger($a_component_id)
Get component logger.
const IL_COMP_SERVICE
Xml importer class.