Claymore.ArchiveWikiBot.ArchiveByPeriod.Process C# (CSharp) Method

Process() public method

public Process ( Wiki wiki, WikiPage page, int &diffSize, int &topics ) : string>.Dictionary
wiki Claymore.SharpMediaWiki.Wiki
page Claymore.SharpMediaWiki.WikiPage
diffSize int
topics int
return string>.Dictionary
        public override Dictionary<string, string> Process(Wiki wiki, WikiPage page, ref int diffSize, ref int topics)
        {
            Dictionary<string, string> results = new Dictionary<string, string>();
            Dictionary<DateTime, List<WikiPageSection>> archives = new Dictionary<DateTime, List<WikiPageSection>>();
            List<WikiPageSection> archivedSections = new List<WikiPageSection>();
            foreach (WikiPageSection section in page.Sections)
            {
                WikiPageSection result = section.Subsections.FirstOrDefault(ss => ss.Title.Trim().ToLower() == "итог");
                bool forceArchivation = LookForLines.Any(s => section.Text.ToLower().Contains(s.ToLower()));
                if (!OnHold.Any(s => section.Text.ToLower().Contains(s.ToLower())) &&
                    ((result != null && !string.IsNullOrEmpty(result.SectionText.Trim())) ||
                     forceArchivation ||
                     !CheckForResult))
                {
                    MatchCollection ms = timeRE.Matches(FilterQuotes(section.Text));
                    DateTime published = NormalizeDate(DateTime.Today);
                    DateTime lastReply = DateTime.MinValue;
                    foreach (Match match in ms)
                    {
                        string value = match.Groups[1].Value;
                        DateTime time;
                        if (!DateTime.TryParse(value, L10i.Culture,
                            DateTimeStyles.AssumeUniversal, out time))
                        {
                            continue;
                        }
                        if (time < published)
                        {
                            published = NormalizeDate(time);
                        }
                        if (time > lastReply)
                        {
                            lastReply = time;
                        }
                    }
                    if (lastReply != DateTime.MinValue &&
                        ((forceArchivation && (DateTime.Today - lastReply).TotalHours >= ForcedArchivationDelay) ||
                        (DateTime.Today - lastReply).TotalHours >= Delay))
                    {
                        if (archives.ContainsKey(published))
                        {
                            archives[published].Add(section);
                        }
                        else
                        {
                            List<WikiPageSection> sections = new List<WikiPageSection>();
                            sections.Add(section);
                            archives.Add(published, sections);
                        }
                        archivedSections.Add(section);
                    }
                }
                if (IsMovedSection(section))
                {
                    section.SectionText = section.SectionText.Trim(new char[] { ' ', '\t', '\n' }) + "\n~~~~\n";
                }
            }

            if (archivedSections.Count == 0)
            {
                return results;
            }

            foreach (DateTime period in archives.Keys)
            {
                ParameterCollection parameters = new ParameterCollection();
                parameters.Add("prop", "info");
                string pageName = DateToPageName(period);
                string pageFileName = _cacheDir + Cache.GenerateCachePath(period.ToString(Format));
                XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, new string[] { pageName });
                XmlNode node = xml.SelectSingleNode("//page");
                string text;
                if (node.Attributes["missing"] == null)
                {
                    text = Cache.LoadPageFromCache(pageFileName,
                            node.Attributes["lastrevid"].Value, pageName);

                    if (string.IsNullOrEmpty(text))
                    {
                        Console.Out.WriteLine("Downloading " + pageName + "...");
                        text = wiki.LoadText(pageName);
                        Cache.CachePage(period.ToString(Format), _cacheDir, node.Attributes["lastrevid"].Value, text);
                    }
                }
                else
                {
                    text = Header;
                }

                WikiPage archivePage = WikiPage.Parse(pageName, text);
                foreach (WikiPageSection section in archives[period])
                {
                    section.Title = ProcessSectionTitle(section.Title);
                    archivePage.Sections.Add(section);
                }
                if (NewSectionsDown)
                {
                    archivePage.Sections.Sort(SectionsDown);
                }
                else
                {
                    archivePage.Sections.Sort(SectionsUp);
                }
                if (!string.IsNullOrEmpty(RemoveFromText))
                {
                    archivePage.Text = archivePage.Text.Replace(RemoveFromText, "");
                }
                results.Add(pageName, archivePage.Text);
            }

            topics = 0;
            diffSize = 0;
            foreach (var section in archivedSections)
            {
                diffSize += Encoding.UTF8.GetByteCount(section.Text);
                ++topics;
                page.Sections.Remove(section);
            }
            return results;
        }