Skip to content

Commit

Permalink
Silverstripe 3 to 4 migration feature review
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Apr 30, 2020
1 parent 74ed315 commit e5af4d2
Show file tree
Hide file tree
Showing 29 changed files with 1,792 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/BatchPublish/Asset/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\BatchPublish\Asset;

use App\Queue;
use SilverStripe\Assets\File;
use SilverStripe\Versioned\Versioned;

class Job extends Queue\Job
{

public function getTitle(): string
{
return 'Batch publish asset';
}

public function hydrate(array $items): void
{
$this->items = $items;
}

/**
* @param mixed $item
*/
protected function processItem($item): void
{
Versioned::withVersionedMode(function () use ($item): void {
Versioned::set_stage(Versioned::DRAFT);

/** @var File $file */
$file = File::get()->byID($item);

if (!$file || !$file->exists()) {
$this->addMessage('File not found ' . $item);

return;
}

$file->write();

// force new version to be written
$file->copyVersionToStage(Versioned::DRAFT, Versioned::DRAFT);

$file->publishRecursive();
});
}
}
37 changes: 37 additions & 0 deletions src/BatchPublish/Asset/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\BatchPublish\Asset;

use App\Queue\Factory;
use SilverStripe\Assets\File;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Versioned\Versioned;

class Task extends Factory\Task
{

private const CHUNK_SIZE = 5;

/**
* @var string
*/
private static $segment = 'batch-publish-asset-task';

public function getDescription(): string
{
return 'Generate asset publish jobs';
}

/**
* @param HTTPRequest $request
*/
public function run($request): void // phpcs:ignore SlevomatCodingStandard.TypeHints
{
Versioned::withVersionedMode(function () use ($request): void {
Versioned::set_stage(Versioned::LIVE);

$list = File::get()->sort('ID', 'ASC');
$this->queueJobsFromList($request, $list, Job::class, self::CHUNK_SIZE);
});
}
}
51 changes: 51 additions & 0 deletions src/BatchPublish/Folder/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\BatchPublish\Folder;

use App\Queue;
use SilverStripe\Assets\Folder;
use SilverStripe\Versioned\Versioned;

class Job extends Queue\Job
{

public function getTitle(): string
{
return 'Batch publish folder';
}

public function hydrate(array $items): void
{
$this->items = $items;
}

/**
* @param mixed $item
*/
protected function processItem($item): void
{
Versioned::withVersionedMode(function () use ($item): void {
Versioned::set_stage(Versioned::DRAFT);

/** @var Folder $folder */
$folder = Folder::get()->byID($item);

if (!$folder) {
$this->addMessage('Folder not found ' . $item);

return;
}

if ($folder->isPublished()) {
return;
}

$folder->write();

// force new version to be written
$folder->copyVersionToStage(Versioned::DRAFT, Versioned::DRAFT);

$folder->publishRecursive();
});
}
}
39 changes: 39 additions & 0 deletions src/BatchPublish/Folder/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\BatchPublish\Folder;

use App\Queue\Factory;
use SilverStripe\Assets\Folder;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Versioned\Versioned;

class Task extends Factory\Task
{

private const CHUNK_SIZE = 1;

/**
* @var string
*/
private static $segment = 'batch-publish-folder-task';

public function getDescription(): string
{
return 'Generate folder publish jobs';
}

/**
* @param HTTPRequest $request
*/
public function run($request): void // phpcs:ignore SlevomatCodingStandard.TypeHints
{
Versioned::withVersionedMode(function () use ($request): void {
Versioned::set_stage(Versioned::DRAFT);

$list = Folder::get()
->sort('ID', 'ASC');

$this->queueJobsFromList($request, $list, Job::class, self::CHUNK_SIZE);
});
}
}
50 changes: 50 additions & 0 deletions src/BatchPublish/Page/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\BatchPublish\Page;

use App\Queue;
use Page;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Versioned\Versioned;

class Job extends Queue\Job
{

public function getTitle(): string
{
return 'Batch publish page';
}

public function hydrate(array $items): void
{
$this->items = $items;
}

/**
* @param mixed $item
*/
protected function processItem($item): void
{
Versioned::withVersionedMode(function () use ($item): void {
Versioned::set_stage(Versioned::DRAFT);

/** @var SiteTree $page */
$page = SiteTree::get()->byID($item);

if ($page === null) {
$this->addMessage('Page not found ' . $item);

return;
}

Page::singleton()->withSkippedSiblingSortPublish(static function () use ($page): void {
$page->write();

// force new version to be written
$page->copyVersionToStage(Versioned::DRAFT, Versioned::DRAFT);

$page->publishRecursive();
});
});
}
}
37 changes: 37 additions & 0 deletions src/BatchPublish/Page/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\BatchPublish\Page;

use App\Queue\Factory;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Versioned\Versioned;

class Task extends Factory\Task
{

private const CHUNK_SIZE = 1;

/**
* @var string
*/
private static $segment = 'batch-publish-page-task';

public function getDescription(): string
{
return 'Generate page publish jobs';
}

/**
* @param HTTPRequest $request
*/
public function run($request): void // phpcs:ignore SlevomatCodingStandard.TypeHints
{
Versioned::withVersionedMode(function () use ($request): void {
Versioned::set_stage(Versioned::LIVE);

$list = SiteTree::get()->sort('ID', 'ASC');
$this->queueJobsFromList($request, $list, Job::class, self::CHUNK_SIZE);
});
}
}
60 changes: 60 additions & 0 deletions src/FileMigration/FileBinary/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\FileMigration\FileBinary;

use Generator;
use SilverStripe\Assets\Dev\Tasks\FileMigrationHelper;
use SilverStripe\Assets\File;
use SilverStripe\ORM\DataList;

class Helper extends FileMigrationHelper
{

/**
* @var array
*/
private $ids = [];

public function setIds(array $ids): self
{
$this->ids = $ids;

return $this;
}

public function getFileQuery(): DataList
{
// public scope change
return parent::getFileQuery();
}

/**
* Code taken from @see FileMigrationHelper::chunk()
*
* @param DataList $query
* @return Generator
*/
protected function chunk(DataList $query): Generator
{
// only select specified files
$query = $query->byIDs($this->ids);

// the rest of the code is just a copy from base
$chunkSize = 100;
$greaterThanID = 0;
$query = $query->limit($chunkSize)->sort('ID');

while ($chunk = $query->filter('ID:GreaterThan', $greaterThanID)) {
/** @var File $file */
foreach ($chunk as $file) {
yield $file;
}

if ($chunk->count() === 0) {
break;
}

$greaterThanID = $file->ID;
}
}
}
Loading

0 comments on commit e5af4d2

Please sign in to comment.