From: Michael Date: Tue, 29 May 2018 05:22:57 +0000 (+0000) Subject: New function to delete items for users X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8329705ebab61159b114b274e794bf6c3d6a6a95;p=friendica.git New function to delete items for users --- diff --git a/include/api.php b/include/api.php index 3832257784..a70c18f3ca 100644 --- a/include/api.php +++ b/include/api.php @@ -2256,7 +2256,7 @@ function api_statuses_destroy($type) $ret = api_statuses_show($type); - Item::deleteById($id, PRIORITY_HIGH, api_user()); + Item::deleteForUser(['id' => $id], api_user()); return $ret; } @@ -4148,7 +4148,7 @@ function api_fr_photoalbum_delete($type) if (!DBM::is_result($photo_item)) { throw new InternalServerErrorException("problem with deleting items occured"); } - Item::deleteById($photo_item[0]['id'], PRIORITY_HIGH, api_user()); + Item::deleteForUser(['id' => $photo_item[0]['id']], api_user()); } // now let's delete all photos from the album @@ -4441,7 +4441,7 @@ function api_fr_photo_delete($type) } // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore // to the user and the contacts of the users (drop_items() do all the necessary magic to avoid orphans in database and federate deletion) - Item::deleteById($photo_item[0]['id'], PRIORITY_HIGH, api_user()); + Item::deleteForUser(['id' => $photo_item[0]['id']], api_user()); $answer = ['result' => 'deleted', 'message' => 'photo with id `' . $photo_id . '` has been deleted from server.']; return api_format_data("photo_delete", $type, ['$result' => $answer]); diff --git a/include/items.php b/include/items.php index b84ac701a6..4533b6077e 100644 --- a/include/items.php +++ b/include/items.php @@ -321,7 +321,7 @@ function drop_items($items) { if (count($items)) { foreach ($items as $item) { - $owner = Item::deleteById($item, PRIORITY_HIGH, local_user()); + $owner = Item::deleteForUser(['id' => $item], local_user()); if ($owner && !$uid) $uid = $owner; } @@ -393,7 +393,7 @@ function drop_item($id) { } // delete the item - Item::deleteById($item['id'], PRIORITY_HIGH, local_user()); + Item::deleteForUser(['id' => $item['id']], local_user()); goaway(System::baseUrl() . '/' . $_SESSION['return_url']); //NOTREACHED diff --git a/mod/admin.php b/mod/admin.php index e374201fff..83dafcfd97 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -555,14 +555,9 @@ function admin_page_deleteitem_post(App $a) if (strpos($guid, '/')) { $guid = substr($guid, strrpos($guid, '/') + 1); } - // Now that we have the GUID get all IDs of the associated entries in the - // item table of the DB and drop those items, which will also delete the + // Now that we have the GUID, drop those items, which will also delete the // associated threads. - $r = dba::select('item', ['id'], ['guid' => $guid]); - while ($row = dba::fetch($r)) { - Item::deleteById($row['id']); - } - dba::close($r); + Item::delete(['guid' => $guid]); } info(L10n::t('Item marked for deletion.') . EOL); diff --git a/mod/events.php b/mod/events.php index e40e69f500..87c358a99e 100644 --- a/mod/events.php +++ b/mod/events.php @@ -545,7 +545,7 @@ function events_content(App $a) { // Delete only real events (no birthdays) if (DBM::is_result($ev) && $ev[0]['type'] == 'event') { - $del = Item::deleteById($ev[0]['itemid'], PRIORITY_HIGH, local_user()); + $del = Item::deleteForUser(['id' => $ev[0]['itemid']], local_user()); } if ($del == 0) { diff --git a/mod/item.php b/mod/item.php index ca79a64028..823350874b 100644 --- a/mod/item.php +++ b/mod/item.php @@ -877,7 +877,7 @@ function item_content(App $a) { $o = ''; if (($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) { if (is_ajax()) { - $o = Item::deleteById($a->argv[2], PRIORITY_HIGH, local_user()); + $o = Item::deleteForUser(['id' => $a->argv[2]], local_user()); } else { $o = drop_item($a->argv[2]); } diff --git a/mod/photos.php b/mod/photos.php index 3d38be5037..30d3728d36 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -284,14 +284,7 @@ function photos_post(App $a) ); // find and delete the corresponding item with all the comments and likes/dislikes - $r = q("SELECT `id` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d", - intval($page_owner_uid) - ); - if (DBM::is_result($r)) { - foreach ($r as $rr) { - Item::deleteById($rr['id'], PRIORITY_HIGH, $page_owner_uid); - } - } + Item::deleteForUser(['resource-id' => $res, 'uid' => $page_owner_uid], $page_owner_uid); // Update the photo albums cache Photo::clearAlbumCache($page_owner_uid); @@ -344,16 +337,11 @@ function photos_post(App $a) intval($page_owner_uid), dbesc($r[0]['resource-id']) ); - $i = q("SELECT `id` FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1", - dbesc($r[0]['resource-id']), - intval($page_owner_uid) - ); - if (DBM::is_result($i)) { - Item::deleteById($i[0]['id'], PRIORITY_HIGH, $page_owner_uid); - // Update the photo albums cache - Photo::clearAlbumCache($page_owner_uid); - } + Item::deleteForUser(['resource-id' => $r[0]['resource-id'], 'uid' => $page_owner_uid], $page_owner_uid); + + // Update the photo albums cache + Photo::clearAlbumCache($page_owner_uid); } goaway('photos/' . $a->data['user']['nickname']); diff --git a/mod/videos.php b/mod/videos.php index 6dee4586aa..0dd653e914 100644 --- a/mod/videos.php +++ b/mod/videos.php @@ -169,7 +169,7 @@ function videos_post(App $a) { ); if (DBM::is_result($i)) { - Item::deleteById($i[0]['id'], PRIORITY_HIGH, local_user()); + Item::deleteForUser(['id' => $i[0]['id']], local_user()); } } diff --git a/src/Model/Item.php b/src/Model/Item.php index ea866152bb..0bac3210d7 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -92,13 +92,38 @@ class Item extends BaseObject * * @param array $condition The condition for finding the item entries * @param integer $priority Priority for the notification - * @param integer $uid User who wants to delete the item */ - public static function delete($condition, $priority = PRIORITY_HIGH, $uid = 0) + public static function delete($condition, $priority = PRIORITY_HIGH) { $items = dba::select('item', ['id'], $condition); while ($item = dba::fetch($items)) { - self::deleteById($item['id'], $priority, $uid); + self::deleteById($item['id'], $priority); + } + dba::close($items); + } + + /** + * @brief Delete an item for an user and notify others about it - if it was ours + * + * @param array $condition The condition for finding the item entries + * @param integer $uid User who wants to delete this item + */ + public static function deleteForUser($condition, $uid) + { + if ($uid == 0) { + return; + } + + $items = dba::select('item', ['id', 'uid'], $condition); + while ($item = dba::fetch($items)) { + // "Deleting" global items just means hiding them + if ($item['uid'] == 0) { + dba::update('user-item', ['hidden' => true], ['iid' => $item['id'], 'uid' => $uid], true); + } elseif ($item['uid'] == $uid) { + self::deleteById($item['id'], PRIORITY_HIGH); + } else { + logger('Wrong ownership. Not deleting item ' . $item['id']); + } } dba::close($items); } @@ -108,11 +133,10 @@ class Item extends BaseObject * * @param integer $item_id Item ID that should be delete * @param integer $priority Priority for the notification - * @param integer $uid User who wants to delete the item * * @return boolean success */ - public static function deleteById($item_id, $priority = PRIORITY_HIGH, $uid = 0) + private static function deleteById($item_id, $priority = PRIORITY_HIGH) { // locate item to be deleted $fields = ['id', 'uri', 'uid', 'parent', 'parent-uri', 'origin', @@ -134,12 +158,6 @@ class Item extends BaseObject $parent = ['origin' => false]; } - // "Deleting" global items just means hiding them - if (($item['uid'] == 0) && ($uid != 0)) { - dba::update('user-item', ['hidden' => true], ['iid' => $item_id, 'uid' => $uid], true); - return true; - } - // clean up categories and tags so they don't end up as orphans $matches = false; diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 5efcfb80b5..bf943f1a15 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -2807,15 +2807,13 @@ class DFRN } } - $entrytype = self::getEntryType($importer, $item); - - if (!$item["deleted"]) { - logger('deleting item '.$item["id"].' uri='.$uri, LOGGER_DEBUG); - } else { + if ($item["deleted"]) { return; } - Item::deleteById($item["id"]); + logger('deleting item '.$item["id"].' uri='.$uri, LOGGER_DEBUG); + + Item::delete(['id' => $item["id"]]); } /** diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index d2a7995d86..f3aa672a21 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -2785,7 +2785,7 @@ class Diaspora while ($item = dba::fetch($r)) { // Fetch the parent item - $parent = dba::selectFirst('item', ['author-link', 'origin'], ['id' => $item["parent"]]); + $parent = dba::selectFirst('item', ['author-link'], ['id' => $item["parent"]]); // Only delete it if the parent author really fits if (!link_compare($parent["author-link"], $contact["url"]) && !link_compare($item["author-link"], $contact["url"])) { @@ -2793,7 +2793,7 @@ class Diaspora continue; } - Item::deleteById($item["id"]); + Item::delete(['id' => $item["id"]]); logger("Deleted target ".$target_guid." (".$item["id"].") from user ".$item["uid"]." parent: ".$item["parent"], LOGGER_DEBUG); } diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index 8b69392144..e91d81f58e 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -537,13 +537,12 @@ class OStatus private static function deleteNotice($item) { $condition = ['uid' => $item['uid'], 'author-link' => $item['author-link'], 'uri' => $item['uri']]; - $deleted = dba::selectFirst('item', ['id', 'parent-uri'], $condition); - if (!DBM::is_result($deleted)) { - logger('Item from '.$item['author-link'].' with uri '.$item['uri'].' for user '.$item['uid']." wasn't found. We don't delete it. "); + if (!dba::exists('item', $condition)) { + logger('Item from '.$item['author-link'].' with uri '.$item['uri'].' for user '.$item['uid']." wasn't found. We don't delete it."); return; } - Item::deleteById($deleted["id"]); + Item::delete($condition); logger('Deleted item with uri '.$item['uri'].' for user '.$item['uid']); }