You are here

private function Gardener::processPrune in Backup and migrate prune 7

Same name and namespace in other branches
  1. 7.2 class/Gardener.php \Gardener::processPrune()

Tests if the file is elegible to be prunned and deletes it if necessary

Parameters

$fileset: File object to be tested an deleted

$slot: The time slot being processed

Return value

The total number of deleted backups

1 call to Gardener::processPrune()
Gardener::prune in class/Gardener.php
Delete the files that match the criteria

File

class/Gardener.php, line 228
Gardener class implementation file

Class

Gardener
Gardener class implementation

Code

private function processPrune($fileset, $slot) {
  $settings = $this
    ->getSettings();
  $prunned_files = 0;
  $destination = $this
    ->getDestination();

  // If there are settings for this time slot AND is active AND there are more
  // than one file.
  // We have a list of all files in $fileset we are going to:
  //   1. Remove the files from $fileset we want to save
  //   2. Delete all remaining files
  if (isset($settings[$slot]) && $settings[$slot]['active']) {
    $category_value = NULL;

    // This gardener has to prune these backups
    if (isset($settings[$slot]['keep'])) {

      // Keep the last backup for the indicated value
      $category_value = empty($fileset[$settings[$slot]['keep']]) ? NULL : $settings[$slot]['keep'];
    }

    // If there is no selected value to keep or the selected value has no
    // backups then select any value with backups
    if (is_null($category_value)) {

      // Keep anyone and extract the last backup from the last
      // category (Monday, January, 3rd week, …)
      foreach ($fileset as $category_value => $files) {

        // Iterate over all category_values and leave $category_value to the
        // first one that has files in it
        if (!empty($files)) {
          break;
        }
      }
    }

    // Remove the last value from the selected category_value to prevent
    // being erased. If there is no backups in the selected category_value
    // preserve any backup.
    if (!empty($fileset[$category_value])) {
      $fileset[$category_value] = array_slice($fileset[$category_value], 0, -1);
    }

    // Iterate over all remaining files to delete them.
    foreach ($fileset as $category_value => $files) {
      foreach ($files as $file) {

        // Delete the file
        $destination
          ->delete_file($file
          ->file_id());
        $prunned_files++;
      }
    }
  }
  return $prunned_files;
}