// 回收站 - 删除选中
        public async Task DeleteTrashAsync(Site site, int channelId, string tableName, List <int> contentIdList, IPluginManager pluginManager)
        {
            if (contentIdList == null || contentIdList.Count == 0)
            {
                return;
            }

            var repository = await GetRepositoryAsync(tableName);

            var cacheKeys = new List <string>
            {
                GetListKey(repository.TableName, site.Id, channelId)
            };

            foreach (var contentId in contentIdList)
            {
                cacheKeys.Add(GetEntityKey(repository.TableName, contentId));
            }

            await repository.DeleteAsync(Q
                                         .Where(nameof(Content.SiteId), site.Id)
                                         .Where(nameof(Content.ChannelId), "<", 0)
                                         .WhereIn(nameof(Content.Id), contentIdList)
                                         .CachingRemove(cacheKeys.ToArray())
                                         );

            channelId = Math.Abs(channelId);

            var handlers = pluginManager.GetExtensions <PluginContentHandler>();

            foreach (var handler in handlers)
            {
                try
                {
                    foreach (var contentId in contentIdList)
                    {
                        handler.OnDeleted(site.Id, channelId, contentId);
                        await handler.OnDeletedAsync(site.Id, channelId, contentId);
                    }
                }
                catch (Exception ex)
                {
                    await _errorLogRepository.AddErrorLogAsync(ex);
                }
            }

            //foreach (var plugin in oldPluginManager.GetPlugins())
            //{
            //    try
            //    {
            //        plugin.OnContentDeleteCompleted(new ContentEventArgs(site.Id, channel.Id, contentId));
            //    }
            //    catch (Exception ex)
            //    {
            //        await _errorLogRepository.AddErrorLogAsync(plugin.PluginId, ex, nameof(plugin.OnContentDeleteCompleted));
            //    }
            //}
        }
