silverstripe-blog/code/compat/tasks/BlogMigrationTask.php

90 lines
1.5 KiB
PHP
Raw Normal View History

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