From f86f7751b1ea4397db1c40e92e16407c28a29040 Mon Sep 17 00:00:00 2001 From: Cam Findlay Date: Sat, 27 Jun 2015 19:05:51 +1200 Subject: [PATCH] FIX Migration of blog 1.0 to 2.0 was broken --- code/compat/pages/BlogEntry.php | 58 +++++++++++++++---------- code/compat/pages/BlogHolder.php | 22 ++++++++++ code/compat/pages/BlogTree.php | 10 +++++ code/compat/tasks/BlogMigrationTask.php | 29 ++++++------- code/compat/widgets/ArchiveWidget.php | 2 + code/compat/widgets/TagCloudWidget.php | 1 + 6 files changed, 83 insertions(+), 39 deletions(-) diff --git a/code/compat/pages/BlogEntry.php b/code/compat/pages/BlogEntry.php index cd4e759..0724396 100644 --- a/code/compat/pages/BlogEntry.php +++ b/code/compat/pages/BlogEntry.php @@ -34,30 +34,53 @@ class BlogEntry extends BlogPost implements MigratableObject { * {@inheritdoc} */ public function up() { + + //Migrate comma separated tags into BlogTag objects. foreach($this->TagNames() as $tag) { - if($this->Tags()->filter('Title', $tag)->count()) { - continue; + + $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(); + + } - $tagObject = new BlogTag(); - $tagObject->Title = $tag; - $tagObject->BlogID = $this->ParentID; - - $tagObject->write(); - - $this->Tags()->add($tagObject); + if($tagObject){ + $this->Tags()->add($tagObject); + } } - $this->PublishDate = $this->Date; - $this->AuthorNames = $this->Author; - + //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(); - } - } } /** diff --git a/code/compat/pages/BlogHolder.php b/code/compat/pages/BlogHolder.php index 714d7ad..04dd093 100644 --- a/code/compat/pages/BlogHolder.php +++ b/code/compat/pages/BlogHolder.php @@ -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; } } diff --git a/code/compat/pages/BlogTree.php b/code/compat/pages/BlogTree.php index f62b026..9c72681 100644 --- a/code/compat/pages/BlogTree.php +++ b/code/compat/pages/BlogTree.php @@ -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; } } diff --git a/code/compat/tasks/BlogMigrationTask.php b/code/compat/tasks/BlogMigrationTask.php index fd636a3..d222633 100644 --- a/code/compat/tasks/BlogMigrationTask.php +++ b/code/compat/tasks/BlogMigrationTask.php @@ -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 . "
"; } } @@ -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; } - $items = $class::get(); + 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'); } diff --git a/code/compat/widgets/ArchiveWidget.php b/code/compat/widgets/ArchiveWidget.php index efab932..7cae2e0 100644 --- a/code/compat/widgets/ArchiveWidget.php +++ b/code/compat/widgets/ArchiveWidget.php @@ -46,5 +46,7 @@ class ArchiveWidget extends BlogArchiveWidget implements MigratableObject { $this->ClassName = 'BlogArchiveWidget'; $this->write(); + return "Migrated " . $this->ArchiveType . " archive widget"; + } } diff --git a/code/compat/widgets/TagCloudWidget.php b/code/compat/widgets/TagCloudWidget.php index 7ee3d57..5f7f832 100644 --- a/code/compat/widgets/TagCloudWidget.php +++ b/code/compat/widgets/TagCloudWidget.php @@ -39,5 +39,6 @@ class TagCloudWidget extends BlogTagsWidget implements MigratableObject { public function up() { $this->ClassName = 'BlogTagsWidget'; $this->write(); + return "Migrated " . $this->Title . " widget"; } }