md_funcs.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /**
  3. * Chamilo metadata/md_funcs.php -->
  4. * 2006/12/15
  5. * Copyright (C) 2006 rene.haentjens@UGent.be - see note at end of text -->
  6. * @package chamilo.metadata
  7. */
  8. /**
  9. * Chamilo Metadata: common functions and mdstore class
  10. *
  11. * This script requires xmd.lib.php and xht.lib.php (Chamilo inc/lib).
  12. *
  13. * Note on the funny characters used in mds_update_xml_and_mdt:
  14. *
  15. * ! and ~ and , are handled by xmd_update, see xmd.lib.php
  16. * ~~ !! ; and = are handled here; note that = excludes newlines in value
  17. *
  18. * path!elem create new element (not for attributes!)
  19. * path=value assign new value to existing element or value to attribute
  20. * path~ delete element (you cannot delete an attribute)
  21. * ~~ delete the whole xmldoc and the DB entry
  22. * !! this (xml) document contains the course keywords
  23. *
  24. * path1,path2,...;subpath=value for all elements in path1, path2, ...
  25. * assign value to subpath (see also xmd_update_many)
  26. *
  27. */
  28. // FETCH GET/POST-DATA; GENERAL FUNCTIONS
  29. define('MDS_TABLE', Database::get_course_table(TABLE_METADATA));
  30. if (isset($getpostvars) && is_array($getpostvars))
  31. foreach ($getpostvars as $gpvar)
  32. if (is_string($gpvar) && (isset($_POST[$gpvar]) || isset($_GET[$gpvar]))) {
  33. $val = isset($_POST[$gpvar]) ? $_POST[$gpvar] : $_GET[$gpvar];
  34. $GLOBALS[$gpvar] = get_magic_quotes_gpc() ? stripslashes($val) : $val;
  35. }
  36. function fgc($filename) {
  37. $fp = fopen($filename, 'rb'); $buffer = fread($fp, filesize($filename));
  38. fclose($fp); return $buffer; // file_get_contents: PHP >= 4.3.0
  39. }
  40. function give_up($msg) {
  41. global $charset;
  42. echo '<p align="center">MetaData:<br /><b>? ',
  43. htmlspecialchars($msg, ENT_QUOTES, $charset), '</b></p>'; exit;
  44. }
  45. function getpar($name, $description, $default = '')
  46. {
  47. $value = isset($_GET[$value = api_strtolower($name)]) ? $_GET[$value] : '';
  48. $value = get_magic_quotes_gpc() ? stripslashes($value) : $value;
  49. if (!$value) $value = $default;
  50. if ($value == '') give_up('URL parameter ' . api_strtoupper($name) . ' - ' .
  51. $description . ' - is required');
  52. define(api_strtoupper($name), $value);
  53. }
  54. function get_course_path() {
  55. return api_get_path(SYS_COURSE_PATH);
  56. }
  57. function get_course_web() {
  58. return api_get_path(WEB_COURSE_PATH);
  59. }
  60. function define_htt($htt_file, $urlp, $course_path) {
  61. global $charset;
  62. ($htt_file_contents = @fgc($htt_file))
  63. or give_up('Templates file "' . $htt_file . '" is missing...');
  64. $xhtDoc = new xhtdoc($htt_file_contents);
  65. if ($xhtDoc->htt_error)
  66. give_up('Templates file "' . $htt_file . '": ' . $xhtDoc->htt_error);
  67. $xhtDoc->xht_param['self'] = api_get_self() . $urlp;
  68. $xhtDoc->xht_param['dateTime'] = date('Y-m-d');
  69. $ckw = $course_path . '/CourseKwds.js';
  70. define('KEYWORDS_CACHE', get_course_path() . $ckw);
  71. if (file_exists(KEYWORDS_CACHE)) $kcdt =
  72. htmlspecialchars(date('Y/m/d H:i:s', filemtime(KEYWORDS_CACHE)), ENT_QUOTES, $charset);
  73. $xhtDoc->xht_param['keywordscache'] = $kcdt ?
  74. '<script type="text/javascript" src="' . get_course_web() . $ckw . '"></script>' .
  75. '<br /><small><i>(CourseKwds cache: ' . $kcdt . ')</i></small>' : '';
  76. return $xhtDoc;
  77. }
  78. function make_uri() {
  79. $regs = array(); // for use with ereg()
  80. $uri = strtr(ereg_replace(
  81. "[^0-9A-Za-z\xC0-\xD6\xD8-\xF6\xF8-\xFF\*\(\('!_.-]", "_",
  82. api_get_setting('siteName')), "\\", "_"); // allow letdigs, and _-.()'!*
  83. if (($p = strpos($uri, '.')) !== FALSE)
  84. $uri = substr($uri, 0, $p);
  85. if (ereg('^([^/]+//)?([^/\?]+)[/\?]',api_get_path(WEB_PATH).'/',$regs))
  86. if (ereg('([^\.]+)(\.ca)?(\.[^\.]+)?', strrev($regs[2]), $regs))
  87. $uri = str_replace('.', '-',
  88. strrev($regs[1].$regs[2].$regs[3])) . ':' . $uri;
  89. $uri = 'urn:' . strtolower($uri);
  90. while (substr($uri, -1)=='.') $uri = substr($uri,0,-1);
  91. return $uri;
  92. }
  93. // IEEE LOM: DEFAULT XML AND DUBLIN CORE MAPPING ------------------------------>
  94. $ieee_xml = <<<EOD
  95. <!-- {-XML-} -->
  96. <item>
  97. <metadata>
  98. <lom xmlns="http://ltsc.ieee.org/xsd/LOM">
  99. <general>
  100. <identifier><catalog>{-H {-P siteUri-}-}.</catalog><entry>{-H {-P entry-}-}</entry></identifier>
  101. <title><string language="{-H {-P mdlang-}-}">{-H {-P title-}-}</string></title>
  102. <language>{-H {-P lang-}-}</language>
  103. <description><string language="{-H {-P mdlang-}-}">{-H {-P description-}-}</string></description>
  104. <coverage><string language="{-H {-P mdlang-}-}">{-H {-P coverage-}-}</string></coverage>
  105. </general>
  106. <lifeCycle>
  107. <version><string language="xx">0.5</string></version>
  108. <status><source>LOMv1.0</source><value>draft</value></status>
  109. <contribute>
  110. <role><source>LOMv1.0</source><value>author</value></role>
  111. <entity>{-H {-P author-}-}</entity>
  112. <date><dateTime>{-H {-P dateTime-}-}</dateTime></date>
  113. </contribute>
  114. </lifeCycle>
  115. <metaMetadata>
  116. <metadataSchema>ADLv1.3</metadataSchema>
  117. </metaMetadata>
  118. <technical>
  119. <format>{-H {-P format-}-}</format>
  120. <size>{-H {-P size-}-}</size>
  121. <location>{-H {-P location-}-}</location>
  122. </technical>
  123. <educational>
  124. <learningResourceType><source>LOMv1.0</source><value>narrative text</value></learningResourceType>
  125. </educational>
  126. <rights>
  127. <cost><source>LOMv1.0</source><value>yes</value></cost>
  128. <copyrightAndOtherRestrictions><source>LOMv1.0</source><value>yes</value></copyrightAndOtherRestrictions>
  129. <description><string language="{-H {-P mdlang-}-}">{-L MdCopyright-}</string></description>
  130. </rights>
  131. <classification>
  132. <purpose><source>LOMv1.0</source><value>educational objective</value></purpose>
  133. </classification>
  134. </lom>
  135. </metadata>
  136. </item>
  137. <!-- {--} -->
  138. EOD;
  139. $ieee_dcmap_e = array(
  140. 'Identifier'=> 'metadata/lom/general/identifier[1]',
  141. 'Title'=> 'metadata/lom/general/title[1]',
  142. 'Language'=> 'metadata/lom/general/language[1]',
  143. 'Description'=> 'metadata/lom/general/description[1]',
  144. 'Coverage'=> 'metadata/lom/general/coverage[1]',
  145. 'Type'=> 'metadata/lom/educational/learningResourceType[1]',
  146. 'Date'=> 'metadata/lom/lifeCycle/contribute[1]/date',
  147. 'Creator'=> 'metadata/lom/lifeCycle/contribute[1]/entity',
  148. 'Format'=> 'metadata/lom/technical/format[1]',
  149. 'Rights'=> 'metadata/lom/rights/description[1]');
  150. // maps Dublin Core elements to xmd paths for elements (not yet complete)
  151. $ieee_dcmap_v = array(
  152. 'Identifier'=> 'metadata/lom/general/identifier[1]/entry',
  153. 'Title'=> 'metadata/lom/general/title[1]/string',
  154. 'Language'=> 'metadata/lom/general/language[1]',
  155. 'Description'=> 'metadata/lom/general/description[1]/string',
  156. 'Coverage'=> 'metadata/lom/general/coverage[1]/string',
  157. 'Type'=> 'metadata/lom/educational/learningResourceType[1]/value',
  158. 'Date'=> 'metadata/lom/lifeCycle/contribute[1]/date/dateTime',
  159. 'Creator'=> 'metadata/lom/lifeCycle/contribute[1]/entity',
  160. 'Format'=> 'metadata/lom/technical/format[1]',
  161. 'Rights'=> 'metadata/lom/rights/description[1]/string');
  162. // maps Dublin Core elements to xmd paths for values (not yet complete)
  163. // KEYWORD TREE
  164. function define_kwds($mdo) {
  165. if (!($newtext = trim(@fgc(get_course_path() . $mdo->mdo_course['path'] .
  166. '/document' . $mdo->mdo_path ))))
  167. {
  168. unlink(KEYWORDS_CACHE); return;
  169. }
  170. // templates to define the tree as JScript object
  171. $xhtDocKw = new xhtdoc(<<<EOD
  172. <!-- {-KWTREE_OBJECT-} -->
  173. KWTREE_OBJECT = {n:"", ti:"{-X @title-}"
  174. , c:[{-R * C DOWN_THE_KWTREE-}]};
  175. document.write(traverseKwObj(KWTREE_OBJECT, '', 0)); KWDS_ARRAY.sort();
  176. <!-- {-DOWN_THE_KWTREE-} -->
  177. {-T number > 1 , -}{n:"{-V @.-}"{-D cm {-X @comment-}-}{-T cm != empty , cm:"{-P cm-}"-}{-D pt {-X @postit-}-}{-T pt != empty , pt:"{-P pt-}"-}{-R * P empty-}{-T number >= 1
  178. , c:[-}{-T number >= 1 R * C DOWN_THE_KWTREE-}{-R * P empty-}{-T number >= 1 ]-}}
  179. <!-- {--} -->
  180. EOD
  181. ); // traverseKwObj (md_script) generates clickable tree and populates KWDS_ARRAY
  182. if ($xhtDocKw->htt_error)
  183. give_up('KwdTree template (metadata/md_funcs): ' . $xhtDocKw->htt_error);
  184. $xhtDocKw->xht_xmldoc = new xmddoc(explode("\n", $newtext));
  185. if ($xhtDocKw->xht_xmldoc->error)
  186. give_up('CourseKwds (metadata/md_funcs): XML error: ' .
  187. $xhtDocKw->xht_xmldoc->error);
  188. if (count($xhtDocKw->xht_xmldoc->children[0]) < 2)
  189. {
  190. unlink(KEYWORDS_CACHE); return;
  191. }
  192. $fileHandler = @fopen(KEYWORDS_CACHE, 'w');
  193. @fwrite($fileHandler, $xhtDocKw->xht_fill_template('KWTREE_OBJECT'));
  194. @fclose($fileHandler);
  195. }
  196. // METADATA STORE
  197. /**
  198. * mdstore class
  199. * @package chamilo.metadata
  200. */
  201. class mdstore {
  202. var $mds_something;
  203. function __construct($allow_create) {
  204. global $_course;
  205. $this->course_id = api_get_course_int_id();
  206. if (!isset($_course)) return;
  207. }
  208. function mds_get($eid, $column = 'mdxmltext', $must_exist = '') {
  209. // none: FALSE
  210. if (($mdt = Database::fetch_array(Database::query("SELECT " . $column ." FROM ".MDS_TABLE." WHERE eid = '$eid' AND c_id = {$this->course_id} "))))
  211. return $mdt[$column];
  212. if ($must_exist) give_up($must_exist . $this->_coldat('eid', $eid));
  213. return FALSE;
  214. }
  215. function mds_get_dc_elements($mdo) // no record: FALSE
  216. {
  217. if (!($mdt = $this->mds_get($mdo->mdo_eid))) return FALSE;
  218. $xmlDoc = new xmddoc(explode("\n", $mdt)); if ($xmlDoc->error) return FALSE;
  219. $result = array();
  220. foreach ($mdo->mdo_dcmap_v as $dce => $xp) {
  221. $result[$dce] = $xmlDoc->xmd_value($xp);
  222. }
  223. return $result;
  224. }
  225. function mds_get_many($columns, $where_clause) {
  226. $cols = '';
  227. foreach (explode(',', $columns) as $col) $cols .= "," . trim($col);
  228. if (!$cols) return;
  229. return $this->_query("SELECT " . api_substr($cols, 1) ." FROM " . MDS_TABLE . " WHERE c_id = {$this->course_id} AND ". $where_clause);
  230. }
  231. function mds_put($eid, $data, $column = 'mdxmltext', $exists = TRUE) {
  232. if ($exists === TRUE)
  233. return $this->_query("UPDATE " . MDS_TABLE . " SET " .$this->_coldat($column, $data) . " WHERE c_id = {$this->course_id} AND ", $eid);
  234. elseif ($exists === FALSE)
  235. return $this->_query("INSERT INTO " . MDS_TABLE . " SET c_id = {$this->course_id} , ".$this->_coldat($column, $data).", ", $eid);
  236. else // user doesn't know, check first whether the record exists
  237. return $this->mds_put($eid, $data, $column, !($this->mds_get($eid) === FALSE));
  238. }
  239. function mds_put_dc_elements($mdo, $dcelem) {
  240. if (($mdt = $this->mds_get($mdo->mdo_eid)) === FALSE) {
  241. $mdt = $mdo->mdo_generate_default_xml_metadata(); $exists = FALSE;
  242. } else
  243. $exists = TRUE;
  244. $xmlDoc = new xmddoc(explode("\n", $mdt)); if ($xmlDoc->error) return FALSE;
  245. foreach ($dcelem as $dce => $value) {
  246. $xmlDoc->xmd_update($mdo->mdo_dcmap_v[$dce], (string) $value);
  247. }
  248. $this->mds_put($mdo->mdo_eid, '', 'md5', $exists);
  249. return $this->mds_put($mdo->mdo_eid, $xmlDoc->xmd_xml());
  250. }
  251. function mds_append($eid, $moredata, $column = 'indexabletext') {
  252. if (($olddata = $this->mds_get($eid, $column)) === FALSE) return FALSE;
  253. $this->mds_put($eid, $olddata . $moredata, $column); return $olddata;
  254. }
  255. function mds_delete($eid) {
  256. return $this->_query("DELETE FROM " . MDS_TABLE . " WHERE c_id = {$this->course_id} AND ", $eid);
  257. }
  258. function mds_delete_offspring($eid, $sep = '.') {
  259. return $this->_query("DELETE FROM " . MDS_TABLE . " WHERE c_id = {$this->course_id} AND ", $eid, $sep);
  260. }
  261. function mds_delete_many($idarray) {
  262. if (!is_array($idarray) || count($idarray) == 0) return FALSE;
  263. return $this->_query("DELETE FROM " . MDS_TABLE . " WHERE c_id = {$this->course_id} AND eid IN ('" .
  264. implode("','", array_map('addslashes', $idarray)) . "')");
  265. }
  266. function mds_update_xml_and_mdt($mdo, &$xmlDoc, $mda, $eid, &$traceinfo,
  267. $exists = TRUE) // note: $xmlDoc and $traceinfo passed by reference
  268. {
  269. foreach (explode("\n",
  270. str_replace("\r", "\n", str_replace("\r\n", "\n", $mda))) as $update)
  271. {
  272. if (!$update) continue;
  273. if (($nameLth = strpos($update, '='))) // e.g. 'gen/tit/str=new'
  274. {
  275. if (($text = api_substr($update, $nameLth + 1)) === FALSE) $text = '';
  276. if (!($path = trim(api_substr($update, 0, $nameLth)))) continue;
  277. if (($sc = api_strpos($path, ';'))) // e.g. 'gen/tit,gen/des;str@lang'
  278. $xmlDoc->xmd_update_many(api_substr($path, 0, $sc),
  279. api_substr($path, $sc + 1), $text);
  280. else
  281. $xmlDoc->xmd_update($path, $text);
  282. }
  283. elseif ($nameLth === FALSE) // e.g. 'gen/tit/str[-1]~'
  284. {
  285. if ($update == '~~')
  286. {
  287. $update = 'DELETE ' . $eid;
  288. if ($exists === FALSE) $update = '';
  289. else $this->mds_delete($eid);
  290. $mda = ''; $exists = TRUE;
  291. foreach ($xmlDoc->children[0] as $key => $child)
  292. unset($xmlDoc->children[0][$key]);
  293. }
  294. elseif ($update == '!!')
  295. {
  296. define_kwds($mdo);
  297. $update = ''; $mda = ''; $exists = TRUE;
  298. }
  299. else
  300. {
  301. $x = $xmlDoc->xmd_update(trim($update), '');
  302. }
  303. }
  304. if ($update) $traceinfo .= $update . '- ';
  305. }
  306. $mdt = $xmlDoc->xmd_xml();
  307. if ($exists === FALSE)
  308. {
  309. $this->mds_put($eid, $mdt, 'mdxmltext', FALSE);
  310. $traceinfo .= 'INSERT ' . $eid . '- ';
  311. }
  312. elseif($mda)
  313. {
  314. $this->mds_put($eid, $mdt, 'mdxmltext');
  315. $traceinfo .= 'UPDATE ' . $eid . '- ';
  316. }
  317. return $mdt;
  318. }
  319. function _coldatstart($column, $data) {
  320. return $column . " LIKE '" . Database::escape_string($data) . "%'";
  321. }
  322. function _coldat($column, $data) {
  323. return $column . "='" . Database::escape_string($data) . "'";
  324. }
  325. function _query($sql, $eid = '', $sep = '') {
  326. if ($eid) $sql .= $sep ? $this->_coldatstart('eid', $eid . $sep) :
  327. $this->_coldat('eid', $eid);
  328. return Database::query($sql);
  329. }
  330. }