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

93 lines
1.7 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) {
if(is_subclass_of($class, 'SiteTree')) {
foreach(array('Stage', 'Live') as $stage) {
$oldMode = Versioned::get_reading_mode();
Versioned::reading_stage($stage);
$this->upClass($class, $stage);
Versioned::set_reading_mode($oldMode);
}
} else {
$this->upClass($class);
}
}
}
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 {
Debug::message($text);
}
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, $stage = null) {
2015-05-09 16:33:12 +02:00
if(!class_exists($class)) {
return;
}
2015-03-09 22:45:39 +01:00
2015-02-08 08:03:55 +01:00
$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 in stage %s.',
$count,
$class,
$stage
)
);
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
*/
2015-02-08 08:03:55 +01:00
$item->up();
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
}