Пример #1
0
        /// <summary>
        /// Backups the specified settings provider.
        /// </summary>
        /// <param name="zipFileName">The zip file name where to store the backup.</param>
        /// <param name="settingsStorageProvider">The source settings provider.</param>
        /// <param name="knownNamespaces">The currently known page namespaces.</param>
        /// <param name="knownPlugins">The currently known plugins.</param>
        /// <returns><c>true</c> if the backup file has been succesfully created.</returns>
        public static bool BackupSettingsStorageProvider(string zipFileName, ISettingsStorageProviderV30 settingsStorageProvider, string[] knownNamespaces, string[] knownPlugins)
        {
            SettingsBackup settingsBackup = new SettingsBackup();

            // Settings
            settingsBackup.Settings = (Dictionary <string, string>)settingsStorageProvider.GetAllSettings();

            // Plugins Status and Configuration
            settingsBackup.PluginsFileNames = knownPlugins.ToList();
            Dictionary <string, bool>   pluginsStatus        = new Dictionary <string, bool>();
            Dictionary <string, string> pluginsConfiguration = new Dictionary <string, string>();

            foreach (string plugin in knownPlugins)
            {
                pluginsStatus[plugin]        = settingsStorageProvider.GetPluginStatus(plugin);
                pluginsConfiguration[plugin] = settingsStorageProvider.GetPluginConfiguration(plugin);
            }
            settingsBackup.PluginsStatus        = pluginsStatus;
            settingsBackup.PluginsConfiguration = pluginsConfiguration;

            // Metadata
            List <MetaData> metadataList = new List <MetaData>();

            // Meta-data (global)
            metadataList.Add(new MetaData()
            {
                Item    = MetaDataItem.AccountActivationMessage,
                Tag     = null,
                Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.AccountActivationMessage, null)
            });
            metadataList.Add(new MetaData()
            {
                Item = MetaDataItem.PasswordResetProcedureMessage, Tag = null, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.PasswordResetProcedureMessage, null)
            });
            metadataList.Add(new MetaData()
            {
                Item = MetaDataItem.LoginNotice, Tag = null, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.LoginNotice, null)
            });
            metadataList.Add(new MetaData()
            {
                Item = MetaDataItem.PageChangeMessage, Tag = null, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.PageChangeMessage, null)
            });
            metadataList.Add(new MetaData()
            {
                Item = MetaDataItem.DiscussionChangeMessage, Tag = null, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.DiscussionChangeMessage, null)
            });
            // Meta-data (ns-specific)
            List <string> namespacesToProcess = new List <string>();

            namespacesToProcess.Add("");
            namespacesToProcess.AddRange(knownNamespaces);
            foreach (string nspace in namespacesToProcess)
            {
                metadataList.Add(new MetaData()
                {
                    Item = MetaDataItem.EditNotice, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.EditNotice, nspace)
                });
                metadataList.Add(new MetaData()
                {
                    Item = MetaDataItem.Footer, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.Footer, nspace)
                });
                metadataList.Add(new MetaData()
                {
                    Item = MetaDataItem.Header, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.Header, nspace)
                });
                metadataList.Add(new MetaData()
                {
                    Item = MetaDataItem.HtmlHead, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.HtmlHead, nspace)
                });
                metadataList.Add(new MetaData()
                {
                    Item = MetaDataItem.PageFooter, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.PageFooter, nspace)
                });
                metadataList.Add(new MetaData()
                {
                    Item = MetaDataItem.PageHeader, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.PageHeader, nspace)
                });
                metadataList.Add(new MetaData()
                {
                    Item = MetaDataItem.Sidebar, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.Sidebar, nspace)
                });
            }
            settingsBackup.Metadata = metadataList;

            // RecentChanges
            settingsBackup.RecentChanges = settingsStorageProvider.GetRecentChanges().ToList();

            // OutgoingLinks
            settingsBackup.OutgoingLinks = (Dictionary <string, string[]>)settingsStorageProvider.GetAllOutgoingLinks();

            // ACLEntries
            AclEntry[] aclEntries = settingsStorageProvider.AclManager.RetrieveAllEntries();
            settingsBackup.AclEntries = new List <AclEntryBackup>(aclEntries.Length);
            foreach (AclEntry aclEntry in aclEntries)
            {
                settingsBackup.AclEntries.Add(new AclEntryBackup()
                {
                    Action   = aclEntry.Action,
                    Resource = aclEntry.Resource,
                    Subject  = aclEntry.Subject,
                    Value    = aclEntry.Value
                });
            }

            JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();

            javascriptSerializer.MaxJsonLength = javascriptSerializer.MaxJsonLength * 10;

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

            Directory.CreateDirectory(tempDir);

            FileStream tempFile = File.Create(Path.Combine(tempDir, "Settings.json"));

            byte[] buffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(settingsBackup));
            tempFile.Write(buffer, 0, buffer.Length);
            tempFile.Close();

            tempFile = File.Create(Path.Combine(tempDir, "Version.json"));
            buffer   = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(generateVersionFile("Settings")));
            tempFile.Write(buffer, 0, buffer.Length);
            tempFile.Close();

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

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Backups the specified settings provider.
        /// </summary>
        /// <param name="zipFileName">The zip file name where to store the backup.</param>
        /// <param name="settingsStorageProvider">The source settings provider.</param>
        /// <param name="knownNamespaces">The currently known page namespaces.</param>
        /// <param name="knownPlugins">The currently known plugins.</param>
        /// <returns><c>true</c> if the backup file has been succesfully created.</returns>
        public static bool BackupSettingsStorageProvider(string zipFileName, ISettingsStorageProviderV30 settingsStorageProvider, string[] knownNamespaces, string[] knownPlugins)
        {
            SettingsBackup settingsBackup = new SettingsBackup();

            // Settings
            settingsBackup.Settings = (Dictionary<string, string>)settingsStorageProvider.GetAllSettings();

            // Plugins Status and Configuration
            settingsBackup.PluginsFileNames = knownPlugins.ToList();
            Dictionary<string, bool> pluginsStatus = new Dictionary<string, bool>();
            Dictionary<string, string> pluginsConfiguration = new Dictionary<string, string>();
            foreach(string plugin in knownPlugins) {
                pluginsStatus[plugin] = settingsStorageProvider.GetPluginStatus(plugin);
                pluginsConfiguration[plugin] = settingsStorageProvider.GetPluginConfiguration(plugin);
            }
            settingsBackup.PluginsStatus = pluginsStatus;
            settingsBackup.PluginsConfiguration = pluginsConfiguration;

            // Metadata
            List<MetaData> metadataList = new List<MetaData>();
            // Meta-data (global)
            metadataList.Add(new MetaData() {
                Item = MetaDataItem.AccountActivationMessage,
                Tag = null,
                Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.AccountActivationMessage, null)
            });
            metadataList.Add(new MetaData() { Item = MetaDataItem.PasswordResetProcedureMessage, Tag = null, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.PasswordResetProcedureMessage, null) });
            metadataList.Add(new MetaData() { Item = MetaDataItem.LoginNotice, Tag = null, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.LoginNotice, null) });
            metadataList.Add(new MetaData() { Item = MetaDataItem.PageChangeMessage, Tag = null, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.PageChangeMessage, null) });
            metadataList.Add(new MetaData() { Item = MetaDataItem.DiscussionChangeMessage, Tag = null, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.DiscussionChangeMessage, null) });
            // Meta-data (ns-specific)
            List<string> namespacesToProcess = new List<string>();
            namespacesToProcess.Add("");
            namespacesToProcess.AddRange(knownNamespaces);
            foreach(string nspace in namespacesToProcess) {
                metadataList.Add(new MetaData() { Item = MetaDataItem.EditNotice, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.EditNotice, nspace) });
                metadataList.Add(new MetaData() { Item = MetaDataItem.Footer, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.Footer, nspace) });
                metadataList.Add(new MetaData() { Item = MetaDataItem.Header, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.Header, nspace) });
                metadataList.Add(new MetaData() { Item = MetaDataItem.HtmlHead, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.HtmlHead, nspace) });
                metadataList.Add(new MetaData() { Item = MetaDataItem.PageFooter, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.PageFooter, nspace) });
                metadataList.Add(new MetaData() { Item = MetaDataItem.PageHeader, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.PageHeader, nspace) });
                metadataList.Add(new MetaData() { Item = MetaDataItem.Sidebar, Tag = nspace, Content = settingsStorageProvider.GetMetaDataItem(MetaDataItem.Sidebar, nspace) });
            }
            settingsBackup.Metadata = metadataList;

            // RecentChanges
            settingsBackup.RecentChanges = settingsStorageProvider.GetRecentChanges().ToList();

            // OutgoingLinks
            settingsBackup.OutgoingLinks = (Dictionary<string, string[]>)settingsStorageProvider.GetAllOutgoingLinks();

            // ACLEntries
            AclEntry[] aclEntries = settingsStorageProvider.AclManager.RetrieveAllEntries();
            settingsBackup.AclEntries = new List<AclEntryBackup>(aclEntries.Length);
            foreach(AclEntry aclEntry in aclEntries) {
                settingsBackup.AclEntries.Add(new AclEntryBackup() {
                    Action = aclEntry.Action,
                    Resource = aclEntry.Resource,
                    Subject = aclEntry.Subject,
                    Value = aclEntry.Value
                });
            }

            JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
            javascriptSerializer.MaxJsonLength = javascriptSerializer.MaxJsonLength * 10;

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

            FileStream tempFile = File.Create(Path.Combine(tempDir, "Settings.json"));
            byte[] buffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(settingsBackup));
            tempFile.Write(buffer, 0, buffer.Length);
            tempFile.Close();

            tempFile = File.Create(Path.Combine(tempDir, "Version.json"));
            buffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(generateVersionFile("Settings")));
            tempFile.Write(buffer, 0, buffer.Length);
            tempFile.Close();

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

            return true;
        }