[kooboo]创建站点过程
KooBoo创建站点是从一个压缩的文件包中取得模板文件的,然后再根据这个模板文件进行进行调整,这样一个过程。
从代码来看:
从创建一个根站点开始:
1,http://localhost:3910/Sites/Site/CreateRootSite ,
[CreateSiteAuthroziation]
public virtual ActionResult CreateRootSite()
{
//取得模板路径下所有 .zip 结尾的文件及取得预览图。
ViewData["TemplatesJSON"] = ServiceFactory.SiteTemplateManager.All().Select(o => { o.Thumbnail = !string.IsNullOrWhiteSpace(o.Thumbnail) ? Url.Content(o.Thumbnail) : null; return o; }).ToJSON();
//取得所有分类
ViewData["TemplateCategorys"] = ServiceFactory.SiteTemplateManager.AllCategories();
return View(new CreateSiteModel() { Version = "1.0.0.0" });
}
2,提交创建过程,这一步就有点意思了。
[CreateSiteAuthroziation]
[HttpPost]
public virtual ActionResult CreateRootSite([Bind(Exclude = "Parent")]Site site, string template, string parent)
{
return Create(site, parent, template);
}
[CreateSiteAuthroziation]
[HttpPost]
public virtual ActionResult Create([Bind(Exclude = "Parent")]Site site, string parent, string template)
{
JsonResultEntry resultEntry = new JsonResultEntry(ViewData.ModelState);
try
{
if (ModelState.IsValid)
{
Site parentSite = null;
if (!string.IsNullOrEmpty(parent))
{
parentSite = new Site(SiteHelper.SplitFullName(parent));
}
var createdSite = Kooboo.CMS.Sites.Services.ServiceFactory.SiteManager.Create(parentSite, site.Name, template, site, User.Identity.Name);
resultEntry.RedirectUrl = Url.Action("SiteMap", new { controller = "home", siteName = createdSite.FullName });
}
}
catch (Exception e)
{
resultEntry.AddException(e);
}
return Json(resultEntry);
}
下面就是根据 zip文件来了创建模板了。
public virtual Site Create(Site parent, string siteName, string templateFullName, Site siteSetting, string userName)
{
if (string.IsNullOrEmpty(templateFullName))
{
if (parent == null)
{
throw new KoobooException("Parent site is required.".Localize());
}
parent = parent.AsActual();
siteSetting.Parent = parent;
// Set the same settings with parent.
siteSetting.EnableJquery = parent.EnableJquery;
siteSetting.EnableStyleEdting = parent.EnableStyleEdting;
siteSetting.EnableVersioning = parent.EnableVersioning;
siteSetting.InlineEditing = parent.InlineEditing;
siteSetting.CustomFields = parent.CustomFields;
siteSetting.Smtp = parent.Smtp;
// 这里有点意思, 模板方法
Add(parent, siteSetting);
if (!string.IsNullOrEmpty(siteSetting.Repository))
{
CopyRepository(parent.GetRepository(), siteSetting.Repository);
}
return siteSetting;
}
else
{
var template = new ItemTemplate(templateFullName);
var itemTemplate = ServiceFactory.SiteTemplateManager.GetItemTemplate(template.Category, template.TemplateName);
if (itemTemplate == null)
{
throw new KoobooException("The template does not exists.");
}
Site site = null;
using (FileStream fs = new FileStream(itemTemplate.TemplateFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
site = Create(parent, siteName, siteSetting.Repository, fs, userName);
}
//copy site setting...
site.Repository = siteSetting.Repository;
site.DisplayName = siteSetting.DisplayName;
site.Culture = siteSetting.Culture;
site.Domains = siteSetting.Domains;
site.SitePath = siteSetting.SitePath;
site.Version = siteSetting.Version;
site.Mode = siteSetting.Mode;
// 这里有点意思, 模板方法
Update(site);
return site;
}
}
public virtual Site Create(Site parent, string siteName, string repositoryName, Stream siteStream, string userName)
{
//注意了,这里 create 是使用的文件,
var site = SiteProvider.Create(parent, siteName, siteStream, repositoryName);
site.Repository = repositoryName;
Update(site);
SiteProvider.Initialize(site);
SiteProvider.Online(site);
return site;
}
初始化存储
namespace Kooboo.CMS.Content.Persistence.SqlServer
{
public class RepositoryProvider : Kooboo.CMS.Content.Persistence.Default.RepositoryProvider
{
public override void Add(Models.Repository item)
{
base.Add(item);
DatabaseHelper.InitializeDatabase(item);
}
public override void Remove(Models.Repository item)
{
try
{
DatabaseHelper.DisposeDatabase(item);
}
catch
{
}
base.Remove(item);
}
public override void Initialize(Models.Repository repository)
{
DatabaseHelper.InitializeDatabase(repository);
base.Initialize(repository);
}
public override bool TestDbConnection()
{
//Do not use sharing database, do not test the sharing connection string.
//if (SqlServerSettings.Instance.SharingDatabase == false)
//{
// return true;
//}
var shareConnectionString = SqlServerSettings.Instance.SharingDatabaseConnectionString;
return SQLServerHelper.TestConnection(shareConnectionString);
}
}
}
public virtual void Initialize(Repository repository)
{
ITextContentProvider textContentProvider = Providers.DefaultProviderFactory.GetProvider<ITextContentProvider>();
Providers.DefaultProviderFactory.GetProvider<IMediaContentProvider>().InitializeMediaContents(repository);
ISchemaProvider schemaProvider = Providers.DefaultProviderFactory.GetProvider<ISchemaProvider>();
//if (!(textContentProvider is TextContentProvider))
//{
ISchemaProvider xmlSchemaProvider = new SchemaProvider();
ITextContentProvider xmlContentProvider = new TextContentProvider();
foreach (var schema in xmlSchemaProvider.All(repository))
{
schemaProvider.Initialize(schema);
textContentProvider.ImportSchemaData(schema, xmlContentProvider.ExportSchemaData(schema));
}
textContentProvider.ImportCategoryData(repository, xmlContentProvider.ExportCategoryData(repository));
//}
}