You are here

public function Gardener::arrangeFiles in Backup and migrate prune 7.2

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

Organizes the files in the time slots according to the timestamp

Return value

Keyed array containing backup_file object keyed by the time slot

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

File

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

Class

Gardener
Gardener class implementation

Code

public function arrangeFiles() {
  $files = array(
    'thisweek_slot' => array(),
    'thismonth_slot' => array(),
    'thisyear_slot' => array(),
    'pastyears_slot' => array(),
  );

  // These files should not be taken into account
  $protected_filenames = array(
    '.',
    '..',
    '.htaccess',
    'test.txt',
  );

  // Instead of using 'now' an arbitrary reference is used in order to allow
  // a much comprehensive testing
  $reference = new \DateTime($this->date_reference);
  foreach ($this
    ->getDestination()
    ->list_files() as $file) {
    if (!in_array($file
      ->filename(), $protected_filenames)) {
      $filedate = new \DateTime();
      $filedate
        ->setTimestamp($file
        ->info('filetime'));
      $timelapse = $filedate
        ->diff($reference);
      $week_num = (int) ($filedate
        ->format('j') / 7) + 1;
      if ($timelapse->days > 0) {
        if ($timelapse->days < 7) {
          if (!isset($files['thisweek_slot']['day-' . $timelapse->d])) {
            $files['thisweek_slot']['day-' . $timelapse->d] = array();
          }
          $files['thisweek_slot']['day-' . $timelapse->d][$filedate
            ->format('G')][] = $file;
        }
        elseif ($timelapse->m < 1 && $timelapse->y < 1) {
          if (!isset($files['thismonth_slot']['week-' . $week_num])) {
            $files['thismonth_slot']['week-' . $week_num] = array();
          }
          $files['thismonth_slot']['week-' . $week_num][$filedate
            ->format('N')][] = $file;
        }
        elseif ($timelapse->y < 1) {
          if (!isset($files['thisyear_slot']['month-' . $timelapse->m])) {
            $files['thisyear_slot']['month-' . $timelapse->m] = array();
          }
          $files['thisyear_slot']['month-' . $timelapse->m][$week_num][] = $file;
        }
        else {
          if (!isset($files['pastyears_slot']['year-' . $timelapse->y])) {
            $files['pastyears_slot']['year-' . $timelapse->y] = array();
          }
          $files['pastyears_slot']['year-' . $timelapse->y][$filedate
            ->format('n')][] = $file;
        }
      }
    }
  }
  return $files;
}