Skip to content

Commit

Permalink
MDL-53425 blocklib: Perform deletion of block instances by chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed May 30, 2016
1 parent 6a74e76 commit 4eba620
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
32 changes: 21 additions & 11 deletions lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2073,21 +2073,31 @@ function blocks_delete_instance($instance, $nolongerused = false, $skipblockstab
function blocks_delete_instances($instanceids) {
global $DB;

$instances = $DB->get_recordset_list('block_instances', 'id', $instanceids);
foreach ($instances as $instance) {
blocks_delete_instance($instance, false, true);
$limit = 1000;
$count = count($instanceids);
$chunks = [$instanceids];
if ($count > $limit) {
$chunks = array_chunk($instanceids, $limit);
}
$instances->close();

$DB->delete_records_list('block_positions', 'blockinstanceid', $instanceids);
$DB->delete_records_list('block_instances', 'id', $instanceids);
// Perform deletion for each chunk.
foreach ($chunks as $chunk) {
$instances = $DB->get_recordset_list('block_instances', 'id', $chunk);
foreach ($instances as $instance) {
blocks_delete_instance($instance, false, true);
}
$instances->close();

$DB->delete_records_list('block_positions', 'blockinstanceid', $chunk);
$DB->delete_records_list('block_instances', 'id', $chunk);

$preferences = array();
foreach ($instanceids as $instanceid) {
$preferences[] = 'block' . $instanceid . 'hidden';
$preferences[] = 'docked_block_instance_' . $instanceid;
$preferences = array();
foreach ($chunk as $instanceid) {
$preferences[] = 'block' . $instanceid . 'hidden';
$preferences[] = 'docked_block_instance_' . $instanceid;
}
$DB->delete_records_list('user_preferences', 'name', $preferences);
}
$DB->delete_records_list('user_preferences', 'name', $preferences);
}

/**
Expand Down
24 changes: 12 additions & 12 deletions my/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,23 @@ function my_reset_page_for_all_users($private = MY_PAGE_PRIVATE, $pagetype = 'my
$where = 'userid IS NOT NULL AND private = :private';
$params = array('private' => $private);
$pages = $DB->get_recordset_select('my_pages', $where, $params, 'id, userid');
$pageids = array();
$blockids = array();

foreach ($pages as $page) {
$pageids[] = $page->id;
$usercontext = context_user::instance($page->userid);

// Find all block instances in that page.
$blocks = $DB->get_recordset('block_instances', array('parentcontextid' => $usercontext->id,
'pagetypepattern' => $pagetype), '', 'id, subpagepattern');
foreach ($blocks as $block) {
if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
$blockids[] = $block->id;
}
$blockswhere = 'parentcontextid = :parentcontextid AND
pagetypepattern = :pagetypepattern AND
(subpagepattern IS NULL OR subpagepattern = :subpagepattern)';
$blockswhereparams = [
'parentcontextid' => $usercontext->id,
'pagetypepattern' => $pagetype,
'subpagepattern' => $page->id
];
if ($pageblockids = $DB->get_fieldset_select('block_instances', 'id', $blockswhere, $blockswhereparams)) {
$blockids = array_merge($blockids, $pageblockids);
}
$blocks->close();
}
$pages->close();

Expand All @@ -178,9 +179,8 @@ function my_reset_page_for_all_users($private = MY_PAGE_PRIVATE, $pagetype = 'my
}

// Finally delete the pages.
if (!empty($pageids)) {
list($insql, $inparams) = $DB->get_in_or_equal($pageids);
$DB->delete_records_select('my_pages', "id $insql", $pageids);
if (!empty($pages)) {
$DB->delete_records_select('my_pages', $where, $params);
}

// We should be good to go now.
Expand Down

0 comments on commit 4eba620

Please sign in to comment.