Example #1
0
        /// <summary>
        /// Backups the pages storage provider.
        /// </summary>
        /// <param name="zipFileName">The zip file name where to store the backup.</param>
        /// <param name="pagesStorageProvider">The pages storage provider.</param>
        /// <returns><c>true</c> if the backup file has been succesfully created.</returns>
        public static bool BackupPagesStorageProvider(string zipFileName, IPagesStorageProviderV30 pagesStorageProvider)
        {
            JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();

            javascriptSerializer.MaxJsonLength = javascriptSerializer.MaxJsonLength * 10;

            string tempDir = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), Guid.NewGuid().ToString());

            Directory.CreateDirectory(tempDir);

            List <NamespaceInfo> nspaces = new List <NamespaceInfo>(pagesStorageProvider.GetNamespaces());

            nspaces.Add(null);
            List <NamespaceBackup> namespaceBackupList = new List <NamespaceBackup>(nspaces.Count);

            foreach (NamespaceInfo nspace in nspaces)
            {
                // Backup categories
                CategoryInfo[]        categories       = pagesStorageProvider.GetCategories(nspace);
                List <CategoryBackup> categoriesBackup = new List <CategoryBackup>(categories.Length);
                foreach (CategoryInfo category in categories)
                {
                    // Add this category to the categoriesBackup list
                    categoriesBackup.Add(new CategoryBackup()
                    {
                        FullName = category.FullName,
                        Pages    = category.Pages
                    });
                }

                // Backup NavigationPaths
                NavigationPath[]            navigationPaths       = pagesStorageProvider.GetNavigationPaths(nspace);
                List <NavigationPathBackup> navigationPathsBackup = new List <NavigationPathBackup>(navigationPaths.Length);
                foreach (NavigationPath navigationPath in navigationPaths)
                {
                    navigationPathsBackup.Add(new NavigationPathBackup()
                    {
                        FullName = navigationPath.FullName,
                        Pages    = navigationPath.Pages
                    });
                }

                // Add this namespace to the namespaceBackup list
                namespaceBackupList.Add(new NamespaceBackup()
                {
                    Name = nspace == null ? "" : nspace.Name,
                    DefaultPageFullName = nspace == null ? "" : nspace.DefaultPage.FullName,
                    Categories          = categoriesBackup,
                    NavigationPaths     = navigationPathsBackup
                });

                // Backup pages (one json file for each page containing a maximum of 100 revisions)
                PageInfo[] pages = pagesStorageProvider.GetPages(nspace);
                foreach (PageInfo page in pages)
                {
                    PageContent pageContent = pagesStorageProvider.GetContent(page);
                    PageBackup  pageBackup  = new PageBackup();
                    pageBackup.FullName         = page.FullName;
                    pageBackup.CreationDateTime = page.CreationDateTime;
                    pageBackup.LastModified     = pageContent.LastModified;
                    pageBackup.Content          = pageContent.Content;
                    pageBackup.Comment          = pageContent.Comment;
                    pageBackup.Description      = pageContent.Description;
                    pageBackup.Keywords         = pageContent.Keywords;
                    pageBackup.Title            = pageContent.Title;
                    pageBackup.User             = pageContent.User;
                    pageBackup.LinkedPages      = pageContent.LinkedPages;
                    pageBackup.Categories       = (from c in pagesStorageProvider.GetCategoriesForPage(page)
                                                   select c.FullName).ToArray();

                    // Backup the 100 most recent versions of the page
                    List <PageRevisionBackup> pageContentBackupList = new List <PageRevisionBackup>();
                    int[] revisions = pagesStorageProvider.GetBackups(page);
                    for (int i = revisions.Length - 1; i > revisions.Length - 100 && i >= 0; i--)
                    {
                        PageContent        pageRevision      = pagesStorageProvider.GetBackupContent(page, revisions[i]);
                        PageRevisionBackup pageContentBackup = new PageRevisionBackup()
                        {
                            Revision     = revisions[i],
                            Content      = pageRevision.Content,
                            Comment      = pageRevision.Comment,
                            Description  = pageRevision.Description,
                            Keywords     = pageRevision.Keywords,
                            Title        = pageRevision.Title,
                            User         = pageRevision.User,
                            LastModified = pageRevision.LastModified
                        };
                        pageContentBackupList.Add(pageContentBackup);
                    }
                    pageBackup.Revisions = pageContentBackupList;

                    // Backup draft of the page
                    PageContent draft = pagesStorageProvider.GetDraft(page);
                    if (draft != null)
                    {
                        pageBackup.Draft = new PageRevisionBackup()
                        {
                            Content      = draft.Content,
                            Comment      = draft.Comment,
                            Description  = draft.Description,
                            Keywords     = draft.Keywords,
                            Title        = draft.Title,
                            User         = draft.User,
                            LastModified = draft.LastModified
                        };
                    }

                    // Backup all messages of the page
                    List <MessageBackup> messageBackupList = new List <MessageBackup>();
                    foreach (Message message in pagesStorageProvider.GetMessages(page))
                    {
                        messageBackupList.Add(BackupMessage(message));
                    }
                    pageBackup.Messages = messageBackupList;

                    FileStream tempFile = File.Create(Path.Combine(tempDir, page.FullName + ".json"));
                    byte[]     buffer   = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(pageBackup));
                    tempFile.Write(buffer, 0, buffer.Length);
                    tempFile.Close();
                }
            }
            FileStream tempNamespacesFile = File.Create(Path.Combine(tempDir, "Namespaces.json"));

            byte[] namespacesBuffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(namespaceBackupList));
            tempNamespacesFile.Write(namespacesBuffer, 0, namespacesBuffer.Length);
            tempNamespacesFile.Close();

            // Backup content templates
            ContentTemplate[]            contentTemplates       = pagesStorageProvider.GetContentTemplates();
            List <ContentTemplateBackup> contentTemplatesBackup = new List <ContentTemplateBackup>(contentTemplates.Length);

            foreach (ContentTemplate contentTemplate in contentTemplates)
            {
                contentTemplatesBackup.Add(new ContentTemplateBackup()
                {
                    Name    = contentTemplate.Name,
                    Content = contentTemplate.Content
                });
            }
            FileStream tempContentTemplatesFile = File.Create(Path.Combine(tempDir, "ContentTemplates.json"));

            byte[] contentTemplateBuffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(contentTemplatesBackup));
            tempContentTemplatesFile.Write(contentTemplateBuffer, 0, contentTemplateBuffer.Length);
            tempContentTemplatesFile.Close();

            // Backup Snippets
            Snippet[]            snippets       = pagesStorageProvider.GetSnippets();
            List <SnippetBackup> snippetsBackup = new List <SnippetBackup>(snippets.Length);

            foreach (Snippet snippet in snippets)
            {
                snippetsBackup.Add(new SnippetBackup()
                {
                    Name    = snippet.Name,
                    Content = snippet.Content
                });
            }
            FileStream tempSnippetsFile = File.Create(Path.Combine(tempDir, "Snippets.json"));

            byte[] snippetBuffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(snippetsBackup));
            tempSnippetsFile.Write(snippetBuffer, 0, snippetBuffer.Length);
            tempSnippetsFile.Close();

            FileStream tempVersionFile = File.Create(Path.Combine(tempDir, "Version.json"));

            byte[] versionBuffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(generateVersionFile("Pages")));
            tempVersionFile.Write(versionBuffer, 0, versionBuffer.Length);
            tempVersionFile.Close();

            using (ZipFile zipFile = new ZipFile()) {
                zipFile.AddDirectory(tempDir, "");
                zipFile.Save(zipFileName);
            }
            Directory.Delete(tempDir, true);

            return(true);
        }
