|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author Robin Appelman <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @license GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
namespace OCA\Files_Versions\Versions; |
|
27
|
|
|
|
|
28
|
|
|
use OCP\Files\File; |
|
29
|
|
|
use OCP\Files\FileInfo; |
|
30
|
|
|
use OCP\Files\IRootFolder; |
|
31
|
|
|
use OCP\Files\Lock\ILock; |
|
32
|
|
|
use OCP\Files\Lock\ILockManager; |
|
33
|
|
|
use OCP\Files\Lock\LockContext; |
|
34
|
|
|
use OCP\Files\Storage\IStorage; |
|
35
|
|
|
use OCP\IUser; |
|
36
|
|
|
use OCP\Lock\ManuallyLockedException; |
|
37
|
|
|
|
|
38
|
|
|
class VersionManager implements IVersionManager, INameableVersionBackend, IDeletableVersionBackend { |
|
39
|
|
|
/** @var (IVersionBackend[])[] */ |
|
40
|
|
|
private $backends = []; |
|
41
|
|
|
|
|
42
|
|
|
public function registerBackend(string $storageType, IVersionBackend $backend) { |
|
43
|
|
|
if (!isset($this->backends[$storageType])) { |
|
44
|
|
|
$this->backends[$storageType] = []; |
|
45
|
|
|
} |
|
46
|
|
|
$this->backends[$storageType][] = $backend; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return (IVersionBackend[])[] |
|
51
|
|
|
*/ |
|
52
|
|
|
private function getBackends(): array { |
|
53
|
|
|
return $this->backends; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param IStorage $storage |
|
58
|
|
|
* @return IVersionBackend |
|
59
|
|
|
* @throws BackendNotFoundException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getBackendForStorage(IStorage $storage): IVersionBackend { |
|
62
|
|
|
$fullType = get_class($storage); |
|
63
|
|
|
$backends = $this->getBackends(); |
|
64
|
|
|
|
|
65
|
|
|
$foundType = ''; |
|
66
|
|
|
$foundBackend = null; |
|
67
|
|
|
|
|
68
|
|
|
foreach ($backends as $type => $backendsForType) { |
|
69
|
|
|
if ( |
|
70
|
|
|
$storage->instanceOfStorage($type) && |
|
71
|
|
|
($foundType === '' || is_subclass_of($type, $foundType)) |
|
72
|
|
|
) { |
|
73
|
|
|
foreach ($backendsForType as $backend) { |
|
74
|
|
|
/** @var IVersionBackend $backend */ |
|
75
|
|
|
if ($backend->useBackendForStorage($storage)) { |
|
76
|
|
|
$foundBackend = $backend; |
|
77
|
|
|
$foundType = $type; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ($foundType === '' || $foundBackend === null) { |
|
|
|
|
|
|
84
|
|
|
throw new BackendNotFoundException("Version backend for $fullType not found"); |
|
85
|
|
|
} else { |
|
86
|
|
|
return $foundBackend; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getVersionsForFile(IUser $user, FileInfo $file): array { |
|
91
|
|
|
$backend = $this->getBackendForStorage($file->getStorage()); |
|
92
|
|
|
return $backend->getVersionsForFile($user, $file); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function createVersion(IUser $user, FileInfo $file) { |
|
96
|
|
|
$backend = $this->getBackendForStorage($file->getStorage()); |
|
97
|
|
|
$backend->createVersion($user, $file); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function rollback(IVersion $version) { |
|
101
|
|
|
$backend = $version->getBackend(); |
|
102
|
|
|
$result = self::handleAppLocks(fn(): ?bool => $backend->rollback($version)); |
|
103
|
|
|
// rollback doesn't have a return type yet and some implementations don't return anything |
|
104
|
|
|
if ($result === null || $result === true) { |
|
105
|
|
|
\OC_Hook::emit('\OCP\Versions', 'rollback', [ |
|
106
|
|
|
'path' => $version->getVersionPath(), |
|
107
|
|
|
'revision' => $version->getRevisionId(), |
|
108
|
|
|
'node' => $version->getSourceFile(), |
|
109
|
|
|
]); |
|
110
|
|
|
} |
|
111
|
|
|
return $result; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function read(IVersion $version) { |
|
115
|
|
|
$backend = $version->getBackend(); |
|
116
|
|
|
return $backend->read($version); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getVersionFile(IUser $user, FileInfo $sourceFile, $revision): File { |
|
120
|
|
|
$backend = $this->getBackendForStorage($sourceFile->getStorage()); |
|
121
|
|
|
return $backend->getVersionFile($user, $sourceFile, $revision); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function useBackendForStorage(IStorage $storage): bool { |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function setVersionLabel(IVersion $version, string $label): void { |
|
129
|
|
|
$backend = $this->getBackendForStorage($version->getSourceFile()->getStorage()); |
|
130
|
|
|
if ($backend instanceof INameableVersionBackend) { |
|
131
|
|
|
$backend->setVersionLabel($version, $label); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function deleteVersion(IVersion $version): void { |
|
136
|
|
|
$backend = $this->getBackendForStorage($version->getSourceFile()->getStorage()); |
|
137
|
|
|
if ($backend instanceof IDeletableVersionBackend) { |
|
138
|
|
|
$backend->deleteVersion($version); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Catch ManuallyLockedException and retry in app context if possible. |
|
144
|
|
|
* |
|
145
|
|
|
* Allow users to go back to old versions via the versions tab in the sidebar |
|
146
|
|
|
* even when the file is opened in the viewer next to it. |
|
147
|
|
|
* |
|
148
|
|
|
* Context: If a file is currently opened for editing |
|
149
|
|
|
* the files_lock app will throw ManuallyLockedExceptions. |
|
150
|
|
|
* This prevented the user from rolling an opened file back to a previous version. |
|
151
|
|
|
* |
|
152
|
|
|
* Text and Richdocuments can handle changes of open files. |
|
153
|
|
|
* So we execute the rollback under their lock context |
|
154
|
|
|
* to let them handle the conflict. |
|
155
|
|
|
* |
|
156
|
|
|
* @param callable $callback function to run with app locks handled |
|
157
|
|
|
* @return bool|null |
|
158
|
|
|
* @throws ManuallyLockedException |
|
159
|
|
|
* |
|
160
|
|
|
*/ |
|
161
|
|
|
private static function handleAppLocks(callable $callback): ?bool { |
|
162
|
|
|
try { |
|
163
|
|
|
return $callback(); |
|
164
|
|
|
} catch (ManuallyLockedException $e) { |
|
165
|
|
|
$owner = (string) $e->getOwner(); |
|
166
|
|
|
$appsThatHandleUpdates = array("text", "richdocuments"); |
|
167
|
|
|
if (!in_array($owner, $appsThatHandleUpdates)) { |
|
168
|
|
|
throw $e; |
|
169
|
|
|
} |
|
170
|
|
|
// The LockWrapper in the files_lock app only compares the lock type and owner |
|
171
|
|
|
// when checking the lock against the current scope. |
|
172
|
|
|
// So we do not need to get the actual node here |
|
173
|
|
|
// and use the root node instead. |
|
174
|
|
|
$root = \OC::$server->get(IRootFolder::class); |
|
175
|
|
|
$lockContext = new LockContext($root, ILock::TYPE_APP, $owner); |
|
176
|
|
|
$lockManager = \OC::$server->get(ILockManager::class); |
|
177
|
|
|
$result = null; |
|
178
|
|
|
$lockManager->runInScope($lockContext, function() use ($callback, &$result) { |
|
179
|
|
|
$result = $callback(); |
|
180
|
|
|
}); |
|
181
|
|
|
return $result; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|