VersionManager.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 {
  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. /**
  112. * Catch ManuallyLockedException and retry in app context if possible.
  113. *
  114. * Allow users to go back to old versions via the versions tab in the sidebar
  115. * even when the file is opened in the viewer next to it.
  116. *
  117. * Context: If a file is currently opened for editing
  118. * the files_lock app will throw ManuallyLockedExceptions.
  119. * This prevented the user from rolling an opened file back to a previous version.
  120. *
  121. * Text and Richdocuments can handle changes of open files.
  122. * So we execute the rollback under their lock context
  123. * to let them handle the conflict.
  124. *
  125. * @param callable $callback function to run with app locks handled
  126. * @return bool|null
  127. * @throws ManuallyLockedException
  128. *
  129. */
  130. private static function handleAppLocks(callable $callback): ?bool {
  131. try {
  132. return $callback();
  133. } catch (ManuallyLockedException $e) {
  134. $owner = (string) $e->getOwner();
  135. $appsThatHandleUpdates = array("text", "richdocuments");
  136. if (!in_array($owner, $appsThatHandleUpdates)) {
  137. throw $e;
  138. }
  139. // The LockWrapper in the files_lock app only compares the lock type and owner
  140. // when checking the lock against the current scope.
  141. // So we do not need to get the actual node here
  142. // and use the root node instead.
  143. $root = \OC::$server->get(IRootFolder::class);
  144. $lockContext = new LockContext($root, ILock::TYPE_APP, $owner);
  145. $lockManager = \OC::$server->get(ILockManager::class);
  146. $result = null;
  147. $lockManager->runInScope($lockContext, function() use ($callback, &$result) {
  148. $result = $callback();
  149. });
  150. return $result;
  151. }
  152. }
  153. }