From acb9f214146cecd1ea85250a929dece290d1e60b Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Thu, 13 Apr 2023 23:16:48 -0700 Subject: [PATCH] Exclude nomedia with CTE (fix #185) Signed-off-by: Varun Patil --- lib/Db/TimelineQueryCTE.php | 52 +++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/lib/Db/TimelineQueryCTE.php b/lib/Db/TimelineQueryCTE.php index ba95c415..df0e748c 100644 --- a/lib/Db/TimelineQueryCTE.php +++ b/lib/Db/TimelineQueryCTE.php @@ -8,28 +8,42 @@ trait TimelineQueryCTE { protected static function CTE_FOLDERS_ALL(bool $notArchive): string { - $extraJoinOn = $notArchive ? "AND f.name <> '.archive'" : ''; + // Whether to filter out the archive folder + $CLS_ARCHIVE_JOIN = $notArchive ? "f.name <> '.archive'" : '1 = 1'; + + // Filter out folder MIME types + $CLS_MIME_FOLDER = "f.mimetype = (SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = 'httpd/unix-directory')"; + + // Select filecache as f + $BASE_QUERY = 'SELECT f.fileid, f.name FROM *PREFIX*filecache f'; + + // From top folders + $CLS_TOP_FOLDER = 'f.fileid IN (:topFolderIds)'; + + // Select 1 if there is a .nomedia file in the folder + $SEL_NOMEDIA = "SELECT 1 FROM *PREFIX*filecache f2 WHERE f2.parent = f.fileid AND f2.name = '.nomedia'"; + + // Check no nomedia file exists in the folder + $CLS_NOMEDIA = "NOT EXISTS ({$SEL_NOMEDIA})"; return "*PREFIX*cte_folders_all(fileid, name) AS ( - SELECT - f.fileid, - f.name - FROM - *PREFIX*filecache f + {$BASE_QUERY} WHERE - f.fileid IN (:topFolderIds) + {$CLS_TOP_FOLDER} AND + {$CLS_NOMEDIA} + UNION ALL - SELECT - f.fileid, - f.name - FROM - *PREFIX*filecache f + + {$BASE_QUERY} INNER JOIN *PREFIX*cte_folders_all c - ON (f.parent = c.fileid - AND f.mimetype = (SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = 'httpd/unix-directory') - {$extraJoinOn} + ON ( + f.parent = c.fileid AND + {$CLS_MIME_FOLDER} AND + {$CLS_ARCHIVE_JOIN} ) + WHERE + {$CLS_NOMEDIA} )"; } @@ -51,13 +65,13 @@ trait TimelineQueryCTE /** CTE to get all archive folders recursively in the given top folders */ protected static function CTE_FOLDERS_ARCHIVE(): string { - $cte = '*PREFIX*cte_folders(fileid) AS ( + $cte = "*PREFIX*cte_folders(fileid) AS ( SELECT cfa.fileid FROM *PREFIX*cte_folders_all cfa WHERE - cfa.name = \'.archive\' + cfa.name = '.archive' GROUP BY cfa.fileid UNION ALL @@ -67,13 +81,13 @@ trait TimelineQueryCTE *PREFIX*filecache f INNER JOIN *PREFIX*cte_folders c ON (f.parent = c.fileid) - )'; + )"; return self::bundleCTEs([self::CTE_FOLDERS_ALL(false), $cte]); } protected static function bundleCTEs(array $ctes): string { - return 'WITH RECURSIVE ' . implode(',', $ctes); + return 'WITH RECURSIVE '.implode(',', $ctes); } }