Merge pull request #277 from camfindlay/migrate

FIX Migration of blog 1.0 to 2.0 was broken
This commit is contained in:
Daniel Hensby 2015-06-27 22:31:03 +01:00
commit b2d1584adc
6 changed files with 83 additions and 39 deletions

View File

@ -34,30 +34,53 @@ class BlogEntry extends BlogPost implements MigratableObject {
* {@inheritdoc}
*/
public function up() {
foreach($this->TagNames() as $tag) {
if($this->Tags()->filter('Title', $tag)->count()) {
continue;
}
//Migrate comma separated tags into BlogTag objects.
foreach($this->TagNames() as $tag) {
$existingTag = BlogTag::get()->filter('Title', $tag);
if($existingTag->count()) {
//if tag already exists we will simply add it to this post.
$tagObject = $existingTag->First();
} else {
//if the tag is now we create it and add it to this post.
$tagObject = new BlogTag();
$tagObject->Title = $tag;
$tagObject->BlogID = $this->ParentID;
$tagObject->write();
$this->Tags()->add($tagObject);
}
$this->PublishDate = $this->Date;
$this->AuthorNames = $this->Author;
if($tagObject){
$this->Tags()->add($tagObject);
}
}
//Store if the original entity was published or not (draft)
$published = $this->IsPublished();
// If a user has subclassed BlogEntry, it should not be turned into a BlogPost.
if($this->ClassName === 'BlogEntry') {
$this->ClassName = 'BlogPost';
$this->RecordClassName = 'BlogPost';
}
//Migrate these key data attributes
$this->PublishDate = $this->Date;
$this->AuthorNames = $this->Author;
$this->InheritSideBar = true;
//Write and additionally publish the item if it was published before.
$this->write();
if($published){
$this->publish('Stage','Live');
$message = "PUBLISHED: ";
} else {
$message = "DRAFT: ";
}
return $message . $this->Title;
}
/**
@ -79,17 +102,6 @@ class BlogEntry extends BlogPost implements MigratableObject {
return $results;
}
/**
* {@inheritdoc}
*/
public function requireDefaultRecords() {
parent::requireDefaultRecords();
if(BlogMigrationTask::config()->run_during_dev_build) {
$task = new BlogMigrationTask();
$task->up();
}
}
}
/**

View File

@ -31,14 +31,36 @@ class BlogHolder extends BlogTree implements MigratableObject {
return false;
}
//Overload these to stop the Uncaught Exception: Object->__call(): the method 'parent' does not exist on 'BlogHolder' error.
public function validURLSegment() {
return true;
}
public function syncLinkTracking() {
return null;
}
/**
* {@inheritdoc}
*/
public function up() {
$published = $this->IsPublished();
if($this->ClassName === 'BlogHolder') {
$this->ClassName = 'Blog';
$this->RecordClassName = 'Blog';
$this->write();
}
if($published){
$this->publish('Stage','Live');
$message = "PUBLISHED: ";
} else {
$message = "DRAFT: ";
}
return $message . $this->Title;
}
}

View File

@ -28,10 +28,20 @@ class BlogTree extends Page implements MigratableObject {
* {@inheritdoc}
*/
public function up() {
$published = $this->IsPublished();
if($this->ClassName === 'BlogTree') {
$this->ClassName = 'Page';
$this->RecordClassName = 'Page';
$this->write();
}
if($published){
$this->publish('Stage','Live');
$message = "PUBLISHED: ";
} else {
$message = "DRAFT: ";
}
return $message . $this->Title;
}
}

View File

@ -19,17 +19,10 @@ class BlogMigrationTask extends MigrationTask {
$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);
}
}
}
/**
@ -39,7 +32,7 @@ class BlogMigrationTask extends MigrationTask {
if(Controller::curr() instanceof DatabaseAdmin) {
DB::alteration_message($text, 'obsolete');
} else {
Debug::message($text);
echo $text . "<br/>";
}
}
@ -49,20 +42,23 @@ class BlogMigrationTask extends MigrationTask {
* @param string $class
* @param null|string $stage
*/
protected function upClass($class, $stage = null) {
protected function upClass($class) {
if(!class_exists($class)) {
return;
}
if(is_subclass_of($class, 'SiteTree')) {
$items = SiteTree::get()->filter('ClassName', $class);
} else {
$items = $class::get();
}
if($count = $items->count()) {
$this->message(
sprintf(
'Migrating %s legacy %s records in stage %s.',
'Migrating %s legacy %s records.',
$count,
$class,
$stage
$class
)
);
@ -76,7 +72,8 @@ class BlogMigrationTask extends MigrationTask {
/**
* @var MigratableObject $item
*/
$item->up();
$result = $item->up();
$this->message($result);
$item->extend('onAfterUp');
}

View File

@ -46,5 +46,7 @@ class ArchiveWidget extends BlogArchiveWidget implements MigratableObject {
$this->ClassName = 'BlogArchiveWidget';
$this->write();
return "Migrated " . $this->ArchiveType . " archive widget";
}
}

View File

@ -39,5 +39,6 @@ class TagCloudWidget extends BlogTagsWidget implements MigratableObject {
public function up() {
$this->ClassName = 'BlogTagsWidget';
$this->write();
return "Migrated " . $this->Title . " widget";
}
}