Version: current

     1 <?php
     2 /*
     3  * Задача удаления неиспользуемых файлов с сайта
     4  *
     5  * @author menedem
     6  */
     7 class DeleteUnusedCustomerFilesTask extends BuildTask {
     8
     9     protected $title = "Delete Unused Files";
    10
    11     protected $description = "Script delete all files, that don't use at site";
    12
    13     /**
    14      * Run the conversion.
    15      *
    16      * @param HTTPRequest
    17      */
    18     function run($request) {
    19         $delete = false;
    20         if ($request->getVar('delete')) {
    21             $delete = true;
    22         }
    23         $IDs = DB::Query("SELECT DISTINCT FileID FROM DataObjectFileTracking")->column();
    24         if (count($IDs)) {
    25             $where = "ClassName <> 'Folder' AND ID NOT IN(" . implode(', ', $IDs) . ")";
    26             if ($files = DataObject::get('File', $where)) { // ??? по идее, памяти должно хватить ???
    27                 echo "\n";
    28                 $totalSize = 0;
    29                 $i = 0;
    30                 foreach($files as $file) {
    31                     $i++;
    32                     echo "{$i}. {$file->Filename} ({$file->FileSize})";
    33                     $totalSize += $file->{'FileSize'};
    34                     if ($delete) {
    35                         $file->delete();
    36                         echo " - deleted";
    37                     }
    38                     echo "\n";
    39                 }
    40                 echo "\nTotal files count: {$i}";
    41                 echo "\nTotal files size: " . File::format_size($totalSize) . " \n";
    42             }
    43         }
    44     }
    45 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -