/tags/1.1.0/ImpEx.Engine/WSSExport.cs
C# | 304 lines | 256 code | 36 blank | 12 comment | 18 complexity | ca0ca58b96deaf768a0a1ebd3b0481e9 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Reflection;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Administration;
- using Microsoft.SharePoint.Deployment;
- using Microsoft.SharePoint.WebPartPages;
- using WebPart=System.Web.UI.WebControls.WebParts.WebPart;
-
- namespace ImpEx.Engine
- {
- /// <summary>
- /// ласс дл€ выполнени€ действий по инкрементальному импорту / экспорту
- /// </summary>
- public class WSSExport
- {
- #if FULLVERSION
- private RequiredItemLibrary _itemLibrary = new RequiredItemLibrary();
- #endif
-
- /// <summary>
- /// ƒелает экспорт, согласно загруженным ранее правилам
- /// </summary>
- /// <param name="filename">»м€ файла дл€ экспорта</param>
- public void Export(string filename, SPSWebApplication app)
- {
- Site = app.SPSSite;
-
- string tmpDirectory = Path.GetTempPath() + "\\WssExport" + Guid.NewGuid();
- //try
- {
- Directory.CreateDirectory(tmpDirectory);
- Logger.WriteLine("Preparing to export...");
-
- using (_zipFile = new ZipPacker())
- {
- ExportContent(tmpDirectory, app);
-
- #if FULLVERSION
- _zipFile.AddFileFromString("dependences.xml", "", RequiredItemLibrary.Serialize(_itemLibrary));
- #endif
-
- _zipFile.SavePackage(filename);
- }
-
- Logger.WriteLine("Export Completed.");
- }
- //catch (Exception exc)
- {
- //Logger.WriteLine("Exception on export: {0}{1}{2}", exc.Message, Environment.NewLine, exc.ToString());
- }
- //finally
- {
- Directory.Delete(tmpDirectory, true);
- }
- }
-
- private void ExportContent(string tmpDirectory, SPSWebApplication app)
- {
- #if FULLVERSION
- _itemLibrary.Clear();
- #endif
-
- ExportSettings = new SPExportSettings();
-
- ExportSettings.CommandLineVerbose = true;
- ExportSettings.BaseFileName = string.Empty;
- ExportSettings.FileLocation = tmpDirectory + "\\Content";
- ExportSettings.OverwriteExistingDataFile = true;
- ExportSettings.IncludeSecurity = SPIncludeSecurity.All;
- ExportSettings.IncludeVersions = SPIncludeVersions.CurrentVersion;
- ExportSettings.FileCompression = false;
- ExportSettings.SiteUrl = app.RootWeb.Url;
- ExportSettings.ExcludeDependencies = true;
- ExportSettings.ExportMethod = SPExportMethodType.ExportAll;
- ExportSettings.LogExportObjectsTable = true;
- ExportSettings.LogFilePath = tmpDirectory + "\\Content\\export.log";
-
- app.Export(this);
- ExportSettings.Validate();
-
- SPExport export = new SPExport(ExportSettings);
- export.ProgressUpdated += ImportExportProgressUpdated;
-
- if (app.RootWeb.GetSelectedChildCount() != 0)
- {
- export.Run();
- WSSExportManifestFix.Execute(app, tmpDirectory + "\\content");
- _zipFile.PackDirectory(tmpDirectory + "\\content", "");
- }
- }
-
- private static List<string> _types = new List<string>(new[]
- {
- "SPWeb", "SPSWeb",
- "SPList", "SPSList",
- "SPListItem", "SPSListItem",
- "SPFile", "SPSFile"
- });
-
- public SPExportSettings ExportSettings;
-
- public SPSSite Site;
- private ZipPacker _zipFile;
-
- public void ExportSPFile(SPSFile sfile)
- {
- #if FULLVERSION
- if (SPSBackup.CanProcessFile(sfile.Name))
- {
- try
- {
- sfile.GetSPObjectInstance(delegate(object o)
- {
- SPFile file = (SPFile) o;
- string strName = file.Name; // - DONOT REMOVE - THIS FIX SHAREPOINT BUG WITH GET BINARY STREAM
- using (Stream fileStream = file.OpenBinaryStream())
- {
- using (StreamReader reader = new StreamReader(fileStream))
- {
- SPSBackup.ProcessFileStream(reader,
- (filename)=>
- {
- ExportAssembly(filename, null);
- });
- }
- }
-
- SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
- foreach (WebPart wp in wpm.WebParts)
- {
- ExportAssembly(wp.GetType().Assembly.FullName, null);
- }
- });
- }
- catch (Exception e)
- {
- Logger.WriteLine(e.Message);
- }
- }
- #endif
- }
-
- public void ExportSPListRequirements(SPList list)
- {
- #if FULLVERSION
- DependedItem item = null;
- if (list.TemplateFeatureId != Guid.Empty)
- {
- SPFeatureDefinition feature = SPFarm.Local.FeatureDefinitions[list.TemplateFeatureId];
- item = ExportFeature(feature, true);
- }
-
- if (!string.IsNullOrEmpty(list.EventSinkAssembly))
- {
- Logger.WriteLine("- Sink Assembly: " + list.EventSinkAssembly);
- ExportAssembly(list.EventSinkAssembly, item);
- }
- foreach (SPEventReceiverDefinition eventReceiver in list.EventReceivers)
- {
- Logger.WriteLine("- EventReceiver: {0}, {1}", eventReceiver.Class, eventReceiver.Assembly);
- ExportAssembly(eventReceiver.Assembly, item);
- }
- #endif
- }
-
- public void ExportFile(string filePath, string packagePath, DependedItem parent)
- {
- Logger.WriteLine("file:" + FileTools.PackPathFormat(packagePath + Path.GetFileName(filePath)));
- _zipFile.PackFile(FileTools.FormatFileName(filePath),
- FileTools.PackPathFormat(packagePath));
-
- #if FULLVERSION
- using (Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
- {
- using (StreamReader reader = new StreamReader(fileStream))
- {
- SPSBackup.ProcessFileStream(reader, delegate(string libName)
- {
- ExportAssembly(libName, parent);
- });
- }
- }
- #endif
- }
-
- public void ExportFile(FSFile file)
- {
- FSBase dir = file;
- while (dir.Parent is FSDirectory)
- {
- dir = (FSBase) dir.Parent;
- }
-
- string filePath = FileTools.FormatFolderName(Path.GetDirectoryName(file.Path));
- string rootDir = FileTools.FormatFolderName(dir.Path);
-
- string packagePath =
- FileTools.FormatFolderName("/resources/" +
- FileTools.FormatFileName(dir.Name) + "/" +
- FileTools.FormatRelativePath(rootDir, filePath));
-
- Logger.WriteLine("file:" + FileTools.PackPathFormat(packagePath + Path.GetFileName(file.Path)));
- ExportFile(FileTools.FormatFileName(file.Path), packagePath, null);
- }
-
- private bool ExportableAssembly(string name)
- {
- name = name.ToLowerInvariant();
- return (
- !name.StartsWith("microsoft.sharepoint.") &&
- !name.StartsWith("microsoft.sharepoint,") &&
- !name.StartsWith("microsoft.build.") &&
- !name.StartsWith("microsoft.build,") &&
- !name.StartsWith("microsoft.office.") &&
- !name.StartsWith("microsoft.office,") &&
- !name.StartsWith("system,") &&
- !name.StartsWith("system.") &&
- !name.StartsWith("mscorlib,"));
- }
-
- #if FULLVERSION
- private void ExportReferencedAssemblies(Assembly a, DependedItem parent)
- {
- AssemblyName[] refs = a.GetReferencedAssemblies();
- foreach (AssemblyName an in refs)
- {
- ExportAssembly(an.FullName, parent);
- //Logger.WriteLine("- Referenced Assembly: {0}", an.FullName);
- }
- }
-
- public void ExportAssembly(string fullName, DependedItem parent)
- {
- if (!string.IsNullOrEmpty(fullName) &&
- ExportableAssembly(fullName) &&
- (parent == null || !parent.IsCircularReferenced(typeof (DependedAssembly), fullName)))
- {
- try
- {
- Assembly assembly = null;
-
- DependedItem item = _itemLibrary.GetItem(typeof (DependedAssembly), fullName);
- if (item == null)
- {
- assembly = Assembly.ReflectionOnlyLoad(fullName);
-
- item = _itemLibrary.Create(typeof (DependedAssembly), assembly.FullName);
- item.Name = Path.GetFileName(assembly.Location);
- item.PackagePath = _zipFile.PackFile(assembly.Location, "resources/GAC/" + fullName);
- Logger.WriteLine(" - Assembly: {0}", assembly.FullName);
- }
- if (parent != null)
- {
- parent.AddChild(item);
- }
- else
- {
- _itemLibrary.Root.Add(item);
- }
-
- if (assembly != null)
- {
- ExportReferencedAssemblies(assembly, item);
- }
- }
- catch (FileNotFoundException)
- {
- }
- }
- }
- #endif
-
- public DependedItem ExportFeature(SPFeatureDefinition feature, bool requered)
- {
- DependedItem item = null;
- #if FULLVERSION
- item = _itemLibrary.GetItem(typeof (DependedFeature), feature.Id.ToString());
- if (item == null)
- {
- item = _itemLibrary.Create(typeof (DependedFeature), feature.Id.ToString());
- item.Name = feature.GetTitle(CultureInfo.CurrentCulture);
- item.PackagePath = _zipFile.PackDirectory(feature.RootDirectory, "/resources/12/TEMPLATE/FEATURES/");
-
- _itemLibrary.Root.Add(item);
- ExportAssembly(feature.ReceiverAssembly, null);
- ExportAssembly(feature.ReceiverAssembly, item);
- }
-
- Logger.WriteLine("- Feature: {0}", item.Name);
- #endif
- return item;
- }
-
- private static void ImportExportProgressUpdated(object sender, SPDeploymentEventArgs e)
- {
- Logger.WriteLine("Export {0} {1}/{2}", e.Status, e.ObjectsProcessed, e.ObjectsTotal);
- }
- }
- }