Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use query builder instead of OC_DB in trashbin #23936

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 56 additions & 21 deletions apps/files_trashbin/lib/Trashbin.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,20 @@ public static function getUidAndFilename($filename) {
* @return array (filename => array (timestamp => original location))
*/
public static function getLocations($user) {
$query = \OC_DB::prepare('SELECT `id`, `timestamp`, `location`'
. ' FROM `*PREFIX*files_trash` WHERE `user`=?');
$result = $query->execute([$user]);
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->select('id', 'timestamp', 'location')
->from('files_trash')
->where($query->expr()->eq('user', $query->createNamedParameter($user)));
$result = $query->execute();
$array = [];
while ($row = $result->fetchRow()) {
while ($row = $result->fetch()) {
if (isset($array[$row['id']])) {
$array[$row['id']][$row['timestamp']] = $row['location'];
} else {
$array[$row['id']] = [$row['timestamp'] => $row['location']];
}
}
$result->closeCursor();
return $array;
}

Expand All @@ -151,11 +154,19 @@ public static function getLocations($user) {
* @return string original location
*/
public static function getLocation($user, $filename, $timestamp) {
$query = \OC_DB::prepare('SELECT `location` FROM `*PREFIX*files_trash`'
. ' WHERE `user`=? AND `id`=? AND `timestamp`=?');
$result = $query->execute([$user, $filename, $timestamp])->fetchAll();
if (isset($result[0]['location'])) {
return $result[0]['location'];
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->select('location')
->from('files_trash')
->where($query->expr()->eq('user', $query->createNamedParameter($user)))
->andWhere($query->expr()->eq('id', $query->createNamedParameter($filename)))
->andWhere($query->expr()->eq('timestamp', $query->createNamedParameter($timestamp)));

$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();

if (isset($row['location'])) {
return $row['location'];
} else {
return false;
}
Expand Down Expand Up @@ -208,8 +219,13 @@ private static function copyFilesToUser($sourcePath, $owner, $targetPath, $user,


if ($view->file_exists($target)) {
$query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)");
$result = $query->execute([$targetFilename, $timestamp, $targetLocation, $user]);
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->insert('files_trash')
->setValue('id', $query->createNamedParameter($targetFilename))
->setValue('timestamp', $query->createNamedParameter($timestamp))
->setValue('location', $query->createNamedParameter($targetLocation))
->setValue('user', $query->createNamedParameter($user));
$result = $query->execute();
if (!$result) {
\OC::$server->getLogger()->error('trash bin database couldn\'t be updated for the files owner', ['app' => 'files_trashbin']);
}
Expand Down Expand Up @@ -330,8 +346,13 @@ public static function move2trash($file_path, $ownerOnly = false) {
}

if ($moveSuccessful) {
$query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)");
$result = $query->execute([$filename, $timestamp, $location, $owner]);
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->insert('files_trash')
->setValue('id', $query->createNamedParameter($filename))
->setValue('timestamp', $query->createNamedParameter($timestamp))
->setValue('location', $query->createNamedParameter($location))
->setValue('user', $query->createNamedParameter($owner));
$result = $query->execute();
if (!$result) {
\OC::$server->getLogger()->error('trash bin database couldn\'t be updated', ['app' => 'files_trashbin']);
}
Expand Down Expand Up @@ -489,8 +510,12 @@ public static function restore($file, $filename, $timestamp) {
self::restoreVersions($view, $file, $filename, $uniqueFilename, $location, $timestamp);

if ($timestamp) {
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=? AND `id`=? AND `timestamp`=?');
$query->execute([$user, $filename, $timestamp]);
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('files_trash')
->where($query->expr()->eq('user', $query->createNamedParameter($user)))
->andWhere($query->expr()->eq('id', $query->createNamedParameter($filename)))
->andWhere($query->expr()->eq('timestamp', $query->createNamedParameter($timestamp)));
$query->execute();
}

return true;
Expand Down Expand Up @@ -576,8 +601,11 @@ public static function deleteAll() {

// actual file deletion
$trash->delete();
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=?');
$query->execute([$user]);

$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('files_trash')
->where($query->expr()->eq('user', $query->createNamedParameter($user)));
$query->execute();

// Bulk PostDelete-Hook
\OC_Hook::emit('\OCP\Trashbin', 'deleteAll', ['paths' => $filePaths]);
Expand Down Expand Up @@ -626,8 +654,13 @@ public static function delete($filename, $user, $timestamp = null) {
$size = 0;

if ($timestamp) {
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=? AND `id`=? AND `timestamp`=?');
$query->execute([$user, $filename, $timestamp]);
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('files_trash')
->where($query->expr()->eq('user', $query->createNamedParameter($user)))
->andWhere($query->expr()->eq('id', $query->createNamedParameter($filename)))
->andWhere($query->expr()->eq('timestamp', $query->createNamedParameter($timestamp)));
$query->execute();

$file = $filename . '.d' . $timestamp;
} else {
$file = $filename;
Expand Down Expand Up @@ -709,8 +742,10 @@ public static function file_exists($filename, $timestamp = null) {
* @return bool result of db delete operation
*/
public static function deleteUser($uid) {
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=?');
return $query->execute([$uid]);
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('files_trash')
->where($query->expr()->eq('user', $query->createNamedParameter($uid)));
return (bool) $query->execute();
}

/**
Expand Down