VersionManager.php 6.6 KB

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