2015-09-08 12:56:38 +02:00
< ? php
2015-10-25 06:17:45 +01:00
/**
* Pony . fm - A community for pony fan music .
2021-02-14 03:39:15 +01:00
* Copyright ( C ) 2015 Feld0 .
2015-10-25 06:17:45 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
2021-02-14 03:34:58 +01:00
namespace App\Console\Commands ;
2015-09-08 12:56:38 +02:00
2021-02-14 03:34:58 +01:00
use App\Models\ResourceLogItem ;
2015-09-08 12:56:38 +02:00
use DB ;
use Illuminate\Console\Command ;
class RefreshCache extends Command
{
/**
* The name and signature of the console command .
*
* @ var string
*/
protected $signature = 'refresh-cache' ;
/**
* The console command description .
*
* @ var string
*/
protected $description = 'Refreshes cache tables for views and downloads' ;
/**
* Create a new command instance .
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Execute the console command .
*
* @ return mixed
*/
public function handle ()
{
DB :: connection () -> disableQueryLog ();
DB :: table ( 'tracks' ) -> update ([ 'comment_count' => DB :: raw ( '(SELECT COUNT(id) FROM comments WHERE comments.track_id = tracks.id AND deleted_at IS NULL)' )]);
DB :: table ( 'albums' ) -> update ([
'comment_count' => DB :: raw ( '(SELECT COUNT(id) FROM comments WHERE comments.album_id = albums.id AND deleted_at IS NULL)' ),
2021-02-14 03:39:15 +01:00
'track_count' => DB :: raw ( '(SELECT COUNT(id) FROM tracks WHERE album_id = albums.id)' ),
2015-09-08 12:56:38 +02:00
]);
DB :: table ( 'playlists' ) -> update ([
'comment_count' => DB :: raw ( '(SELECT COUNT(id) FROM comments WHERE comments.playlist_id = playlists.id AND deleted_at IS NULL)' ),
2021-02-14 03:39:15 +01:00
'track_count' => DB :: raw ( '(SELECT COUNT(id) FROM playlist_track WHERE playlist_id = playlists.id)' ),
2015-09-08 12:56:38 +02:00
]);
DB :: table ( 'users' ) -> update ([
'comment_count' => DB :: raw ( '(SELECT COUNT(id) FROM comments WHERE comments.profile_id = users.id AND deleted_at IS NULL)' ),
2021-02-14 03:39:15 +01:00
'track_count' => DB :: raw ( '(SELECT COUNT(id) FROM tracks WHERE deleted_at IS NULL AND published_at IS NOT NULL AND user_id = users.id)' ),
2015-09-08 12:56:38 +02:00
]);
$users = DB :: table ( 'users' ) -> get ();
$cacheItems = [];
$resources = [
'album' => [],
'playlist' => [],
2021-02-14 03:39:15 +01:00
'track' => [],
2015-09-08 12:56:38 +02:00
];
foreach ( $users as $user ) {
$cacheItems [ $user -> id ] = [
'album' => [],
'playlist' => [],
'track' => [],
];
}
$logItems = DB :: table ( 'resource_log_items' ) -> get ();
foreach ( $logItems as $item ) {
$type = '' ;
$id = 0 ;
if ( $item -> album_id ) {
$type = 'album' ;
$id = $item -> album_id ;
} else {
if ( $item -> playlist_id ) {
$type = 'playlist' ;
$id = $item -> playlist_id ;
} else {
if ( $item -> track_id ) {
$type = 'track' ;
$id = $item -> track_id ;
}
}
}
$resource = $this -> getCacheItem ( $resources , $type , $id );
if ( $item -> user_id != null ) {
$userResource = $this -> getUserCacheItem ( $cacheItems , $item -> user_id , $type , $id );
if ( $item -> log_type == ResourceLogItem :: DOWNLOAD ) {
$userResource [ 'download_count' ] ++ ;
} else {
if ( $item -> log_type == ResourceLogItem :: VIEW ) {
$userResource [ 'view_count' ] ++ ;
} else {
if ( $item -> log_type == ResourceLogItem :: PLAY ) {
$userResource [ 'play_count' ] ++ ;
}
}
}
$cacheItems [ $item -> user_id ][ $type ][ $id ] = $userResource ;
}
if ( $item -> log_type == ResourceLogItem :: DOWNLOAD ) {
$resource [ 'download_count' ] ++ ;
} else {
if ( $item -> log_type == ResourceLogItem :: VIEW ) {
$resource [ 'view_count' ] ++ ;
} else {
if ( $item -> log_type == ResourceLogItem :: PLAY ) {
$resource [ 'play_count' ] ++ ;
}
}
}
$resources [ $type ][ $id ] = $resource ;
}
$pins = DB :: table ( 'pinned_playlists' ) -> get ();
foreach ( $pins as $pin ) {
$userResource = $this -> getUserCacheItem ( $cacheItems , $pin -> user_id , 'playlist' , $pin -> playlist_id );
$userResource [ 'is_pinned' ] = true ;
$cacheItems [ $pin -> user_id ][ 'playlist' ][ $pin -> playlist_id ] = $userResource ;
}
$favs = DB :: table ( 'favourites' ) -> get ();
foreach ( $favs as $fav ) {
$type = '' ;
$id = 0 ;
if ( $fav -> album_id ) {
$type = 'album' ;
$id = $fav -> album_id ;
} else {
if ( $fav -> playlist_id ) {
$type = 'playlist' ;
$id = $fav -> playlist_id ;
} else {
if ( $fav -> track_id ) {
$type = 'track' ;
$id = $fav -> track_id ;
}
}
}
$userResource = $this -> getUserCacheItem ( $cacheItems , $fav -> user_id , $type , $id );
$userResource [ 'is_favourited' ] = true ;
$cacheItems [ $fav -> user_id ][ $type ][ $id ] = $userResource ;
$resource = $this -> getCacheItem ( $resources , $type , $id );
$resource [ 'favourite_count' ] ++ ;
$resources [ $type ][ $id ] = $resource ;
}
foreach ( DB :: table ( 'followers' ) -> get () as $follower ) {
$userResource = $this -> getUserCacheItem ( $cacheItems , $follower -> user_id , 'artist' , $follower -> artist_id );
$userResource [ 'is_followed' ] = true ;
$cacheItems [ $follower -> user_id ][ 'artist' ][ $follower -> artist_id ] = $userResource ;
}
foreach ( $resources as $name => $resourceArray ) {
foreach ( $resourceArray as $id => $resource ) {
2016-06-06 08:29:37 +02:00
DB :: table ( $name . 's' ) -> whereId ( $id ) -> update ( $resource );
2015-09-08 12:56:38 +02:00
}
}
DB :: table ( 'resource_users' ) -> delete ();
foreach ( $cacheItems as $cacheItem ) {
foreach ( $cacheItem as $resources ) {
foreach ( $resources as $resource ) {
DB :: table ( 'resource_users' ) -> insert ( $resource );
}
}
}
}
private function getCacheItem ( & $resources , $type , $id )
{
2021-02-14 03:39:15 +01:00
if ( ! isset ( $resources [ $type ][ $id ])) {
2015-09-08 12:56:38 +02:00
$item = [
'view_count' => 0 ,
'download_count' => 0 ,
'favourite_count' => 0 ,
];
if ( $type == 'track' ) {
$item [ 'play_count' ] = 0 ;
}
$resources [ $type ][ $id ] = $item ;
return $item ;
}
return $resources [ $type ][ $id ];
}
private function getUserCacheItem ( & $items , $userId , $type , $id )
{
2021-02-14 03:39:15 +01:00
if ( ! isset ( $items [ $userId ][ $type ][ $id ])) {
2015-09-08 12:56:38 +02:00
$item = [
'is_followed' => false ,
'is_favourited' => false ,
'is_pinned' => false ,
'view_count' => 0 ,
'play_count' => 0 ,
'download_count' => 0 ,
2021-02-14 03:39:15 +01:00
'user_id' => $userId ,
2015-09-08 12:56:38 +02:00
];
2016-06-06 08:29:37 +02:00
$item [ $type . '_id' ] = $id ;
2015-09-08 12:56:38 +02:00
$items [ $userId ][ $type ][ $id ] = $item ;
return $item ;
}
return $items [ $userId ][ $type ][ $id ];
}
}