2023-04-14 05:49:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
|
|
|
trait TimelineQueryCTE
|
|
|
|
{
|
2023-09-28 01:33:03 +00:00
|
|
|
/**
|
|
|
|
* CTE to get all files recursively in the given top folders
|
2023-09-28 01:57:29 +00:00
|
|
|
* :topFolderIds - The top folders to get files from.
|
2023-09-28 01:33:03 +00:00
|
|
|
*
|
2023-10-03 00:09:10 +00:00
|
|
|
* @param bool $hidden Whether to include files in hidden folders
|
|
|
|
* If the top folder is hidden, the files in it will still be returned
|
|
|
|
* Hidden files are marked as such in the "hidden" field
|
2023-09-28 01:33:03 +00:00
|
|
|
*/
|
2023-10-03 00:09:10 +00:00
|
|
|
protected static function CTE_FOLDERS_ALL(bool $hidden): string
|
2023-04-14 05:49:12 +00:00
|
|
|
{
|
2023-04-14 06:16:48 +00:00
|
|
|
// Filter out folder MIME types
|
2023-09-28 01:43:14 +00:00
|
|
|
$FOLDER_MIME_QUERY = "SELECT MAX(id) FROM *PREFIX*mimetypes WHERE mimetype = 'httpd/unix-directory'";
|
2023-04-14 06:16:48 +00:00
|
|
|
|
|
|
|
// Select 1 if there is a .nomedia file in the folder
|
2023-08-20 16:42:04 +00:00
|
|
|
$SEL_NOMEDIA = "SELECT 1 FROM *PREFIX*filecache f2
|
|
|
|
WHERE (f2.parent = f.fileid)
|
|
|
|
AND (f2.name = '.nomedia' OR f2.name = '.nomemories')";
|
2023-04-14 06:16:48 +00:00
|
|
|
|
|
|
|
// Check no nomedia file exists in the folder
|
|
|
|
$CLS_NOMEDIA = "NOT EXISTS ({$SEL_NOMEDIA})";
|
2023-04-14 05:49:12 +00:00
|
|
|
|
2023-10-03 00:09:10 +00:00
|
|
|
// Whether to filter out hidden folders
|
|
|
|
$CLS_HIDDEN_JOIN = $hidden ? '1 = 1' : "f.name NOT LIKE '.%'";
|
|
|
|
|
2023-04-14 05:49:12 +00:00
|
|
|
return
|
2023-10-03 00:09:10 +00:00
|
|
|
"*PREFIX*cte_folders_all(fileid, name, hidden) AS (
|
|
|
|
SELECT f.fileid, f.name,
|
|
|
|
(0) AS hidden
|
|
|
|
FROM *PREFIX*filecache f
|
2023-04-14 06:20:23 +00:00
|
|
|
WHERE (
|
2023-10-03 00:09:10 +00:00
|
|
|
f.fileid IN (:topFolderIds) AND
|
2023-04-14 06:16:48 +00:00
|
|
|
{$CLS_NOMEDIA}
|
2023-04-14 06:20:23 +00:00
|
|
|
)
|
2023-04-14 06:16:48 +00:00
|
|
|
|
2023-04-14 05:49:12 +00:00
|
|
|
UNION ALL
|
2023-04-14 06:16:48 +00:00
|
|
|
|
2023-10-03 00:09:10 +00:00
|
|
|
SELECT f.fileid, f.name,
|
|
|
|
(CASE WHEN c.hidden = 1 OR f.name LIKE '.%' THEN 1 ELSE 0 END) AS hidden
|
|
|
|
FROM *PREFIX*filecache f
|
2023-04-14 05:49:12 +00:00
|
|
|
INNER JOIN *PREFIX*cte_folders_all c
|
2023-04-14 06:16:48 +00:00
|
|
|
ON (
|
|
|
|
f.parent = c.fileid AND
|
2023-09-28 01:43:14 +00:00
|
|
|
f.mimetype = ({$FOLDER_MIME_QUERY}) AND
|
2023-10-03 00:09:10 +00:00
|
|
|
({$CLS_HIDDEN_JOIN})
|
2023-04-14 05:49:12 +00:00
|
|
|
)
|
2023-04-14 06:20:23 +00:00
|
|
|
WHERE (
|
2023-04-14 06:16:48 +00:00
|
|
|
{$CLS_NOMEDIA}
|
2023-04-14 06:20:23 +00:00
|
|
|
)
|
2023-04-14 05:49:12 +00:00
|
|
|
)";
|
|
|
|
}
|
|
|
|
|
2023-10-03 00:09:10 +00:00
|
|
|
/**
|
|
|
|
* CTE to get all folders recursively in the given top folders.
|
|
|
|
*
|
|
|
|
* @param bool $hidden Whether to include files in hidden folders
|
|
|
|
*/
|
|
|
|
protected static function CTE_FOLDERS(bool $hidden): string
|
2023-04-14 05:49:12 +00:00
|
|
|
{
|
2023-10-03 00:09:10 +00:00
|
|
|
$CLS_HIDDEN = $hidden ? 'MIN(hidden)' : '0';
|
|
|
|
|
|
|
|
$cte = "*PREFIX*cte_folders AS (
|
2023-04-14 05:49:12 +00:00
|
|
|
SELECT
|
2023-10-03 00:09:10 +00:00
|
|
|
fileid, ({$CLS_HIDDEN}) AS hidden
|
2023-04-14 05:49:12 +00:00
|
|
|
FROM
|
|
|
|
*PREFIX*cte_folders_all
|
|
|
|
GROUP BY
|
|
|
|
fileid
|
2023-10-03 00:09:10 +00:00
|
|
|
)";
|
2023-04-14 05:49:12 +00:00
|
|
|
|
2023-10-03 00:09:10 +00:00
|
|
|
return self::bundleCTEs([self::CTE_FOLDERS_ALL($hidden), $cte]);
|
2023-04-14 05:49:12 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
/**
|
|
|
|
* CTE to get all archive folders recursively in the given top folders.
|
|
|
|
*/
|
2023-04-14 05:49:12 +00:00
|
|
|
protected static function CTE_FOLDERS_ARCHIVE(): string
|
|
|
|
{
|
2023-04-14 06:16:48 +00:00
|
|
|
$cte = "*PREFIX*cte_folders(fileid) AS (
|
2023-04-14 05:49:12 +00:00
|
|
|
SELECT
|
|
|
|
cfa.fileid
|
|
|
|
FROM
|
|
|
|
*PREFIX*cte_folders_all cfa
|
|
|
|
WHERE
|
2023-04-14 06:16:48 +00:00
|
|
|
cfa.name = '.archive'
|
2023-04-14 05:49:12 +00:00
|
|
|
GROUP BY
|
|
|
|
cfa.fileid
|
|
|
|
UNION ALL
|
|
|
|
SELECT
|
|
|
|
f.fileid
|
|
|
|
FROM
|
|
|
|
*PREFIX*filecache f
|
|
|
|
INNER JOIN *PREFIX*cte_folders c
|
|
|
|
ON (f.parent = c.fileid)
|
2023-04-14 06:16:48 +00:00
|
|
|
)";
|
2023-04-14 05:49:12 +00:00
|
|
|
|
2023-10-03 00:09:10 +00:00
|
|
|
return self::bundleCTEs([self::CTE_FOLDERS_ALL(true), $cte]);
|
2023-04-14 05:49:12 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $ctes The CTEs to bundle
|
|
|
|
*/
|
2023-04-14 05:49:12 +00:00
|
|
|
protected static function bundleCTEs(array $ctes): string
|
|
|
|
{
|
2023-04-14 06:16:48 +00:00
|
|
|
return 'WITH RECURSIVE '.implode(',', $ctes);
|
2023-04-14 05:49:12 +00:00
|
|
|
}
|
|
|
|
}
|