+
+
+ /**
+ * @brief Move up to 5000 resources to storage $dest
+ *
+ * Copy existing data to destination storage and delete from source.
+ * This method cannot move to legacy in-table `data` field.
+ *
+ * @param string $destination Storage class name
+ * @param array|null $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
+ * @param int $limit Limit of the process batch size, defaults to 5000
+ * @return int Number of moved resources
+ * @throws \Exception
+ */
+ public static function move($destination, $tables = null, $limit = 5000)
+ {
+ if (empty($destination)) {
+ throw new \Exception('Can\'t move to NULL storage backend');
+ }
+
+ if (is_null($tables)) {
+ $tables = ['photo', 'attach'];
+ }
+
+ $moved = 0;
+ foreach ($tables as $table) {
+ // Get the rows where backend class is not the destination backend class
+ $resources = DBA::select(
+ $table,
+ ['id', 'data', 'backend-class', 'backend-ref'],
+ ['`backend-class` IS NULL or `backend-class` != ?', $destination],
+ ['limit' => $limit]
+ );
+
+ while ($resource = DBA::fetch($resources)) {
+ $id = $resource['id'];
+ $data = $resource['data'];
+ /** @var IStorage $backendClass */
+ $backendClass = $resource['backend-class'];
+ $backendRef = $resource['backend-ref'];
+ if (!empty($backendClass)) {
+ Logger::log("get data from old backend " . $backendClass . " : " . $backendRef);
+ $data = $backendClass::get($backendRef);
+ }
+
+ Logger::log("save data to new backend " . $destination);
+ /** @var IStorage $destination */
+ $ref = $destination::put($data);
+ Logger::log("saved data as " . $ref);
+
+ if ($ref !== '') {
+ Logger::log("update row");
+ if (DBA::update($table, ['backend-class' => $destination, 'backend-ref' => $ref, 'data' => ''], ['id' => $id])) {
+ if (!empty($backendClass)) {
+ Logger::log("delete data from old backend " . $backendClass . " : " . $backendRef);
+ $backendClass::delete($backendRef);
+ }
+ $moved++;
+ }
+ }
+ }
+
+ DBA::close($resources);
+ }
+
+ return $moved;
+ }
+}
+