md_phpdig.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Chamilo metadata/md_phpdig.php
  4. * 2005/03/24
  5. * @copyright 2005 rene.haentjens@UGent.be - see metadata/md_funcs.php
  6. * @package chamilo.metadata
  7. *
  8. */
  9. /**
  10. * Chamilo Metadata: PhpDig connection
  11. *
  12. * If PhpDig 1.8.3 is installed in a Chamilo course site, then MD items
  13. * can be indexed for search (via PhpDig's search screen search.php).
  14. *
  15. * The functions below inject the words of metadata/indexabletext directly
  16. * into PhpDig's tables. Affected tables:
  17. *
  18. * keywords: key_id, twoletters, keyword (lowercase, accents removed)
  19. *
  20. * sites: site_id, site_url (e.g. http://xx.yy.zz/), upddate, ...
  21. *
  22. * spider: spider_id, site_id, upddate, num_words, first_words,
  23. * path (e.g. uu/vv/ww/), file (e.g. index.php?sid=xxx), ...
  24. *
  25. * engine: spider_id, key_id, weight
  26. *
  27. * Most of the function code is a simplified version of real PhpDig code
  28. * released under the GNU GPL V2, see www.phpdig.net.
  29. *
  30. */
  31. // PHPDIG CONNECTION ---------------------------------------------------------->
  32. $phpDigInc = get_course_path() . $_course['path'] . '/phpdig-1.8.6/includes/';
  33. $phpDigIncCn = $phpDigInc. 'connect.php'; // to connect to PhpDig's database
  34. $phpDigIncCw = $phpDigInc. 'common_words.txt'; // stopwords
  35. // if (!file_exists($phpDigIncCn)) return(); doesn't seem to work properly...
  36. if (file_exists($phpDigIncCw))
  37. if (is_array($lines = @file($phpDigIncCw)))
  38. while (list($id,$word) = each($lines))
  39. $common_words[trim($word)] = 1;
  40. define('SUMMARY_DISPLAY_LENGTH', 700);
  41. //define('PHPDIG_ENCODING', 'iso-8859-1');
  42. define('PHPDIG_ENCODING', strtolower($charset));
  43. define('SMALL_WORDS_SIZE', 2);
  44. define('MAX_WORDS_SIZE',50);
  45. define('WORDS_CHARS_LATIN1', '[:alnum:]��ߵ');
  46. foreach (array( 'A'=>'������', 'a'=>'������', 'O'=>'������', 'o'=>'������',
  47. 'E'=>'����', 'e'=>'����', 'C'=>'�', 'c'=>'�', 'I'=>'����',
  48. 'i'=>'����', 'U'=>'����', 'u'=>'����', 'Y'=>'�', 'y'=>'��',
  49. 'N'=>'�', 'n'=>'�') as $without => $allwith)
  50. foreach (explode('!', chunk_split($allwith, 1, '!')) as $with)
  51. if ($with) // because last one will be empty!
  52. {
  53. $letterswithout .= $without; $letterswith .= $with;
  54. }
  55. define('LETTERS_WITH_ACCENTS', $letterswith);
  56. define('SAME_WITHOUT_ACCENTS', $letterswithout);
  57. (strlen(LETTERS_WITH_ACCENTS) == strlen(SAME_WITHOUT_ACCENTS))
  58. or give_up('LETTERS_WITH_ACCENTS problem in md_phpdig.php');
  59. function find_site($url)
  60. {
  61. $site_url = "site_url = '" . addslashes($url) . "'";
  62. $result = Database::query("SELECT site_id FROM " . PHPDIG_DB_PREFIX .
  63. "sites WHERE " . $site_url); // find site
  64. if (Database::num_rows($result) == 1)
  65. {
  66. $row = Database::fetch_array($result); return (int) $row['site_id'];
  67. }
  68. else
  69. {
  70. $result = Database::query("INSERT INTO " . PHPDIG_DB_PREFIX .
  71. "sites SET " . $site_url); // new site
  72. $site_id = Database::insert_id();
  73. $result = Database::query("INSERT INTO " . PHPDIG_DB_PREFIX .
  74. "site_page (site_id,num_page) VALUES ('$site_id', '0')");
  75. return $site_id;
  76. }
  77. }
  78. function remove_engine_entries($url, $path, $file = '')
  79. {
  80. global $charset;
  81. $and_path = " AND path = '" . addslashes($path) . "'";
  82. if ($file) $and_path .= " AND file LIKE '" . addslashes(
  83. str_replace(array('_', '%'), array('\_', '\%'), $file)) . "%'";
  84. $result = Database::query("SELECT spider_id FROM " . PHPDIG_DB_PREFIX .
  85. "spider WHERE site_id=" . ($site_id = find_site($url)) . $and_path); // find page(s)
  86. while ($row = Database::fetch_array($result))
  87. {
  88. $result = Database::query("DELETE FROM " . PHPDIG_DB_PREFIX .
  89. "engine WHERE spider_id=" . (int)$row['spider_id']); // delete all references to keywords
  90. $aff .= ' +' . Database::affected_rows($result);
  91. }
  92. Database::query("DELETE FROM " . PHPDIG_DB_PREFIX ."spider WHERE site_id=" . $site_id . $and_path); // delete page
  93. echo htmlspecialchars($url . $path . $file, ENT_QUOTES, $charset), ' (site_id ',
  94. $site_id, '): ', Database::affected_rows(), $aff,
  95. ' pages + word references removed from index.<br />';
  96. return $site_id;
  97. }
  98. function index_words($site_id, $path, $file, $first_words, $keywords)
  99. {
  100. global $common_words;
  101. $spider_set_path_etc = "spider SET path='" . addslashes($path) .
  102. "',file='" . addslashes($file) . "',first_words='" .
  103. addslashes($first_words) . "',site_id='$site_id'";
  104. // do not set upddate,md5,num_words,last_modified,filesize
  105. Database::query("INSERT INTO " . PHPDIG_DB_PREFIX . $spider_set_path_etc);
  106. $spider_id = Database::insert_id(); $new = 0;
  107. foreach ($keywords as $key => $w)
  108. if (strlen($key) > SMALL_WORDS_SIZE and strlen($key) <= MAX_WORDS_SIZE and
  109. !isset($common_words[$key]) and
  110. ereg('^['.WORDS_CHARS_LATIN1.'#$]', $key))
  111. {
  112. $result = Database::query("SELECT key_id FROM " . PHPDIG_DB_PREFIX .
  113. "keywords WHERE keyword = '" . addslashes($key) . "'");
  114. if (Database::num_rows($result) == 0)
  115. {
  116. Database::query("INSERT INTO " . PHPDIG_DB_PREFIX .
  117. "keywords (keyword,twoletters) VALUES ('" . addslashes($key) .
  118. "','" .addslashes(substr(str_replace('\\','',$key),0,2)) ."')");
  119. $key_id = Database::insert_id(); $new++;
  120. }
  121. else
  122. {
  123. $keyid = Database::fetch_row($result); $key_id = $keyid[0];
  124. }
  125. Database::query("INSERT INTO " . PHPDIG_DB_PREFIX .
  126. "engine (spider_id,key_id,weight) VALUES ($spider_id,$key_id,$w)");
  127. }
  128. global $charset;
  129. echo '<tr><td>', htmlspecialchars($file, ENT_QUOTES, $charset), '</td><td>(spider_id ',
  130. $spider_id, '):</td><td align="right">', count($keywords), ' kwds, ',
  131. $new , ' new</td></tr>', "\n";
  132. }
  133. function get_first_words($text, $path, $file)
  134. {
  135. $db_some_text = preg_replace("/([ ]{2}|\n|\r|\r\n)/" ," ", $text);
  136. if (strlen($db_some_text) > SUMMARY_DISPLAY_LENGTH) {
  137. $db_some_text = substr($db_some_text, 0, SUMMARY_DISPLAY_LENGTH) . "...";
  138. }
  139. $titre_resume = $path . $file;
  140. if (($psc = strpos($titre_resume, 'scorm/')) !== FALSE)
  141. $titre_resume = substr($titre_resume, $psc + 6);
  142. if (($pth = strpos($titre_resume, '&thumb')) !== FALSE)
  143. $titre_resume = substr($titre_resume, 0, $pth);
  144. return $titre_resume."\n".$db_some_text;
  145. }
  146. function get_keywords($text)
  147. {
  148. if (($token = strtok(phpdigEpureText($text), ' '))) $nbre_mots[$token] = 1;
  149. while (($token = strtok(' ')))
  150. $nbre_mots[$token] = ($nm = $nbre_mots[$token]) ? $nm + 1 : 1;
  151. return $nbre_mots;
  152. }
  153. function phpdigEpureText($text)
  154. {
  155. $text = strtr(phpdigStripAccents(strtolower($text)), '��', '��');
  156. $text = ereg_replace('[^'.WORDS_CHARS_LATIN1.' \'._~@#$&%/=-]+',' ',$text); // RH: was ' \'._~@#$:&%/;,=-]+', also below
  157. $text = ereg_replace('(['.WORDS_CHARS_LATIN1.'])[\'._~@#$&%/=-]+($|[[:space:]]$|[[:space:]]['.WORDS_CHARS_LATIN1.'])','\1\2',$text);
  158. // the next two repeated lines needed
  159. if (SMALL_WORDS_SIZE >= 1) {
  160. $text = ereg_replace('[[:space:]][^ ]{1,'.SMALL_WORDS_SIZE.'}[[:space:]]',' ',' '.$text.' ');
  161. $text = ereg_replace('[[:space:]][^ ]{1,'.SMALL_WORDS_SIZE.'}[[:space:]]',' ',' '.$text.' ');
  162. }
  163. //$text = ereg_replace('\.+[[:space:]]|\.+$|\.{2,}',' ',$text);
  164. $text = ereg_replace('\.{2,}',' ',$text);
  165. $text = ereg_replace('^[[:space:]]*\.+',' ',$text);
  166. return trim(ereg_replace("[[:space:]]+"," ",$text));
  167. }
  168. function phpdigStripAccents($chaine)
  169. {
  170. $chaine = str_replace('�','ae',str_replace('�','ae',$chaine));
  171. return strtr($chaine, LETTERS_WITH_ACCENTS, SAME_WITHOUT_ACCENTS);
  172. }
  173. ?>