This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 
<?php
defined('C5_EXECUTE') or die("Access Denied.");
class Concrete5_Library_IndexedSearch {
    
    public $searchBatchSize = PAGE_SEARCH_INDEX_BATCH_SIZE;
    public $searchReindexTimeout = PAGE_SEARCH_INDEX_LIFETIME;

    private $cPathSections = array();
    private $searchableAreaNamesManual = array();
    
    public function addSearchableArea($arr) {
        $this->searchableAreaNamesManual[] = $arr;
    }
    
    public function getSearchableAreaAction() {
        $action = Config::get('SEARCH_INDEX_AREA_METHOD');
        if (!strlen($action)) {
            $action = 'blacklist';
        }
        return $action;
    }
    
    public function getSavedSearchableAreas() {
        $areas = Config::get('SEARCH_INDEX_AREA_LIST');
        $areas = unserialize($areas);
        if (!is_array($areas)) {
            $areas = array();
        }
        return $areas;
    }
    
    public function clearSearchIndex() {
        $db = Loader::db();
        $db->Execute('truncate table PageSearchIndex');
    }
    
    public function reindexPage($page) {
        $db = Loader::db();         
        if (is_object($page) && ($page instanceof Collection) && ($page->getAttribute('exclude_search_index') != 1)) {
            $datetime = Loader::helper('date')->getSystemDateTime();
            $db->Replace('PageSearchIndex', array(
                'cID' => $page->getCollectionID(), 
                'cName' => $page->getCollectionName(), 
                'cDescription' => $page->getCollectionDescription(), 
                'cPath' => $page->getCollectionPath(),
                'cDatePublic' => $page->getCollectionDatePublic(), 
                'content' => $this->getBodyContentFromPage($page),
                'cDateLastIndexed' => $datetime
            ), array('cID'), true);         
        } else {
            $db->Execute('delete from PageSearchIndex where cID = ?', array($page->getCollectionID()));
        }
    }   
    
    public function getBodyContentFromPage($c) {
        $searchableAreaNamesInitial=$this->getSavedSearchableAreas();
        foreach($this->searchableAreaNamesManual as $sm) {
            $searchableAreaNamesInitial[] = $sm;
        }
        
        $searchableAreaNames = array();
        if ($this->getSearchableAreaAction() == 'blacklist') {
            $areas = Area::getHandleList();
            foreach($areas as $arHandle) {
                if (!in_array($arHandle, $searchableAreaNamesInitial)) {
                    $searchableAreaNames[] = $arHandle;
                }
            }
        } else {
            $searchableAreaNames = $searchableAreaNamesInitial;
        }       
        
        if (count($searchableAreaNames) == 0) {
            return false;
        }
        
        $text = '';

        $tagsToSpaces=array('<br>','<br/>','<br />','<p>','</p>','</ p>','<div>','</div>','</ div>','&nbsp;');
        $blarray=array();
        $db = Loader::db();
        $r = $db->Execute('select bID, arHandle from CollectionVersionBlocks where cID = ? and cvID = ?', array($c->getCollectionID(), $c->getVersionID()));
        $th = Loader::helper('text');
        while ($row = $r->FetchRow()) {
            if (in_array($row['arHandle'], $searchableAreaNames)) {
                $b = Block::getByID($row['bID'], $c, $row['arHandle']);
                if (!is_object($b)) {
                    continue;
                }
                $bi = $b->getInstance();
                $bi->bActionCID = $c->getCollectionID();
                if(method_exists($bi,'getSearchableContent')){
                    $searchableContent = $bi->getSearchableContent();  
                    if(strlen(trim($searchableContent)))                    
                        $text .= $th->decodeEntities(strip_tags(str_ireplace($tagsToSpaces,' ',$searchableContent)), ENT_QUOTES, APP_CHARSET).' ';
                }
                unset($b);
                unset($bi);
            }       
        }
        
        $returned_text = Events::fire('on_page_body_index', $c, $text);

        if ( $returned_text !== null && $returned_text !== false){ 
            $text = $returned_text;
        }

        return $text;
    }
    
    /** 
     * Reindexes the search engine.
     */
    public function reindexAll($fullReindex = false) {
        Cache::disableLocalCache();
        
        $db = Loader::db();
        Loader::model('collection_attributes');
        
        if ($fullReindex) {
            $db->Execute("truncate table PageSearchIndex");
        }
        
        $pl = new PageList();
        $pl->ignoreAliases();
        $pl->ignorePermissions();
        $pl->sortByCollectionIDAscending();
        $pl->filter(false, '(c.cDateModified > psi.cDateLastIndexed or UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(psi.cDateLastIndexed) > ' . $this->searchReindexTimeout . ' or psi.cID is null or psi.cDateLastIndexed is null)');
        $pl->filter(false, '(ak_exclude_search_index is null or ak_exclude_search_index = 0)');
        $pages = $pl->get($this->searchBatchSize);
        
        $num = 0;
        foreach($pages as $c) { 
            
            // make sure something is approved
            $cv = $c->getVersionObject();
            if(!$cv->cvIsApproved) { 
                continue;
            }       

            $c->reindex($this, true);
            $num++;     
            unset($c);
        }
        
        $pnum = Collection::reindexPendingPages();
        $num = $num + $pnum;
        
        Cache::enableLocalCache();
        $result = new stdClass;
        $result->count = $num;
        return $result;
    }

}