Example #2
0
        public static void AddPluginServices(this IServiceCollection services, IPluginManager pluginManager)
        {
            var instances = pluginManager.GetExtensions <IPluginConfigureServices>();

            if (instances != null)
            {
                foreach (var plugin in instances)
                {
                    try
                    {
                        plugin.ConfigureServices(services);
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
        }
Example #3
0
        public static async Task UsePluginsAsync(this IApplicationBuilder app, ISettingsManager settingsManager,
                                                 IPluginManager pluginManager, IErrorLogRepository errorLogRepository)
        {
            var logger = app.ApplicationServices.GetService <ILoggerFactory>()
                         .CreateLogger <IApplicationBuilder>();

            foreach (var plugin in pluginManager.Plugins)
            {
                if (plugin.Disabled)
                {
                    continue;
                }

                logger.LogInformation("Using Plugin '{0}'", plugin.PluginId);

                DirectoryUtils.CreateDirectoryIfNotExists(plugin.WebRootPath);

                var fileProvider = new PhysicalFileProvider(plugin.WebRootPath);
                app.UseStaticFiles(
                    new StaticFileOptions
                {
                    FileProvider = fileProvider
                });
            }

            var configures = pluginManager.GetExtensions <IPluginConfigure>();

            if (configures != null)
            {
                foreach (var configure in configures)
                {
                    configure.Configure(app);
                }
            }

            var database = settingsManager.Database;

            var tables = settingsManager.GetTables();

            foreach (var table in tables.Where(table => !string.IsNullOrEmpty(table.Id)))
            {
                List <TableColumn> columns;
                if (StringUtils.EqualsIgnoreCase(table.Type, Types.TableTypes.Custom))
                {
                    columns = table.Columns;
                }
                else if (StringUtils.EqualsIgnoreCase(table.Type, Types.TableTypes.Content))
                {
                    columns = database.GetTableColumns(null);
                    columns.AddRange(database.GetTableColumns <Content>());
                    if (table.Columns != null)
                    {
                        foreach (var tableColumn in table.Columns.Where(tableColumn =>
                                                                        !columns.Any(x => StringUtils.EqualsIgnoreCase(x.AttributeName, tableColumn.AttributeName))))
                        {
                            columns.Add(tableColumn);
                        }
                    }
                }
                else
                {
                    columns = database.GetTableColumns(table.Columns);
                }

                if (columns == null || columns.Count == 0)
                {
                    continue;
                }

                try
                {
                    logger.LogInformation("Sync Plugin Table '{0}'", table.Id);
                    if (!await database.IsTableExistsAsync(table.Id))
                    {
                        await database.CreateTableAsync(table.Id, columns);
                    }
                    else
                    {
                        await database.AlterTableAsync(table.Id, columns);
                    }
                }
                catch
                {
                    // ignored
                }
            }
        }
Example #4
0
        public static async Task TranslateAsync(IPathManager pathManager, IDatabaseManager databaseManager, IPluginManager pluginManager, Site site, int channelId, int contentId, int targetSiteId, int targetChannelId, TranslateType translateType, ICreateManager createManager, int adminId)
        {
            if (site == null || channelId <= 0 || contentId <= 0 || targetSiteId <= 0 || targetChannelId <= 0)
            {
                return;
            }

            var targetSite = await databaseManager.SiteRepository.GetAsync(targetSiteId);

            var targetChannelInfo = await databaseManager.ChannelRepository.GetAsync(targetChannelId);

            var channel = await databaseManager.ChannelRepository.GetAsync(channelId);

            var contentInfo = await databaseManager.ContentRepository.GetAsync(site, channel, contentId);

            if (contentInfo == null)
            {
                return;
            }

            contentInfo = contentInfo.Clone <Content>();

            if (translateType == TranslateType.Copy)
            {
                await pathManager.MoveFileByContentAsync(site, targetSite, contentInfo);

                contentInfo.SiteId    = targetSiteId;
                contentInfo.SourceId  = contentInfo.ChannelId;
                contentInfo.ChannelId = targetChannelId;
                contentInfo.Taxis     = 0;
                var theContentId = await databaseManager.ContentRepository.InsertAsync(targetSite, targetChannelInfo, contentInfo);

                var handlers = pluginManager.GetExtensions <PluginContentHandler>();
                foreach (var handler in handlers)
                {
                    try
                    {
                        handler.OnTranslated(site.Id, channel.Id, contentId, targetSiteId, targetChannelId, theContentId);
                        await handler.OnTranslatedAsync(site.Id, channel.Id, contentId, targetSiteId, targetChannelId, theContentId);
                    }
                    catch (Exception ex)
                    {
                        await databaseManager.ErrorLogRepository.AddErrorLogAsync(ex);
                    }
                }

                //foreach (var plugin in oldPluginManager.GetPlugins())
                //{
                //    try
                //    {
                //        plugin.OnContentTranslateCompleted(new ContentTranslateEventArgs(site.Id, channel.Id, contentId, targetSiteId, targetChannelId, theContentId));
                //    }
                //    catch (Exception ex)
                //    {
                //        await databaseManager.ErrorLogRepository.AddErrorLogAsync(plugin.PluginId, ex, nameof(plugin.OnContentTranslateCompleted));
                //    }
                //}

                await createManager.CreateContentAsync(targetSite.Id, contentInfo.ChannelId, theContentId);

                await createManager.TriggerContentChangedEventAsync(targetSite.Id, contentInfo.ChannelId);
            }
            else if (translateType == TranslateType.Cut)
            {
                await pathManager.MoveFileByContentAsync(site, targetSite, contentInfo);

                contentInfo.SiteId    = targetSiteId;
                contentInfo.SourceId  = contentInfo.ChannelId;
                contentInfo.ChannelId = targetChannelId;
                contentInfo.Taxis     = 0;

                var newContentId = await databaseManager.ContentRepository.InsertAsync(targetSite, targetChannelInfo, contentInfo);

                var handlers = pluginManager.GetExtensions <PluginContentHandler>();
                foreach (var handler in handlers)
                {
                    try
                    {
                        handler.OnTranslated(site.Id, channel.Id, contentId, targetSiteId, targetChannelId, newContentId);
                        await handler.OnTranslatedAsync(site.Id, channel.Id, contentId, targetSiteId, targetChannelId, newContentId);
                    }
                    catch (Exception ex)
                    {
                        await databaseManager.ErrorLogRepository.AddErrorLogAsync(ex);
                    }
                }

                //foreach (var plugin in oldPluginManager.GetPlugins())
                //{
                //    try
                //    {
                //        plugin.OnContentTranslateCompleted(new ContentTranslateEventArgs(site.Id, channel.Id, contentId, targetSiteId, targetChannelId, newContentId));
                //    }
                //    catch (Exception ex)
                //    {
                //        await databaseManager.ErrorLogRepository.AddErrorLogAsync(plugin.PluginId, ex, nameof(plugin.OnContentTranslateCompleted));
                //    }
                //}

                await databaseManager.ContentRepository.TrashContentAsync(site, channel, contentId, adminId);

                //await databaseManager.ContentRepository.DeleteAsync(oldPluginManager, pluginManager, site, channel, contentId);

                //GlobalSettings.ContentRepository.DeleteContents(site.Id, tableName, TranslateUtils.ToIntList(contentId), channelId);

                await createManager.CreateContentAsync(targetSite.Id, contentInfo.ChannelId, newContentId);

                await createManager.TriggerContentChangedEventAsync(targetSite.Id, contentInfo.ChannelId);
            }
            else if (translateType == TranslateType.Reference)
            {
                if (contentInfo.ReferenceId != 0)
                {
                    return;
                }

                contentInfo.SiteId      = targetSiteId;
                contentInfo.SourceId    = contentInfo.ChannelId;
                contentInfo.ChannelId   = targetChannelId;
                contentInfo.ReferenceId = contentId;
                contentInfo.Taxis       = 0;
                int theContentId = await databaseManager.ContentRepository.InsertAsync(targetSite, targetChannelInfo, contentInfo);

                await createManager.CreateContentAsync(targetSite.Id, contentInfo.ChannelId, theContentId);

                await createManager.TriggerContentChangedEventAsync(targetSite.Id, contentInfo.ChannelId);
            }
        }
Example #5
0
        public async Task ParseAsync(StringBuilder contentBuilder, string filePath, bool isDynamic)
        {
            var context = new PluginParseContext(this);

            var startParsesAsync = _pluginManager.GetExtensions <IPluginCreateStartAsync>();

            if (startParsesAsync != null)
            {
                foreach (var extension in startParsesAsync)
                {
                    try
                    {
                        await extension.ParseAsync(context);
                    }
                    catch (Exception ex)
                    {
                        await AddStlErrorLogAsync(nameof(IPluginCreateStartAsync), string.Empty,
                                                  ex);
                    }
                }
            }

            var startParses = _pluginManager.GetExtensions <IPluginCreateStart>();

            if (startParses != null)
            {
                foreach (var extension in startParses)
                {
                    try
                    {
                        extension.Parse(context);
                    }
                    catch (Exception ex)
                    {
                        await AddStlErrorLogAsync(nameof(IPluginCreateStartAsync), string.Empty,
                                                  ex);
                    }
                }
            }

            if (contentBuilder.Length > 0)
            {
                await ParseTemplateContentAsync(contentBuilder);
            }

            var endParsesAsync = _pluginManager.GetExtensions <IPluginCreateEndAsync>();

            if (endParsesAsync != null)
            {
                foreach (var extension in endParsesAsync)
                {
                    try
                    {
                        await extension.ParseAsync(context);
                    }
                    catch (Exception ex)
                    {
                        await AddStlErrorLogAsync(nameof(IPluginCreateEndAsync), string.Empty,
                                                  ex);
                    }
                }
            }

            var endParses = _pluginManager.GetExtensions <IPluginCreateEnd>();

            if (endParses != null)
            {
                foreach (var extension in endParses)
                {
                    try
                    {
                        extension.Parse(context);
                    }
                    catch (Exception ex)
                    {
                        await AddStlErrorLogAsync(nameof(IPluginCreateEnd), string.Empty,
                                                  ex);
                    }
                }
            }

            if (FileUtils.IsHtml(PathUtils.GetExtension(filePath)))
            {
                if (isDynamic)
                {
                    var pageUrl = PageUtils.AddProtocolToUrl(
                        PathManager.ParseUrl(
                            $"~/{PathUtils.GetPathDifference(SettingsManager.WebRootPath, filePath)}"));
                    string templateString = $@"
<base href=""{pageUrl}"" />";
                    StringUtils.InsertAfter(new[] { "<head>", "<HEAD>" }, contentBuilder, templateString);
                }

                if (PageInfo.Site.IsCreateBrowserNoCache)
                {
                    const string templateString = @"
<META HTTP-EQUIV=""Pragma"" CONTENT=""no-cache"">
<META HTTP-EQUIV=""Expires"" CONTENT=""-1"">";
                    StringUtils.InsertAfter(new[] { "<head>", "<HEAD>" }, contentBuilder, templateString);
                }

                if (PageInfo.Site.IsCreateIe8Compatible)
                {
                    const string templateString = @"
<META HTTP-EQUIV=""x-ua-compatible"" CONTENT=""ie=7"" />";
                    StringUtils.InsertAfter(new[] { "<head>", "<HEAD>" }, contentBuilder, templateString);
                }

                if (PageInfo.Site.IsCreateJsIgnoreError)
                {
                    const string templateString = @"
<script type=""text/javascript"">window.onerror=function(){return true;}</script>";
                    StringUtils.InsertAfter(new[] { "<head>", "<HEAD>" }, contentBuilder, templateString);
                }

                var isShowPageInfo = PageInfo.Site.IsCreateShowPageInfo;

                if (!PageInfo.IsLocal)
                {
                    if (PageInfo.Site.IsCreateDoubleClick)
                    {
                        var fileTemplateId = 0;
                        if (PageInfo.Template.TemplateType == TemplateType.FileTemplate)
                        {
                            fileTemplateId = PageInfo.Template.Id;
                        }

                        var ajaxUrl = PathManager.GetTriggerApiUrl(PageInfo.SiteId, ContextInfo.ChannelId,
                                                                   ContextInfo.ContentId, fileTemplateId, true);
                        if (!PageInfo.FootCodes.ContainsKey("CreateDoubleClick"))
                        {
                            PageInfo.FootCodes.Add("CreateDoubleClick", $@"
<script type=""text/javascript"" language=""javascript"">document.ondblclick=function(x){{location.href = '{ajaxUrl}&returnUrl=' + encodeURIComponent(location.search);}}</script>");
                        }
                    }
                }
                else
                {
                    isShowPageInfo = true;
                }

                if (isShowPageInfo)
                {
                    contentBuilder.Append($@"
<!-- {PageInfo.Template.RelatedFileName}({PageInfo.Template.TemplateType.GetDisplayName()}) -->");
                }

                var headCodesHtml = PageInfo.HeadCodesHtml;
                if (!string.IsNullOrEmpty(headCodesHtml))
                {
                    if (contentBuilder.ToString().IndexOf("</head>", StringComparison.Ordinal) != -1 ||
                        contentBuilder.ToString().IndexOf("</HEAD>", StringComparison.Ordinal) != -1)
                    {
                        StringUtils.InsertBefore(new[] { "</head>", "</HEAD>" }, contentBuilder, headCodesHtml);
                    }
                    else
                    {
                        contentBuilder.Insert(0, headCodesHtml);
                    }
                }

                var bodyCodesHtml = PageInfo.BodyCodesHtml;
                if (!string.IsNullOrEmpty(bodyCodesHtml))
                {
                    if (contentBuilder.ToString().IndexOf("<body", StringComparison.Ordinal) != -1 ||
                        contentBuilder.ToString().IndexOf("<BODY", StringComparison.Ordinal) != -1)
                    {
                        var index = contentBuilder.ToString().IndexOf("<body", StringComparison.Ordinal);
                        if (index == -1)
                        {
                            index = contentBuilder.ToString().IndexOf("<BODY", StringComparison.Ordinal);
                        }

                        index = contentBuilder.ToString().IndexOf(">", index, StringComparison.Ordinal);
                        contentBuilder.Insert(index + 1,
                                              Constants.ReturnAndNewline + bodyCodesHtml + Constants.ReturnAndNewline);
                    }
                    else
                    {
                        contentBuilder.Insert(0, bodyCodesHtml);
                    }
                }

                var footCodesHtml = PageInfo.FootCodesHtml;
                if (!string.IsNullOrEmpty(footCodesHtml))
                {
                    contentBuilder.Append(footCodesHtml + Constants.ReturnAndNewline);
                }
            }
        }