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);
        }
        /// <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;
        }