public function updateCounts()
{
// This option could take a while so set the timeout.
increaseMaxExecutionTime(60 * 10);
// Define the necessary SQL.
$Sqls = array();
if (!$this->importExists('Discussion', 'LastCommentID')) {
$Sqls['Discussion.LastCommentID'] = $this->GetCountSQL('max', 'Discussion', 'Comment');
}
if (!$this->importExists('Discussion', 'DateLastComment')) {
$Sqls['Discussion.DateLastComment'] = "update :_Discussion d\n left join :_Comment c\n on d.LastCommentID = c.CommentID\n set d.DateLastComment = coalesce(c.DateInserted, d.DateInserted)";
}
if (!$this->importExists('Discussion', 'LastCommentUserID')) {
$Sqls['Discussion.LastCommentUseID'] = "update :_Discussion d\n join :_Comment c\n on d.LastCommentID = c.CommentID\n set d.LastCommentUserID = c.InsertUserID";
}
if (!$this->importExists('Discussion', 'Body')) {
// Update the body of the discussion if it isn't there.
if (!$this->importExists('Discussion', 'FirstCommentID')) {
$Sqls['Discussion.FirstCommentID'] = $this->GetCountSQL('min', 'Discussion', 'Comment', 'FirstCommentID', 'CommentID');
}
$Sqls['Discussion.Body'] = "update :_Discussion d\n join :_Comment c\n on d.FirstCommentID = c.CommentID\n set d.Body = c.Body, d.Format = c.Format";
if ($this->importExists('Media') && Gdn::structure()->TableExists('Media')) {
// Comment Media has to go onto the discussion.
$Sqls['Media.Foreign'] = "update :_Media m\n join :_Discussion d\n on d.FirstCommentID = m.ForeignID and m.ForeignTable = 'comment'\n set m.ForeignID = d.DiscussionID, m.ForeignTable = 'discussion'";
}
$Sqls['Comment.FirstComment.Delete'] = "delete c.*\n from :_Comment c\n inner join :_Discussion d\n on d.FirstCommentID = c.CommentID";
}
if (!$this->importExists('Discussion', 'CountComments')) {
$Sqls['Discussion.CountComments'] = $this->GetCountSQL('count', 'Discussion', 'Comment');
}
if ($this->importExists('UserDiscussion') && !$this->importExists('UserDiscussion', 'CountComments') && $this->importExists('UserDiscussion', 'DateLastViewed')) {
$Sqls['UserDiscussuion.CountComments'] = "update :_UserDiscussion ud\n set CountComments = (\n select count(c.CommentID)\n from :_Comment c\n where c.DiscussionID = ud.DiscussionID\n and c.DateInserted <= ud.DateLastViewed)";
}
if ($this->importExists('Tag') && $this->importExists('TagDiscussion')) {
$Sqls['Tag.CoundDiscussions'] = $this->GetCountSQL('count', 'Tag', 'TagDiscussion', 'CountDiscussions', 'TagID');
}
if ($this->importExists('Poll') && Gdn::structure()->TableExists('Poll')) {
$Sqls['PollOption.CountVotes'] = $this->GetCountSQL('count', 'PollOption', 'PollVote', 'CountVotes', 'PollOptionID');
$Sqls['Poll.CountOptions'] = $this->GetCountSQL('count', 'Poll', 'PollOption', 'CountOptions', 'PollID');
$Sqls['Poll.CountVotes'] = $this->GetCountSQL('sum', 'Poll', 'PollOption', 'CountVotes', 'CountVotes', 'PollID');
}
if ($this->importExists('Activity', 'ActivityType')) {
$Sqls['Activity.ActivityTypeID'] = "\n update :_Activity a\n join :_ActivityType t\n on a.ActivityType = t.Name\n set a.ActivityTypeID = t.ActivityTypeID";
}
if ($this->importExists('Tag') && $this->importExists('TagDiscussion')) {
$Sqls['Tag.CoundDiscussions'] = $this->GetCountSQL('count', 'Tag', 'TagDiscussion', 'CountDiscussions', 'TagID');
}
$Sqls['Category.CountDiscussions'] = $this->GetCountSQL('count', 'Category', 'Discussion');
$Sqls['Category.CountComments'] = $this->GetCountSQL('sum', 'Category', 'Discussion', 'CountComments', 'CountComments');
if (!$this->importExists('Category', 'PermissionCategoryID')) {
$Sqls['Category.PermissionCategoryID'] = "update :_Category set PermissionCategoryID = -1";
}
if ($this->importExists('Conversation') && $this->importExists('ConversationMessage')) {
$Sqls['Conversation.FirstMessageID'] = $this->GetCountSQL('min', 'Conversation', 'ConversationMessage', 'FirstMessageID', 'MessageID');
if (!$this->importExists('Conversation', 'CountMessages')) {
$Sqls['Conversation.CountMessages'] = $this->GetCountSQL('count', 'Conversation', 'ConversationMessage', 'CountMessages', 'MessageID');
}
if (!$this->importExists('Conversation', 'LastMessageID')) {
$Sqls['Conversation.LastMessageID'] = $this->GetCountSQL('max', 'Conversation', 'ConversationMessage', 'LastMessageID', 'MessageID');
}
if (!$this->importExists('Conversation', 'DateUpdated')) {
$Sqls['Converstation.DateUpdated'] = "update :_Conversation c join :_ConversationMessage m on c.LastMessageID = m.MessageID set c.DateUpdated = m.DateInserted";
}
if ($this->importExists('UserConversation')) {
if (!$this->importExists('UserConversation', 'LastMessageID')) {
if ($this->importExists('UserConversation', 'DateLastViewed')) {
// Get the value from the DateLastViewed.
$Sqls['UserConversation.LastMessageID'] = "update :_UserConversation uc\n set LastMessageID = (\n select max(MessageID)\n from :_ConversationMessage m\n where m.ConversationID = uc.ConversationID\n and m.DateInserted >= uc.DateLastViewed)";
} else {
// Get the value from the conversation.
// In this case just mark all of the messages read.
$Sqls['UserConversation.LastMessageID'] = "update :_UserConversation uc\n join :_Conversation c\n on c.ConversationID = uc.ConversationID\n set uc.CountReadMessages = c.CountMessages,\n uc.LastMessageID = c.LastMessageID";
}
} elseif (!$this->importExists('UserConversation', 'DateLastViewed')) {
// We have the last message so grab the date from that.
$Sqls['UserConversation.DateLastViewed'] = "update :_UserConversation uc\n join :_ConversationMessage m\n on m.ConversationID = uc.ConversationID\n and m.MessageID = uc.LastMessageID\n set uc.DateLastViewed = m.DateInserted";
}
}
}
// User counts.
if (!$this->importExists('User', 'DateFirstVisit')) {
$Sqls['User.DateFirstVisit'] = 'update :_User set DateFirstVisit = DateInserted';
}
if (!$this->importExists('User', 'CountDiscussions')) {
$Sqls['User.CountDiscussions'] = $this->GetCountSQL('count', 'User', 'Discussion', 'CountDiscussions', 'DiscussionID', 'UserID', 'InsertUserID');
}
if (!$this->importExists('User', 'CountComments')) {
$Sqls['User.CountComments'] = $this->GetCountSQL('count', 'User', 'Comment', 'CountComments', 'CommentID', 'UserID', 'InsertUserID');
}
if (!$this->importExists('User', 'CountBookmarks')) {
$Sqls['User.CountBookmarks'] = "update :_User u\n set CountBookmarks = (\n select count(ud.DiscussionID)\n from :_UserDiscussion ud\n where ud.Bookmarked = 1\n and ud.UserID = u.UserID\n )";
}
// if (!$this->importExists('User', 'CountUnreadConversations')) {
// $Sqls['User.CountUnreadConversations'] =
// 'update :_User u
// set u.CountUnreadConversations = (
// select count(c.ConversationID)
// from :_Conversation c
// inner join :_UserConversation uc
// on c.ConversationID = uc.ConversationID
// where uc.UserID = u.UserID
// and uc.CountReadMessages < c.CountMessages
// )';
// }
// The updates start here.
$CurrentSubstep = val('CurrentSubstep', $this->Data, 0);
// $Sqls2 = array();
// $i = 1;
// foreach ($Sqls as $Name => $Sql) {
// $Sqls2[] = "/* $i. $Name */\n"
// .str_replace(':_', $this->Database->DatabasePrefix, $Sql)
// .";\n";
// $i++;
// }
// throw new Exception(implode("\n", $Sqls2));
// Execute the SQL.
$Keys = array_keys($Sqls);
for ($i = $CurrentSubstep; $i < count($Keys); $i++) {
$this->Data['CurrentStepMessage'] = sprintf(t('%s of %s'), $CurrentSubstep + 1, count($Keys));
$Sql = $Sqls[$Keys[$i]];
$this->query($Sql);
if ($this->Timer->ElapsedTime() > $this->MaxStepTime) {
$this->Data['CurrentSubstep'] = $i + 1;
return false;
}
}
if (isset($this->Data['CurrentSubstep'])) {
unset($this->Data['CurrentSubstep']);
}
$this->Data['CurrentStepMessage'] = '';
// Update the url codes of categories.
if (!$this->importExists('Category', 'UrlCode')) {
$Categories = CategoryModel::categories();
$TakenCodes = array();
foreach ($Categories as $Category) {
$UrlCode = urldecode(Gdn_Format::url($Category['Name']));
if (strlen($UrlCode) > 50) {
$UrlCode = 'c' . $Category['CategoryID'];
} elseif (is_numeric($UrlCode)) {
$UrlCode = 'c' . $UrlCode;
}
if (in_array($UrlCode, $TakenCodes)) {
$ParentCategory = CategoryModel::categories($Category['ParentCategoryID']);
if ($ParentCategory && $ParentCategory['CategoryID'] != -1) {
$UrlCode = Gdn_Format::url($ParentCategory['Name']) . '-' . $UrlCode;
}
if (in_array($UrlCode, $TakenCodes)) {
$UrlCode = $Category['CategoryID'];
}
}
$TakenCodes[] = $UrlCode;
Gdn::sql()->put('Category', array('UrlCode' => $UrlCode), array('CategoryID' => $Category['CategoryID']));
}
}
// Rebuild the category tree.
$CategoryModel = new CategoryModel();
$CategoryModel->RebuildTree();
$this->SetCategoryPermissionIDs();
return true;
}