dokeos-metadata
[ class tree: dokeos-metadata ] [ index: dokeos-metadata ] [ all elements ]

Source for file md_phpdig.php

Documentation is available at md_phpdig.php

  1. <?php /*                                  <!-- Dokeos metadata/md_phpdig.php -->
  2.                                                              <!-- 2005/03/24 -->
  3.  
  4. <!-- Copyright (C) 2005 rene.haentjens@UGent.be -  see metadata/md_funcs.php -->
  5.  
  6. */
  7.  
  8. /**
  9. ============================================================================== 
  10. *    Dokeos Metadata: PhpDig connection
  11. *
  12. *   If PhpDig 1.8.3 is installed in a Dokeos 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. *    @package dokeos.metadata
  31. ============================================================================== 
  32. */
  33.  
  34.  
  35. // PHPDIG CONNECTION ---------------------------------------------------------->
  36.  
  37. $phpDigInc get_course_path($_course['path''/phpdig-1.8.6/includes/';
  38. $phpDigIncCn $phpDigInc'connect.php';  // to connect to PhpDig's database
  39. $phpDigIncCw $phpDigInc'common_words.txt';  // stopwords
  40.  
  41. //  if (!file_exists($phpDigIncCn)) return(); doesn't seem to work properly...
  42.  
  43.  
  44. if (file_exists($phpDigIncCw))
  45.     if (is_array($lines @file($phpDigIncCw)))
  46.         while (list($id,$wordeach($lines))
  47.             $common_words[trim($word)1;
  48.  
  49. define('SUMMARY_DISPLAY_LENGTH'700);
  50. define('PHPDIG_ENCODING''iso-8859-1');
  51. define('SMALL_WORDS_SIZE'2);
  52. define('MAX_WORDS_SIZE',50);
  53. define('WORDS_CHARS_LATIN1''[:alnum:]ðþßµ');
  54.  
  55. foreach (array'A'=>'ÀÁÂÃÄÅ''a'=>'àáâãäå''O'=>'ÒÓÔÕÖØ''o'=>'òóôõöø'
  56.                 'E'=>'ÈÉÊË''e'=>'èéêë''C'=>'Ç''c'=>'ç''I'=>'ÌÍÎÏ'
  57.                 'i'=>'ìíîï''U'=>'ÙÚÛÜ''u'=>'ùúûü''Y'=>'Ý''y'=>'ÿý'
  58.                 'N'=>'Ñ''n'=>'ñ'as $without => $allwith)
  59.     foreach (explode('!'chunk_split($allwith1'!')) as $with)
  60.     if ($with)  // because last one will be empty!
  61.     {
  62.         $letterswithout .= $without$letterswith .= $with;
  63.     }
  64. define('LETTERS_WITH_ACCENTS'$letterswith);
  65. define('SAME_WITHOUT_ACCENTS'$letterswithout);
  66.  
  67.     or give_up('LETTERS_WITH_ACCENTS problem in md_phpdig.php');
  68.  
  69.  
  70. function find_site($url)
  71. {
  72.     $site_url "site_url = '" addslashes($url"'";
  73.     
  74.     $result api_sql_query("SELECT site_id FROM " PHPDIG_DB_PREFIX 
  75.         "sites WHERE " $site_url__FILE____LINE__);  // find site
  76.     
  77.     if (mysql_num_rows($result== 1)
  78.     {
  79.         $row mysql_fetch_array($result)return (int) $row['site_id'];
  80.     }
  81.     else
  82.     {
  83.         $result api_sql_query("INSERT INTO " PHPDIG_DB_PREFIX 
  84.             "sites SET " $site_url__FILE____LINE__);  // new site
  85.         $site_id mysql_insert_id();
  86.         
  87.         $result api_sql_query("INSERT INTO " PHPDIG_DB_PREFIX 
  88.             "site_page (site_id,num_page) VALUES ('$site_id', '0')");
  89.         
  90.         return $site_id;
  91.     }
  92. }
  93.  
  94. function remove_engine_entries($url$path$file '')
  95. {
  96.     $and_path " AND path = '" addslashes($path"'";
  97.     if ($file$and_path .= " AND file LIKE '" addslashes(
  98.         str_replace(array('_''%')array('\_''\%')$file)) "%'";
  99.     
  100.     $result api_sql_query("SELECT spider_id FROM " PHPDIG_DB_PREFIX 
  101.         "spider WHERE site_id=" ($site_id find_site($url)) $and_path
  102.         __FILE____LINE__);  // find page(s)
  103.     
  104.     while ($row mysql_fetch_array($result))
  105.     {
  106.         api_sql_query("DELETE FROM " PHPDIG_DB_PREFIX 
  107.             "engine WHERE spider_id=" . (int)$row['spider_id']
  108.             __FILE____LINE__);  // delete all references to keywords
  109.         $aff .= ' +' mysql_affected_rows();
  110.     }
  111.     
  112.     api_sql_query("DELETE FROM " PHPDIG_DB_PREFIX 
  113.         "spider WHERE site_id=" $site_id $and_path
  114.         __FILE____LINE__);  // delete page
  115.     
  116.     echo htmlspecialchars($url $path $file)' (site_id '
  117.         $site_id'): 'mysql_affected_rows()$aff
  118.         ' pages + word references removed from index.<br>';
  119.     
  120.     return $site_id;
  121. }
  122.  
  123. function index_words($site_id$path$file$first_words$keywords)
  124. {
  125.     global $common_words;
  126.     
  127.     $spider_set_path_etc "spider SET path='" addslashes($path
  128.         "',file='" addslashes($file"',first_words='" 
  129.         addslashes($first_words"',site_id='$site_id'";
  130.         // do not set upddate,md5,num_words,last_modified,filesize
  131.     
  132.     api_sql_query("INSERT INTO " PHPDIG_DB_PREFIX $spider_set_path_etc
  133.         __FILE____LINE__);
  134.     
  135.     $spider_id mysql_insert_id()$new 0;
  136.     
  137.     foreach ($keywords as $key => $w)
  138.     if (strlen($keySMALL_WORDS_SIZE and strlen($key<= MAX_WORDS_SIZE and 
  139.             !isset($common_words[$key]and 
  140.             ereg('^['.WORDS_CHARS_LATIN1.'#$]'$key))
  141.     {
  142.         $result api_sql_query("SELECT key_id FROM " PHPDIG_DB_PREFIX 
  143.             "keywords WHERE keyword = '" addslashes($key"'"
  144.             __FILE____LINE__);
  145.         
  146.         if (mysql_num_rows($result== 0)
  147.         {
  148.             api_sql_query("INSERT INTO " PHPDIG_DB_PREFIX 
  149.                 "keywords (keyword,twoletters) VALUES ('" addslashes($key
  150.                 "','" .addslashes(substr(str_replace('\\','',$key),0,2)) ."')"
  151.             __FILE____LINE__);
  152.             $key_id mysql_insert_id()$new++;
  153.         }
  154.         else
  155.         {
  156.             $keyid mysql_fetch_row($result)$key_id $keyid[0];
  157.         }
  158.         
  159.         api_sql_query("INSERT INTO " PHPDIG_DB_PREFIX 
  160.             "engine (spider_id,key_id,weight) VALUES ($spider_id,$key_id,$w)"
  161.             __FILE____LINE__);
  162.     }
  163.         
  164.     echo '<tr><td>'htmlspecialchars($file)'</td><td>(spider_id '
  165.         $spider_id'):</td><td align="right">'count($keywords)' kwds, '
  166.         $new ' new</td></tr>'"\n";
  167. }
  168.  
  169. function get_first_words($text$path$file)
  170. {
  171.     $db_some_text preg_replace("/([ ]{2}|\n|\r|\r\n)/" ," "$text);
  172.     if (strlen($db_some_textSUMMARY_DISPLAY_LENGTH{
  173.       $db_some_text substr($db_some_text0SUMMARY_DISPLAY_LENGTH"...";
  174.     }
  175.     
  176.     $titre_resume $path $file;
  177.     if (($psc strpos($titre_resume'scorm/')) !== FALSE)
  178.         $titre_resume substr($titre_resume$psc 6);
  179.     if (($pth strpos($titre_resume'&thumb')) !== FALSE)
  180.         $titre_resume substr($titre_resume0$pth);
  181.     
  182.     return $titre_resume."\n".$db_some_text;
  183. }
  184.  
  185. function get_keywords($text)
  186. {
  187.     if (($token strtok(phpdigEpureText($text)' '))) $nbre_mots[$token1;
  188.     
  189.     while (($token strtok(' ')))
  190.         $nbre_mots[$token($nm $nbre_mots[$token]$nm 1;
  191.      
  192.     return $nbre_mots;
  193. }
  194.  
  195. function phpdigEpureText($text)
  196. {
  197.     $text strtr(phpdigStripAccents(strtolower($text))'ÐÞ''ðþ');
  198.     
  199.     $text ereg_replace('[^'.WORDS_CHARS_LATIN1.' \'._~@#$&%/=-]+',' ',$text);  // RH: was ' \'._~@#$:&%/;,=-]+', also below
  200.     
  201.     $text ereg_replace('(['.WORDS_CHARS_LATIN1.'])[\'._~@#$&%/=-]+($|[[:space:]]$|[[:space:]]['.WORDS_CHARS_LATIN1.'])','\1\2',$text);
  202.     
  203.     // the next two repeated lines needed
  204.     if (SMALL_WORDS_SIZE >= 1{
  205.       $text ereg_replace('[[:space:]][^ ]{1,'.SMALL_WORDS_SIZE.'}[[:space:]]',' ',' '.$text.' ');
  206.       $text ereg_replace('[[:space:]][^ ]{1,'.SMALL_WORDS_SIZE.'}[[:space:]]',' ',' '.$text.' ');
  207.     }
  208.     //$text = ereg_replace('\.+[[:space:]]|\.+$|\.{2,}',' ',$text);
  209.     $text ereg_replace('\.{2,}',' ',$text);
  210.     $text ereg_replace('^[[:space:]]*\.+',' ',$text);
  211.     
  212.     return trim(ereg_replace("[[:space:]]+"," ",$text));
  213. }
  214.  
  215. function phpdigStripAccents($chaine)
  216. {
  217.     $chaine str_replace('Æ','ae',str_replace('æ','ae',$chaine));
  218.     return strtr($chaineLETTERS_WITH_ACCENTSSAME_WITHOUT_ACCENTS);
  219. }
  220. ?>

Documentation generated on Thu, 12 Jun 2008 14:06:11 -0500 by phpDocumentor 1.4.1