New function to delete items for users

This commit is contained in:
Michael 2018-05-29 05:22:57 +00:00
parent deb015be12
commit 8329705eba
11 changed files with 53 additions and 55 deletions

View file

@ -2256,7 +2256,7 @@ function api_statuses_destroy($type)
$ret = api_statuses_show($type); $ret = api_statuses_show($type);
Item::deleteById($id, PRIORITY_HIGH, api_user()); Item::deleteForUser(['id' => $id], api_user());
return $ret; return $ret;
} }
@ -4148,7 +4148,7 @@ function api_fr_photoalbum_delete($type)
if (!DBM::is_result($photo_item)) { if (!DBM::is_result($photo_item)) {
throw new InternalServerErrorException("problem with deleting items occured"); 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 // 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 // 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) // 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.']; $answer = ['result' => 'deleted', 'message' => 'photo with id `' . $photo_id . '` has been deleted from server.'];
return api_format_data("photo_delete", $type, ['$result' => $answer]); return api_format_data("photo_delete", $type, ['$result' => $answer]);

View file

@ -321,7 +321,7 @@ function drop_items($items) {
if (count($items)) { if (count($items)) {
foreach ($items as $item) { foreach ($items as $item) {
$owner = Item::deleteById($item, PRIORITY_HIGH, local_user()); $owner = Item::deleteForUser(['id' => $item], local_user());
if ($owner && !$uid) if ($owner && !$uid)
$uid = $owner; $uid = $owner;
} }
@ -393,7 +393,7 @@ function drop_item($id) {
} }
// delete the item // delete the item
Item::deleteById($item['id'], PRIORITY_HIGH, local_user()); Item::deleteForUser(['id' => $item['id']], local_user());
goaway(System::baseUrl() . '/' . $_SESSION['return_url']); goaway(System::baseUrl() . '/' . $_SESSION['return_url']);
//NOTREACHED //NOTREACHED

View file

@ -555,14 +555,9 @@ function admin_page_deleteitem_post(App $a)
if (strpos($guid, '/')) { if (strpos($guid, '/')) {
$guid = substr($guid, strrpos($guid, '/') + 1); $guid = substr($guid, strrpos($guid, '/') + 1);
} }
// Now that we have the GUID get all IDs of the associated entries in the // Now that we have the GUID, drop those items, which will also delete the
// item table of the DB and drop those items, which will also delete the
// associated threads. // associated threads.
$r = dba::select('item', ['id'], ['guid' => $guid]); Item::delete(['guid' => $guid]);
while ($row = dba::fetch($r)) {
Item::deleteById($row['id']);
}
dba::close($r);
} }
info(L10n::t('Item marked for deletion.') . EOL); info(L10n::t('Item marked for deletion.') . EOL);

View file

@ -545,7 +545,7 @@ function events_content(App $a) {
// Delete only real events (no birthdays) // Delete only real events (no birthdays)
if (DBM::is_result($ev) && $ev[0]['type'] == 'event') { 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) { if ($del == 0) {

View file

@ -877,7 +877,7 @@ function item_content(App $a) {
$o = ''; $o = '';
if (($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) { if (($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
if (is_ajax()) { if (is_ajax()) {
$o = Item::deleteById($a->argv[2], PRIORITY_HIGH, local_user()); $o = Item::deleteForUser(['id' => $a->argv[2]], local_user());
} else { } else {
$o = drop_item($a->argv[2]); $o = drop_item($a->argv[2]);
} }

View file

@ -284,14 +284,7 @@ function photos_post(App $a)
); );
// find and delete the corresponding item with all the comments and likes/dislikes // 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", Item::deleteForUser(['resource-id' => $res, 'uid' => $page_owner_uid], $page_owner_uid);
intval($page_owner_uid)
);
if (DBM::is_result($r)) {
foreach ($r as $rr) {
Item::deleteById($rr['id'], PRIORITY_HIGH, $page_owner_uid);
}
}
// Update the photo albums cache // Update the photo albums cache
Photo::clearAlbumCache($page_owner_uid); Photo::clearAlbumCache($page_owner_uid);
@ -344,17 +337,12 @@ function photos_post(App $a)
intval($page_owner_uid), intval($page_owner_uid),
dbesc($r[0]['resource-id']) 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']), Item::deleteForUser(['resource-id' => $r[0]['resource-id'], 'uid' => $page_owner_uid], $page_owner_uid);
intval($page_owner_uid)
);
if (DBM::is_result($i)) {
Item::deleteById($i[0]['id'], PRIORITY_HIGH, $page_owner_uid);
// Update the photo albums cache // Update the photo albums cache
Photo::clearAlbumCache($page_owner_uid); Photo::clearAlbumCache($page_owner_uid);
} }
}
goaway('photos/' . $a->data['user']['nickname']); goaway('photos/' . $a->data['user']['nickname']);
return; // NOTREACHED return; // NOTREACHED

View file

@ -169,7 +169,7 @@ function videos_post(App $a) {
); );
if (DBM::is_result($i)) { if (DBM::is_result($i)) {
Item::deleteById($i[0]['id'], PRIORITY_HIGH, local_user()); Item::deleteForUser(['id' => $i[0]['id']], local_user());
} }
} }

View file

@ -92,13 +92,38 @@ class Item extends BaseObject
* *
* @param array $condition The condition for finding the item entries * @param array $condition The condition for finding the item entries
* @param integer $priority Priority for the notification * @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); $items = dba::select('item', ['id'], $condition);
while ($item = dba::fetch($items)) { 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); dba::close($items);
} }
@ -108,11 +133,10 @@ class Item extends BaseObject
* *
* @param integer $item_id Item ID that should be delete * @param integer $item_id Item ID that should be delete
* @param integer $priority Priority for the notification * @param integer $priority Priority for the notification
* @param integer $uid User who wants to delete the item
* *
* @return boolean success * @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 // locate item to be deleted
$fields = ['id', 'uri', 'uid', 'parent', 'parent-uri', 'origin', $fields = ['id', 'uri', 'uid', 'parent', 'parent-uri', 'origin',
@ -134,12 +158,6 @@ class Item extends BaseObject
$parent = ['origin' => false]; $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 // clean up categories and tags so they don't end up as orphans
$matches = false; $matches = false;

View file

@ -2807,15 +2807,13 @@ class DFRN
} }
} }
$entrytype = self::getEntryType($importer, $item); if ($item["deleted"]) {
if (!$item["deleted"]) {
logger('deleting item '.$item["id"].' uri='.$uri, LOGGER_DEBUG);
} else {
return; return;
} }
Item::deleteById($item["id"]); logger('deleting item '.$item["id"].' uri='.$uri, LOGGER_DEBUG);
Item::delete(['id' => $item["id"]]);
} }
/** /**

View file

@ -2785,7 +2785,7 @@ class Diaspora
while ($item = dba::fetch($r)) { while ($item = dba::fetch($r)) {
// Fetch the parent item // 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 // Only delete it if the parent author really fits
if (!link_compare($parent["author-link"], $contact["url"]) && !link_compare($item["author-link"], $contact["url"])) { if (!link_compare($parent["author-link"], $contact["url"]) && !link_compare($item["author-link"], $contact["url"])) {
@ -2793,7 +2793,7 @@ class Diaspora
continue; 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); logger("Deleted target ".$target_guid." (".$item["id"].") from user ".$item["uid"]." parent: ".$item["parent"], LOGGER_DEBUG);
} }

View file

@ -537,13 +537,12 @@ class OStatus
private static function deleteNotice($item) private static function deleteNotice($item)
{ {
$condition = ['uid' => $item['uid'], 'author-link' => $item['author-link'], 'uri' => $item['uri']]; $condition = ['uid' => $item['uid'], 'author-link' => $item['author-link'], 'uri' => $item['uri']];
$deleted = dba::selectFirst('item', ['id', 'parent-uri'], $condition); if (!dba::exists('item', $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.");
logger('Item from '.$item['author-link'].' with uri '.$item['uri'].' for user '.$item['uid']." wasn't found. We don't delete it. ");
return; return;
} }
Item::deleteById($deleted["id"]); Item::delete($condition);
logger('Deleted item with uri '.$item['uri'].' for user '.$item['uid']); logger('Deleted item with uri '.$item['uri'].' for user '.$item['uid']);
} }