コード例 #1
0
 private function &execute()
 {
     $results = array();
     if (count($this->tags) <= 0 && count($this->words) <= 0) {
         return $results;
     }
     if (!file_exists(GSDATAOTHERPATH . I18N_WORD_INDEX)) {
         create_i18n_search_index();
     }
     $isi18n = function_exists('i18n_init');
     // use multibyte string functions?
     $ismb = function_exists('mb_ereg_replace');
     if ($ismb) {
         mb_regex_encoding('UTF-8');
     }
     // language?
     $defaultLanguage = function_exists('return_i18n_default_language') ? return_i18n_default_language() : '';
     $languages = function_exists('return_i18n_languages') ? $this->language ? array($this->language) : return_i18n_languages() : array($defaultLanguage);
     // scores per id
     $idScores = null;
     $tagIds = array();
     if ($this->tags && count($this->tags) > 0) {
         for ($i = 0; $i < count($this->tags); $i++) {
             if ($ismb) {
                 $this->tags[$i] = mb_ereg_replace("[^\\w]", "_", mb_strtolower(trim($this->tags[$i]), 'UTF-8'));
             } else {
                 $this->tags[$i] = preg_replace("/[^\\w]/", "_", strtolower(trim($this->tags[$i])));
             }
             $tagIds[$this->tags[$i]] = array();
         }
         $f = fopen(GSDATAOTHERPATH . I18N_TAG_INDEX, "r");
         while (($line = fgets($f)) !== false) {
             foreach ($this->tags as $tag) {
                 if (strncmp($line, $tag . ' ', strlen($tag) + 1) == 0) {
                     $fullids = preg_split("/\\s+/", trim($line));
                     unset($fullids[0]);
                     foreach ($fullids as $fullid) {
                         $tagIds[$tag][$fullid] = 1;
                     }
                 }
             }
         }
         fclose($f);
         foreach ($this->tags as $tag) {
             if ($idScores === null) {
                 $idScores = $tagIds[$tag];
             } else {
                 $idScores = array_intersect_key($idScores, $tagIds[$tag]);
             }
         }
     }
     if ($this->words && count($this->words) > 0) {
         $wordIds = array();
         for ($i = 0; $i < count($this->words); $i++) {
             if ($ismb) {
                 $this->words[$i] = mb_strtolower(trim($this->words[$i]), 'UTF-8');
             } else {
                 $this->words[$i] = strtolower(trim($this->words[$i]));
             }
             $wordIds[$this->words[$i]] = array();
         }
         $f = fopen(GSDATAOTHERPATH . I18N_WORD_INDEX, "r");
         while (($line = fgets($f)) !== false) {
             foreach ($this->words as $word) {
                 if ($this->is_word($line, $word, $ismb)) {
                     $fullidAndScores = preg_split("/\\s+/", trim($line));
                     unset($fullidAndScores[0]);
                     foreach ($fullidAndScores as $fullidAndScore) {
                         $pos = strrpos($fullidAndScore, ":");
                         $score = (int) substr($fullidAndScore, $pos + 1);
                         $fullid = substr($fullidAndScore, 0, $pos);
                         if (isset($wordIds[$word][$fullid])) {
                             $wordIds[$word][$fullid] += $score;
                         } else {
                             if ($idScores == null || isset($idScores[$fullid])) {
                                 $wordIds[$word][$fullid] = $score;
                             }
                         }
                     }
                 }
             }
         }
         fclose($f);
         foreach ($this->words as $word) {
             if ($idScores === null) {
                 $idScores = $wordIds[$word];
             } else {
                 $idScores = array_intersect_key($idScores, $wordIds[$word]);
                 foreach ($idScores as $fullid => $score) {
                     $idScores[$fullid] = $score * $wordIds[$word][$fullid];
                 }
             }
         }
     }
     $filteredresults = array();
     if ($idScores && count($idScores) > 0) {
         $idPubDates = array();
         $idCreDates = array();
         $f = fopen(GSDATAOTHERPATH . I18N_DATE_INDEX, "r");
         while (($line = fgets($f)) !== false) {
             $items = preg_split("/\\s+/", trim($line));
             $fullid = $items[0];
             if (count($items) >= 2 && isset($idScores[$fullid])) {
                 $idPubDates[$fullid] = (int) $items[1];
                 $idCreDates[$fullid] = count($items) >= 3 ? (int) $items[2] : (int) $items[1];
             }
         }
         fclose($f);
         foreach ($idScores as $fullid => $score) {
             if ($isi18n) {
                 $pos = strrpos($fullid, "_");
                 $language = $pos !== false ? substr($fullid, $pos + 1) : $defaultLanguage;
                 $id = $pos !== false ? substr($fullid, 0, $pos) : $fullid;
             } else {
                 $language = '';
                 $id = $fullid;
             }
             if (in_array($language, $languages)) {
                 // ignore language, if not default and not requested by user
                 if (substr($id, 0, 1) == '#') {
                     $ispage = false;
                     $id = substr($id, 1);
                 } else {
                     $ispage = true;
                 }
                 if ($ispage) {
                     $results[] = new I18nSearchResultPage($id, $language, $idCreDates[$fullid], $idPubDates[$fullid], $idScores[$fullid]);
                 } else {
                     global $filters;
                     $item = null;
                     foreach ($filters as $filter) {
                         if ($filter['filter'] == I18N_FILTER_SEARCH_ITEM) {
                             $item = call_user_func_array($filter['function'], array($id, $language, $idCreDates[$fullid], $idPubDates[$fullid], $idScores[$fullid]));
                             if ($item) {
                                 break;
                             }
                         }
                     }
                     $results[] = $item ? $item : new I18nSearchResultItem($id, $language, $idCreDates[$fullid], $idPubDates[$fullid], $idScores[$fullid]);
                 }
             }
         }
         foreach ($results as $item) {
             global $filters;
             $vetoed = false;
             foreach ($filters as $filter) {
                 if ($filter['filter'] == I18N_FILTER_VETO_SEARCH_ITEM) {
                     if (call_user_func_array($filter['function'], array($item))) {
                         $vetoed = true;
                         break;
                     }
                 }
             }
             if (!$vetoed) {
                 $filteredresults[] = $item;
             }
         }
         switch ($this->sort_field) {
             case 'url':
                 usort($filteredresults, array($this, 'compare_url'));
                 break;
             case 'date':
                 usort($filteredresults, array($this, 'compare_date'));
                 break;
             case 'created':
                 usort($filteredresults, array($this, 'compare_created'));
                 break;
             case 'score':
                 usort($filteredresults, array($this, 'compare_score'));
                 break;
             default:
                 usort($filteredresults, array($this, 'compare_field'));
         }
         if ($this->sort_order == '-') {
             $filteredresults = array_reverse($filteredresults);
         }
     }
     return $filteredresults;
 }
