2015-02-08 08:03:55 +01:00
|
|
|
<?php
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
class BlogMigrationTask extends MigrationTask
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Should this task be invoked automatically via dev/build?
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $run_during_dev_build = true;
|
2015-02-08 08:03:55 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function up()
|
|
|
|
{
|
|
|
|
$classes = ClassInfo::implementorsOf('MigratableObject');
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->message('Migrating legacy blog records');
|
2015-02-08 08:03:55 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
foreach ($classes as $class) {
|
|
|
|
$this->upClass($class);
|
|
|
|
}
|
|
|
|
}
|
2015-02-08 08:03:55 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @param string $text
|
|
|
|
*/
|
|
|
|
protected function message($text)
|
|
|
|
{
|
|
|
|
if (Controller::curr() instanceof DatabaseAdmin) {
|
|
|
|
DB::alteration_message($text, 'obsolete');
|
|
|
|
} else {
|
|
|
|
echo $text . "<br/>";
|
|
|
|
}
|
|
|
|
}
|
2015-02-08 08:03:55 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* Migrate records of a single class
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param null|string $stage
|
|
|
|
*/
|
|
|
|
protected function upClass($class)
|
|
|
|
{
|
|
|
|
if (!class_exists($class)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-09 22:45:39 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
if (is_subclass_of($class, 'SiteTree')) {
|
|
|
|
$items = SiteTree::get()->filter('ClassName', $class);
|
|
|
|
} else {
|
|
|
|
$items = $class::get();
|
|
|
|
}
|
2015-03-09 22:45:39 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
if ($count = $items->count()) {
|
|
|
|
$this->message(
|
|
|
|
sprintf(
|
|
|
|
'Migrating %s legacy %s records.',
|
|
|
|
$count,
|
|
|
|
$class
|
|
|
|
)
|
|
|
|
);
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
foreach ($items as $item) {
|
|
|
|
$cancel = $item->extend('onBeforeUp');
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
if ($cancel && min($cancel) === false) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @var MigratableObject $item
|
|
|
|
*/
|
|
|
|
$result = $item->up();
|
|
|
|
$this->message($result);
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$item->extend('onAfterUp');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-09 16:33:12 +02:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function down()
|
|
|
|
{
|
|
|
|
$this->message('BlogMigrationTask::down() not implemented');
|
|
|
|
}
|
2015-02-08 08:03:55 +01:00
|
|
|
}
|