예제 #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;
 }