예제 #1
0
 /**
  * Fetches the data from the persistent data storage.
  * @throws Exception
  * @return array list of data items
  */
 protected function fetchData()
 {
     foreach ($this->queryOptions as $name => $value) {
         $this->query->option($name, $value);
     }
     if (($pagination = $this->getPagination('Pagination')) !== false) {
         $pagination->setItemCount($this->getTotalItemCount());
         $this->query->limit($pagination->getLimit())->offset($pagination->getOffset());
     }
     if (($sort = $this->getSort()) !== false && ($order = $sort->getOrderBy()) != '') {
         foreach (explode(',', $order) as $orderAttribute) {
             $orderAttribute = trim($orderAttribute);
             $field = trim(preg_replace('#(desc|asc)$#sui', '', $orderAttribute));
             if (preg_match('#(desc|asc)$#sui', $orderAttribute, $matches)) {
                 $direction = $matches[1] === 'desc' ? true : false;
             } else {
                 $direction = $sort->getDirection($field);
             }
             $this->query->order($field, $direction ? 'DESC' : 'ASC');
         }
     }
     $sql = $this->query->build();
     $data = array();
     try {
         $data = Yii::app()->sphinx->cache($this->cacheTime)->createCommand($sql)->queryAll();
     } catch (Exception $e) {
         Yii::log('LSphinxDataProvider exception' . PHP_EOL . 'Message: ' . $e->getMessage() . 'Trace: ' . $e->getTraceAsString(), CLogger::LEVEL_ERROR);
         if (YII_DEBUG) {
             throw $e;
         }
     }
     return $data;
 }
예제 #2
0
 /**
  * Returns SphinxQL search object, initialized for tag search
  *
  * @param SearchForm $model
  * @return \ESphinxQL
  */
 protected function getSphinxTagSearchObject(SearchForm $model)
 {
     $objCommon = new ESphinxQL();
     $objCommon->addField('id')->addIndex(Yii::app()->params['sphinx']['indexes']['torrents']);
     $tagPatterns = array();
     if ($model->tags) {
         $tagPatterns[] = '"=' . str_replace(' ', '+', $model->tags) . '"';
     }
     if ($model->category) {
         $tagPatterns[] = '"=' . str_replace(' ', '+', LCategory::getCategoryTag($model->category)) . '"';
         $objCommon->where('category_id', $model->category);
     }
     if (count($tagPatterns)) {
         $searchPattern = '@tags ' . join(' ', $tagPatterns);
     }
     $objCommon->option('ranker', 'MATCHANY');
     // Filter torrents by status
     if (!empty($model->status)) {
         $objCommon->where('torrent_status', Torrent::TORRENT_STATUS_GOOD);
     }
     // Filter torrents by age
     if (!empty($model->age)) {
         $objCommon->where('created_at', time() - $model->age * 86400, '>=', true);
     }
     if (!empty($model->popular)) {
         $objCommon->order('downloads_count', 'DESC');
     }
     if ($model->latest) {
         $objCommon->order('created_at', 'DESC');
         $objCommon->limit($model->latest);
     }
     // If no ordering is set - default weight+id sorting mode
     if (!$objCommon->getOrders()) {
         $objCommon->order('weight()', 'DESC');
         $objCommon->order('id', 'DESC');
     }
     if (!empty($searchPattern)) {
         $objCommon->search($searchPattern);
     }
     return $objCommon;
 }
예제 #3
0
파일: LTorrent.php 프로젝트: maduhu/openbay
 protected function getSphinxSearchObject(SearchForm $model)
 {
     $objCommon = new ESphinxQL();
     $objCommon->addField('id')->addIndex(Yii::app()->params['sphinx']['indexes']['torrents']);
     // Keywords
     $searchPattern = '';
     if (!empty($model->words)) {
         if (is_array($model->words)) {
             $wordQuery = $model->words;
             foreach ($wordQuery as $key => $word) {
                 $wordQuery[$key] = $objCommon->halfEscapeMatch($word);
             }
             $exact = '"' . implode(' ', $wordQuery) . '"';
             $approximite = $exact . '/5';
             usort($wordQuery, function ($a, $b) {
                 $a = mb_strlen($a, 'utf-8');
                 $b = mb_strlen($b, 'utf-8');
                 if ($a === $b) {
                     return 0;
                 }
                 return $a > $b ? -1 : 1;
             });
             $tmp = array();
             $tmp[] = $exact;
             foreach ($wordQuery as $word) {
                 if (mb_strlen($word, 'utf-8') > 3) {
                     $tmp[] = $word;
                 }
             }
             $tmp[] = $approximite;
             $search = join(' | ', $tmp);
         } else {
             $search = $objCommon->halfEscapeMatch($model->words);
         }
         $searchPattern .= '@name ' . $search;
         $objCommon->option('ranker', 'expr(\'' . 'sum((4*lcs+2*(min_hit_pos==1)+exact_hit)*user_weight)*1000+bm25 + ' . '(created_at - 1000000000) / (NOW() - 1000000000) * 100 + if(torrent_status=1,100,0)\')');
     }
     // Tag
     if (!empty($model->tags)) {
         $searchPattern .= ' @tags "=' . $model->tags . '"';
     }
     $objCommon->order('weight()', 'DESC');
     $objCommon->order('id', 'DESC');
     if (!empty($searchPattern)) {
         $objCommon->search($searchPattern);
     }
     // Filter torrents by age
     if (!empty($model->age)) {
         $objCommon->where('created_at', time() - $model->age * 86400, '>=', true);
     }
     return $objCommon;
 }