/** * Browsing latest torrents by category * @return string the rendering result. */ public function actionIndex($q = null, $iht = null, $age = null) { if (is_null($q) && is_null($iht) && is_null($age)) { $this->redirect(Url::toRoute('/browse')); } if (!empty($iht)) { if (preg_match('#^\\d$#sui', $iht) && Category::getTag($iht)) { $iht = Category::getTag($iht); } } if (Yii::$app->request->getQueryParam('ihs', null) === '1') { $_GET['popular'] = '1'; } $searchModel = new SearchForm(); $searchModel->setScenario('simple'); $searchModel->words = $q; $searchModel->tags = $iht; $searchModel->age = Yii::$app->request->getQueryParam('age', 0); $searchModel->popular = empty($_GET['popular']) ? 0 : 1; $searchModel->status = empty($status) ? 0 : 1; $searchModel->validate(); $torrents = null; $totalCount = 0; if (!$searchModel->getErrors()) { $torrents = Search::getTorrentDataProvider($searchModel); $totalCount = $torrents->getTotalCount(); if (empty($totalCount)) { $words = explode(' ', $searchModel->words); if (count($words) > 1) { $searchModel->words = $words; $torrents = Search::getTorrentDataProvider($searchModel); $totalCount = $torrents->getTotalCount(); } } } else { $torrents = new ArrayDataProvider(); } if (isset($_REQUEST['lucky'])) { $t = $torrents->getModels(); if (count($t)) { $tmp = []; foreach ($t as $torrent) { $tmp[$torrent->seeders] = $torrent; } krsort($tmp); $torrent = array_shift($tmp); $this->redirect($torrent->getUrl()); \Yii::$app->end(); } } return $this->render('index', ['q' => $q, 'torrents' => $torrents, 'totalCount' => $totalCount, 'iht' => $iht]); }
public static function getLastTorrentsDataProvider($count = 5, $forceRefresh = false) { $cacheKey = 'browse_torrents'; $torrentsByTags = Yii::$app->cache->get($cacheKey); if (!$torrentsByTags) { $torrentsByTags = []; $torrentsIds = []; $tags = Category::$categoriesTags; foreach ($tags as $tag) { $query = new Query(); $rows = $query->from(static::indexName())->match(new Expression(':match', [':match' => '@tags ' . Yii::$app->sphinx->escapeMatchValue($tag)]))->where(['deleted' => 0])->orderBy(['weight()' => SORT_DESC, 'id' => SORT_DESC])->limit($count)->addOptions(['ranker' => 'SPH04', 'max_matches' => 5]); $torrents = array_map(function ($item) { return $item['id']; }, $rows->all()); $torrentsIds = array_merge($torrentsIds, $torrents); } if (empty($torrentsIds)) { Yii::log('Empty last torrents ids', CLogger::LEVEL_WARNING); } else { $query = (new Torrent())->find()->with('scrapes')->where(['id' => $torrentsIds]); foreach ($query->all() as $torrent) { $torrentsByTags[Category::getTorrentCategoryTag($torrent)][] = $torrent; } } Yii::$app->cache->set($cacheKey, $torrentsByTags, 600); } $result = array_map(function ($data) { return new ArrayDataProvider(['key' => 'id', 'allModels' => $data]); }, $torrentsByTags); return $result; }