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
00182 if(1 or trim($data)!="") {
00183
00184
00185 if ($this->lastTag == "TAGVALUE")
00186 {
00187 $Q = "UPDATE xmlvalue SET tag_value=concat(tag_value,'".addslashes($data)."') WHERE tag_value_pk='".$value_pk."' ";
00188 $this->db->query($Q);
00189 }
00190 else
00191 {
00192 $tag_pk = $this->startElement($this->xml_parser,"TAGVALUE",array());
00193 $this->endElement($this->xml_parser,"TAGVALUE");
00194
00195 $Q = "INSERT INTO xmlvalue (tag_fk,tag_value) VALUES ('".$tag_pk."','".addslashes($data)."') ";
00196 $this->db->query($Q);
00197
00198 $Q = "SELECT LAST_INSERT_ID()";
00199 $r = $this->db->query($Q);
00200 $row = $r->fetchRow();
00201 $value_pk = $row[0];
00202
00203 $this->lastTag = "TAGVALUE";
00204 }
00205
00206 }
00207
00208 }
00209
00217 function endElement($parser, $name)
00218 {
00219
00220 $this->DEPTH--;
00221 $this->LEFT += 1;
00222 $this->lastTag = "";
00223
00224 }
00225
00234 function import($xmldata, $obj_id, $obj_type)
00235 {
00236
00240 $this->db->query("DROP TABLE IF EXISTS NestedSetTemp");
00241
00245 $Q = "CREATE TEMPORARY TABLE NestedSetTemp (
00246 ns_book_fk int(11) NOT NULL,
00247 ns_type char(50) NOT NULL,
00248 ns_tag_fk int(11) NOT NULL,
00249 ns_l int(11) NOT NULL,
00250 ns_r int(11) NOT NULL,
00251 KEY ns_tag_fk (ns_tag_fk),
00252 KEY ns_l (ns_l),
00253 KEY ns_r (ns_r),
00254 KEY ns_book_fk (ns_book_fk)
00255 ) TYPE=MyISAM ";
00256 $this->db->query($Q);
00257
00258 $this->obj_id = $obj_id;
00259 $this->obj_type = $obj_type;
00260 $this->DEPTH = 0;
00261 $this->LEFT = 0;
00262 $this->RIGHT = 0;
00263
00264 $this->db->query("DELETE FROM NestedSetTemp");
00265
00270 $this->xml_parser = xml_parser_create("UTF-8");
00271 xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, false);
00272 xml_set_object($this->xml_parser,$this);
00273 xml_set_element_handler($this->xml_parser, "startElement", "endElement");
00274 xml_set_character_data_handler($this->xml_parser, "characterData");
00275
00276 if (!xml_parse($this->xml_parser, $xmldata)) {
00277 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)));
00278 }
00279 xml_parser_free($this->xml_parser);
00280
00284 $this->deleteAllDbData();
00285
00286 $this->db->query("INSERT INTO xmlnestedset SELECT * FROM NestedSetTemp");
00287 $this->db->query("DROP TABLE IF EXISTS NestedSetTemp");
00288
00289 }
00290
00296 function setParameterModifier(&$a_object, $a_method)
00297 {
00298 $this->param_modifier =& $a_object;
00299 $this->param_modifier_method = $a_method;
00300 }
00301
00313 function export($obj_id, $type)
00314 {
00315
00316 $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";
00317 $result = $this->db->query($query);
00318 if (DB::isError($result))
00319 {
00320 die($this->className."::checkTable(): ".$result->getMessage().":<br>".$q);
00321 }
00322
00323 $xml = "";
00324 $lastDepth = -1;
00325
00326 while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) )
00327 {
00328
00329
00330 $Anfang = "<".$row[tag_name];
00331 $query = "SELECT * FROM xmlparam WHERE tag_fk='$row[tag_pk]'";
00332 $result_param = $this->db->query($query);
00333 while (is_array($row_param = $result_param->fetchRow(DB_FETCHMODE_ASSOC) ) )
00334 {
00335 $param_value = $row_param[param_value];
00336 if (is_object($this->param_modifier))
00337 {
00338 $obj =& $this->param_modifier;
00339 $method = $this->param_modifier_method;
00340 $param_value = $obj->$method($row[tag_name], $row_param[param_name], $param_value);
00341 }
00342 $Anfang .= " ".$row_param[param_name]."=\"".$param_value."\"";
00343 }
00344
00345 $Anfang .= ">";
00346 $Ende = "</".$row[tag_name].">";
00347
00348
00349
00350 if ($row[tag_name]=="TAGVALUE")
00351 {
00352 $query = "SELECT * FROM xmlvalue WHERE tag_fk='$row[tag_pk]' ";
00353 $result_value = $this->db->query($query);
00354 $row_value = $result_value->fetchRow(DB_FETCHMODE_ASSOC);
00355 $Anfang = $row_value["tag_value"];
00356 $Ende = "";
00357
00358 $Anfang = htmlspecialchars($Anfang);
00359
00360 }
00361
00362
00363 $D = $row[tag_depth];
00364
00365 if ($D==$lastDepth)
00366 {
00367 $xml .= $xmlE[$D];
00368 $xml .= $Anfang;
00369 $xmlE[$D] = $Ende;
00370 }
00371 else if ($D>$lastDepth)
00372 {
00373 $xml .= $Anfang;
00374 $xmlE[$D] = $Ende;
00375 }
00376 else
00377 {
00378 for ($i=$lastDepth;$i>=$D;$i--)
00379 {
00380 $xml .= $xmlE[$i];
00381 }
00382 $xml .= $Anfang;
00383 $xmlE[$D] = $Ende;
00384 }
00385
00386 $lastDepth = $D;
00387
00388 }
00389
00390 for ($i=$lastDepth;$i>0;$i--)
00391 {
00392 $xml .= $xmlE[$i];
00393 }
00394
00395 return($xml);
00396
00397 }
00398
00406 function init($obj_id,$obj_type)
00407 {
00408
00409 $query = "SELECT * FROM xmlnestedset,xmltags WHERE ns_book_fk='".$obj_id."' AND ns_type='".
00410 $obj_type."' AND ns_tag_fk=tag_pk ORDER BY ns_l LIMIT 1";
00411 $result = $this->db->query($query);
00412 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00413
00414 $this->LEFT = $row["ns_l"];
00415 $this->RIGHT = $row["ns_r"];
00416 $this->DEPTH = $row["tag_depth"];
00417 $this->obj_id = $obj_id;
00418 $this->obj_type = $obj_type;
00419
00420 }
00421
00429 function getTagName()
00430 {
00431
00432 $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";
00433 $result = $this->db->query($query);
00434 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00435
00436 return($row["tag_name"]);
00437
00438 }
00439
00449 function setTagName($tagName)
00450 {
00451
00452 $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";
00453 $result = $this->db->query($query);
00454 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00455
00456 $query = "UPDATE xmltags SET tag_name='$tagName' WHERE tag_pk='".$row["ns_tag_fk"]."'";
00457 $this->db->query($query);
00458
00459 return($row["tagName"]);
00460
00461 }
00462
00463
00470 function getTagValue()
00471 {
00472
00473 $V = array();
00474
00475 $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";
00476 $result = $this->db->query($query);
00477 while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) )
00478 {
00479 if ($row[tag_name]=="TAGVALUE")
00480 {
00481 $query = "SELECT * FROM xmlvalue WHERE tag_fk='".$row[tag_pk]."' ";
00482 $result2 = $this->db->query($query);
00483 $row2 = $result2->fetchRow(DB_FETCHMODE_ASSOC);
00484 $V[] = $row2[tag_value];
00485 }
00486 else
00487 {
00488 $xml = new ilNestedSetXml();
00489
00490 $xml->LEFT = $row["ns_l"];
00491 $xml->RIGHT = $row["ns_r"];
00492 $xml->DEPTH = $row["tag_depth"];
00493 $xml->obj_id = $obj_id;
00494 $xml->obj_type = $obj_type;
00495
00496 $V[] = $xml;
00497
00498 }
00499 }
00500
00501 return($V);
00502 }
00503
00510 function setTagValue($value)
00511 {
00512 $V = array();
00513
00514 $query = "SELECT * FROM xmlnestedset,xmltags
00515 LEFT JOIN xmlvalue ON xmltags.tag_pk=xmlvalue.tag_fk
00516 WHERE ns_tag_fk=tag_pk AND
00517 ns_book_fk='".$this->obj_id."' AND
00518 ns_type='".$this->obj_type."' AND
00519 ns_l>='".$this->LEFT."' AND
00520 ns_r<='".$this->RIGHT."' AND
00521 tag_depth='".($this->DEPTH+1)."' AND
00522 tag_name = 'TAGVALUE'
00523 ORDER BY ns_l";
00524 $result = $this->db->query($query);
00525
00526 if (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) )
00527 {
00528
00529 $query = "UPDATE xmlvalue SET tag_value='".addslashes($value)."' WHERE tag_value_pk='".$row["tag_value_pk"]."' ";
00530 $this->db->query($query);
00531
00532 }
00533 else
00534 {
00535
00540 }
00541 }
00542
00552 function getXpathNodes(&$doc, $qry)
00553 {
00554 if (is_object($doc))
00555 {
00556 $xpath = $doc->xpath_init();
00557 $ctx = $doc->xpath_new_context();
00558
00559 $result = $ctx->xpath_eval($qry);
00560 if (is_array($result->nodeset))
00561 {
00562 return($result->nodeset);
00563 }
00564 }
00565 return Null;
00566 }
00567
00574 function initDom()
00575 {
00576 $xml = $this->export($this->obj_id, $this->obj_type);
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593 if ($xml=="")
00594 {
00595 return(false);
00596 }
00597 else
00598 {
00599 $this->dom = domxml_open_mem($xml);
00600 return(true);
00601 }
00602 }
00603
00614 function addXMLNode($xPath, $xml, $index = 0)
00615 {
00616 $newDOM = new XML2DOM($xml);
00617
00618 $nodes = $this->getXpathNodes($this->dom, $xPath);
00619
00620 if (count($nodes) > 0)
00621 {
00622 $newDOM->insertNode($this->dom, $nodes[$index]);
00623 return true;
00624 }
00625 else
00626 {
00627 return false;
00628 }
00629 }
00630
00639 function getFirstDomContent($xPath)
00640 {
00641
00642 $content = "";
00643 if (is_object($this->dom))
00644 {
00645 $node = $this->getXpathNodes($this->dom,$xPath);
00646 if (is_array($node))
00647 {
00648 if (is_object($node[0]))
00649 {
00650 $c = $node[0]->children();
00651
00652 if (is_object($c[0]))
00653 {
00654 $content = $c[0]->get_content();
00655 }
00656 }
00657 }
00658 }
00659
00660 return($content);
00661 }
00662
00673 function deleteDomNode($xPath, $name, $index = 0)
00674 {
00675 if ($index == "")
00676 {
00677 $index = 0;
00678 }
00679 if (strpos($index, ","))
00680 {
00681 $indices = explode(",", $index);
00682 $nodes = $this->getXpathNodes($this->dom, $xPath);
00683 if (count($nodes) > 0)
00684 {
00685 $children = $nodes[$indices[0]]->child_nodes();
00686 if (count($children) > 0)
00687 {
00688 $j = 0;
00689 for ($i = 0; $i < count($children); $i++)
00690 {
00691 if ($children[$i]->node_name() == $name)
00692 {
00693 if ($j == $indices[1])
00694 {
00695 $children[$i]->unlink_node();
00696 return true;
00697 }
00698 $j++;
00699 }
00700 }
00701 }
00702 }
00703 }
00704 else
00705 {
00706 $nodes = $this->getXpathNodes($this->dom, $xPath . "/" . $name);
00707 if (count($nodes) > 0)
00708 {
00709 $nodes[$index]->unlink_node();
00710 return true;
00711 }
00712 }
00713 return false;
00714 }
00715
00728 function addDomNode($xPath, $name, $value = "", $attributes = "", $index = 0)
00729 {
00730 $nodes = $this->getXpathNodes($this->dom, $xPath);
00731 if (count($nodes) > 0)
00732 {
00733 $node = $this->dom->create_element($name);
00734 if ($value != "")
00735 {
00736 $node->set_content(utf8_encode($value));
00737 }
00738 if (is_array($attributes))
00739 {
00740 for ($i = 0; $i < count($attributes); $i++)
00741 {
00742 $node->set_attribute($attributes[$i]["name"], utf8_encode($attributes[$i]["value"]));
00743 }
00744 }
00745 $nodes[$index]->append_child($node);
00746 return true;
00747 }
00748 else
00749 {
00750 return false;
00751 }
00752 }
00753
00754 function clean(&$meta)
00755 {
00756 if(is_array($meta))
00757 {
00758 foreach($meta as $key => $value)
00759 {
00760 if(is_array($meta[$key]))
00761 {
00762 $this->clean($meta[$key]);
00763 }
00764 else
00765 {
00766 $meta[$key] = preg_replace("/&(?!amp;|lt;|gt;|quot;)/","&",$meta[$key]);
00767 $meta[$key] = preg_replace("/\"/",""",$meta[$key]);
00768 $meta[$key] = preg_replace("/</","<",$meta[$key]);
00769 $meta[$key] = preg_replace("/>/",">",$meta[$key]);
00770 }
00771 }
00772 }
00773 return true;
00774 }
00783 function updateDomNode($xPath, $meta, $no = 0)
00784 {
00785 $this->clean($meta);
00786 $update = false;
00787 if ($xPath == "//Bibliography")
00788 {
00789 $nodes = $this->getXpathNodes($this->dom, $xPath . "/BibItem[" . ($no+1) . "]");
00790 }
00791 else
00792 {
00793 $nodes = $this->getXpathNodes($this->dom, $xPath);
00794 }
00795 if (count($nodes) > 0)
00796 {
00797
00798
00799 if ($nodes[0]->node_name() == "BibItem")
00800 {
00801 $xml = '<BibItem Type="' . ilUtil::stripSlashes($meta["Type"]) . '" Label="' . ilUtil::stripSlashes($meta["Label"]["Value"]) . '">';
00802 $xml .= '<Identifier Catalog="' . ilUtil::stripSlashes($meta["Identifier"]["Catalog"]) . '" Entry="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Identifier"]["Entry"])) . '"/>';
00803 for ($i = 0; $i < count($meta["Language"]); $i++)
00804 {
00805 $xml .= '<Language Language="' . ilUtil::stripSlashes($meta["Language"][$i]["Language"]) . '"/>';
00806 }
00807 for ($i = 0; $i < count($meta["Author"]); $i++)
00808 {
00809 $xml .= '<Author>';
00810 # for ($j = 0; $j < count($meta["Author"][$i]["FirstName"]); $j++)
00811 # {
00812 $xml .= '<FirstName>' . ilUtil::stripSlashes($meta["Author"][$i]["FirstName"]) . '</FirstName>';
00813 # }
00814 # for ($j = 0; $j < count($meta["Author"][$i]["MiddleName"]); $j++)
00815 # {
00816 $xml .= '<MiddleName>' . ilUtil::stripSlashes($meta["Author"][$i]["MiddleName"]) . '</MiddleName>';
00817 # }
00818 # for ($j = 0; $j < count($meta["Author"][$i]["LastName"]); $j++)
00819 # {
00820 $xml .= '<LastName>' . ilUtil::stripSlashes($meta["Author"][$i]["LastName"]) . '</LastName>';
00821 # }
00822 $xml .= '</Author>';
00823 }
00824 $xml .= '<Booktitle Language="' . ilUtil::stripSlashes($meta["Booktitle"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Booktitle"]["Value"]) . '</Booktitle>';
00825 for ($i = 0; $i < count($meta["CrossRef"]); $i++)
00826 {
00827 $xml .= '<CrossRef>' . ilUtil::stripSlashes($meta["CrossRef"][$i]["Value"]) . '</CrossRef>';
00828 }
00829 $xml .= '<Edition>' . ilUtil::stripSlashes($meta["Edition"]["Value"]) . '</Edition>';
00830 for ($i = 0; $i < count($meta["Editor"]); $i++)
00831 {
00832 $xml .= '<Editor>' . ilUtil::stripSlashes($meta["Editor"][$i]["Value"]) . '</Editor>';
00833 }
00834 $xml .= '<HowPublished Type="' . ilUtil::stripSlashes($meta["HowPublished"]["Type"]) . '"/>';
00835 for ($i = 0; $i < count($meta["WherePublished"]); $i++)
00836 {
00837 $xml .= '<WherePublished>' . ilUtil::stripSlashes($meta["WherePublished"][$i]["Value"]) . '</WherePublished>';
00838 }
00839 for ($i = 0; $i < count($meta["Institution"]); $i++)
00840 {
00841 $xml .= '<Institution>' . ilUtil::stripSlashes($meta["Institution"][$i]["Value"]) . '</Institution>';
00842 }
00843 if (is_array($meta["Journal"]))
00844 {
00845 $xml .= '<Journal Note="' . ilUtil::stripSlashes($meta["Journal"]["Note"]) . '" Number="' . ilUtil::stripSlashes($meta["Journal"]["Number"]) . '" Organization="' . ilUtil::stripSlashes($meta["Journal"]["Organization"]) . '"/>';
00846 }
00847 for ($i = 0; $i < count($meta["Keyword"]); $i++)
00848 {
00849 $xml .= '<Keyword Language="' . ilUtil::stripSlashes($meta["Keyword"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Keyword"][$i]["Value"]) . '</Keyword>';
00850 }
00851 if (is_array($meta["Month"]))
00852 {
00853 $xml .= '<Month>' . ilUtil::stripSlashes($meta["Month"]["Value"]) . '</Month>';
00854 }
00855 if (is_array($meta["Pages"]))
00856 {
00857 $xml .= '<Pages>' . ilUtil::stripSlashes($meta["Pages"]["Value"]) . '</Pages>';
00858 }
00859 $xml .= '<Publisher>' . ilUtil::stripSlashes($meta["Publisher"]["Value"]) . '</Publisher>';
00860 for ($i = 0; $i < count($meta["School"]); $i++)
00861 {
00862 $xml .= '<School>' . ilUtil::stripSlashes($meta["School"][$i]["Value"]) . '</School>';
00863 }
00864 if (is_array($meta["Series"]))
00865 {
00866 $xml .= '<Series>';
00867 $xml .= '<SeriesTitle>' . ilUtil::stripSlashes($meta["Series"]["SeriesTitle"]) . '</SeriesTitle>';
00868 # for ($i = 0; $i < count($meta["Series"]["SeriesEditor"]); $i++)
00869 if (isset($meta["Series"]["SeriesEditor"]))
00870 {
00871 # $xml .= '<SeriesEditor>' . ilUtil::stripSlashes($meta["Series"]["SeriesEditor"][$i]) . '</SeriesEditor>';
00872 $xml .= '<SeriesEditor>' . ilUtil::stripSlashes($meta["Series"]["SeriesEditor"]) . '</SeriesEditor>';
00873 }
00874 if (isset($meta["Series"]["SeriesVolume"]))
00875 {
00876 $xml .= '<SeriesVolume>' . ilUtil::stripSlashes($meta["Series"]["SeriesVolume"]) . '</SeriesVolume>';
00877 }
00878 $xml .= '</Series>';
00879 }
00880 $xml .= '<Year>' . ilUtil::stripSlashes($meta["Year"]["Value"]) . '</Year>';
00881 if ($meta["URL_ISBN_ISSN"]["Type"] == "URL")
00882 {
00883 $xml .= '<URL>' . ilUtil::stripSlashes($meta["URL_ISBN_ISSN"]["Value"]) . '</URL>';
00884 }
00885 else if ($meta["URL_ISBN_ISSN"]["Type"] == "ISBN")
00886 {
00887 $xml .= '<ISBN>' . ilUtil::stripSlashes($meta["URL_ISBN_ISSN"]["Value"]) . '</ISBN>';
00888 }
00889 else if ($meta["URL_ISBN_ISSN"]["Type"] == "ISSN")
00890 {
00891 $xml .= '<ISSN>' . ilUtil::stripSlashes($meta["URL_ISBN_ISSN"]["Value"]) . '</ISSN>';
00892 }
00893 $xml .= '</BibItem>';
00894 # echo htmlspecialchars($xml);
00895
00896 $update = true;
00897 }
00898
00899
00900 else if ($nodes[0]->node_name() == "General")
00901 {
00902
00903 $xml = '<General Structure="' . ilUtil::stripSlashes($meta["Structure"]) . '">';
00904 for ($i = 0; $i < count($meta["Identifier"]); $i++)
00905 {
00906 $xml .= '<Identifier Catalog="' . ilUtil::stripSlashes($meta["Identifier"][$i]["Catalog"]) . '" Entry="' .
00907 str_replace("\"", "", ilUtil::stripSlashes($meta["Identifier"][$i]["Entry"])) . '"/>';
00908 }
00909
00910 $xml .= '<Title Language="' .
00911 ilUtil::stripSlashes($meta["Title"]["Language"]) . '">' .
00912 ilUtil::stripSlashes($meta["Title"]["Value"]) . '</Title>';
00913 for ($i = 0; $i < count($meta["Language"]); $i++)
00914 {
00915 $xml .= '<Language Language="' . ilUtil::stripSlashes($meta["Language"][$i]["Language"]) . '"/>';
00916 }
00917 for ($i = 0; $i < count($meta["Description"]); $i++)
00918 {
00919 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Description"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Description"][$i]["Value"]) . '</Description>';
00920 }
00921 for ($i = 0; $i < count($meta["Keyword"]); $i++)
00922 {
00923 $xml .= '<Keyword Language="' . ilUtil::stripSlashes($meta["Keyword"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Keyword"][$i]["Value"]) . '</Keyword>';
00924 }
00925 if ($meta["Coverage"] != "")
00926 {
00927 $xml .= '<Coverage Language="' . ilUtil::stripSlashes($meta["Coverage"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Coverage"]["Value"]) . '</Coverage>';
00928 }
00929 $xml .= '</General>';
00930
00931
00932 $update = true;
00933 }
00934
00935
00936 else if ($nodes[0]->node_name() == "Lifecycle")
00937 {
00938 $xml = '<Lifecycle Status="' . $meta["Status"] . '">';
00939 $xml .= '<Version Language="' . ilUtil::stripSlashes($meta["Version"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Version"]["Value"]) . '</Version>';
00940 for ($i = 0; $i < count($meta["Contribute"]); $i++)
00941 {
00942 $xml .= '<Contribute Role="' . ilUtil::stripSlashes($meta["Contribute"][$i]["Role"]) . '">';
00943 $xml .= '<Date>' . ilUtil::stripSlashes($meta["Contribute"][$i]["Date"]) . '</Date>';
00944 for ($j = 0; $j < count($meta["Contribute"][$i]["Entity"]); $j++)
00945 {
00946 $xml .= '<Entity>' . ilUtil::stripSlashes($meta["Contribute"][$i]["Entity"][$j]) . '</Entity>';
00947 }
00948 $xml .= '</Contribute>';
00949 }
00950 $xml .= '</Lifecycle>';
00951 # echo htmlspecialchars($xml);
00952
00953 $update = true;
00954 }
00955
00956
00957 else if ($nodes[0]->node_name() == "Meta-Metadata")
00958 {
00959
00960 $xml = '<Meta-Metadata MetadataScheme="LOM v 1.0" Language="' . ilUtil::stripSlashes($meta["Language"]) . '">';
00961 for ($i = 0; $i < count($meta["Identifier"]); $i++)
00962 {
00963 $xml .= '<Identifier Catalog="' . ilUtil::stripSlashes($meta["Identifier"][$i]["Catalog"]) . '" Entry="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Identifier"][$i]["Entry"])) . '"/>';
00964 }
00965 for ($i = 0; $i < count($meta["Contribute"]); $i++)
00966 {
00967 $xml .= '<Contribute Role="' . ilUtil::stripSlashes($meta["Contribute"][$i]["Role"]) . '">';
00968 $xml .= '<Date>' . ilUtil::stripSlashes($meta["Contribute"][$i]["Date"]) . '</Date>';
00969 for ($j = 0; $j < count($meta["Contribute"][$i]["Entity"]); $j++)
00970 {
00971 $xml .= '<Entity>' . ilUtil::stripSlashes($meta["Contribute"][$i]["Entity"][$j]) . '</Entity>';
00972 }
00973 $xml .= '</Contribute>';
00974 }
00975 $xml .= '</Meta-Metadata>';
00976 # echo htmlspecialchars($xml);
00977
00978 $update = true;
00979 }
00980
00981
00982 else if ($nodes[0]->node_name() == "Technical")
00983 {
00984
00985 $xml = '<Technical>';
00986 for ($i = 0; $i < count($meta["Format"]); $i++)
00987 {
00988 $xml .= '<Format>' . ilUtil::stripSlashes($meta["Format"][$i]) . '</Format>';
00989 }
00990 if ($meta["Size"] != "")
00991 {
00992 $xml .= '<Size>' . ilUtil::stripSlashes($meta["Size"]) . '</Size>';
00993 }
00994 for ($i = 0; $i < count($meta["Location"]); $i++)
00995 {
00996 $xml .= '<Location Type="' . ilUtil::stripSlashes($meta["Location"][$i]["Type"]) . '">' . ilUtil::stripSlashes($meta["Location"][$i]["Value"]) . '</Location>';
00997 }
00998 if (is_array($meta["Requirement"]))
00999 {
01000 for ($i = 0; $i < count($meta["Requirement"]); $i++)
01001 {
01002 $xml .= '<Requirement>';
01003 $xml .= '<Type>';
01004 if (is_array($meta["Requirement"][$i]["Type"]["OperatingSystem"]))
01005 {
01006 $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"])) . '"/>';
01007 }
01008 if (is_array($meta["Requirement"][$i]["Type"]["Browser"]))
01009 {
01010 $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"])) . '"/>';
01011 }
01012 $xml .= '</Type>';
01013 $xml .= '</Requirement>';
01014 }
01015 }
01016 else if (is_array($meta["OrComposite"]))
01017 {
01018 for ($j = 0; $j < count($meta["OrComposite"]); $j++)
01019 {
01020 $xml .= '<OrComposite>';
01021 for ($i = 0; $i < count($meta["OrComposite"][$j]["Requirement"]); $i++)
01022 {
01023 $xml .= '<Requirement>';
01024 $xml .= '<Type>';
01025 if (is_array($meta["OrComposite"][$j]["Requirement"][$i]["Type"]["OperatingSystem"]))
01026 {
01027 $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"])) . '"/>';
01028 }
01029 if (is_array($meta["OrComposite"][$j]["Requirement"][$i]["Type"]["Browser"]))
01030 {
01031 $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"])) . '"/>';
01032 }
01033 $xml .= '</Type>';
01034 $xml .= '</Requirement>';
01035 }
01036 $xml .= '</OrComposite>';
01037 }
01038 }
01039 if (is_array($meta["InstallationRemarks"]))
01040 {
01041 $xml .= '<InstallationRemarks Language="' . ilUtil::stripSlashes($meta["InstallationRemarks"]["Language"]) . '">' . ilUtil::stripSlashes($meta["InstallationRemarks"]["Value"]) . '</InstallationRemarks>';
01042 }
01043 if (is_array($meta["OtherPlattformRequirements"]))
01044 {
01045 $xml .= '<OtherPlattformRequirements Language="' . ilUtil::stripSlashes($meta["OtherPlattformRequirements"]["Language"]) . '">' . ilUtil::stripSlashes($meta["OtherPlattformRequirements"]["Value"]) . '</OtherPlattformRequirements>';
01046 }
01047 if ($meta["Duration"] != "")
01048 {
01049 $xml .= '<Duration>' . ilUtil::stripSlashes($meta["Duration"]) . '</Duration>';
01050 }
01051 $xml .= '</Technical>';
01052 # echo htmlspecialchars($xml);
01053
01054 $update = true;
01055 }
01056
01057
01058 else if ($nodes[0]->node_name() == "Educational")
01059 {
01060
01061 $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"]) . '">';
01062 $xml .= '<TypicalLearningTime>' . ilUtil::stripSlashes($meta["TypicalLearningTime"]) . '</TypicalLearningTime>';
01063 for ($i = 0; $i < count($meta["TypicalAgeRange"]); $i++)
01064 {
01065 $xml .= '<TypicalAgeRange Language="' . ilUtil::stripSlashes($meta["TypicalAgeRange"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["TypicalAgeRange"][$i]["Value"]) . '</TypicalAgeRange>';
01066 }
01067 for ($i = 0; $i < count($meta["Description"]); $i++)
01068 {
01069 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Description"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Description"][$i]["Value"]) . '</Description>';
01070 }
01071 for ($i = 0; $i < count($meta["Language"]); $i++)
01072 {
01073 $xml .= '<Language Language="' . ilUtil::stripSlashes($meta["Language"][$i]["Language"]) . '"/>';
01074 }
01075 $xml .= '</Educational>';
01076
01077 $update = true;
01078 }
01079
01080
01081 else if ($nodes[0]->node_name() == "Rights")
01082 {
01083
01084 $xml = '<Rights Cost="' . ilUtil::stripSlashes($meta["Cost"]) . '" CopyrightAndOtherRestrictions="' . ilUtil::stripSlashes($meta["CopyrightAndOtherRestrictions"]) . '">';
01085 for ($i = 0; $i < count($meta["Description"]); $i++)
01086 {
01087 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Description"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Description"][$i]["Value"]) . '</Description>';
01088 }
01089 $xml .= '</Rights>';
01090
01091 $update = true;
01092 }
01093
01094
01095 else if ($nodes[0]->node_name() == "Relation")
01096 {
01097
01098 # for ($j = 0; $j < count($meta["Relation"]); $j++)
01099 # {
01100 $meta["Relation"][0] = $meta;
01101 $j = 0;
01102 $xml = '<Relation Kind="' . ilUtil::stripSlashes($meta["Relation"][$j]["Kind"]) . '">';
01103 $xml .= '<Resource>';
01104 for ($i = 0; $i < count($meta["Relation"][$j]["Resource"]["Identifier"]); $i++)
01105 {
01106 $xml .= '<Identifier_ Catalog="' . ilUtil::stripSlashes($meta["Relation"][$j]["Resource"]["Identifier"][$i]["Catalog"]) . '" Entry="' . str_replace("\"", "", ilUtil::stripSlashes($meta["Relation"][$j]["Resource"]["Identifier"][$i]["Entry"])) . '"/>';
01107 }
01108 for ($i = 0; $i < count($meta["Relation"][$j]["Resource"]["Description"]); $i++)
01109 {
01110 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Relation"][$j]["Resource"]["Description"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Relation"][$j]["Resource"]["Description"][$i]["Value"]) . '</Description>';
01111 }
01112 $xml .= '</Resource>';
01113 $xml .= '</Relation>';
01114 # echo htmlspecialchars($xml);
01115 # }
01116
01117 $update = true;
01118 }
01119
01120
01121 else if ($nodes[0]->node_name() == "Annotation")
01122 {
01123
01124 # for ($i = 0; $i < count($meta["Annotation"]); $i++)
01125 # {
01126 $meta["Annotation"][0] = $meta;
01127 $i = 0;
01128 $xml = '<Annotation>';
01129 $xml .= '<Entity>' . ilUtil::stripSlashes($meta["Annotation"][$i]["Entity"]) . '</Entity>';
01130 $xml .= '<Date>' . ilUtil::stripSlashes($meta["Annotation"][$i]["Date"]) . '</Date>';
01131 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Annotation"][$i]["Description"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Annotation"][$i]["Description"]["Value"]) . '</Description>';
01132 $xml .= '</Annotation>';
01133 # echo htmlspecialchars($xml);
01134 # }
01135
01136 $update = true;
01137 }
01138
01139
01140 else if ($nodes[0]->node_name() == "Classification")
01141 {
01142
01143 # for ($j = 0; $j < count($meta["Classification"]); $j++)
01144 # {
01145 $meta["Classification"][0] = $meta;
01146 $j = 0;
01147 $xml = '<Classification Purpose="' . ilUtil::stripSlashes($meta["Classification"][$j]["Purpose"]) . '">';
01148 for ($k = 0; $k < count($meta["Classification"][$j]["TaxonPath"]); $k++)
01149 {
01150 $xml .= '<TaxonPath>';
01151 $xml .= '<Source Language="' . ilUtil::stripSlashes($meta["Classification"][$j]["TaxonPath"][$k]["Source"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Classification"][$j]["TaxonPath"][$k]["Source"]["Value"]) . '</Source>';
01152 for ($i = 0; $i < count($meta["Classification"][$j]["TaxonPath"][$k]["Taxon"]); $i++)
01153 {
01154 $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>';
01155 }
01156 $xml .= '</TaxonPath>';
01157 }
01158 $xml .= '<Description Language="' . ilUtil::stripSlashes($meta["Classification"][$j]["Description"]["Language"]) . '">' . ilUtil::stripSlashes($meta["Classification"][$j]["Description"]["Value"]) . '</Description>';
01159 for ($i = 0; $i < count($meta["Classification"][$j]["Keyword"]); $i++)
01160 {
01161 $xml .= '<Keyword Language="' . ilUtil::stripSlashes($meta["Classification"][$j]["Keyword"][$i]["Language"]) . '">' . ilUtil::stripSlashes($meta["Classification"][$j]["Keyword"][$i]["Value"]) . '</Keyword>';
01162 }
01163 $xml .= '</Classification>';
01164 # echo htmlspecialchars($xml);
01165 # }
01166
01167 $update = true;
01168 }
01169
01170 if ($update)
01171 {
01172 $nodes[0]->unlink_node();
01173
01174 if ($xPath != "//Bibliography")
01175 {
01176 $xPath = "//MetaData";
01177 }
01178
01179 $this->addXMLNode($xPath, $xml);
01180
01181 }
01182 return true;
01183 }
01184 else
01185 {
01186 return false;
01187 }
01188 }
01189
01200 function getDomContent($xPath, $name = "", $index = 0)
01201 {
01202 if ($index == "")
01203 {
01204 $index = 0;
01205 }
01206 # echo "Index: " . $index . " | Path: " . $xPath . " | Name: " . $name . "<br>\n";
01207 $nodes = $this->getXpathNodes($this->dom, $xPath);
01208 if (count($nodes) > 0)
01209 {
01210 $children = $nodes[$index]->child_nodes();
01211 if (count($children) > 0)
01212 {
01213 $k = 0;
01214 for ($i = 0; $i < count($children); $i++)
01215 {
01216
01217 if ($name == "" ||
01218 $children[$i]->node_name() == $name)
01219 {
01220 $content[$k]["value"] = $children[$i]->get_content();
01221 $a = $children[$i]->attributes();
01222 for ($j = 0; $j < count($a); $j++)
01223 {
01224 $content[$k][$a[$j]->name()] = $a[$j]->value();
01225 }
01226 $k++;
01227 }
01228 }
01229 # vd($content);
01230 return($content);
01231 }
01232 }
01233 return false;
01234 }
01235
01244 function replaceDomContent($xPath, $name = "", $index = 0, $newNode)
01245 {
01246 # echo "Index: " . $index . " | Path: " . $xPath . " | Name: " . $name . "<br>\n";
01247 $nodes = $this->getXpathNodes($this->dom, $xPath);
01248 if (count($nodes) > 0)
01249 {
01250 $children = $nodes[$index]->child_nodes();
01251 if (count($children) > 0)
01252 {
01253 for ($i = 0; $i < count($children); $i++)
01254 {
01255 if ($children[$i]->node_name() == $name &&
01256 is_array($newNode))
01257 {
01258 foreach ($newNode as $key => $val)
01259 {
01260 if ($key == "value")
01261 {
01262 $this->replace_content($children[$i], $val);
01263 }
01264 else
01265 {
01266 $children[$i]->set_attribute($key, $val);
01267 }
01268 }
01269 }
01270 }
01271 }
01272 }
01273 }
01274
01285 function replace_content( &$node, &$new_content )
01286 {
01287 $newnode =& $this->dom->create_element( $node->tagname() );
01288 $newnode->set_content( $new_content );
01289 $atts =& $node->attributes();
01290 foreach ( $atts as $att )
01291 {
01292 $newnode->set_attribute( $att->name(), $att->value() );
01293 }
01294 $kids =& $node->child_nodes();
01295 foreach ( $kids as $kid )
01296 {
01297 if ( $kid->node_type() != XML_TEXT_NODE )
01298 {
01299 $newnode->append_child( $kid );
01300 }
01301 }
01302 $node->replace_node( $newnode );
01303 }
01304
01313 function updateDomContent($xPath, $name = "", $index = 0, $newNode)
01314 {
01315
01316 $nodes = $this->getXpathNodes($this->dom, $xPath);
01317 if (count($nodes) > 0)
01318 {
01319 $children = $nodes[$index]->child_nodes();
01320 if (count($children) > 0)
01321 {
01322 for ($i = 0; $i < count($children); $i++)
01323 {
01324 if ($children[$i]->node_name() == $name &&
01325 is_array($newNode))
01326 {
01327 foreach ($newNode as $key => $val)
01328 {
01329 if ($key == "value")
01330 {
01331 $children[$i]->set_content($val);
01332 }
01333 else
01334 {
01335 $children[$i]->set_attribute($key, $val);
01336 }
01337 }
01338 }
01339 }
01340 }
01341 }
01342 }
01343
01351 function getFirstDomNode($xPath)
01352 {
01353 $node = $this->getXpathNodes($this->dom,$xPath);
01354 return($node[0]);
01355 }
01356
01361 function updateFromDom()
01362 {
01363 $this->deleteAllDbData();
01364 $xml = $this->dom->dump_mem(0);
01365 $this->import($xml,$this->obj_id,$this->obj_type);
01366
01367 }
01368
01373 function deleteAllDbData()
01374 {
01375 global $ilBench;
01376
01377 #$ilBench->start('NestedSet','deleteAllDBData');
01378 $res = $this->db->query("SELECT * FROM xmlnestedset WHERE ns_book_fk='".$this->obj_id."' AND ns_type='".$this->obj_type."' ");
01379 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
01380 {
01381 $this->db->query("DELETE FROM xmlparam WHERE tag_fk='".$row["ns_tag_fk"]."' ");
01382 $this->db->query("DELETE FROM xmlvalue WHERE tag_fk='".$row["ns_tag_fk"]."' ");
01383 $this->db->query("DELETE FROM xmltags WHERE tag_pk='".$row["ns_tag_fk"]."' ");
01384 }
01385 $this->db->query("DELETE FROM xmlnestedset WHERE ns_book_fk='".$this->obj_id."' AND ns_type='".$this->obj_type."' ");
01386 #$ilBench->stop('NestedSet','deleteAllDBData');
01387
01388 }
01389
01397 function _deleteAllChildMetaData($a_ids)
01398 {
01399 global $ilBench,$ilDB;
01400
01401 #$ilBench->start('NestedSet','deleteAllChildMetaData');
01402
01403
01404 $in = " IN ('";
01405 $in .= implode("','", $a_ids);
01406 $in .= "')";
01407
01408 $query = "SELECT ns_tag_fk FROM xmlnestedset ".
01409 "WHERE ns_book_fk ".$in;
01410 $res = $ilDB->query($query);
01411 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
01412 {
01413 $tag_fks[$row->ns_tag_fk] = $row->ns_tag_fk;
01414 }
01415 $ilDB->query("DELETE FROM xmlnestedset WHERE ns_book_fk ".$in);
01416
01417
01418
01419 $in = " IN ('";
01420 $in .= implode("','", $tag_fks);
01421 $in .= "')";
01422
01423 $ilDB->query("DELETE FROM xmlparam WHERE tag_fk ".$in);
01424 $ilDB->query("DELETE FROM xmlvalue WHERE tag_fk ".$in);
01425 $ilDB->query("DELETE FROM xmltags WHERE tag_pk ".$in);
01426
01427 #$ilBench->stop('NestedSet','deleteAllChildMetaData');
01428 return true;
01429 }
01430
01437 function _getAllChildIds($a_obj_id)
01438 {
01439 global $ilDB;
01440
01441 $query = "SELECT obj_id FROM lm_data WHERE lm_id = '".$a_obj_id."'";
01442 $res = $ilDB->query($query);
01443 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
01444 {
01445 $ids[$row->obj_id] = $row->obj_id;
01446 }
01447 $ids[$a_obj_id] = $a_obj_id;
01448
01449 return $ids ? $ids : array();
01450 }
01451
01452
01453
01454
01455 }
01456
01457 ?>