prepare.php
<?php
$sql_host = '127.0.0.1';
$sql_port = 5432;
$sql_database = 'nextcloud';
$sql_user = 'nextcloud';
$sql_password = 'nextcloud';
$PDO = new PDO( "pgsql:host={$sql_host};port={$sql_port};dbname={$sql_database};user={$sql_user};password={$sql_password}" );
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$PDO->beginTransaction();
// 1. Empty trashes
$PDO->exec( '
TRUNCATE oc_files_trash
' );
// 2. Delete trash and versionned files
$PDO->exec( "
DELETE FROM oc_filecache
WHERE path like 'files_trashbin/files/%'
OR path like 'files_trashbin/keys/%'
OR path like 'files_trashbin/versions/%'
OR path like 'files_versions/%'
" );
$PDO->exec( "
DELETE FROM oc_filecache_extended
WHERE fileid NOT IN (SELECT fileid FROM oc_filecache)
" );
// 3. Check data directory
$PDOStatement = $PDO->query( "
SELECT id
FROM oc_storages
WHERE id like 'local::%'
" );
if ( false !== $id = $PDOStatement->fetchColumn() ) {
$root_path = substr( $id, 7 );
echo "ROOT: $root_path\n";
if ( ! is_writable( $root_path ) ) {
die( "Unable to write in {$root_path}" );
}
}
// 4. Create user directories
$PDOStatement = $PDO->query( "
SELECT m.root_id,
m.mount_point
FROM oc_mounts m
LEFT outer join oc_share s
ON s.file_source = m.root_id
WHERE s.file_source IS NULL
" );
$PDOStatementFiles = $PDO->prepare( "
WITH RECURSIVE files AS (
SELECT p.fileid,
p.path,
p.storage_mtime,
p.parent,
p.mimetype
FROM oc_filecache p
WHERE p.fileid = :root
UNION
SELECT c.fileid,
c.path,
c.storage_mtime,
c.parent,
c.mimetype
FROM oc_filecache c
INNER JOIN files f
ON c.parent = f.fileid
)
SELECT f.path, m.mimetype, f.storage_mtime
FROM files f
INNER JOIN oc_mimetypes m
ON m.id = f.mimetype;
" );
while ( false !== $mount_data = $PDOStatement->fetch(PDO::FETCH_ASSOC) ) {
$PDOStatementFiles->execute( [ 'root' => $mount_data['root_id'] ] );
while ( false !== $file_data = $PDOStatementFiles->fetch( PDO::FETCH_ASSOC ) ) {
if ( ! file_exists( $fullpath = "{$root_path}{$mount_data['mount_point']}{$file_data['path']}" ) ) {
if ( $file_data['mimetype'] == 'httpd/unix-directory' ) {
echo "mkdir $fullpath\n";
mkdir( $fullpath, 0755 );
touch( $fullpath, $file_data['storage_mtime'] );
} else {
echo "touch $fullpath\n";
touch( $fullpath, $file_data['storage_mtime'] );
chmod( $fullpath, 0644 );
}
}
}
}
$PDO->exec('
CREATE TABLE oc_share_temp(
id BIGINT,
permissions SMALLINT
);
');
$PDO->exec('
INSERT INTO oc_share_temp(id, permissions)
SELECT id, permissions from oc_share
');
$PDO->exec('
UPDATE oc_share
SET permissions = permissions | 2
');
$PDO->commit();