Permalink
Browse files

fix(pages): operations keep track of more than 10 child pages

Sets `0` limit on queries for child queries, converts to batches, some
small refactoring for clarity.
  • Loading branch information...
mrclay committed Sep 6, 2016
1 parent 11845f4 commit bc5f414bf597fe440ca096333fafea330746a323
Showing with 97 additions and 84 deletions.
  1. +59 −52 mod/pages/actions/pages/delete.php
  2. +24 −25 mod/pages/actions/pages/edit.php
  3. +14 −7 mod/pages/lib/pages.php
@@ -9,64 +9,71 @@
$guid = get_input('guid');
$page = get_entity($guid);
if (pages_is_page($page)) {
elgg_load_library('elgg:pages');
// only allow owners and admin to delete
if (elgg_is_admin_logged_in()
|| elgg_get_logged_in_user_guid() == $page->getOwnerGuid()
|| pages_can_delete_page($page)) {
$container = get_entity($page->container_guid);
/* @var ElggObject $page */
// Bring all child elements forward
$parent = $page->parent_guid;
$children = elgg_get_entities_from_metadata(array(
'metadata_name' => 'parent_guid',
'metadata_value' => $page->getGUID()
));
if ($children) {
$db_prefix = elgg_get_config('dbprefix');
$subtype_id = (int)get_subtype_id('object', 'page_top');
$newentity_cache = is_memcache_available() ? new ElggMemcache('new_entity_cache') : null;
elgg_load_library('elgg:pages');
foreach ($children as $child) {
if ($parent) {
$child->parent_guid = $parent;
} else {
// If no parent, we need to transform $child to a page_top
$child_guid = (int)$child->guid;
if (!pages_is_page($page) || !pages_can_delete_page($page)) {
register_error(elgg_echo('pages:delete:failure'));
forward(REFERER);
}
$container = $page->getContainerEntity();
// Bring all child elements forward
$parent = $page->parent_guid;
$children = new ElggBatch('elgg_get_entities_from_metadata', [
'metadata_name' => 'parent_guid',
'metadata_value' => $page->guid,
'limit' => 0,
]);
$db_prefix = elgg_get_config('dbprefix');
$subtype_id = (int)get_subtype_id('object', 'page_top');
$newentity_cache = is_memcache_available() ? new ElggMemcache('new_entity_cache') : null;
foreach ($children as $child) {
if ($parent) {
$child->parent_guid = $parent;
continue;
}
update_data("UPDATE {$db_prefix}entities
SET subtype = $subtype_id WHERE guid = $child_guid");
// If no parent, we need to transform $child to a page_top
$child_guid = (int)$child->guid;
elgg_delete_metadata(array(
'guid' => $child_guid,
'metadata_name' => 'parent_guid',
));
update_data("
UPDATE {$db_prefix}entities
SET subtype = $subtype_id
WHERE guid = $child_guid
");
_elgg_invalidate_cache_for_entity($child_guid);
if ($newentity_cache) {
$newentity_cache->delete($child_guid);
}
}
}
}
elgg_delete_metadata(array(
'guid' => $child_guid,
'metadata_name' => 'parent_guid',
));
if ($page->delete()) {
system_message(elgg_echo('pages:delete:success'));
if ($parent) {
$parent = get_entity($parent);
if ($parent) {
forward($parent->getURL());
}
}
if (elgg_instanceof($container, 'group')) {
forward("pages/group/$container->guid/all");
} else {
forward("pages/owner/$container->username");
}
}
_elgg_invalidate_cache_for_entity($child_guid);
if ($newentity_cache) {
$newentity_cache->delete($child_guid);
}
}
register_error(elgg_echo('pages:delete:failure'));
forward(REFERER);
if (!$page->delete()) {
register_error(elgg_echo('pages:delete:failure'));
forward(REFERER);
}
system_message(elgg_echo('pages:delete:success'));
if ($parent) {
$parent = get_entity($parent);
if ($parent) {
forward($parent->getURL());
}
}
if (elgg_instanceof($container, 'group')) {
forward("pages/group/$container->guid/all");
} else {
forward("pages/owner/$container->username");
}
@@ -81,40 +81,39 @@
// If is below, bring all child elements forward
if ($page_guid == $tree_page->guid) {
$previous_parent = $page->parent_guid;
$children = elgg_get_entities_from_metadata(array(
$children = new ElggBatch('elgg_get_entities_from_metadata', [
'metadata_name' => 'parent_guid',
'metadata_value' => $page->getGUID()
));
if ($children) {
foreach ($children as $child) {
$child->parent_guid = $previous_parent;
}
'metadata_value' => $page->guid,
'limit' => 0,
]);
foreach ($children as $child) {
$child->parent_guid = $previous_parent;
}
}
}
$page->parent_guid = $parent_guid;
}
if ($page->save()) {
elgg_clear_sticky_form('page');
if (!$page->save()) {
register_error(elgg_echo('pages:notsaved'));
forward(REFERER);
}
// Now save description as an annotation
$page->annotate('page', $page->description, $page->access_id);
elgg_clear_sticky_form('page');
system_message(elgg_echo('pages:saved'));
// Now save description as an annotation
$page->annotate('page', $page->description, $page->access_id);
if ($new_page) {
elgg_create_river_item(array(
'view' => 'river/object/page/create',
'action_type' => 'create',
'subject_guid' => elgg_get_logged_in_user_guid(),
'object_guid' => $page->guid,
));
}
system_message(elgg_echo('pages:saved'));
forward($page->getURL());
} else {
register_error(elgg_echo('pages:notsaved'));
forward(REFERER);
if ($new_page) {
elgg_create_river_item(array(
'view' => 'river/object/page/create',
'action_type' => 'create',
'subject_guid' => elgg_get_logged_in_user_guid(),
'object_guid' => $page->guid,
));
}
forward($page->getURL());
View
@@ -152,18 +152,25 @@ function pages_register_navigation_tree($container) {
}
/**
* Function checking delete permission
* Can the user delete the page?
*
* @package ElggPages
* @param mixed $value
* @param ElggObject $page Page/page-top object
*
* @return bool
*/
function pages_can_delete_page($page) {
if (! $page) {
if (!pages_is_page($page)) {
return false;
} else {
$container = get_entity($page->container_guid);
return $container ? $container->canEdit() : false;
}
/* @var ElggObject $page */
$user = elgg_get_logged_in_user_entity();
if ($user) {
if ($user->guid == $page->owner_guid || $user->isAdmin()) {
return true;
}
}
$container = $page->getContainerEntity();
return $container ? $container->canEdit() : false;
}

0 comments on commit bc5f414

Please sign in to comment.