changepropagator.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Cache;
  9. /**
  10. * Propagates changes in etag and mtime up the filesystem tree
  11. *
  12. * @package OC\Files\Cache
  13. */
  14. class ChangePropagator {
  15. /**
  16. * @var string[]
  17. */
  18. protected $changedFiles = array();
  19. /**
  20. * @var \OC\Files\View
  21. */
  22. protected $view;
  23. /**
  24. * @param \OC\Files\View $view
  25. */
  26. public function __construct(\OC\Files\View $view) {
  27. $this->view = $view;
  28. }
  29. public function addChange($path) {
  30. $this->changedFiles[] = $path;
  31. }
  32. public function getChanges() {
  33. return $this->changedFiles;
  34. }
  35. /**
  36. * propagate the registered changes to their parent folders
  37. *
  38. * @param int $time (optional) the mtime to set for the folders, if not set the current time is used
  39. */
  40. public function propagateChanges($time = null) {
  41. $parents = $this->getAllParents();
  42. $this->changedFiles = array();
  43. if (!$time) {
  44. $time = time();
  45. }
  46. foreach ($parents as $parent) {
  47. /**
  48. * @var \OC\Files\Storage\Storage $storage
  49. * @var string $internalPath
  50. */
  51. list($storage, $internalPath) = $this->view->resolvePath($parent);
  52. $cache = $storage->getCache();
  53. $id = $cache->getId($internalPath);
  54. $cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath)));
  55. }
  56. }
  57. /**
  58. * @return string[]
  59. */
  60. public function getAllParents() {
  61. $parents = array();
  62. foreach ($this->getChanges() as $path) {
  63. $parents = array_values(array_unique(array_merge($parents, $this->getParents($path))));
  64. }
  65. return $parents;
  66. }
  67. /**
  68. * get all parent folders of $path
  69. *
  70. * @param string $path
  71. * @return string[]
  72. */
  73. protected function getParents($path) {
  74. $parts = explode('/', $path);
  75. // remove the singe file
  76. array_pop($parts);
  77. $result = array('/');
  78. $resultPath = '';
  79. foreach ($parts as $part) {
  80. if ($part) {
  81. $resultPath .= '/' . $part;
  82. $result[] = $resultPath;
  83. }
  84. }
  85. return $result;
  86. }
  87. }