Example #2
0
        /// <summary>
        /// Migrates <b>all</b> the data from a Pages Provider to another one.
        /// </summary>
        /// <param name="source">The source Provider.</param>
        /// <param name="destination">The destination Provider.</param>
        public static void MigratePagesStorageProviderData(IPagesStorageProviderV30 source, IPagesStorageProviderV30 destination)
        {
            // Move Snippets
            Snippet[] snippets = source.GetSnippets();
            for (int i = 0; i < snippets.Length; i++)
            {
                destination.AddSnippet(snippets[i].Name, snippets[i].Content);
                source.RemoveSnippet(snippets[i].Name);
            }

            // Move Content Templates
            ContentTemplate[] templates = source.GetContentTemplates();
            for (int i = 0; i < templates.Length; i++)
            {
                destination.AddContentTemplate(templates[i].Name, templates[i].Content);
                source.RemoveContentTemplate(templates[i].Name);
            }

            // Create namespaces
            NamespaceInfo[] namespaces        = source.GetNamespaces();
            NamespaceInfo[] createdNamespaces = new NamespaceInfo[namespaces.Length];
            for (int i = 0; i < namespaces.Length; i++)
            {
                createdNamespaces[i] = destination.AddNamespace(namespaces[i].Name);
            }

            List <NamespaceInfo> sourceNamespaces = new List <NamespaceInfo>();

            sourceNamespaces.Add(null);
            sourceNamespaces.AddRange(namespaces);

            int currentNamespaceIndex = 0;

            foreach (NamespaceInfo currentNamespace in sourceNamespaces)
            {
                // Load all nav paths now to avoid problems with missing pages from source provider
                // after the pages have been moved already
                NavigationPath[] sourceNavPaths = source.GetNavigationPaths(currentNamespace);

                // Copy categories (removed from source later)
                CategoryInfo[] sourceCats = source.GetCategories(currentNamespace);
                for (int i = 0; i < sourceCats.Length; i++)
                {
                    destination.AddCategory(NameTools.GetNamespace(sourceCats[i].FullName), NameTools.GetLocalName(sourceCats[i].FullName));
                }

                // Move Pages
                PageInfo[] pages = source.GetPages(currentNamespace);
                for (int i = 0; i < pages.Length; i++)
                {
                    // Create Page
                    PageInfo newPage = destination.AddPage(NameTools.GetNamespace(pages[i].FullName),
                                                           NameTools.GetLocalName(pages[i].FullName), pages[i].CreationDateTime);
                    if (newPage == null)
                    {
                        Log.LogEntry("Unable to move Page " + pages[i].FullName + " - Skipping", EntryType.Error, Log.SystemUsername);
                        continue;
                    }

                    // Get content and store it, without backup
                    PageContent c = source.GetContent(pages[i]);
                    destination.ModifyPage(newPage, c.Title, c.User, c.LastModified, c.Comment, c.Content, c.Keywords, c.Description, SaveMode.Normal);

                    // Move all the backups
                    int[] revs = source.GetBackups(pages[i]);
                    for (int k = 0; k < revs.Length; k++)
                    {
                        c = source.GetBackupContent(pages[i], revs[k]);
                        destination.SetBackupContent(c, revs[k]);
                    }

                    // Move all messages
                    Message[] messages = source.GetMessages(pages[i]);
                    destination.BulkStoreMessages(newPage, messages);

                    // Bind current page (find all proper categories, and use them to bind the page)
                    List <string> pageCats = new List <string>();
                    for (int k = 0; k < sourceCats.Length; k++)
                    {
                        for (int z = 0; z < sourceCats[k].Pages.Length; z++)
                        {
                            if (sourceCats[k].Pages[z].Equals(newPage.FullName))
                            {
                                pageCats.Add(sourceCats[k].FullName);
                                break;
                            }
                        }
                    }
                    destination.RebindPage(newPage, pageCats.ToArray());

                    // Copy draft
                    PageContent draft = source.GetDraft(pages[i]);
                    if (draft != null)
                    {
                        destination.ModifyPage(newPage, draft.Title, draft.User, draft.LastModified,
                                               draft.Comment, draft.Content, draft.Keywords, draft.Description, SaveMode.Draft);
                    }

                    // Remove Page from source
                    source.RemovePage(pages[i]);                     // Also deletes the Messages
                }

                // Remove Categories from source
                for (int i = 0; i < sourceCats.Length; i++)
                {
                    source.RemoveCategory(sourceCats[i]);
                }

                // Move navigation paths
                List <PageInfo> newPages = new List <PageInfo>(destination.GetPages(currentNamespace == null ? null : createdNamespaces[currentNamespaceIndex]));
                for (int i = 0; i < sourceNavPaths.Length; i++)
                {
                    PageInfo[] tmp = new PageInfo[sourceNavPaths[i].Pages.Length];
                    for (int k = 0; k < tmp.Length; k++)
                    {
                        tmp[k] = newPages.Find(delegate(PageInfo p) { return(p.FullName == sourceNavPaths[i].Pages[k]); });
                    }
                    destination.AddNavigationPath(NameTools.GetNamespace(sourceNavPaths[i].FullName),
                                                  NameTools.GetLocalName(sourceNavPaths[i].FullName), tmp);
                    source.RemoveNavigationPath(sourceNavPaths[i]);
                }

                if (currentNamespace != null)
                {
                    // Set default page
                    PageInfo defaultPage = currentNamespace.DefaultPage == null ? null :
                                           newPages.Find(delegate(PageInfo p) { return(p.FullName == currentNamespace.DefaultPage.FullName); });
                    destination.SetNamespaceDefaultPage(createdNamespaces[currentNamespaceIndex], defaultPage);

                    // Remove namespace from source
                    source.RemoveNamespace(currentNamespace);

                    currentNamespaceIndex++;
                }
            }
        }
