VersionManager.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\NotFoundException;
  32. use OCP\Files\Storage\IStorage;
  33. use OCP\IUser;
  34. use OCP\Lock\ManuallyLockedException;
  35. class VersionManager implements IVersionManager, INameableVersionBackend, IDeletableVersionBackend, INeedSyncVersionBackend {
  36. /** @var (IVersionBackend[])[] */
  37. private $backends = [];
  38. public function registerBackend(string $storageType, IVersionBackend $backend) {
  39. if (!isset($this->backends[$storageType])) {
  40. $this->backends[$storageType] = [];
  41. }
  42. $this->backends[$storageType][] = $backend;
  43. }
  44. /**
  45. * @return (IVersionBackend[])[]
  46. */
  47. private function getBackends(): array {
  48. return $this->backends;
  49. }
  50. /**
  51. * @param IStorage $storage
  52. * @return IVersionBackend
  53. * @throws BackendNotFoundException
  54. */
  55. public function getBackendForStorage(IStorage $storage): IVersionBackend {
  56. $fullType = get_class($storage);
  57. $backends = $this->getBackends();
  58. $foundType = '';
  59. $foundBackend = null;
  60. foreach ($backends as $type => $backendsForType) {
  61. if (
  62. $storage->instanceOfStorage($type) &&
  63. ($foundType === '' || is_subclass_of($type, $foundType))
  64. ) {
  65. foreach ($backendsForType as $backend) {
  66. /** @var IVersionBackend $backend */
  67. if ($backend->useBackendForStorage($storage)) {
  68. $foundBackend = $backend;
  69. $foundType = $type;
  70. }
  71. }
  72. }
  73. }
  74. if ($foundType === '' || $foundBackend === null) {
  75. throw new BackendNotFoundException("Version backend for $fullType not found");
  76. } else {
  77. return $foundBackend;
  78. }
  79. }
  80. public function getVersionsForFile(IUser $user, FileInfo $file): array {
  81. $backend = $this->getBackendForStorage($file->getStorage());
  82. return $backend->getVersionsForFile($user, $file);
  83. }
  84. public function createVersion(IUser $user, FileInfo $file) {
  85. $backend = $this->getBackendForStorage($file->getStorage());
  86. $backend->createVersion($user, $file);
  87. }
  88. public function rollback(IVersion $version) {
  89. $backend = $version->getBackend();
  90. $result = self::handleAppLocks(fn(): ?bool => $backend->rollback($version));
  91. // rollback doesn't have a return type yet and some implementations don't return anything
  92. if ($result === null || $result === true) {
  93. \OC_Hook::emit('\OCP\Versions', 'rollback', [
  94. 'path' => $version->getVersionPath(),
  95. 'revision' => $version->getRevisionId(),
  96. 'node' => $version->getSourceFile(),
  97. ]);
  98. }
  99. return $result;
  100. }
  101. public function read(IVersion $version) {
  102. $backend = $version->getBackend();
  103. return $backend->read($version);
  104. }
  105. public function getVersionFile(IUser $user, FileInfo $sourceFile, $revision): File {
  106. $backend = $this->getBackendForStorage($sourceFile->getStorage());
  107. return $backend->getVersionFile($user, $sourceFile, $revision);
  108. }
  109. public function useBackendForStorage(IStorage $storage): bool {
  110. return false;
  111. }
  112. public function setVersionLabel(IVersion $version, string $label): void {
  113. $backend = $this->getBackendForStorage($version->getSourceFile()->getStorage());
  114. if ($backend instanceof INameableVersionBackend) {
  115. $backend->setVersionLabel($version, $label);
  116. }
  117. }
  118. public function deleteVersion(IVersion $version): void {
  119. $backend = $this->getBackendForStorage($version->getSourceFile()->getStorage());
  120. if ($backend instanceof IDeletableVersionBackend) {
  121. $backend->deleteVersion($version);
  122. }
  123. }
  124. public function createVersionEntity(File $file): void {
  125. $backend = $this->getBackendForStorage($file->getStorage());
  126. if ($backend instanceof INeedSyncVersionBackend) {
  127. $backend->createVersionEntity($file);
  128. }
  129. }
  130. public function updateVersionEntity(File $sourceFile, int $revision, array $properties): void {
  131. $backend = $this->getBackendForStorage($sourceFile->getStorage());
  132. if ($backend instanceof INeedSyncVersionBackend) {
  133. $backend->updateVersionEntity($sourceFile, $revision, $properties);
  134. }
  135. }
  136. public function deleteVersionsEntity(File $file): void {
  137. $backend = $this->getBackendForStorage($file->getStorage());
  138. if ($backend instanceof INeedSyncVersionBackend) {
  139. $backend->deleteVersionsEntity($file);
  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. }