Ejemplo n.º 1
0
 /**
  * Recalculate counters.
  *
  * @param bool $Table
  * @param bool $Column
  * @param bool $From
  * @param bool $To
  * @param bool $Max
  * @throws Exception
  * @throws Gdn_UserException
  */
 public function counts($Table = false, $Column = false, $From = false, $To = false, $Max = false)
 {
     increaseMaxExecutionTime(300);
     $this->permission('Garden.Settings.Manage');
     if ($Table && $Column && strcasecmp($this->Request->requestMethod(), Gdn_Request::INPUT_POST) == 0) {
         if (!ValidateRequired($Table)) {
             throw new Gdn_UserException("Table is required.");
         }
         if (!ValidateRequired($Column)) {
             throw new Gdn_UserException("Column is required.");
         }
         $Result = $this->Model->counts($Table, $Column, $From, $To);
         $this->setData('Result', $Result);
     } else {
         $this->setData('Jobs', array());
         $this->fireEvent('CountJobs');
     }
     $this->setData('Title', t('Recalculate Counts'));
     $this->render('Job');
 }
Ejemplo n.º 2
0
 /**
  *
  *
  * @return bool
  */
 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;
 }
Ejemplo n.º 3
0
 /**
  * Logic for export process.
  */
 public function doExport()
 {
     global $supported;
     // Test connection
     $msg = $this->testDatabase();
     if ($msg === true) {
         // Test src tables' existence structure
         $msg = $this->ex->verifySource($this->sourceTables);
         if ($msg === true) {
             // Good src tables - Start dump
             $this->ex->useCompression(true);
             $this->ex->filenamePrefix = $this->dbInfo['dbname'];
             increaseMaxExecutionTime(3600);
             //            ob_start();
             $this->forumExport($this->ex);
             //            $Errors = ob_get_clean();
             $msg = $this->ex->comments;
             // Write the results.  Send no path if we don't know where it went.
             $relativePath = $this->param('destpath', false) ? false : $this->ex->path;
             viewExportResult($msg, 'Info', $relativePath);
         } else {
             viewForm(array('Supported' => $supported, 'Msg' => $msg, 'Info' => $this->dbInfo));
         }
         // Back to form with error
     } else {
         viewForm(array('Supported' => $supported, 'Msg' => $msg, 'Info' => $this->dbInfo));
     }
     // Back to form with error
 }