Skip to content

Commit

Permalink
Update playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neto committed Sep 13, 2024
1 parent b7988d7 commit 58a1d02
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 186 deletions.
5 changes: 3 additions & 2 deletions objects/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ public function deleteCache($clearFirstPageCache = false, $schedule = true)
AVideoPlugin::loadPlugin('Cache');
}
if (class_exists('CachesInDB')) {
//_error_log("deleteCache CachesInDB prefix=$prefix");
//_error_log("deleteCache CachesInDB prefix=$prefix line=".__LINE__);
CacheDB::deleteCacheStartingWith($prefix, $schedule);
}
TimeLogEnd($timeLog, __LINE__);
Expand All @@ -1226,10 +1226,11 @@ public function deleteCache($clearFirstPageCache = false, $schedule = true)

TimeLogEnd($timeLog, __LINE__);

//_error_log("deleteCache prefix=$prefix line=".__LINE__);
return true;
} else {
$resp = execAsync("rm -R {$dir}");
//_error_log("deleteCache schedule ".json_encode(debug_backtrace()));
_error_log("deleteCache not schedule {$dir} ".json_encode(debug_backtrace()));
return false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion objects/functionsFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@ function getTmpDir($subdir = "")
}
if (empty($_SESSION['getTmpDir'][$subdir . "_"])) {
if (empty($global['tmpDir'])) {
$tmpDir = sys_get_temp_dir();
// disabled it because command line and web were generating different caches
//$tmpDir = sys_get_temp_dir();
if (empty($tmpDir) || !_isWritable($tmpDir)) {
$obj = AVideoPlugin::getDataObjectIfEnabled('Cache');
$tmpDir = $obj->cacheDir;
Expand Down
1 change: 1 addition & 0 deletions objects/playListAddVideo.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
$obj->users_id = $playList->getUsers_id();
$obj->id = $playList->getId();
$obj->error = empty($obj->status);
$obj->type = $playList->getStatus();

//log_error("videos id: ".$_REQUEST['videos_id']." playlist_id: ".$_REQUEST['playlists_id']);
die(json_encode($obj));
68 changes: 46 additions & 22 deletions objects/playlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ public static function getAllFromPlaylistsID($playlists_id)
return $videosP;
}

public static function getUserSpecialPlaylist($users_id, $type){
public static function getUserSpecialPlaylist($users_id, $type)
{

$sql = "SELECT pl.* FROM " . static::getTableName() . " pl WHERE users_id = ? ";
if($type=='favorite'){
if ($type == 'favorite') {
$sql .= " AND status = 'favorite' ";
}else{
} else {
$sql .= " AND status = 'watch_later' ";
}


$res = sqlDAL::readSql($sql, 'i', [$users_id], true);
$row = sqlDAL::fetchAssoc($res);
sqlDAL::close($res);
return $row;

}

/**
Expand All @@ -144,7 +144,7 @@ public static function getUserSpecialPlaylist($users_id, $type){
*/
public static function getAllFromUser($userId, $publicOnly = true, $status = false, $playlists_id = 0, $try = 0, $includeSeries = false)
{
global $global, $config, $refreshCacheFromPlaylist;
global $global, $config, $refreshCacheFromPlaylist, $playListGetAllFromUserWasCache;
$playlists_id = intval($playlists_id);
$formats = '';
$values = [];
Expand Down Expand Up @@ -196,8 +196,15 @@ public static function getAllFromUser($userId, $publicOnly = true, $status = fal
$cacheName = md5($sql . json_encode($values));
$cacheHandler = new PlayListUserCacheHandler($userId);
//playlist cache
$rows = $cacheHandler->getCache($cacheName, rand(300, 3600));
$rows = array();
if (empty($_REQUEST['clearPlaylistCache'])) {
$rows = $cacheHandler->getCache($cacheName, 0);
} else {
$cacheHandler->setSuffix($cacheName);
}
$playListGetAllFromUserWasCache = false;
if (!empty($rows)) {
$playListGetAllFromUserWasCache = true;
//_error_log("playlist getAllFromUser($userId) cache ");
$rows = object_to_array($rows);
//$rows['return'] = __LINE__;
Expand All @@ -215,7 +222,7 @@ public static function getAllFromUser($userId, $publicOnly = true, $status = fal
$favoriteCount = 0;
$watch_laterCount = 0;
if ($res !== false) {
$TimeLog2 = "playList getAllFromUser foreach ($userId) total=".count($fullData);
$TimeLog2 = "playList getAllFromUser foreach ($userId) total=" . count($fullData);
TimeLogEnd($TimeLog2, __LINE__);
foreach ($fullData as $row) {
//$row = cleanUpRowFromDatabase($row);
Expand Down Expand Up @@ -476,13 +483,13 @@ public static function getAllFromUserVideo($userId, $videos_id, $publicOnly = tr
$rows = $cacheHandler->getCache($cacheName, 0);
if (empty($rows)) {
$rows = self::getAllFromUser($userId, $publicOnly, $status);
TimeLogEnd($TimeLog1, __LINE__);
TimeLogEnd($TimeLog1, __LINE__, 0.05);
foreach ($rows as $key => $value) {
$rows[$key]['name_translated'] = __($rows[$key]['name']);
$videos = self::getVideosIdFromPlaylist($value['id']);
$rows[$key]['isOnPlaylist'] = in_array($videos_id, $videos);
}
TimeLogEnd($TimeLog1, __LINE__);
TimeLogEnd($TimeLog1, __LINE__, 0.05);
$cacheHandler->setCache($rows);
} else {
$rows = object_to_array($rows);
Expand All @@ -492,7 +499,7 @@ public static function getAllFromUserVideo($userId, $videos_id, $publicOnly = tr
return $rows;
}

static function removeCache($videos_id, $schedule=true)
static function removeCache($videos_id, $schedule = true)
{

$cacheHandler = new VideoCacheHandler($videos_id);
Expand Down Expand Up @@ -522,7 +529,8 @@ public static function getVideosIndexFromPlaylistLight($playlists_id, $videos_id
return 0;
}

private static function getOrderBy($prefix=''){
private static function getOrderBy($prefix = '')
{
$order = " ORDER BY {$prefix}`order` ASC";

//if add in the top
Expand All @@ -548,7 +556,7 @@ public static function getVideosIDFromPlaylistLight($playlists_id)
$formats = 's';
$values = [Video::$statusActive];
} else {
$sql = "SELECT * FROM playlists_has_videos p WHERE playlists_id = ? ".self::getOrderBy('p.');
$sql = "SELECT * FROM playlists_has_videos p WHERE playlists_id = ? " . self::getOrderBy('p.');
$formats = 'i';
$values = [$playlists_id];
}
Expand All @@ -575,6 +583,7 @@ public static function getVideosIDFromPlaylistLight($playlists_id)

public static function getVideosFromPlaylist($playlists_id, $getExtraInfo = true, $forceRecreateCache = false)
{
global $getVideosFromPlaylistWasCache;
//_error_log("playlist::getVideosFromPlaylist($playlists_id)");
$sql = "SELECT v.*, p.*,v.created as cre, p.`order` as video_order "
//. ", (SELECT count(id) FROM likes as l where l.videos_id = v.id AND `like` = 1 ) as likes "
Expand All @@ -593,12 +602,13 @@ public static function getVideosFromPlaylist($playlists_id, $getExtraInfo = true
//playlist cache
$cacheHandler = new PlayListCacheHandler($playlists_id);
$suffix = md5($sql);
if(empty($forceRecreateCache)){
if (empty($forceRecreateCache) && empty($_REQUEST['clearPlaylistCache'])) {
$cacheObj = $cacheHandler->getCache($suffix, 0);
}else{
} else {
$cacheHandler->setSuffix($suffix);
}
$rows = object_to_array($cacheObj);
$getVideosFromPlaylistWasCache = false;
if (empty($rows)) {
global $global;
$tolerance = 0.1;
Expand All @@ -610,6 +620,17 @@ public static function getVideosFromPlaylist($playlists_id, $getExtraInfo = true
$SubtitleSwitcher = AVideoPlugin::loadPluginIfEnabled("SubtitleSwitcher");
if ($res !== false) {
foreach ($fullData as $row) {
$cacheHandlerVideo = new PlayListCacheHandler($playlists_id);
$suffixVideo = "Video{$row['id']}";
if (empty($forceRecreateCache)) {
$cacheObjVideo = $cacheHandler->getCache($suffixVideo, 0);
} else {
$cacheHandlerVideo->setSuffix($suffixVideo);
}
if (!empty($cacheObjVideo)) {
$rows[] = object_to_array($cacheObj);
continue;
}
$row = cleanUpRowFromDatabase($row);
$timeName2 = TimeLogStart("getVideosFromPlaylist foreach {$row['id']} {$row['filename']}");
$images = Video::getImageFromFilename($row['filename'], $row['type']);
Expand Down Expand Up @@ -658,6 +679,8 @@ public static function getVideosFromPlaylist($playlists_id, $getExtraInfo = true
}
$row['id'] = $row['videos_id'];
$rows[] = $row;

$cacheHandlerVideo->setCache($row);
}

$cacheHandler->setCache($rows);
Expand All @@ -666,7 +689,8 @@ public static function getVideosFromPlaylist($playlists_id, $getExtraInfo = true
$rows = [];
}
TimeLogEnd($timeName1, __LINE__, 0.5);
}else{
} else {
$getVideosFromPlaylistWasCache = true;
//_error_log("playlist getVideosFromPlaylist($playlists_id) cache ");
}
return $rows;
Expand Down Expand Up @@ -960,7 +984,7 @@ public function save()
return false;
}

_error_log('Playlist::save '.json_encode(debug_backtrace()));
_error_log('Playlist::save ' . json_encode(debug_backtrace()));

$this->clearEmptyLists();
if (empty($this->getUsers_id()) || !PlayLists::canManageAllPlaylists()) {
Expand Down Expand Up @@ -1028,11 +1052,11 @@ public function addVideo($videos_id, $add, $order = 0, $_deleteCache = true)
return false;
}

if(!_empty($add)){
if (!_empty($add)) {
//make sure it is not a serie and you are not adding into itself
$vid = Video::getVideoLight($videos_id, true);
if(!empty($vid['serie_playlists_id'])){
if($vid['serie_playlists_id'] == $this->id){
if (!empty($vid['serie_playlists_id'])) {
if ($vid['serie_playlists_id'] == $this->id) {
return false;
}
}
Expand Down Expand Up @@ -1064,14 +1088,14 @@ public function addVideo($videos_id, $add, $order = 0, $_deleteCache = true)
self::deleteCacheDir($this->id, true);
//_error_log('playlistSort addVideo line=' . __LINE__);
self::removeCache($videos_id);
}else{
} else {
execAsync("php {$global['systemRootPath']}objects/deletePlaylistCache.php {$this->id} {$videos_id}");
}
//_error_log('playlistSort addVideo line=' . __LINE__);
return $result;
}

static function deleteCacheDir($playlists_id, $schedule=false)
static function deleteCacheDir($playlists_id, $schedule = false)
{
$cacheHandler = new PlayListCacheHandler($playlists_id);
$cacheHandler->deleteCache(false, $schedule);
Expand Down
10 changes: 9 additions & 1 deletion objects/playlists.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@
unset($row[$key]['videos'][$key2]['description']);
}
}

$obj = new stdClass();
$obj->error = false;
$obj->msg = '';
$obj->rows = $row;
$obj->playListGetAllFromUserWasCache = !empty($playListGetAllFromUserWasCache);
$obj->getVideosFromPlaylistWasCache = !empty($getVideosFromPlaylistWasCache);

//mysqlCommit();
echo json_encode($row);
echo json_encode($obj);
Loading

0 comments on commit 58a1d02

Please sign in to comment.