예제 #1
0
 public function OldIndex($Step = 0)
 {
     $this->Permission('Garden.Data.Import');
     // This permission doesn't exist, so only users with Admin == '1' will succeed.
     $this->AddJsFile('upgrade.js', 'vanilla');
     $Step = is_numeric($Step) && $Step >= 0 && $Step < 20 ? $Step : '';
     $Database = Gdn::Database();
     $Construct = $Database->Structure();
     $PDO = $Database->Connection();
     $SourcePrefix = Gdn::Config('Garden.Import.SourcePrefix', 'LUM_');
     $DestPrefix = Gdn::Config('Database.DatabasePrefix', '');
     if ($Step == 0) {
         $this->View = 'import';
         if ($this->Form->AuthenticatedPostBack()) {
             // Make sure that all of the destination tables exist (assuming that
             // columns are there if tables are there since they were likely just
             // installed moments ago).
             $DbTables = $Database->SQL()->FetchTables();
             $DestTables = explode(',', 'Role,User,UserRole,Conversation,ConversationMessage,UserConversation,Category,Discussion,Comment,UserDiscussion');
             for ($i = 0; $i < count($DestTables); ++$i) {
                 $Table = $DestPrefix . $DestTables[$i];
                 if (!InArrayI($Table, $DbTables)) {
                     $this->Form->AddError('The "' . $Table . '" table is required for import.');
                     break;
                 }
             }
             if ($this->Form->ErrorCount() == 0) {
                 // Make sure that all of the source tables & columns exist.
                 $SourcePrefix = $this->Form->GetFormValue('SourcePrefix');
                 $SourceTables = explode(',', 'Role,User,UserRoleHistory,UserDiscussionWatch,UserBookmark,Category,Discussion,Comment');
                 for ($i = 0; $i < count($SourceTables); ++$i) {
                     $Table = $SourcePrefix . $SourceTables[$i];
                     if (!InArrayI($Table, $DbTables)) {
                         $this->Form->AddError('The "' . $Table . '" source table was not found. Are you sure "' . $SourcePrefix . '" is the correct table prefix for your Vanilla 1 tables?');
                         break;
                     }
                     $Columns = $Database->SQL()->FetchColumns($Table);
                     switch ($SourceTables[$i]) {
                         case 'Role':
                             $RequiredColumns = explode(',', 'RoleID,Name,Description');
                             break;
                         case 'User':
                             $RequiredColumns = explode(',', 'UserID,RoleID,Name,Email,UtilizeEmail,CountVisit,Discovery,DateFirstVisit,DateLastActive,DateFirstVisit,DateLastActive,CountDiscussions,CountComments');
                             break;
                         case 'UserRoleHistory':
                             $RequiredColumns = explode(',', 'UserID,RoleID,AdminUserID,Notes,Date');
                             break;
                         case 'UserDiscussionWatch':
                             $RequiredColumns = explode(',', 'UserID,DiscussionID,CountComments,LastViewed');
                             break;
                         case 'UserBookmark':
                             $RequiredColumns = explode(',', 'UserID,DiscussionID');
                             break;
                         case 'Category':
                             $RequiredColumns = explode(',', 'CategoryID,Name,Description,Priority');
                             break;
                         case 'Discussion':
                             $RequiredColumns = explode(',', 'DiscussionID,CategoryID,AuthUserID,LastUserID,WhisperUserID,Active,Name,CountComments,Closed,Sticky,Sink,DateCreated,DateLastActive');
                             break;
                         case 'Comment':
                             $RequiredColumns = explode(',', 'CommentID,DiscussionID,AuthUserID,EditUserID,WhisperUserID,Deleted,Body,FormatType,DateCreated,DateEdited');
                             break;
                         default:
                             $RequiredColumns = array();
                             break;
                     }
                     if (is_array($RequiredColumns)) {
                         for ($j = 0; $j < count($RequiredColumns); ++$j) {
                             if (!InArrayI($RequiredColumns[$j], $Columns)) {
                                 $this->Form->AddError('The "' . $Table . '" source table does not have the "' . $RequiredColumns[$j] . '" column.');
                                 break;
                             }
                         }
                     }
                 }
             }
             // If there were no errors...
             if ($this->Form->ErrorCount() == 0) {
                 // Save the sourceprefix
                 SaveToConfig('Garden.Import.SourcePrefix', $SourcePrefix);
                 // Proceed with the next step
                 $this->Message = T('<strong>1/19</strong> Checking source & destination tables.');
                 $this->View = 'index';
                 $this->RedirectUrl = Url('/upgrade/1');
                 if ($this->DeliveryType() == DELIVERY_TYPE_ALL) {
                     Redirect('/upgrade/1');
                 }
             }
         }
     } else {
         if ($Step == 1) {
             // 1. Add Import IDs to various tables where necessary
             $Construct->Table('Role')->Column('ImportID', 'int', TRUE, 'key')->Set();
             $Construct->Table('User')->Column('ImportID', 'int', TRUE, 'key')->Set();
             $Construct->Table('Category')->Column('ImportID', 'int', TRUE, 'key')->Set();
             $Construct->Table('Discussion')->Column('ImportID', 'int', TRUE, 'key')->Set();
             $Construct->DatabasePrefix($SourcePrefix);
             $Construct->Table('Comment')->Column('ConversationID', 'int', TRUE, 'key')->Set();
             $Construct->DatabasePrefix($DestPrefix);
             $this->Message = T('<strong>2/19</strong> Preparing tables for import.');
             $this->RedirectUrl = Url('/upgrade/2');
         } else {
             if ($Step == 2) {
                 // 2. Move roles from old database into new one.
                 $RoleModel = new RoleModel();
                 // Get the old roles
                 $OldRoles = $Database->Query('select * from ' . $SourcePrefix . 'Role');
                 // Loop through each, inserting if it doesn't exist and updating ImportID if it does
                 foreach ($OldRoles->Result() as $OldRole) {
                     $RoleData = $Database->Query("select * from " . $DestPrefix . "Role where Name = " . $PDO->quote($OldRole->Name));
                     if ($RoleData->NumRows() == 0) {
                         $Role = array();
                         $Role['ImportID'] = $OldRole->RoleID;
                         $Role['Name'] = $OldRole->Name;
                         $Role['Description'] = $OldRole->Description;
                         $RoleModel->Save($Role);
                     } else {
                         $Database->Query("update " . $DestPrefix . "Role set ImportID = '" . $OldRole->RoleID . "' where RoleID = " . $RoleData->FirstRow()->RoleID);
                     }
                 }
                 $this->Message = T('<strong>3/19</strong> Importing roles.');
                 $this->RedirectUrl = Url('/upgrade/3');
             } else {
                 if ($Step == 3) {
                     // 3. Import users
                     $Database->Query("insert into " . $DestPrefix . "User\n         (Name, Password, Email, ShowEmail,    Gender, CountVisits, CountInvitations, InviteUserID, DiscoveryText, Preferences, Permissions, Attributes, DateSetInvitations, DateOfBirth, DateFirstVisit, DateLastActive, DateInserted,   DateUpdated,    HourOffset, About, CountNotifications, CountUnreadConversations, CountDiscussions, CountUnreadDiscussions, CountComments, CountDrafts, CountBookmarks, ImportID) select\n          Name, Password, Email, UtilizeEmail, 'm',    CountVisit,  0,                null,         Discovery,     null,        null,        null,       null,               null,        DateFirstVisit, DateLastActive, DateFirstVisit, DateLastActive, 0,          null,  0,                  0,                        CountDiscussions, 0,                      CountComments, 0,           0,              UserID\n         from " . $SourcePrefix . "User");
                     $this->Message = T('<strong>4/19</strong> Importing users.');
                     $this->RedirectUrl = Url('/upgrade/4');
                 } else {
                     if ($Step == 4) {
                         // 4. Import user role relationships
                         $Database->Query("insert into " . $DestPrefix . "UserRole\n         (UserID, RoleID)\n         select u.UserID, r.RoleID\n         from " . $DestPrefix . "User u\n         inner join " . $SourcePrefix . "User ou\n            on u.ImportID = ou.UserID\n         inner join " . $DestPrefix . "Role r\n            on ou.RoleID = r.ImportID");
                         $this->Message = T('<strong>5/19</strong> Importing user/role relationships.');
                         $this->RedirectUrl = Url('/upgrade/5');
                     } else {
                         if ($Step == 5) {
                             // 5. Import user role history into activity table
                             $Database->Query("insert into " . $DestPrefix . "Activity\n         (ActivityTypeID, ActivityUserID, RegardingUserID, Story, InsertUserID, DateInserted)\n         select 9, au.UserID, nu.UserID, concat('Assigned to ', r.Name, ' Role <blockquote>', rh.Notes, '</blockquote>'), au.UserID, rh.Date\n         from " . $SourcePrefix . "UserRoleHistory rh\n         inner join " . $SourcePrefix . "Role r\n            on rh.RoleID = r.RoleID\n         inner join " . $SourcePrefix . "User u\n            on rh.UserID = u.UserID\n         inner join " . $DestPrefix . "User nu\n            on u.UserID = nu.ImportID\n         inner join " . $DestPrefix . "User au\n            on rh.AdminUserID = au.ImportID\n         order by rh.Date asc");
                             $this->Message = T('<strong>6/19</strong> Importing role histories.');
                             $this->RedirectUrl = Url('/upgrade/6');
                         } else {
                             if ($Step == 6) {
                                 // 6. Update the WhisperUserID on all comments that are within whispered discussions
                                 $Database->Query("update " . $SourcePrefix . "Comment c\n         join " . $SourcePrefix . "Discussion d\n           on c.DiscussionID = d.DiscussionID\n         set c.WhisperUserID = d.WhisperUserID\n         where d.WhisperUserID > 0\n           and c.AuthUserID <> d.WhisperUserID");
                                 $Database->Query("update " . $SourcePrefix . "Comment c\n         join " . $SourcePrefix . "Discussion d\n           on c.DiscussionID = d.DiscussionID\n         set c.WhisperUserID = d.AuthUserID\n         where d.WhisperUserID > 0\n           and c.AuthUserID <> d.AuthUserID");
                                 $this->Message = T('<strong>7/19</strong> Preparing whispers.');
                                 $this->RedirectUrl = Url('/upgrade/7');
                             } else {
                                 if ($Step == 7) {
                                     // 7. Create conversations
                                     $Database->Query("insert into " . $DestPrefix . "Conversation\n         (InsertUserID, DateInserted, UpdateUserID, DateUpdated, Contributors)\n         select AuthUserID, now(), WhisperUserID, now(), ''\n         from " . $SourcePrefix . "Comment\n         where WhisperUserID > 0\n         group by AuthUserID, WhisperUserID");
                                     // 7b. Remove duplicate combinations
                                     $Database->Query("delete " . $DestPrefix . "Conversation c\n         from " . $DestPrefix . "Conversation c\n         join " . $DestPrefix . "Conversation c2\n           on c.InsertUserID = c2.UpdateUserID\n           and c.UpdateUserID = c2.InsertUserID\n         where c.ConversationID > c2.ConversationID");
                                     $this->Message = T('<strong>8/19</strong> Creating conversations.');
                                     $this->RedirectUrl = Url('/upgrade/8');
                                 } else {
                                     if ($Step == 8) {
                                         // 8. Update old comment table with conversation ids
                                         $Database->Query("update " . $SourcePrefix . "Comment cm\n         inner join " . $DestPrefix . "Conversation cn\n           on cm.AuthUserID = cn.InsertUserID\n           and cm.WhisperUserID = cn.UpdateUserID\n         set cm.ConversationID = cn.ConversationID");
                                         $Database->Query("update " . $SourcePrefix . "Comment cm\n         inner join " . $DestPrefix . "Conversation cn\n           on cm.WhisperUserID = cn.InsertUserID\n           and cm.AuthUserID = cn.UpdateUserID\n         set cm.ConversationID = cn.ConversationID");
                                         $this->Message = T('<strong>9/19</strong> Preparing conversations messages.');
                                         $this->RedirectUrl = Url('/upgrade/9');
                                     } else {
                                         if ($Step == 9) {
                                             // 9. Insert whispers as conversation messages
                                             $Database->Query("insert into " . $DestPrefix . "ConversationMessage\n         (ConversationID, Body, InsertUserID, DateInserted)\n         select cm.ConversationID, cm.Body, nu.UserID, cm.DateCreated\n         from " . $SourcePrefix . "Comment cm\n         join " . $SourcePrefix . "User ou\n           on cm.AuthUserID = ou.UserID\n         inner join " . $DestPrefix . "User nu\n           on nu.ImportID = ou.UserID\n         where cm.ConversationID > 0");
                                             $this->Message = T('<strong>10/19</strong> Transforming whispers into conversations.');
                                             $this->RedirectUrl = Url('/upgrade/10');
                                         } else {
                                             if ($Step == 10) {
                                                 // 10. Insert the userconversation records so that messages are linked to conversations
                                                 $Database->Query("insert into " . $DestPrefix . "UserConversation\n         (UserID, ConversationID, CountNewMessages, CountMessages, LastMessageID, DateLastViewed)\n         select InsertUserID, ConversationID, 0, 0, max(MessageID), null\n         from " . $DestPrefix . "ConversationMessage\n         group by InsertUserID, ConversationID");
                                                 $this->Message = T('<strong>11/19</strong> Finalizing whisper messages.');
                                                 $this->RedirectUrl = Url('/upgrade/11');
                                             } else {
                                                 if ($Step == 11) {
                                                     // 11. Update the conversation record fields
                                                     $Database->Query("update " . $DestPrefix . "Conversation c\n         join (\n           select ConversationID, min(MessageID) as FirstMessageID, min(DateInserted) as DateInserted\n           from " . $DestPrefix . "ConversationMessage\n           group by ConversationID\n         ) cm\n           on c.ConversationID = cm.ConversationID\n         set c.FirstMessageID = cm.FirstMessageID,\n           c.DateInserted = cm.DateInserted");
                                                     $Database->Query("update " . $DestPrefix . "Conversation c\n         join (\n           select ConversationID, max(MessageID) as LastMessageID\n           from " . $DestPrefix . "ConversationMessage\n           group by ConversationID\n         ) cm\n           on c.ConversationID = cm.ConversationID\n         join " . $DestPrefix . "ConversationMessage lm\n           on cm.LastMessageID = lm.MessageID\n         set c.UpdateUserID = lm.InsertUserID,\n           c.DateUpdated = lm.DateInserted");
                                                     // Fudge your way back from the messages
                                                     $Database->Query("update " . $DestPrefix . "Conversation c\n         join " . $DestPrefix . "ConversationMessage m\n           on c.FirstMessageID = m.MessageID\n         set c.InsertUserID = m.InsertUserID");
                                                     // Update the UserConversation.LastMessageID records
                                                     // (ie. the last message in a conversation by someone other than the userconversation.userid person)
                                                     $Database->Query("update " . $DestPrefix . "UserConversation uc\n         join (\n           select ConversationID, InsertUserID, max(MessageID) as LastMessageID\n           from " . $DestPrefix . "ConversationMessage\n           group by ConversationID, InsertUserID\n         ) m\n           on uc.ConversationId = m.ConversationID\n           and uc.UserID <> m.InsertUserID\n         set uc.LastMessageID = m.LastMessageID");
                                                     // Update the message count for all users and all conversations
                                                     $Database->Query("update " . $DestPrefix . "UserConversation uc\n         join (\n           select ConversationID, count(MessageID) as CountMessages\n           from " . $DestPrefix . "ConversationMessage\n           group by ConversationID\n         ) m\n           on uc.ConversationID = m.ConversationID\n         set uc.CountMessages = m.CountMessages");
                                                     $this->Message = T('<strong>12/19</strong> Finalizing conversations.');
                                                     $this->RedirectUrl = Url('/upgrade/12');
                                                 } else {
                                                     if ($Step == 12) {
                                                         // 12. Import Categories
                                                         $Database->Query("insert into " . $DestPrefix . "Category\n         (Name, Description, Sort, InsertUserID, UpdateUserID, DateInserted, DateUpdated, ImportID)\n         select left(Name,30), Description, Priority, 1, 1, now(), now(), CategoryID\n         from " . $SourcePrefix . "Category");
                                                         $this->Message = T('<strong>13/19</strong> Importing discussion categories.');
                                                         $this->RedirectUrl = Url('/upgrade/13');
                                                     } else {
                                                         if ($Step == 13) {
                                                             // 13. Import Discussions
                                                             $Database->Query("insert into " . $DestPrefix . "Discussion\n         (ImportID, CategoryID, InsertUserID, UpdateUserID, Name, CountComments, Closed, Announce, Sink, DateInserted, DateUpdated, DateLastComment)\n         select od.DiscussionID, nc.CategoryID, niu.UserID, nuu.UserID, od.Name, od.CountComments, od.Closed, od.Sticky, od.Sink, od.DateCreated, od.DateLastActive, od.DateLastActive\n         from " . $SourcePrefix . "Discussion od\n         join " . $DestPrefix . "Category nc\n            on od.CategoryID = nc.ImportID\n         join " . $DestPrefix . "User niu\n            on od.AuthUserID = niu.ImportID\n         join " . $DestPrefix . "User nuu\n            on od.LastUserID = nuu.ImportID\n         where od.WhisperUserID = 0\n            and od.Active = '1'");
                                                             $this->Message = T('<strong>14/19</strong> Importing discussions.');
                                                             $this->RedirectUrl = Url('/upgrade/14');
                                                         } else {
                                                             if ($Step == 14) {
                                                                 // 14. Import Comments
                                                                 $Database->Query("insert into " . $DestPrefix . "Comment\n         (DiscussionID, InsertUserID, UpdateUserID, Body, Format, DateInserted, DateUpdated)\n         select nd.DiscussionID, niu.UserID, nuu.UserID, Body, case FormatType when 'Text' then 'Display' else FormatType end, oc.DateCreated, oc.DateEdited\n         from " . $SourcePrefix . "Comment oc\n         join " . $DestPrefix . "Discussion nd\n            on oc.DiscussionID = nd.ImportID\n         join " . $DestPrefix . "User niu\n            on oc.AuthUserID = niu.ImportID\n         left join " . $DestPrefix . "User nuu\n            on oc.EditUserID = nuu.ImportID\n         where (oc.WhisperUserID is null or oc.WhisperUserID = 0)\n            and oc.Deleted = '0'");
                                                                 $this->Message = T('<strong>15/19</strong> Importing comments.');
                                                                 $this->RedirectUrl = Url('/upgrade/15');
                                                             } else {
                                                                 if ($Step == 15) {
                                                                     // 15. Update Discussions with first & last comment ids
                                                                     $Database->Query("update " . $DestPrefix . "Discussion d\n         join (\n           select DiscussionID, min(CommentID) as FirstCommentID\n           from " . $DestPrefix . "Comment\n           group by DiscussionID\n         ) c\n           on d.DiscussionID = c.DiscussionID\n         set d.FirstCommentID = c.FirstCommentID");
                                                                     $Database->Query("update " . $DestPrefix . "Discussion d\n         join (\n           select DiscussionID, max(CommentID) as LastCommentID\n           from " . $DestPrefix . "Comment\n           group by DiscussionID\n         ) c\n           on d.DiscussionID = c.DiscussionID\n         set d.LastCommentID = c.LastCommentID");
                                                                     // Update the CountDiscussions column on the category table
                                                                     $Database->Query("update " . $DestPrefix . "Category c\n         join (\n           select CategoryID, count(DiscussionID) as CountDiscussions\n           from " . $DestPrefix . "Discussion\n           group by CategoryID\n         ) cc\n           on c.CategoryID = cc.CategoryID\n         set c.CountDiscussions = cc.CountDiscussions");
                                                                     $this->Message = T('<strong>16/19</strong> Finalizing discussions.');
                                                                     $this->RedirectUrl = Url('/upgrade/16');
                                                                 } else {
                                                                     if ($Step == 16) {
                                                                         // 16. Import UserDiscussion (watch & bookmark data)
                                                                         $Database->Query("insert into " . $DestPrefix . "UserDiscussion\n         (UserID, DiscussionID, CountComments, DateLastViewed, Bookmarked)\n         select nu.UserID, nd.DiscussionID, ow.CountComments, ow.LastViewed, if(isnull(ob.DiscussionID), '0', '1') as Bookmarked\n         from " . $SourcePrefix . "UserDiscussionWatch ow\n         join " . $SourcePrefix . "Discussion od\n            on ow.DiscussionID = od.DiscussionID\n         join " . $DestPrefix . "Discussion nd\n            on od.DiscussionID = nd.ImportID\n         join " . $DestPrefix . "User nu\n            on ow.UserID = nu.ImportID\n         left join " . $SourcePrefix . "UserBookmark ob\n            on ow.DiscussionID = ob.DiscussionID\n            and ow.UserID = ob.UserID\n         where od.Active = '1'");
                                                                         $this->Message = T('<strong>17/19</strong> Importing bookmarks & watch data.');
                                                                         $this->RedirectUrl = Url('/upgrade/17');
                                                                     } else {
                                                                         if ($Step == 17) {
                                                                             // 17. Remove temp columns
                                                                             $Construct->Table('Role')->DropColumn('ImportID');
                                                                             $Construct->Table('User')->DropColumn('ImportID');
                                                                             $Construct->Table('Category')->DropColumn('ImportID');
                                                                             $Construct->Table('Discussion')->DropColumn('ImportID');
                                                                             $Construct->DatabasePrefix($SourcePrefix);
                                                                             $Construct->Table('Comment')->DropColumn('ConversationID');
                                                                             $Construct->DatabasePrefix($DestPrefix);
                                                                             $this->Message = T('<strong>18/19</strong> Removing import structure.');
                                                                             $this->RedirectUrl = Url('/upgrade/18');
                                                                         } else {
                                                                             if ($Step == 18) {
                                                                                 // 18. remove whisperuserids from old comment table where the entire discussion is whispered
                                                                                 $Database->Query("update " . $SourcePrefix . "Comment c\n         inner join " . $SourcePrefix . "Discussion d\n            on c.DiscussionID = d.DiscussionID\n         set c.WhisperUserID = null\n         where d.WhisperUserID > 0");
                                                                                 $this->Message = T('<strong>19/19</strong> Restoring original comment structure.');
                                                                                 $this->RedirectUrl = Url('/upgrade/19');
                                                                             } else {
                                                                                 if ($Step == 19) {
                                                                                     // Finished!
                                                                                     $this->RedirectUrl = 'Finished';
                                                                                     $this->View = 'finished';
                                                                                 }
                                                                             }
                                                                         }
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $this->SetJson('NextUrl', $this->RedirectUrl);
     $this->RedirectUrl = '';
     $this->MasterView = 'setup';
     $this->Render();
 }