Пример #1
0
        private static async Task UpdateTemplate(MediaWiki wiki, UserInfo user, string title, string page, JObject settings)
        {
            var template = new Template {
                Name = settings.Value <string>("name")
            };

            var args = settings.Value <JArray>("args");

            foreach (var arg in args.Values <JObject>())
            {
                foreach (var prop in arg.Properties())
                {
                    prop.Value = prop.Value.Value <string>().Replace("%user.name%", user.Username);
                }
                template.Args.Add(arg.ToObject <Template.Argument>());
            }

            var templateString = template + "\n";

            if (settings.Value <bool>("talkPage"))
            {
                title = "Talk:" + title;
                page  = await wiki.GetPage(title) ?? "";
            }

            var existing = ParserUtils.FindTemplates(page, template.Name);

            if (existing.Count == 0)
            {
                page = page.Insert(0, templateString);
            }
            else
            {
                var regions = existing.Values.OrderByDescending(r => r.Offset).ToArray();
                foreach (var r in regions)
                {
                    var region = ParserUtils.ExpandToWholeLine(page, r);
                    page = page.Remove(region.Offset, region.Length);
                }
                page = page.Insert(regions.Last().Offset, templateString);
            }

            await wiki.EditPage(title, page, "Automatically adding template");
        }
        private static async Task UpdateTemplate(MediaWiki wiki, UserInfo user, string title, string page, TemplateConfig settings, string message)
        {
            var template = new Template {
                Name = settings.Name
            };

            foreach (var arg in settings.Args)
            {
                template.Args.Add(new Template.Argument
                {
                    Name  = arg.Name,
                    Value = arg.Value.Replace("%user.name%", user.Username),
                });
            }

            if (settings.TalkPage)
            {
                title = "Talk:" + title;
                page  = await wiki.GetPage(title) ?? "";
            }

            var templateString = template + "\n";
            var existing       = ParserUtils.FindTemplates(page, template.Name);

            if (existing.Count == 0)
            {
                page = page.Insert(0, templateString);
            }
            else
            {
                var regions = existing.Values.OrderByDescending(r => r.Offset).ToArray();
                foreach (var r in regions)
                {
                    var region = ParserUtils.ExpandToWholeLine(page, r);
                    page = page.Remove(region.Offset, region.Length);
                }
                page = page.Insert(regions.Last().Offset, templateString);
            }

            await wiki.EditPage(title, page, message);
        }