Version: current

     1 <?php
     2
     3 /**
     4  * Задача для приведение AssociatedFolder к новой схеме формирования папок (page-files/$hash/$ID)
     5  * запускать: sake /dev/tasks/FolderRenameTask
     6  *
     7  * @author dvp
     8  */
     9
    10 class FolderRenameTask extends BuildTask {
    11
    12     protected $title = 'FolderRenameTask';
    13
    14     protected $description = 'Приводит AssociatedFolder к новой схеме формирования папок (page-files/$hash/$ID)';
    15
    16     function init() {
    17         if (!(Director::isDev() || Director::is_cli() || Permission::check("ADMIN")))
    18             return Security::permissionFailure($this);
    19
    20         parent::init();
    21     }
    22
    23     function run($request) {
    24         if (!Director::is_cli()) return 'Задача может быть запущена только с консоли';
    25
    26         $baseFolder = Folder::findOrMake(AssociatedFolderDecorator::getDefaultRootFolderName());
    27         print "New base: $baseFolder->ID $baseFolder->Filename\n\n";
    28
    29         // Убираем зацикливания
    30         DB::query('update File set ParentID=0 where ID=ParentID');
    31
    32         foreach (DataObject::get('MediawebPage') as $page) {
    33             if ($page->AssociatedFolderID == 0) continue;
    34
    35             if ($page->AssociatedFolderID == $baseFolder->ID) {
    36                 $page->AssociatedFolderID = 0;
    37                 $page->write();
    38                 continue;
    39             }
    40
    41             $folder = $page->AssociatedFolder();
    42
    43             if (!$folder || $folder->ID == 0) { // folder not found
    44                 // find by URLSegment
    45                 $folder = DataObject::get_one('Folder', "Name='".Convert::raw2sql($page->URLSegment)."'");
    46             }
    47
    48             if (!$folder || $folder->ID == $baseFolder->ID) {
    49                 $page->AssociatedFolderID = 0;
    50                 $page->write();
    51                 continue;
    52             }
    53
    54             print "\nP:$page->ID\tAF:$page->AssociatedFolderID\tF:$folder->ID\t$folder->Filename\n";
    55
    56             // fix ID difference
    57             if ($page->AssociatedFolderID != $folder->ID) {
    58                 $page->AssociatedFolderID = $folder->ID;
    59                 $page->write();
    60             }
    61
    62             // already converted
    63             if ($folder->Parent()->ParentID == $baseFolder->ID) continue;
    64
    65             if (!$folder->hasChildren()) {
    66                 print "Empty $folder->Filename - DELETED\n";
    67                 $folder->delete();
    68                 $page->AssociatedFolderID = 0;
    69                 $page->write();
    70                 continue;
    71             }
    72
    73 //continue;
    74             $parent = Folder::findOrMake($page->getAssociatedFolderParentName());
    75             $folder->setParentID($parent->ID);
    76             $folder->Title = $page->getAssociatedFolderTitle();
    77             $folder->Name = $page->ID;
    78             $folder->write();
    79             print "New path: $folder->Filename\n";
    80         }
    81     }
    82 }
    83

[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. -