* @param string $preview Preview picture
* @return boolean
*/
- private static function isPictureLink(string $page, string $preview): bool
+ private static function isLinkToPhoto(string $page, string $preview): bool
{
- return (preg_match('#/photo/.*-0\.#ism', $page) || preg_match('#/photos/.*/image/#ism', $page)) && preg_match('#/photo/.*-[01]\.#ism', $preview);
+ return preg_match('#/photo/.*-0\.#ism', $page) && preg_match('#/photo/.*-[01]\.#ism', $preview);
+ }
+
+ /**
+ * Tests for path patterns that are usef for picture links in Friendica
+ *
+ * @param string $page Link to the image page
+ * @param string $preview Preview picture
+ * @return boolean
+ */
+ private static function isLinkToImagePage(string $page, string $preview): bool
+ {
+ return preg_match('#/photos/.*/image/#ism', $page) && preg_match('#/photo/.*-[01]\.#ism', $preview);
}
/**
* Add media links and remove them from the body
*
* @param integer $uriid
- * @param string $body
+ * @param string $body
+ * @param bool $endmatch
+ * @param bool $removepicturelinks
* @return string Body without media links
*/
- public static function insertFromBody(int $uriid, string $body, bool $endmatch = false): string
+ public static function insertFromBody(int $uriid, string $body, bool $endmatch = false, bool $removepicturelinks = false): string
{
$endmatchpattern = $endmatch ? '\z' : '';
// Simplify image codes
$attachments = [];
if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) {
- if (self::isPictureLink($picture[1], $picture[2])) {
+ if (self::isLinkToImagePage($picture[1], $picture[2])) {
$body = str_replace($picture[0], '', $body);
$image = str_replace('-1.', '-0.', $picture[2]);
$attachments[$image] = [
'uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
'preview' => $picture[2], 'description' => $picture[3]
];
- } else {
+ } elseif (self::isLinkToPhoto($picture[1], $picture[2])) {
+ $body = str_replace($picture[0], '', $body);
+ $attachments[$picture[1]] = [
+ 'uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1],
+ 'preview' => $picture[2], 'description' => $picture[3]
+ ];
+ } elseif ($removepicturelinks) {
$body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = [
'uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $picture[1],
if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) {
- if (self::isPictureLink($picture[1], $picture[2])) {
+ if (self::isLinkToImagePage($picture[1], $picture[2])) {
$body = str_replace($picture[0], '', $body);
$image = str_replace('-1.', '-0.', $picture[2]);
$attachments[$image] = [
'uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
'preview' => $picture[2], 'description' => null
];
- } else {
+ } elseif (self::isLinkToPhoto($picture[1], $picture[2])) {
+ $body = str_replace($picture[0], '', $body);
+ $attachments[$picture[1]] = [
+ 'uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1],
+ 'preview' => $picture[2], 'description' => null
+ ];
+ } elseif ($removepicturelinks) {
$body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = [
'uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $picture[1],
{
do {
$prebody = $body;
- $body = self::insertFromBody(0, $body);
+ $body = self::insertFromBody(0, $body, false, true);
} while ($prebody != $body);
return $body;
}
return DBA::selectToArray('post-media', [], $condition, ['order' => ['id']]);
}
+ /**
+ * Retrieves the media attachment with the provided media id.
+ *
+ * @param int $id id
+ * @return array|bool Array on success, false on error
+ * @throws \Exception
+ */
+ public static function getById(int $id)
+ {
+ return DBA::selectFirst('post-media', [], ['id' => $id]);
+ }
+
+ /**
+ * Update post-media entries
+ *
+ * @param array $fields
+ * @param int $id
+ * @return bool
+ */
+ public static function updateById(array $fields, int $id): bool
+ {
+ return DBA::update('post-media', $fields, ['id' => $id]);
+ }
+
/**
* Checks if media attachments are associated with the provided item ID.
*
*
* @param int $uri_id URI id
* @param array $types Media types
- * @return bool Whether media attachment exists
+ * @return bool result of deletion
* @throws \Exception
*/
public static function deleteByURIId(int $uri_id, array $types = []): bool
return DBA::delete('post-media', $condition);
}
+ /**
+ * Delete media by id
+ *
+ * @param int $id media id
+ * @return bool result of deletion
+ * @throws \Exception
+ */
+ public static function deleteById(int $id): bool
+ {
+ return DBA::delete('post-media', ['id' => $id]);
+ }
+
/**
* Split the attachment media in the three segments "visual", "link" and "additional"
*