Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.2 KiB

3 years ago
3 years ago
  1. <?php
  2. namespace Friendica\Core;
  3. use Friendica\Database\DBA;
  4. use Friendica\Core\Config;
  5. /**
  6. * @brief Manage storage backends
  7. *
  8. * Core code uses this class to get and set current storage backend class.
  9. * Addons use this class to register and unregister additional backends.
  10. */
  11. class StorageManager
  12. {
  13. private static $default_backends = [
  14. 'Filesystem' => Friendica\Model\Storage\Filesystem::class,
  15. 'Database' => Friendica\Model\Storage\Database::class,
  16. ];
  17. private static $backends = [];
  18. private static function setup()
  19. {
  20. if (count(self::$backends)==0) {
  21. self::$backends = Config::get('storage', 'backends', self::$default_backends);
  22. }
  23. }
  24. /**
  25. * @brief Return current storage backend class
  26. * @return string
  27. */
  28. public static function getBackend()
  29. {
  30. return Config::get('storage', 'class', '');
  31. }
  32. /**
  33. * @brief Return storage backend class by registered name
  34. *
  35. * @param string $name Backend name
  36. * @return string Empty if no backend registered at $name exists
  37. */
  38. public static function getByName($name)
  39. {
  40. self::setup();
  41. return defaults(self::$backends, $name, '');
  42. }
  43. /**
  44. * @brief Set current storage backend class
  45. *
  46. * @param string $class Backend class name
  47. */
  48. public static function setBackend($class)
  49. {
  50. /// @todo Check that $class implements IStorage
  51. Config::set('storage', 'class', $class);
  52. }
  53. /**
  54. * @brief Get registered backends
  55. *
  56. * @return array
  57. */
  58. public static function listBackends()
  59. {
  60. self::setup();
  61. return self::$backends;
  62. }
  63. /**
  64. * @brief Register a storage backend class
  65. *
  66. * @param string $name User readable backend name
  67. * @param string $class Backend class name
  68. */
  69. public static function register($name, $class)
  70. {
  71. /// @todo Check that $class implements IStorage
  72. self::setup();
  73. self::$backends[$name] = $class;
  74. Config::set('storage', 'backends', self::$backends);
  75. }
  76. /**
  77. * @brief Unregister a storage backend class
  78. *
  79. * @param string $name User readable backend name
  80. */
  81. public static function unregister($class)
  82. {
  83. self::setup();
  84. unset(self::$backends[$name]);
  85. Config::set('storage', 'backends', self::$backends);
  86. }
  87. /**
  88. * @brief Move resources to storage $dest
  89. *
  90. * @param string $dest Destination storage class name
  91. * @param array $tables Tables to look in for resources. Optional, defaults to ['photo']
  92. *
  93. * @retur int Number of moved resources
  94. */
  95. public static function move($dest, $tables = null)
  96. {
  97. if (is_null($tables)) {
  98. $tables = ['photo'];
  99. }
  100. $moved = 0;
  101. foreach ($tables as $table) {
  102. $rr = DBA::select($table, ['id', 'data', 'backend-class', 'backend-ref'], ['`backend-class` != ?', $dest]);
  103. if (DBA::isResult($rr)) {
  104. while($r = $rr->fetch()) {
  105. $id = $r['id'];
  106. $data = $r['data'];
  107. $backendClass = $r['backend-class'];
  108. $backendRef = $r['backend-ref'];
  109. if ($backendClass !== '') {
  110. $data = $backendClass::get($backendRef);
  111. }
  112. $ref = $dest::put($data);
  113. if ($ref !== '') {
  114. $ru = DBA::update($table, ['backend-class' => $dest, 'backend-ref' => $ref, 'data' => ''], ['id' => $id]);
  115. if ($ru) {
  116. if ($backendClass !== '') {
  117. $backendClass::delete($backendRef);
  118. }
  119. $moved++;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return $moved;
  126. }
  127. }