diff --git a/Changelog b/Changelog index b12176107..81bbb0040 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2017-12-27 DariusIII + * Chg: Move more functions from Releases to appropriate models * Chg: Move updateGrab, getById functions from Releases class to Release model (rename getById to getCatByRelId) * Chg: Move createGUID function to helpers.php * Chg: Update laravel/framework and nikic/php-parser to latest versions diff --git a/app/Models/Category.php b/app/Models/Category.php index d1734f4ca..868d3f867 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -3,6 +3,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Cache; class Category extends Model { @@ -45,4 +47,30 @@ class Category extends Model { return $this->belongsTo(RoleExcludedCategory::class, 'categories_id'); } + + /** + * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] + */ + public static function getRecentlyAdded() + { + $recent = Cache::get('recentlyadded'); + if ($recent !== null) { + return $recent; + } + + $recent = self::query() + ->where('r.adddate', '>', Carbon::now()->subWeek()) + ->selectRaw('CONCAT(cp.title, " > ", categories.title) as title') + ->selectRaw('COUNT(r.id) as count') + ->join('categories as cp', 'cp.id', '=', 'categories.parentid') + ->join('releases as r', 'r.categories_id', '=', 'categories.id') + ->groupBy('title') + ->orderBy('count', 'desc') + ->get(); + + $expiresAt = Carbon::now()->addSeconds(NN_CACHE_EXPIRY_LONG); + Cache::put('recentlyadded', $recent, $expiresAt); + + return $recent; + } } diff --git a/app/Models/Release.php b/app/Models/Release.php index af800a62f..5e08c25c0 100644 --- a/app/Models/Release.php +++ b/app/Models/Release.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Support\Facades\Cache; use nntmux\SphinxSearch; use Illuminate\Support\Carbon; use Illuminate\Database\Eloquent\Model; @@ -209,4 +210,73 @@ class Release extends Model { return self::query()->where('id', $id)->first(['categories_id']); } + + /** + * @param $videoId + * @return int + */ + public static function removeVideoIdFromReleases($videoId): int + { + return self::query()->where('videos_id', $videoId)->update(['videos_id' => 0, 'tv_episodes_id' => 0]); + } + + /** + * @param $anidbID + * @return int + */ + public static function removeAnidbIdFromReleases($anidbID): int + { + return self::query()->where('anidbid', $anidbID)->update(['anidbid' => -1]); + } + + /** + * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] + */ + public static function getTopDownloads() + { + $releases = Cache::get('topdownloads'); + if ($releases !== null) { + return $releases; + } + + $releases = self::query() + ->where('grabs', '>', 0) + ->select(['id', 'searchname', 'guid', 'adddate']) + ->selectRaw('SUM(grabs) as grabs') + ->groupBy('id', 'searchname', 'adddate') + ->havingRaw('SUM(grabs) > 0') + ->orderBy('grabs', 'desc') + ->limit(10) + ->get(); + + $expiresAt = Carbon::now()->addSeconds(NN_CACHE_EXPIRY_LONG); + Cache::put('topdownloads', $releases, $expiresAt); + + return $releases; + } + + /** + * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] + */ + public static function getTopComments() + { + $comments = Cache::get('topcomments'); + if ($comments !== null) { + return $comments; + } + + $comments = self::query() + ->where('comments', '>', 0) + ->select(['id', 'guid', 'searchname']) + ->selectRaw('SUM(comments) AS comments') + ->groupBy('id', 'searchname', 'adddate') + ->havingRaw('SUM(comments) > 0') + ->orderBy('comments', 'desc') + ->limit(10) + ->get(); + $expiresAt = Carbon::now()->addSeconds(NN_CACHE_EXPIRY_LONG); + Cache::put('topcomments', $comments, $expiresAt); + + return $comments; + } } diff --git a/app/Models/ReleaseNfo.php b/app/Models/ReleaseNfo.php index 7e887b295..34f0288c1 100644 --- a/app/Models/ReleaseNfo.php +++ b/app/Models/ReleaseNfo.php @@ -27,4 +27,19 @@ class ReleaseNfo extends Model { return $this->belongsTo(Release::class, 'releases_id'); } + + /** + * @param $id + * @param bool $getNfoString + * @return \Illuminate\Database\Eloquent\Model|null|static + */ + public static function getReleaseNfo($id, $getNfoString = true) + { + $nfo = self::query()->where('releases_id', $id)->whereNotNull('nfo')->select(['releases_id']); + if ($getNfoString === true) { + $nfo->selectRaw('UNCOMPRESS(nfo) AS nfo'); + } + + return $nfo->first(); + } } diff --git a/nntmux/Releases.php b/nntmux/Releases.php index 020385450..0e614f58f 100755 --- a/nntmux/Releases.php +++ b/nntmux/Releases.php @@ -1313,116 +1313,6 @@ class Releases return $zipFile->file(); } - /** - * @param $videoId - * @return int - */ - public function removeVideoIdFromReleases($videoId): int - { - return Release::query()->where('videos_id', $videoId)->update(['videos_id' => 0, 'tv_episodes_id' => 0]); - } - - /** - * @param $anidbID - * @return int - */ - public function removeAnidbIdFromReleases($anidbID): int - { - return Release::query()->where('anidbid', $anidbID)->update(['anidbid' => -1]); - } - - /** - * @param $id - * @param bool $getNfoString - * @return \Illuminate\Database\Eloquent\Model|null|static - */ - public function getReleaseNfo($id, $getNfoString = true) - { - $nfo = ReleaseNfo::query()->where('releases_id', $id)->whereNotNull('nfo')->select(['releases_id']); - if ($getNfoString === true) { - $nfo->selectRaw('UNCOMPRESS(nfo) AS nfo'); - } - - return $nfo->first(); - } - - /** - * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] - */ - public function getTopDownloads() - { - $releases = Cache::get('topdownloads'); - if ($releases !== null) { - return $releases; - } - - $releases = Release::query() - ->where('grabs', '>', 0) - ->select(['id', 'searchname', 'guid', 'adddate']) - ->selectRaw('SUM(grabs) as grabs') - ->groupBy('id', 'searchname', 'adddate') - ->havingRaw('SUM(grabs) > 0') - ->orderBy('grabs', 'desc') - ->limit(10) - ->get(); - - $expiresAt = Carbon::now()->addSeconds(NN_CACHE_EXPIRY_LONG); - Cache::put('topdownloads', $releases, $expiresAt); - - return $releases; - } - - /** - * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] - */ - public function getTopComments() - { - $comments = Cache::get('topcomments'); - if ($comments !== null) { - return $comments; - } - - $comments = Release::query() - ->where('comments', '>', 0) - ->select(['id', 'guid', 'searchname']) - ->selectRaw('SUM(comments) AS comments') - ->groupBy('id', 'searchname', 'adddate') - ->havingRaw('SUM(comments) > 0') - ->orderBy('comments', 'desc') - ->limit(10) - ->get(); - $expiresAt = Carbon::now()->addSeconds(NN_CACHE_EXPIRY_LONG); - Cache::put('topcomments', $comments, $expiresAt); - - return $comments; - } - - /** - * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] - */ - public function getRecentlyAdded() - { - $recent = Cache::get('recentlyadded'); - if ($recent !== null) { - return $recent; - } - - $recent = CategoryModel::query() - ->where('r.adddate', '>', Carbon::now()->subWeek()) - ->selectRaw('CONCAT(cp.title, " > ", categories.title) as title') - ->selectRaw('COUNT(r.id) as count') - ->join('categories as cp', 'cp.id', '=', 'categories.parentid') - ->join('releases as r', 'r.categories_id', '=', 'categories.id') - ->groupBy('title') - ->orderBy('count', 'desc') - ->get(); - - $expiresAt = Carbon::now()->addSeconds(NN_CACHE_EXPIRY_LONG); - Cache::put('recentlyadded', $recent, $expiresAt); - - return $recent; - } - /** * Get all newest movies with coves for poster wall. * diff --git a/public/admin/anidb-remove.php b/public/admin/anidb-remove.php index cafa2e593..c62576528 100644 --- a/public/admin/anidb-remove.php +++ b/public/admin/anidb-remove.php @@ -2,15 +2,14 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; -use nntmux\Releases; +use App\Models\Release; $page = new AdminPage(); -$releases = new Releases(['Settings' => $page->pdo]); $success = false; if (isset($_GET['id'])) { - $success = $releases->removeAnidbIdFromReleases($_GET['id']); + $success = Release::removeAnidbIdFromReleases($_GET['id']); $page->smarty->assign('anidbid', $_GET['id']); } $page->smarty->assign('success', $success); diff --git a/public/admin/show-remove.php b/public/admin/show-remove.php index 55246c44b..be7cdbea8 100755 --- a/public/admin/show-remove.php +++ b/public/admin/show-remove.php @@ -2,15 +2,14 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; -use nntmux\Releases; +use App\Models\Release; $page = new AdminPage(); -$releases = new Releases(['Settings' => $page->pdo]); $success = false; if (isset($_GET['id'])) { - $success = $releases->removeVideoIdFromReleases($_GET['id']); + $success = Release::removeVideoIdFromReleases($_GET['id']); $page->smarty->assign('videoid', $_GET['id']); } diff --git a/public/admin/site-stats.php b/public/admin/site-stats.php index 88d3e0623..85f6ace85 100644 --- a/public/admin/site-stats.php +++ b/public/admin/site-stats.php @@ -2,25 +2,25 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; +use App\Models\Category; +use App\Models\Release; use App\Models\User; -use nntmux\Releases; use App\Models\UserRole; $page = new AdminPage(); -$releases = new Releases(); $page->title = 'Site Stats'; $topgrabs = User::getTopGrabbers(); $page->smarty->assign('topgrabs', $topgrabs); -$topdownloads = $releases->getTopDownloads(); +$topdownloads = Release::getTopDownloads(); $page->smarty->assign('topdownloads', $topdownloads); -$topcomments = $releases->getTopComments(); +$topcomments = Release::getTopComments(); $page->smarty->assign('topcomments', $topcomments); -$recent = $releases->getRecentlyAdded(); +$recent = Category::getRecentlyAdded(); $page->smarty->assign('recent', $recent); $usersbymonth = User::getUsersByMonth(); diff --git a/public/pages/api.php b/public/pages/api.php index adeb5c1f7..40660d79b 100644 --- a/public/pages/api.php +++ b/public/pages/api.php @@ -1,5 +1,6 @@ getByGuid($_GET['id']); - $data = $releases->getReleaseNfo($rel['id']); + $data = ReleaseNfo::getReleaseNfo($rel['id']); if ($rel !== false && ! empty($rel)) { if ($data !== false) { diff --git a/public/pages/details.php b/public/pages/details.php index c05116ede..332bc683b 100644 --- a/public/pages/details.php +++ b/public/pages/details.php @@ -1,5 +1,6 @@ addComment($data['id'], $data['gid'], $_POST['txtAddComment'], User::currentUserId(), $_SERVER['REMOTE_ADDR']); } - $nfo = $releases->getReleaseNfo($data['id']); + $nfo = ReleaseNfo::getReleaseNfo($data['id']); $reVideo = $re->getVideo($data['id']); $reAudio = $re->getAudio($data['id']); $reSubs = $re->getSubs($data['id']); diff --git a/public/pages/nfo.php b/public/pages/nfo.php index f5c752e50..976a7ad67 100644 --- a/public/pages/nfo.php +++ b/public/pages/nfo.php @@ -1,5 +1,6 @@ show404(); } - $nfo = $releases->getReleaseNfo($rel['id']); + $nfo = ReleaseNfo::getReleaseNfo($rel['id']); $nfo['nfoUTF'] = Utility::cp437toUTF($nfo['nfo']); $page->smarty->assign('rel', $rel);