PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/ImpEx.Engine/WSSExport.cs

#
C# | 306 lines | 257 code | 37 blank | 12 comment | 20 complexity | 78751acce8ffbc20414fe9f1a601e3bb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Web.UI.WebControls.WebParts;
  7. using System.Xml;
  8. using Microsoft.SharePoint;
  9. using Microsoft.SharePoint.Administration;
  10. using Microsoft.SharePoint.Deployment;
  11. using Microsoft.SharePoint.WebPartPages;
  12. using WebPart=System.Web.UI.WebControls.WebParts.WebPart;
  13. namespace ImpEx.Engine
  14. {
  15. /// <summary>
  16. ///  ласс дл€ выполнени€ действий по инкрементальному импорту / экспорту
  17. /// </summary>
  18. public class WSSExport
  19. {
  20. #if FULLVERSION
  21. private RequiredItemLibrary _itemLibrary = new RequiredItemLibrary();
  22. #endif
  23. /// <summary>
  24. /// ƒелает экспорт, согласно загруженным ранее правилам
  25. /// </summary>
  26. /// <param name="filename">»м€ файла дл€ экспорта</param>
  27. public void Export(string filename, SPSWebApplication app)
  28. {
  29. Site = app.SPSSite;
  30. string tmpDirectory = Path.GetTempPath() + "\\WssExport" + Guid.NewGuid();
  31. //try
  32. {
  33. Directory.CreateDirectory(tmpDirectory);
  34. Logger.WriteLine("Preparing to export...");
  35. using (_zipFile = new ZipPacker())
  36. {
  37. ExportContent(tmpDirectory, app);
  38. #if FULLVERSION
  39. _zipFile.AddFileFromString("dependences.xml", "", RequiredItemLibrary.Serialize(_itemLibrary));
  40. #endif
  41. _zipFile.SavePackage(filename);
  42. }
  43. Logger.WriteLine("Export Completed.");
  44. }
  45. //catch (Exception exc)
  46. {
  47. //Logger.WriteLine("Exception on export: {0}{1}{2}", exc.Message, Environment.NewLine, exc.ToString());
  48. }
  49. //finally
  50. {
  51. Directory.Delete(tmpDirectory, true);
  52. }
  53. }
  54. private void ExportContent(string tmpDirectory, SPSWebApplication app)
  55. {
  56. #if FULLVERSION
  57. _itemLibrary.Clear();
  58. #endif
  59. ExportSettings = new SPExportSettings();
  60. ExportSettings.CommandLineVerbose = true;
  61. ExportSettings.BaseFileName = string.Empty;
  62. ExportSettings.FileLocation = tmpDirectory + "\\Content";
  63. ExportSettings.OverwriteExistingDataFile = true;
  64. ExportSettings.IncludeSecurity = SPIncludeSecurity.All;
  65. ExportSettings.IncludeVersions = SPIncludeVersions.CurrentVersion;
  66. ExportSettings.FileCompression = false;
  67. ExportSettings.SiteUrl = app.RootWeb.Url;
  68. ExportSettings.ExcludeDependencies = true;
  69. ExportSettings.ExportMethod = SPExportMethodType.ExportAll;
  70. ExportSettings.LogExportObjectsTable = true;
  71. ExportSettings.LogFilePath = tmpDirectory + "\\Content\\export.log";
  72. app.Export(this);
  73. ExportSettings.Validate();
  74. SPExport export = new SPExport(ExportSettings);
  75. export.ProgressUpdated += ImportExportProgressUpdated;
  76. if (app.RootWeb.GetSelectedChildCount() != 0)
  77. {
  78. export.Run();
  79. WSSExportManifestFix.Execute(app, tmpDirectory + "\\content");
  80. _zipFile.PackDirectory(tmpDirectory + "\\content", "");
  81. }
  82. }
  83. private static List<string> _types = new List<string>(new[]
  84. {
  85. "SPWeb", "SPSWeb",
  86. "SPList", "SPSList",
  87. "SPListItem", "SPSListItem",
  88. "SPFile", "SPSFile"
  89. });
  90. public SPExportSettings ExportSettings;
  91. public SPSSite Site;
  92. private ZipPacker _zipFile;
  93. public void ExportSPFile(SPSFile sfile)
  94. {
  95. #if FULLVERSION
  96. if (SPSBackup.CanProcessFile(sfile.Name))
  97. {
  98. try
  99. {
  100. sfile.GetSPObjectInstance(delegate(object o)
  101. {
  102. SPFile file = (SPFile) o;
  103. string strName = file.Name; // - DONOT REMOVE - THIS FIX SHAREPOINT BUG WITH GET BINARY STREAM
  104. using (Stream fileStream = file.OpenBinaryStream())
  105. {
  106. using (StreamReader reader = new StreamReader(fileStream))
  107. {
  108. SPSBackup.ProcessFileStream(reader,
  109. (filename)=>
  110. {
  111. ExportAssembly(filename, null);
  112. });
  113. }
  114. }
  115. SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
  116. foreach (WebPart wp in wpm.WebParts)
  117. {
  118. ExportAssembly(wp.GetType().Assembly.FullName, null);
  119. }
  120. });
  121. }
  122. catch (Exception e)
  123. {
  124. Logger.WriteLine(e.Message);
  125. }
  126. }
  127. #endif
  128. }
  129. public void ExportSPListRequirements(SPList list)
  130. {
  131. #if FULLVERSION
  132. DependedItem item = null;
  133. if (list.TemplateFeatureId != Guid.Empty)
  134. {
  135. SPFeatureDefinition feature = SPFarm.Local.FeatureDefinitions[list.TemplateFeatureId];
  136. item = ExportFeature(feature, true);
  137. }
  138. if (!string.IsNullOrEmpty(list.EventSinkAssembly))
  139. {
  140. Logger.WriteLine("- Sink Assembly: " + list.EventSinkAssembly);
  141. ExportAssembly(list.EventSinkAssembly, item);
  142. }
  143. foreach (SPEventReceiverDefinition eventReceiver in list.EventReceivers)
  144. {
  145. Logger.WriteLine("- EventReceiver: {0}, {1}", eventReceiver.Class, eventReceiver.Assembly);
  146. ExportAssembly(eventReceiver.Assembly, item);
  147. }
  148. #endif
  149. }
  150. public void ExportFile(string filePath, string packagePath, DependedItem parent)
  151. {
  152. Logger.WriteLine("file:" + FileTools.PackPathFormat(packagePath + Path.GetFileName(filePath)));
  153. _zipFile.PackFile(FileTools.FormatFileName(filePath),
  154. FileTools.PackPathFormat(packagePath));
  155. #if FULLVERSION
  156. using (Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  157. {
  158. using (StreamReader reader = new StreamReader(fileStream))
  159. {
  160. SPSBackup.ProcessFileStream(reader, delegate(string libName)
  161. {
  162. ExportAssembly(libName, parent);
  163. });
  164. }
  165. }
  166. #endif
  167. }
  168. public void ExportFile(FSFile file)
  169. {
  170. FSBase dir = file;
  171. while (dir.Parent is FSDirectory)
  172. {
  173. dir = (FSBase) dir.Parent;
  174. }
  175. string filePath = FileTools.FormatFolderName(Path.GetDirectoryName(file.Path));
  176. string rootDir = FileTools.FormatFolderName(dir.Path);
  177. string packagePath =
  178. FileTools.FormatFolderName("/resources/" +
  179. FileTools.FormatFileName(dir.Name) + "/" +
  180. FileTools.FormatRelativePath(rootDir, filePath));
  181. Logger.WriteLine("file:" + FileTools.PackPathFormat(packagePath + Path.GetFileName(file.Path)));
  182. ExportFile(FileTools.FormatFileName(file.Path), packagePath, null);
  183. }
  184. private bool ExportableAssembly(string name)
  185. {
  186. name = name.ToLowerInvariant();
  187. return (
  188. !name.StartsWith("microsoft.sharepoint.") &&
  189. !name.StartsWith("microsoft.sharepoint,") &&
  190. !name.StartsWith("microsoft.build.") &&
  191. !name.StartsWith("microsoft.build,") &&
  192. !name.StartsWith("microsoft.office.") &&
  193. !name.StartsWith("microsoft.office,") &&
  194. !name.StartsWith("system,") &&
  195. !name.StartsWith("system.") &&
  196. !name.StartsWith("mscorlib,"));
  197. }
  198. #if FULLVERSION
  199. private void ExportReferencedAssemblies(Assembly a, DependedItem parent)
  200. {
  201. AssemblyName[] refs = a.GetReferencedAssemblies();
  202. foreach (AssemblyName an in refs)
  203. {
  204. ExportAssembly(an.FullName, parent);
  205. //Logger.WriteLine("- Referenced Assembly: {0}", an.FullName);
  206. }
  207. }
  208. public void ExportAssembly(string fullName, DependedItem parent)
  209. {
  210. if (!string.IsNullOrEmpty(fullName) &&
  211. ExportableAssembly(fullName) &&
  212. (parent == null || !parent.IsCircularReferenced(typeof (DependedAssembly), fullName)))
  213. {
  214. try
  215. {
  216. Assembly assembly = null;
  217. DependedItem item = _itemLibrary.GetItem(typeof (DependedAssembly), fullName);
  218. if (item == null)
  219. {
  220. assembly = Assembly.ReflectionOnlyLoad(fullName);
  221. item = _itemLibrary.Create(typeof (DependedAssembly), assembly.FullName);
  222. item.Name = Path.GetFileName(assembly.Location);
  223. item.PackagePath = _zipFile.PackFile(assembly.Location, "resources/GAC/" + fullName);
  224. Logger.WriteLine(" - Assembly: {0}", assembly.FullName);
  225. }
  226. if (parent != null)
  227. {
  228. parent.AddChild(item);
  229. }
  230. else
  231. {
  232. _itemLibrary.Root.Add(item);
  233. }
  234. if (assembly != null)
  235. {
  236. ExportReferencedAssemblies(assembly, item);
  237. }
  238. }
  239. catch (FileNotFoundException)
  240. {
  241. }
  242. }
  243. }
  244. #endif
  245. public DependedItem ExportFeature(SPFeatureDefinition feature, bool required)
  246. {
  247. DependedItem item = null;
  248. if (feature != null)
  249. {
  250. item = _itemLibrary.GetItem(typeof(DependedFeature), feature.Id.ToString());
  251. if (item == null)
  252. {
  253. item = _itemLibrary.Create(typeof(DependedFeature), feature.Id.ToString());
  254. item.Name = feature.GetTitle(CultureInfo.CurrentCulture);
  255. item.PackagePath = _zipFile.PackDirectory(feature.RootDirectory, "/resources/12/TEMPLATE/FEATURES/");
  256. _itemLibrary.Root.Add(item);
  257. ExportAssembly(feature.ReceiverAssembly, null);
  258. ExportAssembly(feature.ReceiverAssembly, item);
  259. }
  260. Logger.WriteLine("- Feature: {0}", item.Name);
  261. }
  262. return item;
  263. }
  264. private static void ImportExportProgressUpdated(object sender, SPDeploymentEventArgs e)
  265. {
  266. Logger.WriteLine("Export {0} {1}/{2}", e.Status, e.ObjectsProcessed, e.ObjectsTotal);
  267. }
  268. }
  269. }