function run($request) { set_time_limit(0); // check to see if this has been run before. If it has then we will have already // have removed the parentID field $checkForMigration = DB::query("SHOW COLUMNS FROM \"Post\"")->column(); if (!in_array('ParentID', $checkForMigration)) { echo "Script has already ran. You can only run the migration script once.\n"; return false; } // go through all the posts with a parent ID = 0 and create the new thread objects $oldThreads = DB::query("SELECT * FROM \"Post\" WHERE \"ParentID\" = '0'"); if ($oldThreads) { $toCreateTotal = $oldThreads->numRecords(); echo "Creating " . $toCreateTotal . " new Forum Threads \n"; $holder = DataObject::get_one("ForumHolder"); if (!$holder) { return user_error('No Forum Holder Found', E_USER_ERROR); } $failbackForum = new Forum(); $failbackForum->Title = "Unimported Threads"; $failbackForum->ParentID = $holder->ID; $failbackForum->write(); $needsFailback = false; $totalThreadsSuccessfulCount = 0; $totalThreadsErroredCount = 0; while ($oldThread = $oldThreads->nextRecord()) { $hasError = false; $thread = new ForumThread(); if (isset($oldThread['Title'])) { $thread->Title = $oldThread['Title']; } else { $hasError = true; $thread->Title = "Question"; } $thread->NumViews = isset($oldThread['NumViews']) ? $oldThread['NumViews'] : 0; $thread->IsSticky = isset($oldThread['IsSticky']) ? $oldThread['IsSticky'] : false; $thread->IsReadOnly = isset($oldThread['IsReadOnly']) ? $oldThread['IsReadOnly'] : false; $thread->IsGlobalSticky = isset($oldThread['IsGlobalSticky']) ? $oldThread['IsGlobalSticky'] : false; if (isset($oldThread['ForumID'])) { $thread->ForumID = $oldThread['ForumID']; } else { $hasError = true; $needsFailback = true; $thread->ForumID = $failbackForum->ID; } $thread->write(); echo "Converted Thread: {$thread->ID} - {$thread->Title}. \n"; // update all the children DB::query("\n\t\t\t\t\tUPDATE \"Post\" \n\t\t\t\t\tSET \"ThreadID\" = '{$thread->ID}', \"ForumID\" = '{$thread->ForumID}'\n\t\t\t\t\tWHERE \"TopicID\" = '" . $oldThread['ID'] . "'\n\t\t\t\t"); if (!$hasError) { $totalThreadsSuccessfulCount++; } else { $totalThreadsErroredCount++; } } } echo "Converted {$totalThreadsSuccessfulCount} threads. Could not import {$totalThreadsErroredCount} threads.<br />"; if (!$needsFailback) { $failbackForum->delete(); } else { echo "Incorrectly imported threads are available to self moderate at <a href='" . $failbackForum->Link() . "'>here</a><br />"; } // transfer subscriptions // was a rename table but mysql had locking issues. $subscriptions = DB::query("SELECT * FROM \"Post_Subscription\""); $subCount = 0; if ($subscriptions) { while ($sub = $subscriptions->nextRecord()) { // don't import really odd data if (isset($sub['TopicID']) && isset($sub['MemberID'])) { $subCount++; $threadSub = new ForumThread_Subscription(); $threadSub->ThreadID = $sub['TopicID']; $threadSub->MemberID = $sub['MemberID']; $threadSub->write(); } } } echo "Transferred {$subCount} Thread Subscriptions<br />"; // Update the permissions on the forums. The Posters, Viewers have changed from a int field // to an actual modelled relationship $forums = DataObject::get('Forum'); if ($forums) { foreach ($forums as $forum) { $forum->ForumPostersGroupID = DB::query("SELECT \"ForumPostersGroup\" FROM \"Forum\" WHERE \"ID\" = '{$forum->ID}'")->value(); if ($viewingGroup = DB::query("SELECT \"ForumViewersGroup\" FROM \"Forum\" WHERE \"ID\" = '{$forum->ID}'")->value()) { $forum->ViewerGroups()->add(DataObject::get_by_id('Group', $viewingGroup)); } $forum->write(); } } // cleanup task. Delete old columns which are hanging round DB::dontRequireField('Post', 'ParentID'); DB::dontRequireField('Post', 'TopicID'); DB::dontRequireField('Post', 'Title'); DB::dontRequireField('Post', 'NumViews'); DB::dontRequireField('Post', 'IsSticky'); DB::dontRequireField('Post', 'IsReadOnly'); DB::dontRequireField('Post', 'IsGlobalSticky'); DB::dontRequireTable('Post_Subscription'); DB::dontRequireField('Forum', 'ForumViewersGroup'); DB::dontRequireField('Forum', 'ForumViewersGroupID'); DB::dontRequireField("Forum", 'ForumPostersGroup'); echo "Renamed old data columns in Post and removed Post_Subscription table <br />"; $this->attachLastPostIDs(); echo "Set ForumThread last post and update LastEdited to the most recent post <br />"; echo "Finished<br />"; }
/** * Add default records to database * * This function is called whenever the database is built, after the * database tables have all been created. */ public function requireDefaultRecords() { parent::requireDefaultRecords(); $code = "ACCESS_FORUM"; if (!($forumGroup = DataObject::get_one("Group", "\"Group\".\"Code\" = 'forum-members'"))) { $group = new Group(); $group->Code = 'forum-members'; $group->Title = "Forum Members"; $group->write(); Permission::grant($group->ID, $code); DB::alteration_message(_t('Forum.GROUPCREATED', 'Forum Members group created'), "created"); } else { if (DB::query("SELECT * FROM \"Permission\" WHERE \"GroupID\" = '{$forumGroup->ID}' AND \"Code\" LIKE '{$code}'")->numRecords() == 0) { Permission::grant($forumGroup->ID, $code); } } if (!($category = DataObject::get_one("ForumCategory"))) { $category = new ForumCategory(); $category->Title = _t('Forum.DEFAULTCATEGORY', 'General'); $category->write(); } if (!DataObject::get_one("ForumHolder")) { $forumholder = new ForumHolder(); $forumholder->Title = "Forums"; $forumholder->URLSegment = "forums"; $forumholder->Content = "<p>" . _t('Forum.WELCOMEFORUMHOLDER', 'Welcome to SilverStripe Forum Module! This is the default ForumHolder page. You can now add forums.') . "</p>"; $forumholder->Status = "Published"; $forumholder->write(); $forumholder->publish("Stage", "Live"); DB::alteration_message(_t('Forum.FORUMHOLDERCREATED', 'ForumHolder page created'), "created"); $forum = new Forum(); $forum->Title = _t('Forum.TITLE', 'General Discussion'); $forum->URLSegment = "general-discussion"; $forum->ParentID = $forumholder->ID; $forum->Content = "<p>" . _t('Forum.WELCOMEFORUM', 'Welcome to SilverStripe Forum Module! This is the default Forum page. You can now add topics.') . "</p>"; $forum->Status = "Published"; $forum->CategoryID = $category->ID; $forum->write(); $forum->publish("Stage", "Live"); DB::alteration_message(_t('Forum.FORUMCREATED', 'Forum page created'), "created"); } }
/** * Add default records to database * * This function is called whenever the database is built, after the * database tables have all been created. */ public function requireDefaultRecords() { parent::requireDefaultRecords(); $code = "ACCESS_FORUM"; if (!($forumGroup = Group::get()->filter('Code', 'forum-members')->first())) { $group = new Group(); $group->Code = 'forum-members'; $group->Title = "Forum Members"; $group->write(); Permission::grant($group->ID, $code); DB::alteration_message(_t('Forum.GROUPCREATED', 'Forum Members group created'), 'created'); } else { if (!Permission::get()->filter(array('GroupID' => $forumGroup->ID, 'Code' => $code))->exists()) { Permission::grant($forumGroup->ID, $code); } } if (!($category = ForumCategory::get()->first())) { $category = new ForumCategory(); $category->Title = _t('Forum.DEFAULTCATEGORY', 'General'); $category->write(); } if (!ForumHolder::get()->exists()) { $forumholder = new ForumHolder(); $forumholder->Title = "Forums"; $forumholder->URLSegment = "forums"; $forumholder->Content = "<p>" . _t('Forum.WELCOMEFORUMHOLDER', 'Welcome to SilverStripe Forum Module! This is the default ForumHolder page. You can now add forums.') . "</p>"; $forumholder->Status = "Published"; $forumholder->write(); $forumholder->publish("Stage", "Live"); DB::alteration_message(_t('Forum.FORUMHOLDERCREATED', 'ForumHolder page created'), "created"); $forum = new Forum(); $forum->Title = _t('Forum.TITLE', 'General Discussion'); $forum->URLSegment = "general-discussion"; $forum->ParentID = $forumholder->ID; $forum->Content = "<p>" . _t('Forum.WELCOMEFORUM', 'Welcome to SilverStripe Forum Module! This is the default Forum page. You can now add topics.') . "</p>"; $forum->Status = "Published"; $forum->CategoryID = $category->ID; $forum->write(); $forum->publish("Stage", "Live"); DB::alteration_message(_t('Forum.FORUMCREATED', 'Forum page created'), "created"); } }