00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 require_once "./classes/class.ilXML2DOM.php";
00025
00037 class ilNestedSetXML
00038 {
00039
00040
00044 var $db;
00045
00049 var $LEFT = 0;
00050 var $RIGHT = 0;
00051
00056 var $DEPTH = 0;
00057
00061 var $obj_id;
00062
00066 var $obj_type;
00067
00071 var $xml_parser;
00072
00076 var $lastTag = "";
00077
00083 var $ilias;
00084
00090 var $dom;
00091
00092
00093
00099 function ilNestedSetXML()
00100 {
00101 global $ilias;
00102
00103 $this->ilias =& $ilias;
00104
00105 $this->db =& $this->ilias->db;
00106 $this->LEFT = 0;
00107 $this->RIGHT = 0;
00108 $this->DEPTH = 0;
00109
00110 $this->param_modifier = "";
00111 }
00112
00113
00124 function startElement($parser, $name, $attrs)
00125 {
00126
00127
00128 $this->lastTag = $name;
00129 $this->LEFT += 1;
00130 $this->RIGHT = $this->LEFT + 1;
00131 $this->DEPTH++;
00132
00136 $this->db->query("INSERT INTO xmltags ( tag_name,tag_depth ) VALUES ('".$name."','".$this->DEPTH."') ");
00137
00138 $r = $this->db->query("SELECT LAST_INSERT_ID()");
00139 $row = $r->fetchRow();
00140
00141 $pk = $row[0];
00142
00143 $Q = "UPDATE NestedSetTemp SET ns_r=ns_r+2 WHERE ns_r>='".($this->LEFT)."' AND ns_book_fk='".$this->obj_id."' ";
00144 $this->db->query($Q);
00145
00146 $Q = "INSERT INTO NestedSetTemp (ns_book_fk,ns_type,ns_tag_fk,ns_l,ns_r) VALUES ('".$this->obj_id."','".$this->obj_type."','".$pk."',".$this->LEFT.",".$this->RIGHT.") ";
00147 $this->db->query($Q);
00148
00149 $this->clean($attrs);
00150 if (is_array($attrs) && count($attrs)>0)
00151 {
00152 reset ($attrs);
00153 while (list ($key, $val) = each ($attrs))
00154 {
00155 $this->db->query("INSERT INTO xmlparam ( tag_fk,param_name,param_value ) VALUES ('".$pk."','$key','".addslashes($val)."') ");
00156 }
00157 }
00158
00159 return($pk);
00160
00161 }
00162
00171 function characterData($parser, $data)
00172 {
00173
00179 static $value_pk;
00180
00181 if(trim($data)!="") {
00182
00183 if ($this->lastTag == "TAGVALUE")
00184 {
00185 $Q = "UPDATE xmlvalue SET tag_value=concat(tag_value,'".addslashes($data)."') WHERE tag_value_pk='".$value_pk."' ";
00186 $this->db->query($Q);
00187 }
00188 else
00189 {
00190 $tag_pk = $this->startElement($this->xml_parser,"TAGVALUE",array());
00191 $this->endElement($this->xml_parser,"TAGVALUE");
00192
00193 $Q = "INSERT INTO xmlvalue (tag_fk,tag_value) VALUES ('".$tag_pk."','".addslashes($data)."') ";
00194 $this->db->query($Q);
00195
00196 $Q = "SELECT LAST_INSERT_ID()";
00197 $r = $this->db->query($Q);
00198 $row = $r->fetchRow();
00199 $value_pk = $row[0];
00200
00201 $this->lastTag = "TAGVALUE";
00202 }
00203
00204 }
00205
00206 }
00207
00215 function endElement($parser, $name)
00216 {
00217
00218 $this->DEPTH--;
00219 $this->LEFT += 1;
00220 $this->lastTag = "";
00221
00222 }
00223
00232 function import($xmldata, $obj_id, $obj_type)
00233 {
00234
00238 $this->db->query("DROP TABLE IF EXISTS NestedSetTemp");
00239
00243 $Q = "CREATE TEMPORARY TABLE NestedSetTemp (
00244 ns_book_fk int(11) NOT NULL,
00245 ns_type char(50) NOT NULL,
00246 ns_tag_fk int(11) NOT NULL,
00247 ns_l int(11) NOT NULL,
00248 ns_r int(11) NOT NULL,
00249 KEY ns_tag_fk (ns_tag_fk),
00250 KEY ns_l (ns_l),
00251 KEY ns_r (ns_r),
00252 KEY ns_book_fk (ns_book_fk)
00253 ) TYPE=MyISAM ";
00254 $this->db->query($Q);
00255
00256 $this->obj_id = $obj_id;
00257 $this->obj_type = $obj_type;
00258 $this->DEPTH = 0;
00259 $this->LEFT = 0;
00260 $this->RIGHT = 0;
00261
00262 $this->db->query("DELETE FROM NestedSetTemp");
00263
00267 $this->xml_parser = xml_parser_create("UTF-8");
00268 xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, false);
00269 xml_set_object($this->xml_parser,$this);
00270 xml_set_element_handler($this->xml_parser, "startElement", "endElement");
00271 xml_set_character_data_handler($this->xml_parser, "characterData");
00272
00273 if (!xml_parse($this->xml_parser, $xmldata)) {
00274 die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)),xml_get_current_line_number($this->xml_parser)));
00275 }
00276 xml_parser_free($this->xml_parser);
00277
00281 $this->deleteAllDbData();
00282
00283 $this->db->query("INSERT INTO xmlnestedset SELECT * FROM NestedSetTemp");
00284 $this->db->query("DROP TABLE IF EXISTS NestedSetTemp");
00285
00286 }
00287
00293 function setParameterModifier(&$a_object, $a_method)
00294 {
00295 $this->param_modifier =& $a_object;
00296 $this->param_modifier_method = $a_method;
00297 }
00298
00310 function export($obj_id, $type)
00311 {
00312
00313 $query = "SELECT * FROM xmlnestedset,xmltags WHERE ns_tag_fk=tag_pk AND ns_book_fk='$obj_id' AND ns_type='$type' ORDER BY ns_l";
00314 $result = $this->db->query($query);
00315 if (DB::isError($result))
00316 {
00317 die($this->className."::checkTable(): ".$result->getMessage().":<br>".$q);
00318 }
00319
00320 $xml = "";
00321 $lastDepth = -1;
00322
00323 while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) )
00324 {
00325
00326
00327 $Anfang = "<".$row[tag_name];
00328 $query = "SELECT * FROM xmlparam WHERE tag_fk='$row[tag_pk]'";
00329 $result_param = $this->db->query($query);
00330 while (is_array($row_param = $result_param->fetchRow(DB_FETCHMODE_ASSOC) ) )
00331 {
00332 $param_value = $row_param[param_value];
00333 if (is_object($this->param_modifier))
00334 {
00335 $obj =& $this->param_modifier;
00336 $method = $this->param_modifier_method;
00337 $param_value = $obj->$method($row[tag_name], $row_param[param_name], $param_value);
00338 }
00339 $Anfang .= " ".$row_param[param_name]."=\"".$param_value."\"";
00340 }
00341
00342 $Anfang .= ">";
00343 $Ende = "</".$row[tag_name].">";
00344
00345
00346
00347 if ($row[tag_name]=="TAGVALUE")
00348 {
00349 $query = "SELECT * FROM xmlvalue WHERE tag_fk='$row[tag_pk]' ";
00350 $result_value = $this->db->query($query);
00351 $row_value = $result_value->fetchRow(DB_FETCHMODE_ASSOC);
00352 $Anfang = $row_value["tag_value"];
00353 $Ende = "";
00354
00355 $Anfang = htmlspecialchars($Anfang);
00356
00357 }
00358
00359
00360 $D = $row[tag_depth];
00361
00362 if ($D==$lastDepth)
00363 {
00364 $xml .= $xmlE[$D];
00365 $xml .= $Anfang;
00366 $xmlE[$D] = $Ende;
00367 }
00368 else if ($D>$lastDepth)
00369 {
00370 $xml .= $Anfang;
00371 $xmlE[$D] = $Ende;
00372 }
00373 else
00374 {
00375 for ($i=$lastDepth;$i>=$D;$i--)
00376 {
00377 $xml .= $xmlE[$i];
00378 }
00379 $xml .= $Anfang;
00380 $xmlE[$D] = $Ende;
00381 }
00382
00383 $lastDepth = $D;
00384
00385 }
00386
00387 for ($i=$lastDepth;$i>0;$i--)
00388 {
00389 $xml .= $xmlE[$i];
00390 }
00391
00392 return($xml);
00393
00394 }
00395
00403 function init($obj_id,$obj_type)
00404 {
00405
00406 $query = "SELECT * FROM xmlnestedset,xmltags WHERE ns_book_fk='".$obj_id."' AND ns_type='".
00407 $obj_type."' AND ns_tag_fk=tag_pk ORDER BY ns_l LIMIT 1";
00408 $result = $this->db->query($query);
00409 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00410
00411 $this->LEFT = $row["ns_l"];
00412 $this->RIGHT = $row["ns_r"];
00413 $this->DEPTH = $row["tag_depth"];
00414 $this->obj_id = $obj_id;
00415 $this->obj_type = $obj_type;
00416
00417 }
00418
00426 function getTagName()
00427 {
00428
00429 $query = "SELECT * FROM xmlnestedset,xmltags WHERE ns_book_fk='".$this->obj_id."' AND ns_type='".$this->obj_type."' AND ns_l='".$this->LEFT."' AND ns_r='".$this->RIGHT."' AND ns_tag_fk=tag_pk LIMIT 1";
00430 $result = $this->db->query($query);
00431 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00432
00433 return($row["tag_name"]);
00434
00435 }
00436
00446 function setTagName($tagName)
00447 {
00448
00449 $query = "SELECT * FROM xmlnestedset WHERE ns_book_fk='".$this->obj_id."' AND ns_type='".$this->obj_type."' AND ns_l='".$this->LEFT."' AND ns_r='".$this->RIGHT."' LIMIT 1";
00450 $result = $this->db->query($query);
00451 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00452
00453 $query = "UPDATE xmltags SET tag_name='$tagName' WHERE tag_pk='".$row["ns_tag_fk"]."'";
00454 $this->db->query($query);
00455
00456 return($row["tagName"]);
00457
00458 }
00459
00460
00467 function getTagValue()
00468 {
00469
00470 $V = array();
00471
00472 $query = "SELECT * FROM xmlnestedset,xmltags WHERE ns_tag_fk=tag_pk AND ns_book_fk='".$this->obj_id."' AND ns_type='".$this->obj_type."' AND ns_l>='".$this->LEFT."' AND ns_r<='".$this->RIGHT."' AND tag_depth='".($this->DEPTH+1)."' ORDER BY ns_l";
00473 $result = $this->db->query($query);
00474 while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) )
00475 {
00476 if ($row[tag_name]=="TAGVALUE")
00477 {
00478 $query = "SELECT * FROM xmlvalue WHERE tag_fk='".$row[tag_pk]."' ";
00479 $result2 = $this->db->query($query);
00480 $row2 = $result2->fetchRow(DB_FETCHMODE_ASSOC);
00481 $V[] = $row2[tag_value];
00482 }
00483 else
00484 {
00485 $xml = new ilNestedSetXml();
00486
00487 $xml->LEFT = $row["ns_l"];
00488 $xml->RIGHT = $row["ns_r"];
00489 $xml->DEPTH = $row["tag_depth"];
00490 $xml->obj_id = $obj_id;
00491 $xml->obj_type = $obj_type;
00492
00493 $V[] = $xml;
00494
00495 }
00496 }
00497
00498 return($V);
00499 }
00500
00507 function setTagValue($value)
00508 {
00509 $V = array();
00510
00511 $query = "SELECT * FROM xmlnestedset,xmltags
00512 LEFT JOIN xmlvalue ON xmltags.tag_pk=xmlvalue.tag_fk
00513 WHERE ns_tag_fk=tag_pk AND
00514 ns_book_fk='".$this->obj_id."' AND
00515 ns_type='".$this->obj_type."' AND
00516 ns_l>='".$this->LEFT."' AND
00517 ns_r<='".$this->RIGHT."' AND
00518 tag_depth='".($this->DEPTH+1)."' AND
00519 tag_name = 'TAGVALUE'
00520 ORDER BY ns_l";
00521 $result = $this->db->query($query);
00522
00523 if (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) )
00524 {
00525
00526 $query = "UPDATE xmlvalue SET tag_value='".addslashes($value)."' WHERE tag_value_pk='".$row["tag_value_pk"]."' ";
00527 $this->db->query($query);
00528
00529 }
00530 else
00531 {
00532
00537 }
00538 }
00539
00549 function getXpathNodes(&$doc, $qry)
00550 {
00551 if (is_object($doc))
00552 {
00553 $xpath = $doc->xpath_init();
00554 $ctx = $doc->xpath_new_context();
00555
00556 $result = $ctx->xpath_eval($qry);
00557 if (is_array($result->nodeset))
00558 {
00559 return($result->nodeset);
00560 }
00561 }
00562 return Null;
00563 }
00564
00571 function initDom()
00572 {
00573 $xml = $this->export($this->obj_id, $this->obj_type);
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590 if ($xml=="")
00591 {
00592 return(false);
00593 }
00594 else
00595 {
00596 $this->dom = domxml_open_mem($xml);
00597 return(true);
00598 }
00599 }
00600
00611 function addXMLNode($xPath, $xml, $index = 0)
00612 {
00613 $newDOM = new XML2DOM($xml);
00614
00615 $nodes = $this->getXpathNodes($this->dom, $xPath);
00616
00617 if (count($nodes) > 0)
00618 {
00619 $newDOM->insertNode($this->dom, $nodes[$index]);
00620 return true;
00621 }
00622 else
00623 {
00624 return false;
00625 }
00626 }
00627
00636 function getFirstDomContent($xPath)
00637 {
00638
00639 $content = "";
00640 if (is_object($this->dom))
00641 {
00642 $node = $this->getXpathNodes($this->dom,$xPath);
00643 if (is_array($node))
00644 {
00645 $c = $node[0]->children();
00646
00647 if (is_object($c[0]))
00648 {
00649 $content = $c[0]->get_content();
00650 }
00651 }
00652 }
00653
00654 return($content);
00655 }
00656
00667 function deleteDomNode($xPath, $name, $index = 0)
00668 {
00669 if ($index == "")
00670 {
00671 $index = 0;
00672 }
00673 if (strpos($index, ","))
00674 {
00675 $indices = explode(",", $index);
00676 $nodes = $this->getXpathNodes($this->dom, $xPath);
00677 if (count($nodes) > 0)
00678 {
00679 $children = $nodes[$indices[0]]->child_nodes();
00680 if (count($children) > 0)
00681 {
00682 $j = 0;
00683 for ($i = 0; $i < count($children); $i++)
00684 {
00685 if ($children[$i]->node_name() == $name)
00686 {
00687 if ($j == $indices[1])
00688 {
00689 $children[$i]->unlink_node();
00690 return true;
00691 }
00692 $j++;
00693 }
00694 }
00695 }
00696 }
00697 }
00698 else
00699 {
00700 $nodes = $this->getXpathNodes($this->dom, $xPath . "/" . $name);
00701 if (count($nodes) > 0)
00702 {
00703 $nodes[$index]->unlink_node();
00704 return true;
00705 }
00706 }
00707 return false;
00708 }
00709
00722 function addDomNode($xPath, $name, $value = "", $attributes = "", $index = 0)
00723 {
00724 $nodes = $this->getXpathNodes($this->dom, $xPath);
00725 if (count($nodes) > 0)
00726 {
00727 $node = $this->dom->create_element($name);
00728 if ($value != "")
00729 {
00730 $node->set_content(utf8_encode($value));
00731 }
00732 if (is_array($attributes))
00733 {
00734 for ($i = 0; $i < count($attributes); $i++)
00735 {
00736 $node->set_attribute($attributes[$i]["name"], utf8_encode($attributes[$i]["value"]));
00737 }
00738 }
00739 $nodes[$index]->append_child($node);
00740 return true;
00741 }
00742 else
00743 {
00744 return false;
00745 }
00746 }
00747
00748 function clean(&$meta)
00749 {
00750 if(is_array($meta))
00751 {
00752 foreach($meta as $key => $value)
00753 {
00754 if(is_array($meta[$key]))
00755 {
00756 $this->clean($meta[$key]);
00757 }
00758 else
00759 {
00760 $meta[$key] = preg_replace("/&(?!amp;|lt;|gt;|quot;)/","&",$meta[$key]);
00761 $meta[$key] = preg_replace("/\"/",""",$meta[$key]);
00762 $meta[$key] = preg_replace("/</","<",$meta[$key]);
00763 $meta[$key] = preg_replace("/>/",">",$meta[$key]);
00764 }
00765 }
00766 }
00767 return true;
00768 }
00777 function updateDomNode($xPath, $meta, $no = 0)
00778 {
00779 $this->clean($meta);
00780 $update = false;
00781 if ($xPath == "//Bibliography")
00782 {
00783 $nodes = $this->getXpathNodes($this->dom, $xPath . "/BibItem[" . ($no+1) . "]");
00784 }
00785 else
00786 {
00787 $nodes = $this->getXpathNodes($this->dom, $xPath);
00788 }
00789 if (count($nodes) > 0)
00790 {
00791
00792
00793 if ($nodes[0]->node_name() == "BibItem")
00794 {
00795 $xml = '<BibItem Type="' . ilUtil::stripSlashes($meta["Type"]) . '" Label="' . ilUtil::stripSlashes($meta["Label"]["Value"]) . '">';
00796 $xml .= '<Identifier Catalog="' . ilUtil::stripSlashes($meta["Identifier"]["Catalog"]) . '" Entry="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Identifier"]["Entry"])) . '"/>';
00797 for ($i = 0; $i < count($meta["Language"]); $i++)
00798 {
00799 $xml .= '<Language Language="' . ilUtil::stripSlashes($meta["Language"][$i]["Language"]) . '"/>';
00800 }
00801 for ($i = 0; $i < count($meta["Author"]); $i++)
00802 {
00803 $xml .= '<Author>';
00804 # for ($j = 0; $j < count($meta["Author"][$i]["FirstName"]); $j++)
00805 # {
00806 $xml .= '<FirstName>' . ilUtil::stripSlashes($meta["Author"][$i]["FirstName"]) . '</FirstName>';
00807 # }
00808 # for ($j = 0; $j < count($meta["Author"][$i]["MiddleName"]); $j++)
00809 # {
00810 $xml .= '<MiddleName>' . ilUtil::stripSlashes($meta["Author"][$i]["MiddleName"]) . '</MiddleName>';
00811 # }
00812 # for ($j = 0; $j < count($meta["Author"][$i]["LastName"]); $j++)
00813 # {
00814 $xml .= '<LastName>' . ilUtil::stripSlashes($meta["Author"][$i]["LastName"]) . '</LastName>';
00815 # }
00816 $xml .= '</Author>';
00817 }
00818 $xml .= '<Booktitle Language="' . ilUtil::stripSlashes($meta["Booktitle"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Booktitle"]["Value"]) . '</Booktitle>';
00819 for ($i = 0; $i < count($meta["CrossRef"]); $i++)
00820 {
00821 $xml .= '<CrossRef>' . ilUtil::stripSlashes($meta["CrossRef"][$i]["Value"]) . '</CrossRef>';
00822 }
00823 $xml .= '<Edition>' . ilUtil::stripSlashes($meta["Edition"]["Value"]) . '</Edition>';
00824 for ($i = 0; $i < count($meta["Editor"]); $i++)
00825 {
00826 $xml .= '<Editor>' . ilUtil::stripSlashes($meta["Editor"][$i]["Value"]) . '</Editor>';
00827 }
00828 $xml .= '<HowPublished Type="' . ilUtil::stripSlashes($meta["HowPublished"]["Type"]) . '"/>';
00829 for ($i = 0; $i < count($meta["WherePublished"]); $i++)
00830 {
00831 $xml .= '<WherePublished>' . ilUtil::stripSlashes($meta["WherePublished"][$i]["Value"]) . '</WherePublished>';
00832 }
00833 for ($i = 0; $i < count($meta["Institution"]); $i++)
00834 {
00835 $xml .= '<Institution>' . ilUtil::stripSlashes($meta["Institution"][$i]["Value"]) . '</Institution>';
00836 }
00837 if (is_array($meta["Journal"]))
00838 {
00839 $xml .= '<Journal Note="' . ilUtil::stripSlashes($meta["Journal"]["Note"]) . '" Number="' . ilUtil::stripSlashes($meta["Journal"]["Number"]) . '" Organization="' . ilUtil::stripSlashes($meta["Journal"]["Organization"]) . '"/>';
00840 }
00841 for ($i = 0; $i < count($meta["Keyword"]); $i++)
00842 {
00843 $xml .= '<Keyword Language="' . ilUtil::stripSlashes($meta["Keyword"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Keyword"][$i]["Value"]) . '</Keyword>';
00844 }
00845 if (is_array($meta["Month"]))
00846 {
00847 $xml .= '<Month>' . ilUtil::stripSlashes($meta["Month"]["Value"]) . '</Month>';
00848 }
00849 if (is_array($meta["Pages"]))
00850 {
00851 $xml .= '<Pages>' . ilUtil::stripSlashes($meta["Pages"]["Value"]) . '</Pages>';
00852 }
00853 $xml .= '<Publisher>' . ilUtil::stripSlashes($meta["Publisher"]["Value"]) . '</Publisher>';
00854 for ($i = 0; $i < count($meta["School"]); $i++)
00855 {
00856 $xml .= '<School>' . ilUtil::stripSlashes($meta["School"][$i]["Value"]) . '</School>';
00857 }
00858 if (is_array($meta["Series"]))
00859 {
00860 $xml .= '<Series>';
00861 $xml .= '<SeriesTitle>' . ilUtil::stripSlashes($meta["Series"]["SeriesTitle"]) . '</SeriesTitle>';
00862 # for ($i = 0; $i < count($meta["Series"]["SeriesEditor"]); $i++)
00863 if (isset($meta["Series"]["SeriesEditor"]))
00864 {
00865 # $xml .= '<SeriesEditor>' . ilUtil::stripSlashes($meta["Series"]["SeriesEditor"][$i]) . '</SeriesEditor>';
00866 $xml .= '<SeriesEditor>' . ilUtil::stripSlashes($meta["Series"]["SeriesEditor"]) . '</SeriesEditor>';
00867 }
00868 if (isset($meta["Series"]["SeriesVolume"]))
00869 {
00870 $xml .= '<SeriesVolume>' . ilUtil::stripSlashes($meta["Series"]["SeriesVolume"]) . '</SeriesVolume>';
00871 }
00872 $xml .= '</Series>';
00873 }
00874 $xml .= '<Year>' . ilUtil::stripSlashes($meta["Year"]["Value"]) . '</Year>';
00875 if ($meta["URL_ISBN_ISSN"]["Type"] == "URL")
00876 {
00877 $xml .= '<URL>' . ilUtil::stripSlashes($meta["URL_ISBN_ISSN"]["Value"]) . '</URL>';
00878 }
00879 else if ($meta["URL_ISBN_ISSN"]["Type"] == "ISBN")
00880 {
00881 $xml .= '<ISBN>' . ilUtil::stripSlashes($meta["URL_ISBN_ISSN"]["Value"]) . '</ISBN>';
00882 }
00883 else if ($meta["URL_ISBN_ISSN"]["Type"] == "ISSN")
00884 {
00885 $xml .= '<ISSN>' . ilUtil::stripSlashes($meta["URL_ISBN_ISSN"]["Value"]) . '</ISSN>';
00886 }
00887 $xml .= '</BibItem>';
00888 # echo htmlspecialchars($xml);
00889
00890 $update = true;
00891 }
00892
00893
00894 else if ($nodes[0]->node_name() == "General")
00895 {
00896
00897 $xml = '<General Structure="' . ilUtil::stripSlashes($meta["Structure"]) . '">';
00898 for ($i = 0; $i < count($meta["Identifier"]); $i++)
00899 {
00900 $xml .= '<Identifier Catalog="' . ilUtil::stripSlashes($meta["Identifier"][$i]["Catalog"]) . '" Entry="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Identifier"][$i]["Entry"])) . '"/>';
00901 }
00902 $xml .= '<Title Language="' . ilUtil::stripSlashes($meta["Title"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Title"]["Value"]) . '</Title>';
00903 for ($i = 0; $i < count($meta["Language"]); $i++)
00904 {
00905 $xml .= '<Language Language="' . ilUtil::stripSlashes($meta["Language"][$i]["Language"]) . '"/>';
00906 }
00907 for ($i = 0; $i < count($meta["Description"]); $i++)
00908 {
00909 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Description"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Description"][$i]["Value"]) . '</Description>';
00910 }
00911 for ($i = 0; $i < count($meta["Keyword"]); $i++)
00912 {
00913 $xml .= '<Keyword Language="' . ilUtil::stripSlashes($meta["Keyword"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Keyword"][$i]["Value"]) . '</Keyword>';
00914 }
00915 if ($meta["Coverage"] != "")
00916 {
00917 $xml .= '<Coverage Language="' . ilUtil::stripSlashes($meta["Coverage"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Coverage"]["Value"]) . '</Coverage>';
00918 }
00919 $xml .= '</General>';
00920
00921
00922 $update = true;
00923 }
00924
00925
00926 else if ($nodes[0]->node_name() == "Lifecycle")
00927 {
00928 $xml = '<Lifecycle Status="' . $meta["Status"] . '">';
00929 $xml .= '<Version Language="' . ilUtil::stripSlashes($meta["Version"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Version"]["Value"]) . '</Version>';
00930 for ($i = 0; $i < count($meta["Contribute"]); $i++)
00931 {
00932 $xml .= '<Contribute Role="' . ilUtil::stripSlashes($meta["Contribute"][$i]["Role"]) . '">';
00933 $xml .= '<Date>' . ilUtil::stripSlashes($meta["Contribute"][$i]["Date"]) . '</Date>';
00934 for ($j = 0; $j < count($meta["Contribute"][$i]["Entity"]); $j++)
00935 {
00936 $xml .= '<Entity>' . ilUtil::stripSlashes($meta["Contribute"][$i]["Entity"][$j]) . '</Entity>';
00937 }
00938 $xml .= '</Contribute>';
00939 }
00940 $xml .= '</Lifecycle>';
00941 # echo htmlspecialchars($xml);
00942
00943 $update = true;
00944 }
00945
00946
00947 else if ($nodes[0]->node_name() == "Meta-Metadata")
00948 {
00949
00950 $xml = '<Meta-Metadata MetadataScheme="LOM v 1.0" Language="' . ilUtil::stripSlashes($meta["Language"]) . '">';
00951 for ($i = 0; $i < count($meta["Identifier"]); $i++)
00952 {
00953 $xml .= '<Identifier Catalog="' . ilUtil::stripSlashes($meta["Identifier"][$i]["Catalog"]) . '" Entry="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Identifier"][$i]["Entry"])) . '"/>';
00954 }
00955 for ($i = 0; $i < count($meta["Contribute"]); $i++)
00956 {
00957 $xml .= '<Contribute Role="' . ilUtil::stripSlashes($meta["Contribute"][$i]["Role"]) . '">';
00958 $xml .= '<Date>' . ilUtil::stripSlashes($meta["Contribute"][$i]["Date"]) . '</Date>';
00959 for ($j = 0; $j < count($meta["Contribute"][$i]["Entity"]); $j++)
00960 {
00961 $xml .= '<Entity>' . ilUtil::stripSlashes($meta["Contribute"][$i]["Entity"][$j]) . '</Entity>';
00962 }
00963 $xml .= '</Contribute>';
00964 }
00965 $xml .= '</Meta-Metadata>';
00966 # echo htmlspecialchars($xml);
00967
00968 $update = true;
00969 }
00970
00971
00972 else if ($nodes[0]->node_name() == "Technical")
00973 {
00974
00975 $xml = '<Technical>';
00976 for ($i = 0; $i < count($meta["Format"]); $i++)
00977 {
00978 $xml .= '<Format>' . ilUtil::stripSlashes($meta["Format"][$i]) . '</Format>';
00979 }
00980 if ($meta["Size"] != "")
00981 {
00982 $xml .= '<Size>' . ilUtil::stripSlashes($meta["Size"]) . '</Size>';
00983 }
00984 for ($i = 0; $i < count($meta["Location"]); $i++)
00985 {
00986 $xml .= '<Location Type="' . ilUtil::stripSlashes($meta["Location"][$i]["Type"]) . '">' . ilUtil::stripSlashes($meta["Location"][$i]["Value"]) . '</Location>';
00987 }
00988 if (is_array($meta["Requirement"]))
00989 {
00990 for ($i = 0; $i < count($meta["Requirement"]); $i++)
00991 {
00992 $xml .= '<Requirement>';
00993 $xml .= '<Type>';
00994 if (is_array($meta["Requirement"][$i]["Type"]["OperatingSystem"]))
00995 {
00996 $xml .= '<OperatingSystem Name="' . ilUtil::stripSlashes($meta["Requirement"][$i]["Type"]["OperatingSystem"]["Name"]) . '" MinimumVersion="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Requirement"][$i]["Type"]["OperatingSystem"]["MinimumVersion"])) . '" MaximumVersion="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Requirement"][$i]["Type"]["OperatingSystem"]["MaximumVersion"])) . '"/>';
00997 }
00998 if (is_array($meta["Requirement"][$i]["Type"]["Browser"]))
00999 {
01000 $xml .= '<Browser Name="' . ilUtil::stripSlashes($meta["Requirement"][$i]["Type"]["Browser"]["Name"]) . '" MinimumVersion="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Requirement"][$i]["Type"]["Browser"]["MinimumVersion"])) . '" MaximumVersion="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Requirement"][$i]["Type"]["Browser"]["MaximumVersion"])) . '"/>';
01001 }
01002 $xml .= '</Type>';
01003 $xml .= '</Requirement>';
01004 }
01005 }
01006 else if (is_array($meta["OrComposite"]))
01007 {
01008 for ($j = 0; $j < count($meta["OrComposite"]); $j++)
01009 {
01010 $xml .= '<OrComposite>';
01011 for ($i = 0; $i < count($meta["OrComposite"][$j]["Requirement"]); $i++)
01012 {
01013 $xml .= '<Requirement>';
01014 $xml .= '<Type>';
01015 if (is_array($meta["OrComposite"][$j]["Requirement"][$i]["Type"]["OperatingSystem"]))
01016 {
01017 $xml .= '<OperatingSystem Name="' . ilUtil::stripSlashes($meta["OrComposite"][$j]["Requirement"][$i]["Type"]["OperatingSystem"]["Name"]) . '" MinimumVersion="' . str_replace("\"", "", ilUtil::stripSlashes($meta["OrComposite"][$j]["Requirement"][$i]["Type"]["OperatingSystem"]["MinimumVersion"])) . '" MaximumVersion="' . str_replace("\"", "", ilUtil::stripSlashes($meta["OrComposite"][$j]["Requirement"][$i]["Type"]["OperatingSystem"]["MaximumVersion"])) . '"/>';
01018 }
01019 if (is_array($meta["OrComposite"][$j]["Requirement"][$i]["Type"]["Browser"]))
01020 {
01021 $xml .= '<Browser Name="' . ilUtil::stripSlashes($meta["OrComposite"][$j]["Requirement"][$i]["Type"]["Browser"]["Name"]) . '" MinimumVersion="' . str_replace("\"", "", ilUtil::stripSlashes($meta["OrComposite"][$j]["Requirement"][$i]["Type"]["Browser"]["MinimumVersion"])) . '" MaximumVersion="' . str_replace("\"", "", ilUtil::stripSlashes($meta["OrComposite"][$j]["Requirement"][$i]["Type"]["Browser"]["MaximumVersion"])) . '"/>';
01022 }
01023 $xml .= '</Type>';
01024 $xml .= '</Requirement>';
01025 }
01026 $xml .= '</OrComposite>';
01027 }
01028 }
01029 if (is_array($meta["InstallationRemarks"]))
01030 {
01031 $xml .= '<InstallationRemarks Language="' . ilUtil::stripSlashes($meta["InstallationRemarks"]["Language"]) . '">' . ilUtil::stripSlashes($meta["InstallationRemarks"]["Value"]) . '</InstallationRemarks>';
01032 }
01033 if (is_array($meta["OtherPlattformRequirements"]))
01034 {
01035 $xml .= '<OtherPlattformRequirements Language="' . ilUtil::stripSlashes($meta["OtherPlattformRequirements"]["Language"]) . '">' . ilUtil::stripSlashes($meta["OtherPlattformRequirements"]["Value"]) . '</OtherPlattformRequirements>';
01036 }
01037 if ($meta["Duration"] != "")
01038 {
01039 $xml .= '<Duration>' . ilUtil::stripSlashes($meta["Duration"]) . '</Duration>';
01040 }
01041 $xml .= '</Technical>';
01042 # echo htmlspecialchars($xml);
01043
01044 $update = true;
01045 }
01046
01047
01048 else if ($nodes[0]->node_name() == "Educational")
01049 {
01050
01051 $xml = '<Educational InteractivityType="' . ilUtil::stripSlashes($meta["InteractivityType"]) . '" LearningResourceType="' . ilUtil::stripSlashes($meta["LearningResourceType"]) . '" InteractivityLevel="' . ilUtil::stripSlashes($meta["InteractivityLevel"]) . '" SemanticDensity="' . ilUtil::stripSlashes($meta["SemanticDensity"]) . '" IntendedEndUserRole="' . ilUtil::stripSlashes($meta["IntendedEndUserRole"]) . '" Context="' . ilUtil::stripSlashes($meta["Context"]) . '" Difficulty="' . ilUtil::stripSlashes($meta["Difficulty"]) . '">';
01052 $xml .= '<TypicalLearningTime>' . ilUtil::stripSlashes($meta["TypicalLearningTime"]) . '</TypicalLearningTime>';
01053 for ($i = 0; $i < count($meta["TypicalAgeRange"]); $i++)
01054 {
01055 $xml .= '<TypicalAgeRange Language="' . ilUtil::stripSlashes($meta["TypicalAgeRange"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["TypicalAgeRange"][$i]["Value"]) . '</TypicalAgeRange>';
01056 }
01057 for ($i = 0; $i < count($meta["Description"]); $i++)
01058 {
01059 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Description"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Description"][$i]["Value"]) . '</Description>';
01060 }
01061 for ($i = 0; $i < count($meta["Language"]); $i++)
01062 {
01063 $xml .= '<Language Language="' . ilUtil::stripSlashes($meta["Language"][$i]["Language"]) . '"/>';
01064 }
01065 $xml .= '</Educational>';
01066
01067 $update = true;
01068 }
01069
01070
01071 else if ($nodes[0]->node_name() == "Rights")
01072 {
01073
01074 $xml = '<Rights Cost="' . ilUtil::stripSlashes($meta["Cost"]) . '" CopyrightAndOtherRestrictions="' . ilUtil::stripSlashes($meta["CopyrightAndOtherRestrictions"]) . '">';
01075 for ($i = 0; $i < count($meta["Description"]); $i++)
01076 {
01077 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Description"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Description"][$i]["Value"]) . '</Description>';
01078 }
01079 $xml .= '</Rights>';
01080
01081 $update = true;
01082 }
01083
01084
01085 else if ($nodes[0]->node_name() == "Relation")
01086 {
01087
01088 # for ($j = 0; $j < count($meta["Relation"]); $j++)
01089 # {
01090 $meta["Relation"][0] = $meta;
01091 $j = 0;
01092 $xml = '<Relation Kind="' . ilUtil::stripSlashes($meta["Relation"][$j]["Kind"]) . '">';
01093 $xml .= '<Resource>';
01094 for ($i = 0; $i < count($meta["Relation"][$j]["Resource"]["Identifier"]); $i++)
01095 {
01096 $xml .= '<Identifier_ Catalog="' . ilUtil::stripSlashes($meta["Relation"][$j]["Resource"]["Identifier"][$i]["Catalog"]) . '" Entry="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Relation"][$j]["Resource"]["Identifier"][$i]["Entry"])) . '"/>';
01097 }
01098 for ($i = 0; $i < count($meta["Relation"][$j]["Resource"]["Description"]); $i++)
01099 {
01100 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Relation"][$j]["Resource"]["Description"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Relation"][$j]["Resource"]["Description"][$i]["Value"]) . '</Description>';
01101 }
01102 $xml .= '</Resource>';
01103 $xml .= '</Relation>';
01104 # echo htmlspecialchars($xml);
01105 # }
01106
01107 $update = true;
01108 }
01109
01110
01111 else if ($nodes[0]->node_name() == "Annotation")
01112 {
01113
01114 # for ($i = 0; $i < count($meta["Annotation"]); $i++)
01115 # {
01116 $meta["Annotation"][0] = $meta;
01117 $i = 0;
01118 $xml = '<Annotation>';
01119 $xml .= '<Entity>' . ilUtil::stripSlashes($meta["Annotation"][$i]["Entity"]) . '</Entity>';
01120 $xml .= '<Date>' . ilUtil::stripSlashes($meta["Annotation"][$i]["Date"]) . '</Date>';
01121 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Annotation"][$i]["Description"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Annotation"][$i]["Description"]["Value"]) . '</Description>';
01122 $xml .= '</Annotation>';
01123 # echo htmlspecialchars($xml);
01124 # }
01125
01126 $update = true;
01127 }
01128
01129
01130 else if ($nodes[0]->node_name() == "Classification")
01131 {
01132
01133 # for ($j = 0; $j < count($meta["Classification"]); $j++)
01134 # {
01135 $meta["Classification"][0] = $meta;
01136 $j = 0;
01137 $xml = '<Classification Purpose="' . ilUtil::stripSlashes($meta["Classification"][$j]["Purpose"]) . '">';
01138 for ($k = 0; $k < count($meta["Classification"][$j]["TaxonPath"]); $k++)
01139 {
01140 $xml .= '<TaxonPath>';
01141 $xml .= '<Source Language="' . ilUtil::stripSlashes($meta["Classification"][$j]["TaxonPath"][$k]["Source"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Classification"][$j]["TaxonPath"][$k]["Source"]["Value"]) . '</Source>';
01142 for ($i = 0; $i < count($meta["Classification"][$j]["TaxonPath"][$k]["Taxon"]); $i++)
01143 {
01144 $xml .= '<Taxon Language="' . ilUtil::stripSlashes($meta["Classification"][$j]["TaxonPath"][$k]["Taxon"][$i]["Language"]) . '" Id="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Classification"][$j]["TaxonPath"][$k]["Taxon"][$i]["Id"])) . '">' . ilUtil::stripSlashes($meta["Classification"][$j]["TaxonPath"][$k]["Taxon"][$i]["Value"]) . '</Taxon>';
01145 }
01146 $xml .= '</TaxonPath>';
01147 }
01148 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Classification"][$j]["Description"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Classification"][$j]["Description"]["Value"]) . '</Description>';
01149 for ($i = 0; $i < count($meta["Classification"][$j]["Keyword"]); $i++)
01150 {
01151 $xml .= '<Keyword Language="' . ilUtil::stripSlashes($meta["Classification"][$j]["Keyword"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Classification"][$j]["Keyword"][$i]["Value"]) . '</Keyword>';
01152 }
01153 $xml .= '</Classification>';
01154 # echo htmlspecialchars($xml);
01155 # }
01156
01157 $update = true;
01158 }
01159
01160 if ($update)
01161 {
01162 $nodes[0]->unlink_node();
01163
01164 if ($xPath != "//Bibliography")
01165 {
01166 $xPath = "//MetaData";
01167 }
01168
01169 $this->addXMLNode($xPath, $xml);
01170
01171 }
01172 return true;
01173 }
01174 else
01175 {
01176 return false;
01177 }
01178 }
01179
01190 function getDomContent($xPath, $name = "", $index = 0)
01191 {
01192 if ($index == "")
01193 {
01194 $index = 0;
01195 }
01196 # echo "Index: " . $index . " | Path: " . $xPath . " | Name: " . $name . "<br>\n";
01197 $nodes = $this->getXpathNodes($this->dom, $xPath);
01198 if (count($nodes) > 0)
01199 {
01200 $children = $nodes[$index]->child_nodes();
01201 if (count($children) > 0)
01202 {
01203 $k = 0;
01204 for ($i = 0; $i < count($children); $i++)
01205 {
01206
01207 if ($name == "" ||
01208 $children[$i]->node_name() == $name)
01209 {
01210 $content[$k]["value"] = $children[$i]->get_content();
01211 $a = $children[$i]->attributes();
01212 for ($j = 0; $j < count($a); $j++)
01213 {
01214 $content[$k][$a[$j]->name()] = $a[$j]->value();
01215 }
01216 $k++;
01217 }
01218 }
01219 # vd($content);
01220 return($content);
01221 }
01222 }
01223 return false;
01224 }
01225
01234 function replaceDomContent($xPath, $name = "", $index = 0, $newNode)
01235 {
01236 # echo "Index: " . $index . " | Path: " . $xPath . " | Name: " . $name . "<br>\n";
01237 $nodes = $this->getXpathNodes($this->dom, $xPath);
01238 if (count($nodes) > 0)
01239 {
01240 $children = $nodes[$index]->child_nodes();
01241 if (count($children) > 0)
01242 {
01243 for ($i = 0; $i < count($children); $i++)
01244 {
01245 if ($children[$i]->node_name() == $name &&
01246 is_array($newNode))
01247 {
01248 foreach ($newNode as $key => $val)
01249 {
01250 if ($key == "value")
01251 {
01252 $this->replace_content($children[$i], $val);
01253 }
01254 else
01255 {
01256 $children[$i]->set_attribute($key, $val);
01257 }
01258 }
01259 }
01260 }
01261 }
01262 }
01263 }
01264
01275 function replace_content( &$node, &$new_content )
01276 {
01277 $newnode =& $this->dom->create_element( $node->tagname() );
01278 $newnode->set_content( $new_content );
01279 $atts =& $node->attributes();
01280 foreach ( $atts as $att )
01281 {
01282 $newnode->set_attribute( $att->name(), $att->value() );
01283 }
01284 $kids =& $node->child_nodes();
01285 foreach ( $kids as $kid )
01286 {
01287 if ( $kid->node_type() != XML_TEXT_NODE )
01288 {
01289 $newnode->append_child( $kid );
01290 }
01291 }
01292 $node->replace_node( $newnode );
01293 }
01294
01303 function updateDomContent($xPath, $name = "", $index = 0, $newNode)
01304 {
01305 # echo "Index: " . $index . " | Path: " . $xPath . " | Name: " . $name . "<br>\n";
01306 $nodes = $this->getXpathNodes($this->dom, $xPath);
01307 if (count($nodes) > 0)
01308 {
01309 $children = $nodes[$index]->child_nodes();
01310 if (count($children) > 0)
01311 {
01312 for ($i = 0; $i < count($children); $i++)
01313 {
01314 if ($children[$i]->node_name() == $name &&
01315 is_array($newNode))
01316 {
01317 foreach ($newNode as $key => $val)
01318 {
01319 if ($key == "value")
01320 {
01321 $children[$i]->set_content($val);
01322 }
01323 else
01324 {
01325 $children[$i]->set_attribute($key, $val);
01326 }
01327 }
01328 }
01329 }
01330 }
01331 }
01332 }
01333
01341 function getFirstDomNode($xPath)
01342 {
01343 $node = $this->getXpathNodes($this->dom,$xPath);
01344 return($node[0]);
01345 }
01346
01351 function updateFromDom()
01352 {
01353 $this->deleteAllDbData();
01354 $xml = $this->dom->dump_mem(0);
01355 $this->import($xml,$this->obj_id,$this->obj_type);
01356
01357 }
01358
01363 function deleteAllDbData()
01364 {
01365 global $ilBench;
01366
01367 #$ilBench->start('NestedSet','deleteAllDBData');
01368 $res = $this->db->query("SELECT * FROM xmlnestedset WHERE ns_book_fk='".$this->obj_id."' AND ns_type='".$this->obj_type."' ");
01369 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
01370 {
01371 $this->db->query("DELETE FROM xmlparam WHERE tag_fk='".$row["ns_tag_fk"]."' ");
01372 $this->db->query("DELETE FROM xmlvalue WHERE tag_fk='".$row["ns_tag_fk"]."' ");
01373 $this->db->query("DELETE FROM xmltags WHERE tag_pk='".$row["ns_tag_fk"]."' ");
01374 }
01375 $this->db->query("DELETE FROM xmlnestedset WHERE ns_book_fk='".$this->obj_id."' AND ns_type='".$this->obj_type."' ");
01376 #$ilBench->stop('NestedSet','deleteAllDBData');
01377
01378 }
01379
01387 function _deleteAllChildMetaData($a_ids)
01388 {
01389 global $ilBench,$ilDB;
01390
01391 #$ilBench->start('NestedSet','deleteAllChildMetaData');
01392
01393
01394 $in = " IN ('";
01395 $in .= implode("','", $a_ids);
01396 $in .= "')";
01397
01398 $query = "SELECT ns_tag_fk FROM xmlnestedset ".
01399 "WHERE ns_book_fk ".$in;
01400 $res = $ilDB->query($query);
01401 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
01402 {
01403 $tag_fks[$row->ns_tag_fk] = $row->ns_tag_fk;
01404 }
01405 $ilDB->query("DELETE FROM xmlnestedset WHERE ns_book_fk ".$in);
01406
01407
01408
01409 $in = " IN ('";
01410 $in .= implode("','", $tag_fks);
01411 $in .= "')";
01412
01413 $ilDB->query("DELETE FROM xmlparam WHERE tag_fk ".$in);
01414 $ilDB->query("DELETE FROM xmlvalue WHERE tag_fk ".$in);
01415 $ilDB->query("DELETE FROM xmltags WHERE tag_pk ".$in);
01416
01417 #$ilBench->stop('NestedSet','deleteAllChildMetaData');
01418 return true;
01419 }
01420
01427 function _getAllChildIds($a_obj_id)
01428 {
01429 global $ilDB;
01430
01431 $query = "SELECT obj_id FROM lm_data WHERE lm_id = '".$a_obj_id."'";
01432 $res = $ilDB->query($query);
01433 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
01434 {
01435 $ids[$row->obj_id] = $row->obj_id;
01436 }
01437 $ids[$a_obj_id] = $a_obj_id;
01438
01439 return $ids ? $ids : array();
01440 }
01441
01442
01443
01444
01445 }
01446
01447 ?>