/** * 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; }
/** * 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; }
public static function getLastTorrentIdsByCategories($count = 5, $forceRefresh = false) { $key = 'tags_last_torrents_ids'; $torrentsIds = Yii::app()->cache->get($key); try { if (empty($torrentsIds) || $forceRefresh) { $torrentsIds = array(); $tags = LCategory::$categoriesTags; foreach ($tags as $tag) { $obj = new ESphinxQL(); $obj->addField('id')->addIndex(Yii::app()->params['sphinx']['indexes']['torrents'])->option('ranker', 'SPH04')->search('@tags "' . $obj->halfEscapeMatch($tag) . '"')->where('torrent_status', LTorrent::TORRENT_STATUS_GOOD)->order('weight()', 'DESC')->order('id', 'DESC')->limit($count); $torrentsIds = array_merge($torrentsIds, Yii::app()->sphinx->cache(600)->createCommand($obj->build())->queryColumn()); } Yii::app()->cache->set($key, $torrentsIds); } } catch (Exception $e) { Yii::log('getLastTorrentIdsByCategories failed. Exception: ' . $e->getMessage() . PHP_EOL . 'Trace: ' . $e->getTrace(), CLogger::LEVEL_ERROR); if (YII_DEBUG) { throw $e; } } return $torrentsIds; }