<?php
/**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
use Friendica\Core\Logger;
use Friendica\Core\System;
+use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\Images;
// We are storing as fast as possible to avoid duplicated network requests
// when fetching additional information for pictures and other content.
- $result = DBA::insert('post-media', $media, true);
+ $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
Logger::info('Stored media', ['result' => $result, 'media' => $media, 'callstack' => System::callstack()]);
$stored = $media;
$media = self::fetchAdditionalData($media);
if (array_diff_assoc($media, $stored)) {
- $result = DBA::insert('post-media', $media, true);
+ $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
Logger::info('Updated media', ['result' => $result, 'media' => $media]);
} else {
Logger::info('Nothing to update', ['media' => $media]);
}
}
+ /**
+ * Copy attachments from one uri-id to another
+ *
+ * @param integer $from_uri_id
+ * @param integer $to_uri_id
+ * @return void
+ */
+ public static function copy(int $from_uri_id, int $to_uri_id)
+ {
+ $attachments = self::getByURIId($from_uri_id);
+ foreach ($attachments as $attachment) {
+ $attachment['uri-id'] = $to_uri_id;
+ self::insert($attachment);
+ }
+ }
+
/**
* Creates the "[attach]" element from the given attributes
*
self::insert($media);
}
}
+
+ /**
+ * Retrieves the media attachments associated with the provided item ID.
+ *
+ * @param int $uri_id
+ * @param array $types
+ * @return array
+ * @throws \Exception
+ */
+ public static function getByURIId(int $uri_id, array $types = [])
+ {
+ $condition = ['uri-id' => $uri_id];
+
+ if (!empty($types)) {
+ $condition = DBA::mergeConditions($condition, ['type' => $types]);
+ }
+
+ return DBA::selectToArray('post-media', [], $condition);
+ }
}