コード例 #1
0
 /**
  * Set index specified prefix terms for all items in this path
  * @param   string  Comma-separated list of terms
  * @param   char Xapian term prefix
  * @return  boolean False on error, true otherwise
  */
 public function set_terms_by_prefix($terms_string, $prefix)
 {
     $course_id = api_get_course_int_id();
     if (api_get_setting('search_enabled') !== 'true') {
         return false;
     }
     if (!extension_loaded('xapian')) {
         return false;
     }
     $terms_string = trim($terms_string);
     $terms = explode(',', $terms_string);
     array_walk($terms, 'trim_value');
     $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
     // Don't do anything if no change, verify only at DB, not the search engine.
     if (count(array_diff($terms, $stored_terms)) == 0 && count(array_diff($stored_terms, $terms)) == 0) {
         return false;
     }
     require_once 'xapian.php';
     // TODO: Try catch every xapian use or make wrappers on API.
     require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
     require_once api_get_path(LIBRARY_PATH) . 'search/xapian/XapianQuery.php';
     require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
     $items_table = Database::get_course_table(TABLE_LP_ITEM);
     // TODO: Make query secure agains XSS : use member attr instead of post var.
     $lp_id = intval($_POST['lp_id']);
     $sql = "SELECT * FROM {$items_table} WHERE c_id = {$course_id} AND lp_id = {$lp_id}";
     $result = Database::query($sql);
     $di = new ChamiloIndexer();
     while ($lp_item = Database::fetch_array($result)) {
         // Get search_did.
         $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
         $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1';
         $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp_id, $lp_item['id']);
         //echo $sql; echo '<br>';
         $res = Database::query($sql);
         if (Database::num_rows($res) > 0) {
             $se_ref = Database::fetch_array($res);
             // Compare terms.
             $doc = $di->get_document($se_ref['search_did']);
             $xapian_terms = xapian_get_doc_terms($doc, $prefix);
             $xterms = array();
             foreach ($xapian_terms as $xapian_term) {
                 $xterms[] = substr($xapian_term['name'], 1);
             }
             $dterms = $terms;
             $missing_terms = array_diff($dterms, $xterms);
             $deprecated_terms = array_diff($xterms, $dterms);
             // Save it to search engine.
             foreach ($missing_terms as $term) {
                 $doc->add_term($prefix . $term, 1);
             }
             foreach ($deprecated_terms as $term) {
                 $doc->remove_term($prefix . $term);
             }
             $di->getDb()->replace_document((int) $se_ref['search_did'], $doc);
             $di->getDb()->flush();
         } else {
             //@todo What we should do here?
         }
     }
     return true;
 }
コード例 #2
0
 /**
  * Set index specified prefix terms for all items in this path
  * @param   string  Comma-separated list of terms
  * @param   char Xapian term prefix
  * @return  boolean False on error, true otherwise
  */
 public function set_terms_by_prefix($terms_string, $prefix)
 {
     $em = Database::getManager();
     $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
     if (api_get_setting('search.search_enabled') !== 'true') {
         return false;
     }
     if (!extension_loaded('xapian')) {
         return false;
     }
     $terms_string = trim($terms_string);
     $terms = explode(',', $terms_string);
     array_walk($terms, 'trim_value');
     $stored_terms = $this->get_common_index_terms_by_prefix($prefix);
     // Don't do anything if no change, verify only at DB, not the search engine.
     if (count(array_diff($terms, $stored_terms)) == 0 && count(array_diff($stored_terms, $terms)) == 0) {
         return false;
     }
     require_once 'xapian.php';
     // TODO: Try catch every xapian use or make wrappers on API.
     require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
     require_once api_get_path(LIBRARY_PATH) . 'search/xapian/XapianQuery.php';
     require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
     $items_table = Database::get_course_table(TABLE_LP_ITEM);
     // TODO: Make query secure agains XSS : use member attr instead of post var.
     $lp_id = intval($_POST['lp_id']);
     $sql = "SELECT * FROM {$items_table} WHERE c_id = {$course->getId()} AND lp_id = {$lp_id}";
     $result = Database::query($sql);
     $di = new ChamiloIndexer();
     while ($lp_item = Database::fetch_array($result)) {
         $lpCourse = $em->getRepository('ChamiloCoreBundle:Course')->findOneBy(['code' => $this->cc]);
         // Get search_did.
         $se_ref = $em->getRepository('ChamiloCoreBundle:SearchEngineRef')->findOneBy(['course' => $lpCourse, 'toolId' => TOOL_LEARNPATH, 'refIdHighLevel' => $lp_id, 'refIdSecondLevel' => $lp_item['id']]);
         if ($se_ref) {
             // Compare terms.
             $doc = $di->get_document($se_ref->getSearchDid());
             $xapian_terms = xapian_get_doc_terms($doc, $prefix);
             $xterms = array();
             foreach ($xapian_terms as $xapian_term) {
                 $xterms[] = substr($xapian_term['name'], 1);
             }
             $dterms = $terms;
             $missing_terms = array_diff($dterms, $xterms);
             $deprecated_terms = array_diff($xterms, $dterms);
             // Save it to search engine.
             foreach ($missing_terms as $term) {
                 $doc->add_term($prefix . $term, 1);
             }
             foreach ($deprecated_terms as $term) {
                 $doc->remove_term($prefix . $term);
             }
             $di->getDb()->replace_document($se_ref->getSearchDid(), $doc);
             $di->getDb()->flush();
         } else {
             //@todo What we should do here?
         }
     }
     return true;
 }