FIX Migration of blog 1.0 to 2.0 was broken

This commit is contained in:
Cam Findlay 2015-06-27 19:05:51 +12:00
parent 82681d3073
commit f86f7751b1
6 changed files with 83 additions and 39 deletions

View File

@ -34,30 +34,53 @@ class BlogEntry extends BlogPost implements MigratableObject {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function up() { 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 = new BlogTag();
$tagObject->Title = $tag; $tagObject->Title = $tag;
$tagObject->BlogID = $this->ParentID; $tagObject->BlogID = $this->ParentID;
$tagObject->write(); $tagObject->write();
$this->Tags()->add($tagObject);
} }
$this->PublishDate = $this->Date; if($tagObject){
$this->AuthorNames = $this->Author; $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 a user has subclassed BlogEntry, it should not be turned into a BlogPost.
if($this->ClassName === 'BlogEntry') { if($this->ClassName === 'BlogEntry') {
$this->ClassName = 'BlogPost'; $this->ClassName = 'BlogPost';
$this->RecordClassName = '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(); $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; 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; 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} * {@inheritdoc}
*/ */
public function up() { public function up() {
$published = $this->IsPublished();
if($this->ClassName === 'BlogHolder') { if($this->ClassName === 'BlogHolder') {
$this->ClassName = 'Blog'; $this->ClassName = 'Blog';
$this->RecordClassName = 'Blog';
$this->write(); $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} * {@inheritdoc}
*/ */
public function up() { public function up() {
$published = $this->IsPublished();
if($this->ClassName === 'BlogTree') { if($this->ClassName === 'BlogTree') {
$this->ClassName = 'Page'; $this->ClassName = 'Page';
$this->RecordClassName = 'Page';
$this->write(); $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'); $this->message('Migrating legacy blog records');
foreach($classes as $class) { 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); $this->upClass($class);
} }
}
} }
/** /**
@ -39,7 +32,7 @@ class BlogMigrationTask extends MigrationTask {
if(Controller::curr() instanceof DatabaseAdmin) { if(Controller::curr() instanceof DatabaseAdmin) {
DB::alteration_message($text, 'obsolete'); DB::alteration_message($text, 'obsolete');
} else { } else {
Debug::message($text); echo $text . "<br/>";
} }
} }
@ -49,20 +42,23 @@ class BlogMigrationTask extends MigrationTask {
* @param string $class * @param string $class
* @param null|string $stage * @param null|string $stage
*/ */
protected function upClass($class, $stage = null) { protected function upClass($class) {
if(!class_exists($class)) { if(!class_exists($class)) {
return; return;
} }
if(is_subclass_of($class, 'SiteTree')) {
$items = SiteTree::get()->filter('ClassName', $class);
} else {
$items = $class::get(); $items = $class::get();
}
if($count = $items->count()) { if($count = $items->count()) {
$this->message( $this->message(
sprintf( sprintf(
'Migrating %s legacy %s records in stage %s.', 'Migrating %s legacy %s records.',
$count, $count,
$class, $class
$stage
) )
); );
@ -76,7 +72,8 @@ class BlogMigrationTask extends MigrationTask {
/** /**
* @var MigratableObject $item * @var MigratableObject $item
*/ */
$item->up(); $result = $item->up();
$this->message($result);
$item->extend('onAfterUp'); $item->extend('onAfterUp');
} }

View File

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

View File

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