コード例 #2
0
ファイル: tags.php プロジェクト: sevenns/alpremstroy
<?php

$slug = @$params['slug'];
$is_ajax = !isset($params['ajax']) || $params['ajax'];
$addtags = isset($params['addTags']) ? preg_split('/\\s+/', trim($params['addTags'])) : null;
$reqtags = trim(@$_REQUEST['tags']) ? preg_split('/\\s+/', trim($_REQUEST['tags'])) : null;
$language = isset($params['lang']) ? $params['lang'] : null;
// languages
$isi18n = function_exists('return_i18n_languages');
$defaultLanguage = $isi18n ? return_i18n_default_language() : '';
$languages = $isi18n ? $language ? array($language) : return_i18n_languages() : null;
// read tag file
if (!file_exists(GSDATAOTHERPATH . I18N_WORD_INDEX)) {
    create_i18n_search_index();
}
$alltags = array();
$f = fopen(GSDATAOTHERPATH . I18N_TAG_INDEX, "r");
while (($line = fgets($f)) !== false) {
    $items = preg_split("/\\s+/", trim($line));
    $tag = array_shift($items);
    if ($languages) {
        // filter items
        $filteredItems = array();
        foreach ($items as $item) {
            $pos = strrpos($item, '_');
            $lang = $pos !== false ? substr($item, $pos + 1) : $defaultLanguage;
            if (in_array($lang, $languages)) {
                $filteredItems[] = $item;
            }
        }
        if (count($filteredItems) > 0) {