Example #3
0
        public void MigratePagesStorageProviderData()
        {
            MockRepository mocks = new MockRepository();

            IPagesStorageProviderV30 source      = mocks.StrictMock <IPagesStorageProviderV30>();
            IPagesStorageProviderV30 destination = mocks.StrictMock <IPagesStorageProviderV30>();

            // Setup SOURCE -------------------------

            // Setup snippets
            Snippet s1 = new Snippet("S1", "Blah1", source);
            Snippet s2 = new Snippet("S2", "Blah2", source);

            Expect.Call(source.GetSnippets()).Return(new Snippet[] { s1, s2 });

            // Setup content templates
            ContentTemplate ct1 = new ContentTemplate("CT1", "Template 1", source);
            ContentTemplate ct2 = new ContentTemplate("CT2", "Template 2", source);

            Expect.Call(source.GetContentTemplates()).Return(new ContentTemplate[] { ct1, ct2 });

            // Setup namespaces
            NamespaceInfo ns1 = new NamespaceInfo("NS1", source, null);
            NamespaceInfo ns2 = new NamespaceInfo("NS2", source, null);

            Expect.Call(source.GetNamespaces()).Return(new NamespaceInfo[] { ns1, ns2 });

            // Setup pages
            PageInfo p1 = new PageInfo("Page", source, DateTime.Now);
            PageInfo p2 = new PageInfo(NameTools.GetFullName(ns1.Name, "Page"), source, DateTime.Now);
            PageInfo p3 = new PageInfo(NameTools.GetFullName(ns1.Name, "Page1"), source, DateTime.Now);

            Expect.Call(source.GetPages(null)).Return(new PageInfo[] { p1 });
            Expect.Call(source.GetPages(ns1)).Return(new PageInfo[] { p2, p3 });
            Expect.Call(source.GetPages(ns2)).Return(new PageInfo[0]);

            // Set default page for NS1
            ns1.DefaultPage = p2;

            // Setup categories/bindings
            CategoryInfo c1 = new CategoryInfo("Cat", source);

            c1.Pages = new string[] { p1.FullName };
            CategoryInfo c2 = new CategoryInfo(NameTools.GetFullName(ns1.Name, "Cat"), source);

            c2.Pages = new string[] { p2.FullName };
            CategoryInfo c3 = new CategoryInfo(NameTools.GetFullName(ns1.Name, "Cat1"), source);

            c3.Pages = new string[0];
            Expect.Call(source.GetCategories(null)).Return(new CategoryInfo[] { c1 });
            Expect.Call(source.GetCategories(ns1)).Return(new CategoryInfo[] { c2, c3 });
            Expect.Call(source.GetCategories(ns2)).Return(new CategoryInfo[0]);

            // Setup drafts
            PageContent d1 = new PageContent(p1, "Draft", "NUnit", DateTime.Now, "Comm", "Cont", new string[] { "k1", "k2" }, "Descr");

            Expect.Call(source.GetDraft(p1)).Return(d1);
            Expect.Call(source.GetDraft(p2)).Return(null);
            Expect.Call(source.GetDraft(p3)).Return(null);

            // Setup content
            PageContent ctn1 = new PageContent(p1, "Title1", "User1", DateTime.Now, "Comm1", "Cont1", null, "Descr1");
            PageContent ctn2 = new PageContent(p2, "Title2", "User2", DateTime.Now, "Comm2", "Cont2", null, "Descr2");
            PageContent ctn3 = new PageContent(p3, "Title3", "User3", DateTime.Now, "Comm3", "Cont3", null, "Descr3");

            Expect.Call(source.GetContent(p1)).Return(ctn1);
            Expect.Call(source.GetContent(p2)).Return(ctn2);
            Expect.Call(source.GetContent(p3)).Return(ctn3);

            // Setup backups
            Expect.Call(source.GetBackups(p1)).Return(new int[] { 0, 1 });
            Expect.Call(source.GetBackups(p2)).Return(new int[] { 0 });
            Expect.Call(source.GetBackups(p3)).Return(new int[0]);
            PageContent bak1_0 = new PageContent(p1, "K1_0", "U1_0", DateTime.Now, "", "Cont", null, null);
            PageContent bak1_1 = new PageContent(p1, "K1_1", "U1_1", DateTime.Now, "", "Cont", null, null);
            PageContent bak2_0 = new PageContent(p2, "K2_0", "U2_0", DateTime.Now, "", "Cont", null, null);

            Expect.Call(source.GetBackupContent(p1, 0)).Return(bak1_0);
            Expect.Call(source.GetBackupContent(p1, 1)).Return(bak1_1);
            Expect.Call(source.GetBackupContent(p2, 0)).Return(bak2_0);

            // Messages
            Message m1 = new Message(1, "User1", "Subject1", DateTime.Now, "Body1");

            m1.Replies = new Message[] { new Message(2, "User2", "Subject2", DateTime.Now, "Body2") };
            Message[] p1m = new Message[] { m1 };
            Message[] p2m = new Message[0];
            Message[] p3m = new Message[0];
            Expect.Call(source.GetMessages(p1)).Return(p1m);
            Expect.Call(source.GetMessages(p2)).Return(p2m);
            Expect.Call(source.GetMessages(p3)).Return(p3m);

            // Setup navigation paths
            NavigationPath n1 = new NavigationPath("N1", source);

            n1.Pages = new string[] { p1.FullName };
            NavigationPath n2 = new NavigationPath(NameTools.GetFullName(ns1.Name, "N1"), source);

            n2.Pages = new string[] { p2.FullName, p3.FullName };
            Expect.Call(source.GetNavigationPaths(null)).Return(new NavigationPath[] { n1 });
            Expect.Call(source.GetNavigationPaths(ns1)).Return(new NavigationPath[] { n2 });
            Expect.Call(source.GetNavigationPaths(ns2)).Return(new NavigationPath[0]);

            // Setup DESTINATION --------------------------

            // Snippets
            Expect.Call(destination.AddSnippet(s1.Name, s1.Content)).Return(new Snippet(s1.Name, s1.Content, destination));
            Expect.Call(source.RemoveSnippet(s1.Name)).Return(true);
            Expect.Call(destination.AddSnippet(s2.Name, s2.Content)).Return(new Snippet(s2.Name, s2.Content, destination));
            Expect.Call(source.RemoveSnippet(s2.Name)).Return(true);

            // Content templates
            Expect.Call(destination.AddContentTemplate(ct1.Name, ct1.Content)).Return(new ContentTemplate(ct1.Name, ct1.Name, destination));
            Expect.Call(source.RemoveContentTemplate(ct1.Name)).Return(true);
            Expect.Call(destination.AddContentTemplate(ct2.Name, ct2.Content)).Return(new ContentTemplate(ct2.Name, ct2.Name, destination));
            Expect.Call(source.RemoveContentTemplate(ct2.Name)).Return(true);

            // Namespaces
            NamespaceInfo ns1Out = new NamespaceInfo(ns1.Name, destination, null);
            NamespaceInfo ns2Out = new NamespaceInfo(ns2.Name, destination, null);

            Expect.Call(destination.AddNamespace(ns1.Name)).Return(ns1Out);
            Expect.Call(source.RemoveNamespace(ns1)).Return(true);
            Expect.Call(destination.AddNamespace(ns2.Name)).Return(ns2Out);
            Expect.Call(source.RemoveNamespace(ns2)).Return(true);

            // Pages/drafts/content/backups/messages
            PageInfo p1Out = new PageInfo(p1.FullName, destination, p1.CreationDateTime);

            Expect.Call(destination.AddPage(null, p1.FullName, p1.CreationDateTime)).Return(p1Out);
            Expect.Call(destination.ModifyPage(p1Out, ctn1.Title, ctn1.User, ctn1.LastModified, ctn1.Comment, ctn1.Content, ctn1.Keywords, ctn1.Description, SaveMode.Normal)).Return(true);
            Expect.Call(destination.ModifyPage(p1Out, d1.Title, d1.User, d1.LastModified, d1.Comment, d1.Content, d1.Keywords, d1.Description, SaveMode.Draft)).Return(true);
            Expect.Call(destination.SetBackupContent(bak1_0, 0)).Return(true);
            Expect.Call(destination.SetBackupContent(bak1_1, 1)).Return(true);
            Expect.Call(destination.BulkStoreMessages(p1Out, p1m)).Return(true);
            Expect.Call(source.RemovePage(p1)).Return(true);

            PageInfo p2Out = new PageInfo(p2.FullName, destination, p2.CreationDateTime);

            Expect.Call(destination.AddPage(NameTools.GetNamespace(p2.FullName), NameTools.GetLocalName(p2.FullName),
                                            p2.CreationDateTime)).Return(p2Out);
            Expect.Call(destination.ModifyPage(p2Out, ctn2.Title, ctn2.User, ctn2.LastModified, ctn2.Comment, ctn2.Content, ctn2.Keywords, ctn2.Description, SaveMode.Normal)).Return(true);
            Expect.Call(destination.SetBackupContent(bak2_0, 0)).Return(true);
            Expect.Call(destination.BulkStoreMessages(p2Out, p2m)).Return(true);
            Expect.Call(source.RemovePage(p2)).Return(true);

            PageInfo p3Out = new PageInfo(p3.FullName, destination, p3.CreationDateTime);

            Expect.Call(destination.AddPage(NameTools.GetNamespace(p3.FullName), NameTools.GetLocalName(p3.FullName),
                                            p3.CreationDateTime)).Return(p3Out);
            Expect.Call(destination.ModifyPage(p3Out, ctn3.Title, ctn3.User, ctn3.LastModified, ctn3.Comment, ctn3.Content, ctn3.Keywords, ctn3.Description, SaveMode.Normal)).Return(true);
            Expect.Call(destination.BulkStoreMessages(p3Out, p3m)).Return(true);
            Expect.Call(source.RemovePage(p3)).Return(true);

            // Categories/bindings
            CategoryInfo c1Out = new CategoryInfo(c1.FullName, destination);
            CategoryInfo c2Out = new CategoryInfo(c2.FullName, destination);
            CategoryInfo c3Out = new CategoryInfo(c3.FullName, destination);

            Expect.Call(destination.AddCategory(null, c1.FullName)).Return(c1Out);
            Expect.Call(destination.AddCategory(NameTools.GetNamespace(c2.FullName), NameTools.GetLocalName(c2.FullName))).Return(c2Out);
            Expect.Call(destination.AddCategory(NameTools.GetNamespace(c3.FullName), NameTools.GetLocalName(c3.FullName))).Return(c3Out);
            Expect.Call(destination.RebindPage(p1Out, new string[] { c1.FullName })).Return(true);
            Expect.Call(destination.RebindPage(p2Out, new string[] { c2.FullName })).Return(true);
            Expect.Call(destination.RebindPage(p3Out, new string[0])).Return(true);
            Expect.Call(source.RemoveCategory(c1)).Return(true);
            Expect.Call(source.RemoveCategory(c2)).Return(true);
            Expect.Call(source.RemoveCategory(c3)).Return(true);

            // Navigation paths
            NavigationPath n1Out = new NavigationPath(n1.FullName, destination);

            n1Out.Pages = n1.Pages;
            NavigationPath n2Out = new NavigationPath(n2.FullName, destination);

            n2Out.Pages = n2.Pages;

            Expect.Call(destination.AddNavigationPath(null, n1.FullName, new PageInfo[] { p1 })).Return(n1Out).Constraints(
                Rhino.Mocks.Constraints.Is.Null(), Rhino.Mocks.Constraints.Is.Equal(n1.FullName),
                Rhino.Mocks.Constraints.Is.Matching <PageInfo[]>(delegate(PageInfo[] array) {
                return(array[0].FullName == p1.FullName);
            }));

            Expect.Call(destination.AddNavigationPath(NameTools.GetNamespace(n2.FullName), NameTools.GetLocalName(n2.FullName), new PageInfo[] { p2, p3 })).Return(n2Out).Constraints(
                Rhino.Mocks.Constraints.Is.Equal(NameTools.GetNamespace(n2.FullName)), Rhino.Mocks.Constraints.Is.Equal(NameTools.GetLocalName(n2.FullName)),
                Rhino.Mocks.Constraints.Is.Matching <PageInfo[]>(delegate(PageInfo[] array) {
                return(array[0].FullName == p2.FullName && array[1].FullName == p3.FullName);
            }));

            Expect.Call(source.RemoveNavigationPath(n1)).Return(true);
            Expect.Call(source.RemoveNavigationPath(n2)).Return(true);

            Expect.Call(destination.SetNamespaceDefaultPage(ns1Out, p2Out)).Return(ns1Out);
            Expect.Call(destination.SetNamespaceDefaultPage(ns2Out, null)).Return(ns2Out);

            // Used for navigation paths
            Expect.Call(destination.GetPages(null)).Return(new PageInfo[] { p1Out });
            Expect.Call(destination.GetPages(ns1Out)).Return(new PageInfo[] { p2Out, p3Out });
            Expect.Call(destination.GetPages(ns2Out)).Return(new PageInfo[0]);

            mocks.Replay(source);
            mocks.Replay(destination);

            DataMigrator.MigratePagesStorageProviderData(source, destination);

            mocks.Verify(source);
            mocks.Verify(destination);
        }
        /// <summary>
        /// Backups the pages storage provider.
        /// </summary>
        /// <param name="zipFileName">The zip file name where to store the backup.</param>
        /// <param name="pagesStorageProvider">The pages storage provider.</param>
        /// <returns><c>true</c> if the backup file has been succesfully created.</returns>
        public static bool BackupPagesStorageProvider(string zipFileName, IPagesStorageProviderV30 pagesStorageProvider)
        {
            JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
            javascriptSerializer.MaxJsonLength = javascriptSerializer.MaxJsonLength * 10;

            string tempDir = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), Guid.NewGuid().ToString());
            Directory.CreateDirectory(tempDir);

            List<NamespaceInfo> nspaces = new List<NamespaceInfo>(pagesStorageProvider.GetNamespaces());
            nspaces.Add(null);
            List<NamespaceBackup> namespaceBackupList = new List<NamespaceBackup>(nspaces.Count);
            foreach(NamespaceInfo nspace in nspaces) {

                // Backup categories
                CategoryInfo[] categories = pagesStorageProvider.GetCategories(nspace);
                List<CategoryBackup> categoriesBackup = new List<CategoryBackup>(categories.Length);
                foreach(CategoryInfo category in categories) {
                    // Add this category to the categoriesBackup list
                    categoriesBackup.Add(new CategoryBackup() {
                        FullName = category.FullName,
                        Pages = category.Pages
                    });
                }

                // Backup NavigationPaths
                NavigationPath[] navigationPaths = pagesStorageProvider.GetNavigationPaths(nspace);
                List<NavigationPathBackup> navigationPathsBackup = new List<NavigationPathBackup>(navigationPaths.Length);
                foreach(NavigationPath navigationPath in navigationPaths) {
                    navigationPathsBackup.Add(new NavigationPathBackup() {
                        FullName = navigationPath.FullName,
                        Pages = navigationPath.Pages
                    });
                }

                // Add this namespace to the namespaceBackup list
                namespaceBackupList.Add(new NamespaceBackup() {
                    Name = nspace == null ? "" : nspace.Name,
                    DefaultPageFullName = nspace == null ? "" : nspace.DefaultPage.FullName,
                    Categories = categoriesBackup,
                    NavigationPaths = navigationPathsBackup
                });

                // Backup pages (one json file for each page containing a maximum of 100 revisions)
                PageInfo[] pages = pagesStorageProvider.GetPages(nspace);
                foreach(PageInfo page in pages) {
                    PageContent pageContent = pagesStorageProvider.GetContent(page);
                    PageBackup pageBackup = new PageBackup();
                    pageBackup.FullName = page.FullName;
                    pageBackup.CreationDateTime = page.CreationDateTime;
                    pageBackup.LastModified = pageContent.LastModified;
                    pageBackup.Content = pageContent.Content;
                    pageBackup.Comment = pageContent.Comment;
                    pageBackup.Description = pageContent.Description;
                    pageBackup.Keywords = pageContent.Keywords;
                    pageBackup.Title = pageContent.Title;
                    pageBackup.User = pageContent.User;
                    pageBackup.LinkedPages = pageContent.LinkedPages;
                    pageBackup.Categories = (from c in pagesStorageProvider.GetCategoriesForPage(page)
                                             select c.FullName).ToArray();

                    // Backup the 100 most recent versions of the page
                    List<PageRevisionBackup> pageContentBackupList = new List<PageRevisionBackup>();
                    int[] revisions = pagesStorageProvider.GetBackups(page);
                    for(int i = revisions.Length - 1; i > revisions.Length - 100 && i >= 0; i--) {
                        PageContent pageRevision = pagesStorageProvider.GetBackupContent(page, revisions[i]);
                        PageRevisionBackup pageContentBackup = new PageRevisionBackup() {
                            Revision = revisions[i],
                            Content = pageRevision.Content,
                            Comment = pageRevision.Comment,
                            Description = pageRevision.Description,
                            Keywords = pageRevision.Keywords,
                            Title = pageRevision.Title,
                            User = pageRevision.User,
                            LastModified = pageRevision.LastModified
                        };
                        pageContentBackupList.Add(pageContentBackup);
                    }
                    pageBackup.Revisions = pageContentBackupList;

                    // Backup draft of the page
                    PageContent draft = pagesStorageProvider.GetDraft(page);
                    if(draft != null) {
                        pageBackup.Draft = new PageRevisionBackup() {
                            Content = draft.Content,
                            Comment = draft.Comment,
                            Description = draft.Description,
                            Keywords = draft.Keywords,
                            Title = draft.Title,
                            User = draft.User,
                            LastModified = draft.LastModified
                        };
                    }

                    // Backup all messages of the page
                    List<MessageBackup> messageBackupList = new List<MessageBackup>();
                    foreach(Message message in pagesStorageProvider.GetMessages(page)) {
                        messageBackupList.Add(BackupMessage(message));
                    }
                    pageBackup.Messages = messageBackupList;

                    FileStream tempFile = File.Create(Path.Combine(tempDir, page.FullName + ".json"));
                    byte[] buffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(pageBackup));
                    tempFile.Write(buffer, 0, buffer.Length);
                    tempFile.Close();
                }
            }
            FileStream tempNamespacesFile = File.Create(Path.Combine(tempDir, "Namespaces.json"));
            byte[] namespacesBuffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(namespaceBackupList));
            tempNamespacesFile.Write(namespacesBuffer, 0, namespacesBuffer.Length);
            tempNamespacesFile.Close();

            // Backup content templates
            ContentTemplate[] contentTemplates = pagesStorageProvider.GetContentTemplates();
            List<ContentTemplateBackup> contentTemplatesBackup = new List<ContentTemplateBackup>(contentTemplates.Length);
            foreach(ContentTemplate contentTemplate in contentTemplates) {
                contentTemplatesBackup.Add(new ContentTemplateBackup() {
                    Name = contentTemplate.Name,
                    Content = contentTemplate.Content
                });
            }
            FileStream tempContentTemplatesFile = File.Create(Path.Combine(tempDir, "ContentTemplates.json"));
            byte[] contentTemplateBuffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(contentTemplatesBackup));
            tempContentTemplatesFile.Write(contentTemplateBuffer, 0, contentTemplateBuffer.Length);
            tempContentTemplatesFile.Close();

            // Backup Snippets
            Snippet[] snippets = pagesStorageProvider.GetSnippets();
            List<SnippetBackup> snippetsBackup = new List<SnippetBackup>(snippets.Length);
            foreach(Snippet snippet in snippets) {
                snippetsBackup.Add(new SnippetBackup() {
                    Name = snippet.Name,
                    Content = snippet.Content
                });
            }
            FileStream tempSnippetsFile = File.Create(Path.Combine(tempDir, "Snippets.json"));
            byte[] snippetBuffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(snippetsBackup));
            tempSnippetsFile.Write(snippetBuffer, 0, snippetBuffer.Length);
            tempSnippetsFile.Close();

            FileStream tempVersionFile = File.Create(Path.Combine(tempDir, "Version.json"));
            byte[] versionBuffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(generateVersionFile("Pages")));
            tempVersionFile.Write(versionBuffer, 0, versionBuffer.Length);
            tempVersionFile.Close();

            using(ZipFile zipFile = new ZipFile()) {
                zipFile.AddDirectory(tempDir, "");
                zipFile.Save(zipFileName);
            }
            Directory.Delete(tempDir, true);

            return true;
        }