Query() public method

public Query ( QueryBy queryBy, ParameterCollection parameters, IEnumerable ids ) : XmlDocument
queryBy QueryBy
parameters ParameterCollection
ids IEnumerable
return System.Xml.XmlDocument
Ejemplo n.º 1
0
        public static WikiPage Load(Wiki wiki, string title, string directory)
        {
            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("prop", "info|revisions");
            parameters.Add("intoken", "edit");
            parameters.Add("rvprop", "timestamp");
            XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, new string[] { title });
            XmlNode node = xml.SelectSingleNode("//rev");
            string baseTimeStamp = null;
            if (node != null && node.Attributes["timestamp"] != null)
            {
                baseTimeStamp = node.Attributes["timestamp"].Value;
            }
            node = xml.SelectSingleNode("//page");
            string editToken = node.Attributes["edittoken"].Value;

            string pageFileName = directory + EscapePath(title);
            string text = LoadPageFromCache(pageFileName, node.Attributes["lastrevid"].Value, title);

            if (string.IsNullOrEmpty(text))
            {
                Console.Out.WriteLine("Downloading " + title + "...");
                text = wiki.LoadText(title);
                CachePage(pageFileName, node.Attributes["lastrevid"].Value, text);
            }

            WikiPage page = WikiPage.Parse(title, text);
            page.BaseTimestamp = baseTimeStamp;
            page.Token = editToken;
            return page;
        }
Ejemplo n.º 2
0
 private void PutNotification(Wiki wiki, string title, string date)
 {
     string talkPage = wiki.GetNamespace(1) + ":" + title;
     Console.Out.WriteLine("Updating " + talkPage + "...");
     try
     {
         ParameterCollection parameters = new ParameterCollection();
         parameters.Add("rvprop", "content");
         parameters.Add("rvsection", "0)");
         parameters.Add("prop", "revisions");
         XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, new string[] { talkPage });
         string content;
         XmlNode node = xml.SelectSingleNode("//rev");
         if (node != null && node.FirstChild != null)
         {
             content = node.FirstChild.Value;
         }
         else
         {
             content = "";
         }
         int index = content.IndexOf("{{" + _l10i.NotificationTemplate + "|", StringComparison.CurrentCultureIgnoreCase);
         if (index != -1)
         {
             int endIndex = content.IndexOf("}}", index);
             if (endIndex != -1)
             {
                 content = content.Insert(endIndex, "|" + date);
             }
         }
         else
         {
             index = content.IndexOf("{{talkheader", StringComparison.CurrentCultureIgnoreCase);
             if (index != -1)
             {
                 int endIndex = content.IndexOf("}}", index);
                 if (endIndex != -1)
                 {
                     content = content.Insert(endIndex + 2, "\n{{" + _l10i.NotificationTemplate + "|" + date + "}}\n");
                 }
             }
             else
             {
                 index = content.IndexOf("{{заголовок обсуждения", StringComparison.CurrentCultureIgnoreCase);
                 if (index != -1)
                 {
                     int endIndex = content.IndexOf("}}", index);
                     if (endIndex != -1)
                     {
                         content = content.Insert(endIndex + 2, "\n{{" + _l10i.NotificationTemplate + "|" + date + "}}\n");
                     }
                 }
                 else
                 {
                     content = content.Insert(0, "\n{{" + _l10i.NotificationTemplate + "|" + date + "}}\n");
                 }
             }
         }
         wiki.SaveSection(talkPage,
             "0",
             content,
             _l10i.MainPageUpdateComment);
     }
     catch (WikiException e)
     {
         Console.Out.WriteLine("Failed to update " + talkPage + ":" + e.Message);
     }
 }
Ejemplo n.º 3
0
        public void Analyse(Wiki wiki)
        {
            Directory.CreateDirectory(_cacheDir);

            Regex wikiLinkRE = new Regex(@"\[{2}(.+?)(\|.+?)?]{2}");

            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("generator", "categorymembers");
            parameters.Add("gcmtitle", _l10i.Category);
            parameters.Add("gcmlimit", "max");
            parameters.Add("gcmnamespace", "4");
            parameters.Add("prop", "info");

            XmlDocument doc = wiki.Enumerate(parameters, true);
            XmlNodeList pages = doc.SelectNodes("//page");

            List<Day> days = new List<Day>();
            foreach (XmlNode page in pages)
            {
                string prefix = _l10i.MainPage + "/";
                string pageName = page.Attributes["title"].Value;
                if (pageName.Length < prefix.Length)
                {
                    continue;
                }
                string date = pageName.Substring(prefix.Length);
                Day day = new Day();
                if (!DateTime.TryParse(date, CultureInfo.CreateSpecificCulture(_l10i.Culture),
                        DateTimeStyles.AssumeUniversal, out day.Date))
                {
                    continue;
                }

                string fileName = _cacheDir + date + ".bin";

                string text = "";
                if (File.Exists(fileName))
                {
                    using (FileStream fs = new FileStream(fileName, FileMode.Open))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
                    using (TextReader sr = new StreamReader(gs))
                    {
                        string revid = sr.ReadLine();
                        if (revid == page.Attributes["lastrevid"].Value)
                        {
                            Console.Out.WriteLine("Loading " + pageName + "...");
                            text = sr.ReadToEnd();
                        }
                    }
                }
                if (string.IsNullOrEmpty(text))
                {
                    Console.Out.WriteLine("Downloading " + pageName + "...");
                    text = wiki.LoadText(pageName);
                    using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                    using (StreamWriter sw = new StreamWriter(gs))
                    {
                        sw.WriteLine(page.Attributes["lastrevid"].Value);
                        sw.Write(text);
                    }
                }
                day.Page = WikiPage.Parse(pageName, text);
                days.Add(day);
            }

            days.Sort(CompareDays);
            using (StreamWriter sw =
                        new StreamWriter(_cacheDir + "Main.txt"))
            {
                sw.WriteLine("{{" + _l10i.TopTemplate + "}}\n");

                foreach (Day day in days)
                {
                    sw.Write("{{" + _l10i.Template + "|" + day.Date.ToString("yyyy-M-d") + "|");
                    List<string> titles = new List<string>();

                    foreach (WikiPageSection section in day.Page.Sections)
                    {
                        ReplaceEmptyResults(section);
                        RemoveStrikeOut(section);
                        StrikeOutSection(section);
                        string result = section.Reduce("", SubsectionsList);
                        if (result.Length > 0)
                        {
                            result = " • <small>" + result.Substring(3) + "</small>";
                        }
                        string title;
                        if (_l10i.Processor != null)
                        {
                            title = _l10i.Processor(section).Trim();
                        }
                        else
                        {
                            title = section.Title.Trim();
                        }
                        titles.Add(title + result);
                    }
                    sw.Write(string.Join(" • ", titles.ConvertAll(c => c).ToArray()));
                    sw.Write("}}\n\n");
                }

                sw.WriteLine("{{" + _l10i.BottomTemplate + "}}");
            }

            List<string> dates = new List<string>();
            DateTime today = DateTime.Today;
            DateTime currentMonth = new DateTime(today.Year, today.Month, 1);
            for (int i = 0; i < 2; ++i)
            {
                DateTime start = currentMonth.AddMonths(-i);
                DateTime end = start.AddMonths(1);
                while (start < end)
                {
                    string date = start.ToString("d MMMM yyyy",
                        CultureInfo.CreateSpecificCulture(_l10i.Culture));
                    string prefix = _l10i.MainPage + "/";
                    string pageName = prefix + date;
                    dates.Add(pageName);
                    start = start.AddDays(1);
                }
            }

            parameters.Clear();
            parameters.Add("prop", "info");
            XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, dates);
            days.Clear();
            foreach (string pageName in dates)
            {
                string prefix = _l10i.MainPage + "/";
                string date = pageName.Substring(prefix.Length);
                string fileName = _cacheDir + date + ".bin";
                bool archived = doc.SelectSingleNode("//page[@title=\"" + pageName + "\"]") == null;
                XmlNode page = xml.SelectSingleNode("//page[@title=\"" + pageName + "\"]");

                Day day = new Day();
                day.Archived = archived;
                if (!DateTime.TryParse(date, CultureInfo.CreateSpecificCulture(_l10i.Culture),
                        DateTimeStyles.AssumeUniversal, out day.Date))
                {
                    continue;
                }

                if (page.Attributes["missing"] != null)
                {
                    day.Exists = false;
                    days.Add(day);
                    continue;
                }

                string text = "";
                if (File.Exists(fileName))
                {
                    using (FileStream fs = new FileStream(fileName, FileMode.Open))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
                    using (TextReader sr = new StreamReader(gs))
                    {
                        string revid = sr.ReadLine();
                        if (revid == page.Attributes["lastrevid"].Value)
                        {
                            Console.Out.WriteLine("Loading " + pageName + "...");
                            text = sr.ReadToEnd();
                        }
                    }
                }
                if (string.IsNullOrEmpty(text))
                {
                    Console.Out.WriteLine("Downloading " + pageName + "...");
                    text = wiki.LoadText(pageName);
                    using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                    using (StreamWriter sw = new StreamWriter(gs))
                    {
                        sw.WriteLine(page.Attributes["lastrevid"].Value);
                        sw.Write(text);
                    }
                }
                day.Exists = true;
                day.Page = WikiPage.Parse(pageName, text);
                days.Add(day);
            }

            days.Sort(CompareDays);

            for (int i = 0; i < 2; ++i)
            {
                DateTime start = currentMonth.AddMonths(-i);
                DateTime end = start.AddMonths(1);
                string archiveDate = start.ToString("yyyy-MM");

                IEnumerable<Day> daysInMonth = days.Where(d => d.Date >= start &&
                    d.Date < end);

                using (StreamWriter sw =
                            new StreamWriter(_cacheDir + "Archive-" + archiveDate + ".txt"))
                {
                    sw.WriteLine(_l10i.ArchiveHeader);

                    StringBuilder sb = new StringBuilder();
                    foreach (Day day in daysInMonth)
                    {
                        sb.Append("{{" + _l10i.Template + "|" + day.Date.ToString("yyyy-M-d") + "|");
                        if (!day.Exists)
                        {
                            sb.Append("''" + _l10i.EmptyArchive + "''}}\n\n");
                            continue;
                        }
                        List<string> titles = new List<string>();
                        foreach (WikiPageSection section in day.Page.Sections)
                        {
                            string result = section.Reduce("", SubsectionsList);
                            if (result.Length > 0)
                            {
                                result = " • <small>" + result.Substring(3) + "</small>";
                            }
                            string title;
                            if (_l10i.Processor != null)
                            {
                                title = _l10i.Processor(section).Trim();
                            }
                            else
                            {
                                title = section.Title.Trim();
                            }
                            titles.Add(title + result);
                        }
                        sb.Append(string.Join(" • ", titles.ConvertAll(c => c).ToArray()));
                        sb.Append("}}\n\n");
                    }
                    sb.Replace("<s>", "");
                    sb.Replace("</s>", "");
                    sb.Replace("<strike>", "");
                    sb.Replace("</strike>", "");

                    sw.Write(sb.ToString());

                    sw.WriteLine(_l10i.ArchiveFooter);
                }
            }
        }
Ejemplo n.º 4
0
        public void UpdatePages(Wiki wiki)
        {
            Console.Out.WriteLine("Updating articles for deletion...");
            Regex wikiLinkRE = new Regex(@"\[{2}(.+?)(\|.+?)?]{2}");
            Regex timeRE = new Regex(@"(\d{2}:\d{2}\, \d\d? [а-я]+ \d{4}) \(UTC\)");

            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("generator", "categorymembers");
            parameters.Add("gcmtitle", _l10i.Category);
            parameters.Add("gcmlimit", "max");
            parameters.Add("gcmnamespace", "4");
            parameters.Add("prop", "info|revisions");
            parameters.Add("intoken", "edit");
            XmlDocument doc = wiki.Enumerate(parameters, true);

            string queryTimestamp = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");

            XmlNodeList pages = doc.SelectNodes("//page");
            foreach (XmlNode page in pages)
            {
                string starttimestamp = queryTimestamp;
                int results = 0;
                string prefix = _l10i.MainPage + "/";
                string pageName = page.Attributes["title"].Value;

                string basetimestamp = page.FirstChild.FirstChild.Attributes["timestamp"].Value;
                string editToken = page.Attributes["edittoken"].Value;

                if (pageName.Length < prefix.Length)
                {
                    continue;
                }
                string date = pageName.Substring(prefix.Length);
                Day day = new Day();
                if (!DateTime.TryParse(date, CultureInfo.CreateSpecificCulture(_l10i.Culture),
                        DateTimeStyles.AssumeUniversal, out day.Date))
                {
                    continue;
                }

                string text = "";
                string fileName = _cacheDir + date + ".bin";
                if (File.Exists(fileName))
                {
                    using (FileStream fs = new FileStream(fileName, FileMode.Open))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
                    using (TextReader sr = new StreamReader(gs))
                    {
                        string revid = sr.ReadLine();
                        if (revid == page.Attributes["lastrevid"].Value)
                        {
                            Console.Out.WriteLine("Loading " + pageName + "...");
                            text = sr.ReadToEnd();
                        }
                    }
                }
                if (string.IsNullOrEmpty(text))
                {
                    try
                    {
                        Console.Out.WriteLine("Downloading " + pageName + "...");
                        text = wiki.LoadText(pageName);
                        starttimestamp = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");
                    }
                    catch (WikiPageNotFound)
                    {
                        continue;
                    }
                    using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                    using (StreamWriter sw = new StreamWriter(gs))
                    {
                        sw.WriteLine(page.Attributes["lastrevid"].Value);
                        sw.Write(text);
                    }
                }

                List<string> titlesWithResults = new List<string>();
                Dictionary<string, List<WikiPageSection>> titles = new Dictionary<string, List<WikiPageSection>>();
                day.Page = WikiPage.Parse(pageName, text);
                foreach (WikiPageSection section in day.Page.Sections)
                {
                    ReplaceEmptyResults(section);
                    RemoveStrikeOut(section);
                    StrikeOutSection(section);
                    if (section.Subsections.Count(s => _l10i.Processor != null
                        ? _l10i.Results.Any(r => r == _l10i.Processor(s).Trim())
                        : _l10i.Results.Any(r => r == s.Title.Trim())) == 0 &&
                        section.Subsections.Count(s => s.Title.Trim() == _l10i.ChallengedResult) == 0)
                    {
                        Match m = wikiLinkRE.Match(section.Title);
                        if (m.Success)
                        {
                            string title = m.Groups[1].Value.Trim();

                            if (titles.ContainsKey(title))
                            {
                                titles[title].Add(section);
                            }
                            else
                            {
                                titles.Add(title, new List<WikiPageSection>());
                                titles[title].Add(section);
                            }
                        }
                    }
                    {
                        Match m = wikiLinkRE.Match(section.Title);
                        if (m.Success && section.Title.Contains("<s>"))
                        {
                            titlesWithResults.Add(m.Groups[1].Value.Trim());
                        }
                        List<WikiPageSection> sections = new List<WikiPageSection>();
                        section.Reduce(sections, SubsectionsList);
                        foreach (WikiPageSection subsection in sections)
                        {
                            m = wikiLinkRE.Match(subsection.Title);
                            if (m.Success && subsection.Title.Contains("<s>"))
                            {
                                titlesWithResults.Add(m.Groups[1].Value.Trim());
                            }
                            if (m.Success &&
                                !subsection.Title.Contains("<s>") &&
                                subsection.Subsections.Count(s => s.Title.Trim() == _l10i.ChallengedResult) == 0 &&
                                subsection.Subsections.Count(s => _l10i.Processor != null
                                    ? _l10i.Results.Any(r => r == _l10i.Processor(s).Trim())
                                    : _l10i.Results.Any(r => r == s.Title.Trim())) == 0)
                            {
                                string title = m.Groups[1].Value.Trim();

                                if (titles.ContainsKey(title))
                                {
                                    titles[title].Add(subsection);
                                }
                                else
                                {
                                    titles.Add(title, new List<WikiPageSection>());
                                    titles[title].Add(subsection);
                                }
                            }
                        }
                    }
                }

                parameters.Clear();
                parameters.Add("prop", "info");
                Dictionary<string, string> normalizedTitles = new Dictionary<string, string>();
                XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, titles.Keys);
                foreach (XmlNode node in xml.SelectNodes("//n"))
                {
                    normalizedTitles.Add(node.Attributes["to"].Value,
                        node.Attributes["from"].Value);
                }
                List<string> notificationList = new List<string>();
                XmlNodeList missingTitles = xml.SelectNodes("//page");
                foreach (XmlNode node in missingTitles)
                {
                    string title = node.Attributes["title"].Value;

                    IEnumerable<WikiPageSection> sections;
                    if (titles.ContainsKey(title))
                    {
                        sections = titles[title];
                    }
                    else
                    {
                        sections = titles[normalizedTitles[title]];
                    }
                    if (node.Attributes["missing"] != null)
                    {
                        DateTime start = day.Date;
                        //foreach (WikiPageSection section in sections)
                        //{
                        //    Match m = timeRE.Match(section.Text);
                        //    if (m.Success)
                        //    {
                        //        start = DateTime.Parse(m.Groups[1].Value,
                        //            CultureInfo.CreateSpecificCulture(_l10i.Culture),
                        //            DateTimeStyles.AssumeUniversal);
                        //    }
                        //}
                        parameters.Clear();
                        parameters.Add("list", "logevents");
                        parameters.Add("letype", "delete");
                        parameters.Add("lemlimit", "max");
                        parameters.Add("lestart", start.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"));
                        parameters.Add("ledir", "newer");
                        parameters.Add("letitle", title);
                        parameters.Add("leprop", "comment|type|user|timestamp");
                        XmlDocument log = wiki.Enumerate(parameters, true);
                        XmlNodeList items = log.SelectNodes("//item");
                        List<DeleteLogEvent> events = new List<DeleteLogEvent>();
                        foreach (XmlNode item in items)
                        {
                            DeleteLogEvent ev = new DeleteLogEvent();
                            ev.Comment = item.Attributes["comment"].Value;
                            ev.Deleted = item.Attributes["action"].Value == "delete";
                            ev.User = item.Attributes["user"].Value;
                            ev.Timestamp = DateTime.Parse(item.Attributes["timestamp"].Value,
                                null,
                                DateTimeStyles.AssumeUniversal);
                            events.Add(ev);
                        }
                        events.Sort(CompareDeleteLogEvents);
                        if (events.Count > 0 &&
                            events[0].Deleted &&
                            (DateTime.Now - events[0].Timestamp).TotalHours > 2)
                        {
                            string comment = FilterWikiMarkup(events[0].Comment);
                            string message = string.Format(_l10i.AutoResultMessage,
                                events[0].User,
                                events[0].Timestamp.ToUniversalTime().ToString(_l10i.DateFormat, CultureInfo.CreateSpecificCulture(_l10i.Culture)),
                                comment);
                            var pageSections = titles.ContainsKey(title)
                                ? titles[title]
                                : titles[normalizedTitles[title]];
                            foreach (WikiPageSection section in pageSections)
                            {
                                WikiPageSection verdict = new WikiPageSection(" " + _l10i.AutoResultSection + " ",
                                    section.Level + 1,
                                    message);
                                section.AddSubsection(verdict);
                                StrikeOutSection(section);
                                ++results;
                            }
                        }
                    }
                }

                if (_l10i.Culture != "ru-RU")
                {
                    parameters.Clear();
                    parameters.Add("prop", "info");
                    xml = wiki.Query(QueryBy.Titles, parameters, titlesWithResults);
                    foreach (XmlNode node in xml.SelectNodes("//page"))
                    {
                        if (node.Attributes["missing"] == null &&
                            node.Attributes["redirect"] == null &&
                            node.Attributes["ns"].Value == "0")
                        {
                            notificationList.Add(node.Attributes["title"].Value);
                        }
                    }
                    if (notificationList.Count > 0)
                    {
                        parameters.Clear();
                        parameters.Add("list", "backlinks");
                        parameters.Add("bltitle", pageName);
                        parameters.Add("blfilterredir", "nonredirects");
                        parameters.Add("blnamespace", "1");
                        parameters.Add("bllimit", "max");

                        XmlDocument backlinks = wiki.Enumerate(parameters, true);
                        foreach (string title in notificationList)
                        {
                            string talkPage = wiki.GetNamespace(1) + ":" + title;
                            if (backlinks.SelectSingleNode("//bl[@title=" + GenerateConcatForXPath(talkPage) + "]") == null)
                            {
                                PutNotification(wiki, title, date);
                            }
                        }
                    }
                }

                string newText = day.Page.Text;
                if (newText.Trim() == text.Trim())
                {
                    continue;
                }
                try
                {
                    Console.Out.WriteLine("Updating " + pageName + "...");
                    string revid = wiki.Save(pageName,
                        "",
                        newText,
                        _l10i.StrikeOutComment + (results > 0 ? _l10i.AutoResultComment : ""),
                        MinorFlags.Minor,
                        CreateFlags.NoCreate,
                        WatchFlags.None,
                        SaveFlags.Replace,
                        true,
                        basetimestamp,
                        "",
                        editToken);

                    using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                    using (StreamWriter sw = new StreamWriter(gs))
                    {
                        sw.WriteLine(revid);
                        sw.Write(newText);
                    }
                }
                catch (WikiException)
                {
                }
            }
        }
Ejemplo n.º 5
0
        public void UpdateArchivePages(Wiki wiki)
        {
            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("generator", "categorymembers");
            parameters.Add("gcmtitle", "Категория:Википедия:Незакрытые обсуждения категорий");
            parameters.Add("gcmlimit", "max");
            parameters.Add("gcmnamespace", "4");
            parameters.Add("prop", "info");

            XmlDocument doc = wiki.Enumerate(parameters, true);
            XmlNodeList pages = doc.SelectNodes("//page");

            DateTime minDate = DateTime.Now;
            foreach (XmlNode page in pages)
            {
                string pageName = page.Attributes["title"].Value;
                string date = pageName.Substring("Википедия:Обсуждение категорий/".Length);
                DateTime day;
                if (DateTime.TryParse(date,
                        CultureInfo.CreateSpecificCulture("ru-RU"),
                        DateTimeStyles.AssumeUniversal, out day))
                {
                    if (day < minDate)
                    {
                        minDate = day;
                    }
                }
                else
                {
                    continue;
                }
            }

            List<string> titles = new List<string>();
            minDate = new DateTime(minDate.Year, minDate.Month, 1);
            DateTime currentMonth = new DateTime(DateTime.Today.Year,
                DateTime.Today.Month, 1);
            DateTime start = new DateTime(minDate.Year, 1, 1);
            while (start <= currentMonth)
            {
                string date = start.ToString("yyyy");
                string pageName = "Википедия:Обсуждение категорий/Архив/" + date;
                titles.Add(pageName);
                start = start.AddYears(1);
            }

            parameters.Clear();
            parameters.Add("prop", "info");

            XmlDocument archivesDoc = wiki.Query(QueryBy.Titles, parameters, titles);
            pages = archivesDoc.SelectNodes("//page");
            foreach (XmlNode archivePage in pages)
            {
                string archiveName = archivePage.Attributes["title"].Value;
                string date = archiveName.Substring("Википедия:Обсуждение категорий/Архив/".Length);
                int year;
                if (!int.TryParse(date, out year))
                {
                    continue;
                }
                DateTime archiveDate = new DateTime(year, 1, 1);
                string fileName = _cacheDir + "Archive-" + date + ".txt";

                start = archiveDate;
                DateTime end = start.AddYears(1);
                titles.Clear();
                while (start < end)
                {
                    string pageDate = start.ToString("MMMM yyyy",
                        CultureInfo.CreateSpecificCulture("ru-RU"));
                    string prefix = "Википедия:Обсуждение категорий/";
                    string pageName = prefix + pageDate;
                    titles.Add(pageName);

                    start = start.AddMonths(1);
                }

                parameters.Clear();
                parameters.Add("prop", "info");

                List<Day> days = new List<Day>();
                XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, titles);
                XmlNodeList archives = xml.SelectNodes("//page");
                foreach (XmlNode page in archives)
                {
                    string pageName = page.Attributes["title"].Value;
                    string dateString = pageName.Substring("Википедия:Обсуждение категорий/".Length);

                    string pageFileName = _cacheDir + dateString + ".bin";
                    Day day = new Day();

                    if (!DateTime.TryParse(dateString,
                        CultureInfo.CreateSpecificCulture("ru-RU"),
                        DateTimeStyles.AssumeUniversal, out day.Date))
                    {
                        continue;
                    }

                    if (page.Attributes["missing"] != null)
                    {
                        continue;
                    }

                    string text = LoadPageFromCache(pageFileName,
                        page.Attributes["lastrevid"].Value, pageName);

                    if (string.IsNullOrEmpty(text))
                    {
                        Console.Out.WriteLine("Downloading " + pageName + "...");
                        text = wiki.LoadText(pageName);
                        CachePage(pageFileName, page.Attributes["lastrevid"].Value, text);
                    }
                    day.Page = WikiPage.Parse(pageName, text);
                    days.Add(day);
                }

                days.Sort(CompareDays);

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("{{Википедия:Обсуждение категорий/Обсуждаемые категории}}\n");

                foreach (Day day in days)
                {
                    sb.Append("{{Википедия:Обсуждение категорий/Месяц|" + day.Date.ToString("yyyy-M") + "|\n");

                    List<string> sectionTitles = new List<string>();
                    foreach (WikiPageSection section in day.Page.Sections)
                    {
                        string filler = "";
                        for (int i = 0; i < section.Level - 1; ++i)
                        {
                            filler += "*";
                        }
                        sectionTitles.Add(filler + " " + section.Title.Trim());

                        List<WikiPageSection> sections = new List<WikiPageSection>();
                        section.Reduce(sections, SubsectionsList);
                        foreach (WikiPageSection subsection in sections)
                        {
                            filler = "";
                            for (int i = 0; i < subsection.Level - 1; ++i)
                            {
                                filler += "*";
                            }
                            sectionTitles.Add(filler + " " + subsection.Title.Trim());
                        }
                    }
                    if (sectionTitles.Count(s => s.Contains("=")) > 0)
                    {
                        sectionTitles[0] = "2=<div>" + sectionTitles[0].Substring(2);
                    }
                    sb.Append(string.Join("\n", sectionTitles.ConvertAll(c => c).ToArray()));
                    if (sectionTitles.Count(s => s.Contains("=")) > 0)
                    {
                        sb.Append("</div>");
                    }
                    sb.Append("}}\n\n");
                }

                sb.Append("|}");
                sb.Replace("<s>", "");
                sb.Replace("</s>", "");
                sb.Replace("<strike>", "");
                sb.Replace("</strike>", "");

                if (File.Exists(fileName))
                {
                    using (TextReader sr = new StreamReader(fileName))
                    {
                        string txt = sr.ReadToEnd();
                        if (txt == sb.ToString())
                        {
                            continue;
                        }
                    }
                }

                Console.Out.WriteLine("Updating " + archiveName + "...");
                wiki.Save(archiveName,
                    sb.ToString(),
                    "обновление");
                using (StreamWriter sw =
                        new StreamWriter(fileName))
                {
                    sw.Write(sb.ToString());
                }
            }
        }
Ejemplo n.º 6
0
        public void UpdateArchivePages(Wiki wiki)
        {
            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("generator", "categorymembers");
            parameters.Add("gcmtitle", "Категория:Википедия:Незакрытые обсуждения объединения страниц");
            parameters.Add("gcmlimit", "max");
            parameters.Add("gcmnamespace", "4");
            parameters.Add("prop", "info");

            XmlDocument doc = wiki.Enumerate(parameters, true);
            XmlNodeList pages = doc.SelectNodes("//page");

            DateTime minDate = DateTime.Now;
            foreach (XmlNode page in pages)
            {
                string pageName = page.Attributes["title"].Value;
                string date = pageName.Substring("Википедия:К объединению/".Length);
                try
                {
                    DateTime day = DateTime.Parse(date,
                        CultureInfo.CreateSpecificCulture("ru-RU"),
                        DateTimeStyles.AssumeUniversal);
                    if (day < minDate)
                    {
                        minDate = day;
                    }
                }
                catch (FormatException)
                {
                    continue;
                }
            }

            List<string> titles = new List<string>();
            minDate = new DateTime(minDate.Year, minDate.Month, 1);
            DateTime currentMonth = new DateTime(DateTime.Today.Year,
                DateTime.Today.Month, 1);
            DateTime start = minDate;
            while (start <= currentMonth)
            {
                string date = start.ToString("yyyy-MM");
                string pageName = "Википедия:Архив запросов на объединение/" + date;
                titles.Add(pageName);
                start = start.AddMonths(1);
            }

            parameters.Clear();
            parameters.Add("prop", "info");

            XmlDocument archivesDoc = wiki.Query(QueryBy.Titles, parameters, titles);
            pages = archivesDoc.SelectNodes("//page");
            foreach (XmlNode archivePage in pages)
            {
                string archiveName = archivePage.Attributes["title"].Value;
                string date = archiveName.Substring("Википедия:Архив запросов на объединение/".Length);
                DateTime archiveDate;
                try
                {
                    archiveDate = DateTime.Parse(date,
                        CultureInfo.CreateSpecificCulture("ru-RU"),
                        DateTimeStyles.AssumeUniversal);
                }
                catch (FormatException)
                {
                    continue;
                }
                string fileName = _cacheDir + "Archive-" + date + ".txt";
                start = archiveDate;
                DateTime end = start.AddMonths(1);
                titles.Clear();
                while (start < end)
                {
                    string pageDate = start.ToString("d MMMM yyyy",
                        CultureInfo.CreateSpecificCulture("ru-RU"));
                    string prefix = "Википедия:К объединению/";
                    string pageName = prefix + pageDate;
                    titles.Add(pageName);

                    start = start.AddDays(1);
                }

                parameters.Clear();
                parameters.Add("prop", "info");

                List<Day> days = new List<Day>();
                XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, titles);
                XmlNodeList archives = xml.SelectNodes("//page");
                foreach (XmlNode page in archives)
                {
                    string pageName = page.Attributes["title"].Value;
                    string dateString = pageName.Substring("Википедия:К объединению/".Length);

                    string pageFileName = _cacheDir + dateString + ".bin";
                    Day day = new Day();
                    day.Archived = doc.SelectSingleNode("//page[@title=\"" + pageName + "\"]") == null;

                    try
                    {
                        day.Date = DateTime.Parse(dateString,
                            CultureInfo.CreateSpecificCulture("ru-RU"),
                            DateTimeStyles.AssumeUniversal);
                    }
                    catch (FormatException)
                    {
                        continue;
                    }

                    if (page.Attributes["missing"] != null)
                    {
                        day.Exists = false;
                        days.Add(day);
                        continue;
                    }

                    string text = LoadPageFromCache(pageFileName,
                        page.Attributes["lastrevid"].Value, pageName);

                    if (string.IsNullOrEmpty(text))
                    {
                        Console.Out.WriteLine("Downloading " + pageName + "...");
                        text = wiki.LoadText(pageName);
                        CachePage(pageFileName, page.Attributes["lastrevid"].Value, text);
                    }

                    day.Exists = true;
                    day.Page = WikiPage.Parse(pageName, text);
                    days.Add(day);
                }

                days.Sort(CompareDays);

                StringBuilder textBuilder = new StringBuilder();

                textBuilder.AppendLine("{{Навигация по архиву КОБ}}\n{{Объединение статей/Начало}}");

                StringBuilder sb = new StringBuilder();
                foreach (Day day in days)
                {
                    sb.Append("{{Объединение статей/День|" + day.Date.ToString("yyyy-M-d") + "|\n");
                    if (!day.Archived && day.Exists)
                    {
                        sb.Append("''обсуждение не завершено''}}\n\n");
                        continue;
                    }
                    if (!day.Exists)
                    {
                        sb.Append("''нет обсуждений''}}\n\n");
                        continue;
                    }
                    List<string> sectionTitles = new List<string>();
                    foreach (WikiPageSection section in day.Page.Sections)
                    {
                        string filler = "";
                        for (int i = 0; i < section.Level - 1; ++i)
                        {
                            filler += "*";
                        }
                        sectionTitles.Add(filler + " " + section.Title.Trim());

                        List<WikiPageSection> sections = new List<WikiPageSection>();
                        section.Reduce(sections, SubsectionsList);
                        foreach (WikiPageSection subsection in sections)
                        {
                            filler = "";
                            for (int i = 0; i < subsection.Level - 1; ++i)
                            {
                                filler += "*";
                            }
                            sectionTitles.Add(filler + " " + subsection.Title.Trim());
                        }
                    }
                    if (sectionTitles.Count(s => s.Contains("=")) > 0)
                    {
                        titles[0] = "2=<li>" + titles[0].Substring(2) + "</li>";
                    }
                    sb.Append(string.Join("\n", sectionTitles.ConvertAll(c => c).ToArray()));
                    sb.Append("}}\n\n");
                }
                sb.Replace("<s>", "");
                sb.Replace("</s>", "");
                sb.Replace("<strike>", "");
                sb.Replace("</strike>", "");

                textBuilder.Append(sb.ToString());

                textBuilder.AppendLine("{{Объединение статей/Конец}}");

                if (File.Exists(fileName))
                {
                    using (TextReader sr = new StreamReader(fileName))
                    {
                        string text = sr.ReadToEnd();
                        if (text == textBuilder.ToString())
                        {
                            continue;
                        }
                    }
                }

                Console.Out.WriteLine("Updating " + archiveName + "...");
                wiki.Save(archiveName,
                    textBuilder.ToString(),
                    "обновление");
                using (StreamWriter sw =
                        new StreamWriter(fileName))
                {
                    sw.Write(textBuilder.ToString());
                }
            }
        }
Ejemplo n.º 7
0
        public void UpdatePages(Wiki wiki)
        {
            Regex closedRE = new Regex(@"(\{{2}ВПКПМ-Навигация\}{2}\s*\{{2}(Закрыто|Closed|закрыто|closed)\}{2})|(\{{2}(Закрыто|Closed|закрыто|closed)\}{2}\s*\{{2}ВПКПМ-Навигация\}{2})");
            Regex wikiLinkRE = new Regex(@"\[{2}(.+?)(\|.+?)?]{2}");
            Regex timeRE = new Regex(@"(\d{2}:\d{2}\, \d\d? [а-я]+ \d{4}) \(UTC\)");

            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("generator", "categorymembers");
            parameters.Add("gcmtitle", "Категория:Википедия:Незакрытые обсуждения переименования страниц");
            parameters.Add("gcmlimit", "max");
            parameters.Add("gcmnamespace", "4");
            parameters.Add("prop", "info|revisions");
            parameters.Add("intoken", "edit");
            XmlDocument doc = wiki.Enumerate(parameters, true);
            string queryTimestamp = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");
            XmlNodeList pages = doc.SelectNodes("//page");
            foreach (XmlNode page in pages)
            {
                int results = 0;
                string pageName = page.Attributes["title"].Value;
                string date = pageName.Substring("Википедия:К переименованию/".Length);
                string basetimestamp = page.FirstChild.FirstChild.Attributes["timestamp"].Value;
                string editToken = page.Attributes["edittoken"].Value;

                Day day = new Day();
                if (!DateTime.TryParse(date, CultureInfo.CreateSpecificCulture("ru-RU"),
                        DateTimeStyles.AssumeUniversal, out day.Date))
                {
                    continue;
                }

                string fileName = _cacheDir + date + ".bin";
                string text = "";
                if (File.Exists(fileName))
                {
                    using (FileStream fs = new FileStream(fileName, FileMode.Open))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
                    using (TextReader sr = new StreamReader(gs))
                    {
                        string revid = sr.ReadLine();
                        if (revid == page.Attributes["lastrevid"].Value)
                        {
                            Console.Out.WriteLine("Loading " + pageName + "...");
                            text = sr.ReadToEnd();
                        }
                    }
                }
                if (string.IsNullOrEmpty(text))
                {
                    Console.Out.WriteLine("Downloading " + pageName + "...");
                    text = wiki.LoadText(pageName);
                    using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                    using (StreamWriter sw = new StreamWriter(gs))
                    {
                        sw.WriteLine(page.Attributes["lastrevid"].Value);
                        sw.Write(text);
                    }
                }
                day.Page = WikiPage.Parse(pageName, text);

                List<string> titlesWithResults = new List<string>();
                foreach (WikiPageSection section in day.Page.Sections)
                {
                    RemoveStrikeOut(section);
                    StrikeOutSection(section);

                    Match m = wikiLinkRE.Match(section.Title);
                    if (m.Success && !m.Groups[1].Value.StartsWith(":Категория:"))
                    {
                        string link = m.Groups[1].Value;
                        string movedTo;
                        string movedBy;
                        DateTime movedAt;

                        DateTime start = day.Date;
                        m = timeRE.Match(section.SectionText);
                        if (m.Success)
                        {
                            start = DateTime.Parse(m.Groups[1].Value,
                                CultureInfo.CreateSpecificCulture("ru-RU"),
                                DateTimeStyles.AssumeUniversal);
                        }

                        bool moved = MovedTo(wiki,
                            link,
                            start,
                            out movedTo,
                            out movedBy,
                            out movedAt);

                        WikiPageSection autoVerdictSection = section.Subsections.FirstOrDefault(s => s.Title.Trim() == "Автоматический итог");
                        if (autoVerdictSection != null && !moved)
                        {
                            autoVerdictSection.Title = " Оспоренный итог ";
                            RemoveStrikeOut(section);
                            StrikeOutSection(section);
                        }

                        if (section.Subsections.Count(s => s.Title.Trim() == "Оспоренный итог") == 0 &&
                            section.Subsections.Count(s => s.Title.ToLower().Trim() == "итог") == 0 &&
                            section.Subsections.Count(s => s.Title.Trim() == "Автоматический итог") == 0 &&
                            moved && !string.IsNullOrEmpty(movedTo))
                        {
                            string message = string.Format("Страница была переименована {2} в «[[{0}]]» участником [[User:{1}|]]. Данное сообщение было автоматически сгенерировано ботом ~~~~.\n",
                                    movedTo,
                                    movedBy,
                                    movedAt.ToUniversalTime().ToString("d MMMM yyyy в HH:mm (UTC)"));
                            WikiPageSection verdict = new WikiPageSection(" Автоматический итог ",
                                section.Level + 1,
                                message);
                            section.AddSubsection(verdict);
                            StrikeOutSection(section);
                            ++results;
                        }
                    }

                    m = wikiLinkRE.Match(section.Title);
                    if (m.Success && section.Title.Contains("<s>"))
                    {
                        titlesWithResults.Add(m.Groups[1].Value);
                    }
                    List<WikiPageSection> sections = new List<WikiPageSection>();
                    section.Reduce(sections, SubsectionsList);
                    foreach (WikiPageSection subsection in sections)
                    {
                        m = wikiLinkRE.Match(subsection.Title);
                        if (m.Success && subsection.Title.Contains("<s>"))
                        {
                            titlesWithResults.Add(m.Groups[1].Value.Trim());
                        }
                        if (m.Success &&
                            !subsection.Title.Contains("<s>") &&
                            !m.Groups[1].Value.StartsWith(":Категория:"))
                        {
                            string link = m.Groups[1].Value;
                            string movedTo;
                            string movedBy;
                            DateTime movedAt;

                            DateTime start = day.Date;
                            m = timeRE.Match(subsection.SectionText);
                            if (m.Success)
                            {
                                start = DateTime.Parse(m.Groups[1].Value,
                                    CultureInfo.CreateSpecificCulture("ru-RU"),
                                    DateTimeStyles.AssumeUniversal);
                            }
                            bool moved = MovedTo(wiki,
                                link,
                                start,
                                out movedTo,
                                out movedBy,
                                out movedAt);

                            WikiPageSection verdictSection = subsection.Subsections.FirstOrDefault(s => s.Title.Trim() == "Автоматический итог");
                            if (verdictSection != null && !moved)
                            {
                                verdictSection.Title = " Оспоренный итог ";
                                RemoveStrikeOut(subsection);
                                StrikeOutSection(subsection);
                            }

                            if (subsection.Subsections.Count(s => s.Title.Trim() == "Оспоренный итог") == 0 &&
                                subsection.Subsections.Count(s => s.Title.ToLower().Trim() == "итог") == 0 &&
                                subsection.Subsections.Count(s => s.Title.Trim() == "Автоматический итог") == 0 &&
                                moved && !string.IsNullOrEmpty(movedTo))
                            {
                                string message = string.Format("Страница была переименована {2} в «[[{0}]]» участником [[User:{1}|]]. Данное сообщение было автоматически сгенерировано ботом ~~~~.\n",
                                        movedTo,
                                        movedBy,
                                        movedAt.ToUniversalTime().ToString("d MMMM yyyy в HH:mm (UTC)"));
                                WikiPageSection verdict = new WikiPageSection(" Автоматический итог ",
                                    subsection.Level + 1,
                                    message);
                                subsection.AddSubsection(verdict);
                                StrikeOutSection(subsection);
                                ++results;
                            }
                        }
                    }
                }

                Match matchClosed = closedRE.Match(text);
                if (matchClosed.Success)
                {
                    List<TalkResult> talkResults = new List<TalkResult>();
                    foreach (string name in titlesWithResults)
                    {
                        if (wiki.PageNamespace(name) > 0)
                        {
                            continue;
                        }
                        string movedTo;
                        string movedBy;
                        DateTime movedAt;

                        bool moved = MovedTo(wiki,
                                       name,
                                       day.Date,
                                       out movedTo,
                                       out movedBy,
                                       out movedAt);
                        talkResults.Add(new TalkResult(name, movedTo, moved));
                    }

                    parameters.Clear();
                    parameters.Add("prop", "info");
                    XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, talkResults.ConvertAll(r => r.Moved ? r.MovedTo : r.Title));
                    List<string> notificationList = new List<string>();
                    foreach (XmlNode node in xml.SelectNodes("//page"))
                    {
                        if (node.Attributes["missing"] == null && node.Attributes["ns"].Value == "0")
                        {
                            notificationList.Add(node.Attributes["title"].Value);
                        }
                    }
                    if (notificationList.Count > 0)
                    {
                        parameters.Clear();
                        parameters.Add("list", "backlinks");
                        parameters.Add("bltitle", pageName);
                        parameters.Add("blfilterredir", "nonredirects");
                        parameters.Add("blnamespace", "1");
                        parameters.Add("bllimit", "max");

                        XmlDocument backlinks = wiki.Enumerate(parameters, true);
                        foreach (string title in notificationList)
                        {
                            if (backlinks.SelectSingleNode("//bl[@title=\"Обсуждение:" + title + "\"]") == null)
                            {
                                TalkResult tr = talkResults.Find(r => r.Moved ? r.MovedTo == title : r.Title == title);
                                PutNotification(wiki, tr, day.Date);
                            }
                        }
                    }
                }

                string newText = day.Page.Text;
                if (newText.Trim() == text.Trim())
                {
                    continue;
                }
                try
                {
                    Console.Out.WriteLine("Updating " + pageName + "...");
                    string revid = wiki.Save(pageName,
                        "",
                        newText,
                        "зачёркивание заголовков" + (results > 0 ? ", сообщение об итогах" : ""),
                        MinorFlags.Minor,
                        CreateFlags.NoCreate,
                        WatchFlags.None,
                        SaveFlags.Replace,
                        true,
                        basetimestamp,
                        "",
                        editToken);

                    using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                    using (StreamWriter sw = new StreamWriter(gs))
                    {
                        sw.WriteLine(revid);
                        sw.Write(newText);
                    }
                }
                catch (WikiException)
                {
                }
            }
        }
Ejemplo n.º 8
0
        public static PageInfo LoadPageInformation(Wiki wiki, string language, string title)
        {
            if (!File.Exists(string.Format(@"Cache\{0}\pages.txt", language)))
            {
                FileStream stream = File.Create(string.Format(@"Cache\{0}\pages.txt", language));
                stream.Close();
            }

            using (TextReader streamReader = new StreamReader(string.Format(@"Cache\{0}\pages.txt", language)))
            {
                string line;
                while ((line = streamReader.ReadLine()) != null)
                {
                    string[] groups = line.Split(new char[] { '\t' });
                    if (groups[0] == title || (groups.Length > 3 && groups[3] == title))
                    {
                        DateTime time = DateTime.Parse(groups[2],
                                    null,
                                    DateTimeStyles.AssumeUniversal);
                        return new PageInfo(groups[0], groups[1], time, 0);
                    }
                }
            }

            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("prop", "revisions");
            parameters.Add("rvprop", "timestamp|user");
            parameters.Add("rvdir", "newer");
            parameters.Add("rvlimit", "1");
            parameters.Add("redirects");

            XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, new string[] { title });
            XmlNode node = xml.SelectSingleNode("//rev");
            if (node != null)
            {
                string pageName = xml.SelectSingleNode("//page").Attributes["title"].Value;
                string user = node.Attributes["user"].Value;
                string timestamp = node.Attributes["timestamp"].Value;
                DateTime time = DateTime.Parse(timestamp,
                                    null,
                                    DateTimeStyles.AssumeUniversal);
                using (TextWriter streamWriter = new StreamWriter(string.Format(@"Cache\{0}\pages.txt", language), true))
                {
                    streamWriter.WriteLine("{0}\t{1}\t{2}\t{3}",
                        pageName,
                        user,
                        timestamp,
                        pageName != title ? title : "");
                }
                return new PageInfo(pageName, user, time, 0);
            }
            return null;
        }
Ejemplo n.º 9
0
        internal void UpdatePages(Wiki wiki)
        {
            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("generator", "categorymembers");
            parameters.Add("gcmtitle", "Категория:Википедия:Незакрытые обсуждения восстановления страниц");
            parameters.Add("gcmlimit", "max");
            parameters.Add("gcmnamespace", "4");
            parameters.Add("prop", "info|revisions");
            parameters.Add("intoken", "edit");
            XmlDocument doc = wiki.Enumerate(parameters, true);

            string queryTimestamp = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");

            XmlNodeList pages = doc.SelectNodes("//page");
            foreach (XmlNode page in pages)
            {
                string starttimestamp = queryTimestamp;
                int results = 0;
                string prefix = "Википедия:К восстановлению/";
                string pageName = page.Attributes["title"].Value;

                string basetimestamp = page.FirstChild.FirstChild.Attributes["timestamp"].Value;
                string editToken = page.Attributes["edittoken"].Value;

                string date = pageName.Substring(prefix.Length);
                Day day = new Day();
                if (!DateTime.TryParse(date, CultureInfo.CreateSpecificCulture("ru-RU"),
                        DateTimeStyles.AssumeUniversal, out day.Date))
                {
                    continue;
                }

                string text = "";
                string fileName = _cacheDir + date + ".bin";
                if (File.Exists(fileName))
                {
                    using (FileStream fs = new FileStream(fileName, FileMode.Open))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
                    using (TextReader sr = new StreamReader(gs))
                    {
                        string revid = sr.ReadLine();
                        if (revid == page.Attributes["lastrevid"].Value)
                        {
                            Console.Out.WriteLine("Loading " + pageName + "...");
                            text = sr.ReadToEnd();
                        }
                    }
                }
                if (string.IsNullOrEmpty(text))
                {
                    try
                    {
                        Console.Out.WriteLine("Downloading " + pageName + "...");
                        text = wiki.LoadText(pageName);
                        starttimestamp = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");
                    }
                    catch (WikiPageNotFound)
                    {
                        continue;
                    }
                    using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                    using (StreamWriter sw = new StreamWriter(gs))
                    {
                        sw.WriteLine(page.Attributes["lastrevid"].Value);
                        sw.Write(text);
                    }
                }

                Dictionary<string, List<WikiPageSection>> titles = new Dictionary<string, List<WikiPageSection>>();
                day.Page = WikiPage.Parse(pageName, text);
                foreach (WikiPageSection section in day.Page.Sections)
                {
                    RemoveStrikeOut(section);
                    StrikeOutSection(section);
                    if (section.Subsections.Count(s => _results.Any(r => r.ToLower() == s.Title.Trim().ToLower())) == 0)
                    {
                        Match m = _wikiLinkRE.Match(section.Title);
                        if (m.Success)
                        {
                            string title = m.Groups[1].Value.Trim();

                            if (titles.ContainsKey(title))
                            {
                                titles[title].Add(section);
                            }
                            else
                            {
                                titles.Add(title, new List<WikiPageSection>());
                                titles[title].Add(section);
                            }
                        }
                    }
                    {
                        List<WikiPageSection> sections = new List<WikiPageSection>();
                        section.Reduce(sections, SubsectionsList);
                        foreach (WikiPageSection subsection in sections)
                        {
                            Match m = _wikiLinkRE.Match(subsection.Title);
                            if (m.Success &&
                                !subsection.Title.Contains("<s>") &&
                                subsection.Subsections.Count(s => _results.Any(r => r.ToLower() == s.Title.Trim().ToLower())) == 0)
                            {
                                string title = m.Groups[1].Value.Trim();

                                if (titles.ContainsKey(title))
                                {
                                    titles[title].Add(subsection);
                                }
                                else
                                {
                                    titles.Add(title, new List<WikiPageSection>());
                                    titles[title].Add(subsection);
                                }
                            }
                        }
                    }
                }

                parameters.Clear();
                parameters.Add("prop", "info");
                Dictionary<string, string> normalizedTitles = new Dictionary<string, string>();
                XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, titles.Keys);
                foreach (XmlNode node in xml.SelectNodes("//n"))
                {
                    normalizedTitles.Add(node.Attributes["to"].Value,
                        node.Attributes["from"].Value);
                }
                List<string> notificationList = new List<string>();
                XmlNodeList missingTitles = xml.SelectNodes("//page");
                foreach (XmlNode node in missingTitles)
                {
                    string title = node.Attributes["title"].Value;

                    IEnumerable<WikiPageSection> sections;
                    if (titles.ContainsKey(title))
                    {
                        sections = titles[title];
                    }
                    else
                    {
                        sections = titles[normalizedTitles[title]];
                    }
                    if (node.Attributes["missing"] == null)
                    {
                        DateTime start = day.Date;
                        parameters.Clear();
                        parameters.Add("list", "logevents");
                        parameters.Add("letype", "delete");
                        parameters.Add("lemlimit", "max");
                        parameters.Add("lestart", start.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"));
                        parameters.Add("ledir", "newer");
                        parameters.Add("letitle", title);
                        parameters.Add("leprop", "comment|type|user|timestamp");
                        XmlDocument log = wiki.Enumerate(parameters, true);
                        XmlNodeList items = log.SelectNodes("//item");
                        List<DeleteLogEvent> events = new List<DeleteLogEvent>();
                        foreach (XmlNode item in items)
                        {
                            DeleteLogEvent ev = new DeleteLogEvent();
                            ev.Comment = item.Attributes["comment"].Value;
                            ev.Restored = item.Attributes["action"].Value == "restore";
                            ev.Deleted = item.Attributes["action"].Value == "delete";
                            ev.User = item.Attributes["user"].Value;
                            ev.Timestamp = DateTime.Parse(item.Attributes["timestamp"].Value,
                                null,
                                DateTimeStyles.AssumeUniversal);
                            events.Add(ev);
                        }
                        events.Sort(CompareDeleteLogEvents);
                        if (events.Count > 0 &&
                            events[0].Restored &&
                            (DateTime.Now - events[0].Timestamp).TotalHours > 2)
                        {
                            Regex commentRE = new Regex(@" восстановлено: (.+)");
                            Match m = commentRE.Match(events[0].Comment);
                            string comment;
                            if (m.Success)
                            {
                                comment = m.Groups[1].Value;
                            }
                            else
                            {
                                comment = "<nowiki>" + events[0].Comment + "</nowiki>";
                            }
                            string message = string.Format("Страница была восстановлена {1} администратором [[User:{0}|]]. Была указана следующая причина: «{2}». Данное сообщение было автоматически сгенерировано ботом ~~~~.\n",
                                events[0].User,
                                events[0].Timestamp.ToUniversalTime().ToString("d MMMM yyyy в HH:mm (UTC)"),
                                comment);
                            if (!titles.ContainsKey(title))
                            {
                                continue;
                            }
                            foreach (WikiPageSection section in titles[title])
                            {
                                WikiPageSection verdict = new WikiPageSection(" Автоматический итог ",
                                    section.Level + 1,
                                    message);
                                section.AddSubsection(verdict);
                                StrikeOutSection(section);
                                ++results;
                            }
                        }
                        else if (events.Count > 0 &&
                            events[0].Deleted)
                        {
                            parameters.Clear();
                            parameters.Add("prop", "revisions");
                            parameters.Add("rvprop", "timestamp|user");
                            parameters.Add("rvdir", "newer");
                            parameters.Add("rvlimit", "1");
                            parameters.Add("redirects");

                            log = wiki.Query(QueryBy.Titles, parameters, new string[] { title });
                            XmlNode revision = log.SelectSingleNode("//rev");
                            if (revision != null)
                            {
                                string user = revision.Attributes["user"].Value;
                                string timestamp = revision.Attributes["timestamp"].Value;
                                DateTime time = DateTime.Parse(timestamp,
                                    null,
                                    DateTimeStyles.AssumeUniversal);

                                string message = string.Format("Страница была создана заново {1} участником [[User:{0}|]]. Данное сообщение было автоматически сгенерировано ботом ~~~~.\n",
                                    user,
                                    time.ToUniversalTime().ToString("d MMMM yyyy в HH:mm (UTC)"));

                                if (!titles.ContainsKey(title))
                                {
                                    continue;
                                }
                                foreach (WikiPageSection section in titles[title])
                                {
                                    WikiPageSection verdict = new WikiPageSection(" Автоматический итог ",
                                        section.Level + 1,
                                        message);
                                    section.AddSubsection(verdict);
                                    StrikeOutSection(section);
                                    ++results;
                                }
                            }
                        }
                    }
                }

                string newText = day.Page.Text;
                if (newText.Trim() == text.Trim())
                {
                    continue;
                }
                try
                {
                    Console.Out.WriteLine("Updating " + pageName + "...");
                    string revid = wiki.Save(pageName,
                        "",
                        newText,
                        "зачёркивание заголовков" + (results > 0 ? " и подведение итогов" : ""),
                        MinorFlags.Minor,
                        CreateFlags.NoCreate,
                        WatchFlags.None,
                        SaveFlags.Replace,
                        true,
                        basetimestamp,
                        "",
                        editToken);

                    using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                    using (StreamWriter sw = new StreamWriter(gs))
                    {
                        sw.WriteLine(revid);
                        sw.Write(newText);
                    }
                }
                catch (WikiException)
                {
                }
            }
        }
Ejemplo n.º 10
0
        public void Analyze(Wiki wiki)
        {
            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("generator", "categorymembers");
            parameters.Add("gcmtitle", "Категория:Википедия:Незакрытые обсуждения восстановления страниц");
            parameters.Add("gcmlimit", "max");
            parameters.Add("gcmnamespace", "4");
            parameters.Add("prop", "info|revisions");
            parameters.Add("rvprop", "timestamp");

            XmlDocument doc = wiki.Enumerate(parameters, true);
            XmlNodeList pages = doc.SelectNodes("//page");

            List<Day> days = new List<Day>();
            DateTime start = DateTime.Today;
            Regex closedRE = new Regex(@"({{ВПВУС-Навигация}}\s*{{(Закрыто|Closed|закрыто|closed)}})|({{(Закрыто|Closed|закрыто|closed)}}\s*{{ВПВУС-Навигация}})");

            foreach (XmlNode page in pages)
            {
                string pageName = page.Attributes["title"].Value;
                string date = pageName.Substring("Википедия:К восстановлению/".Length);
                DateTime cutOffDate = new DateTime(2008, 11, 13);
                Day day = new Day();
                if (!DateTime.TryParse(date,
                        CultureInfo.CreateSpecificCulture("ru-RU"),
                        DateTimeStyles.AssumeUniversal, out day.Date))
                {
                    continue;
                }

                string fileName = _cacheDir + date + ".bin";
                string text = "";
                if (File.Exists(fileName))
                {
                    using (FileStream fs = new FileStream(fileName, FileMode.Open))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
                    using (TextReader sr = new StreamReader(gs))
                    {
                        string revid = sr.ReadLine();
                        if (revid == page.Attributes["lastrevid"].Value)
                        {
                            Console.Out.WriteLine("Loading " + pageName + "...");
                            text = sr.ReadToEnd();
                        }
                    }
                }
                if (string.IsNullOrEmpty(text))
                {
                    Console.Out.WriteLine("Downloading " + pageName + "...");
                    text = wiki.LoadText(pageName);

                    using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                    using (StreamWriter sw = new StreamWriter(gs))
                    {
                        sw.WriteLine(page.Attributes["lastrevid"].Value);
                        sw.Write(text);
                    }
                }
                DateTime lastEdit = DateTime.Parse(page.FirstChild.FirstChild.Attributes["timestamp"].Value, null, DateTimeStyles.AssumeUniversal);
                Match m = closedRE.Match(text);
                if ((DateTime.Now - lastEdit).TotalDays > 2 && (m.Success || day.Date < cutOffDate))
                {
                    Console.Out.WriteLine("Closing " + pageName + "...");
                    text = text.Replace("{{ВПВУС-Навигация}}", "{{ВПВУС-Навигация|nocat=1}}");
                    wiki.Save(pageName,
                        text,
                        "обсуждение закрыто");
                    continue;
                }
                day.Page = WikiPage.Parse(pageName, text);
                days.Add(day);
            }

            days.Sort(CompareDays);

            Regex wikiLinkRE = new Regex(@"\[{2}(.+?)(\|.+?)?]{2}");
            List<string> sectionTitles = new List<string>();
            foreach (Day day in days)
            {
                foreach (WikiPageSection section in day.Page.Sections)
                {
                    RemoveStrikeOut(section);
                    StrikeOutSection(section);
                    if (section.Title.Contains("<s>"))
                    {
                        Match m = wikiLinkRE.Match(section.Title);
                        if (m.Success)
                        {
                            sectionTitles.Add(m.Groups[1].Value);
                        }
                    }

                    List<WikiPageSection> sections = new List<WikiPageSection>();
                    section.Reduce(sections, SubsectionsList);
                    foreach (WikiPageSection subsection in sections)
                    {
                        if (subsection.Title.Contains("<s>"))
                        {
                            Match m = wikiLinkRE.Match(subsection.Title);
                            if (m.Success)
                            {
                                sectionTitles.Add(m.Groups[1].Value);
                            }
                        }
                    }
                }
            }

            parameters.Clear();
            parameters.Add("prop", "info");
            XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, sectionTitles);

            using (StreamWriter sw =
                        new StreamWriter(_cacheDir + "MainPage.txt"))
            {
                sw.WriteLine("== Текущие обсуждения ==\n");
                sw.WriteLine("{{Восстановление статей/Статьи, вынесенные на обсуждение восстановления}}\n");

                foreach (Day day in days)
                {
                    sw.Write("{{Восстановление статей/День|" + day.Date.ToString("yyyy-M-d") + "|\n");

                    List<string> titles = new List<string>();
                    foreach (WikiPageSection section in day.Page.Sections)
                    {
                        string result = "";
                        string filler = "";
                        RemoveStrikeOut(section);
                        StrikeOutSection(section);

                        if (section.Title.Contains("<s>"))
                        {
                            Match m = wikiLinkRE.Match(section.Title);
                            if (m.Success)
                            {
                                string link = m.Groups[1].Value;
                                XmlNode node = xml.SelectSingleNode("//page[@title='" + link.Replace("'", @"&apos;") + "']");
                                if (node != null)
                                {
                                    if (node.Attributes["missing"] == null)
                                    {
                                        WikiPageSection autoresult = section.Subsections.FirstOrDefault(s => s.Title.Trim() == "Автоматический итог");
                                        if (autoresult != null &&
                                            autoresult.SectionText.Contains("Страница была создана заново"))
                                        {
                                            result = " ''(создана заново)''";
                                        }
                                        else
                                        {
                                            result = " ''(восстановлена)''";
                                        }
                                    }
                                    else
                                    {
                                        result = " ''(не восстановлена)''";
                                    }
                                }
                            }
                        }

                        for (int i = 0; i < section.Level - 1; ++i)
                        {
                            filler += "*";
                        }
                        titles.Add(filler + " " + section.Title.Trim() + result);

                        List<WikiPageSection> sections = new List<WikiPageSection>();
                        section.Reduce(sections, SubsectionsList);
                        foreach (WikiPageSection subsection in sections)
                        {
                            result = "";
                            if (subsection.Title.Contains("<s>"))
                            {
                                Match m = wikiLinkRE.Match(subsection.Title);
                                if (m.Success)
                                {
                                    string link = m.Groups[1].Value;
                                    XmlNode node = xml.SelectSingleNode("//page[@title='" + link + "']");
                                    if (node != null)
                                    {
                                        if (node.Attributes["missing"] == null)
                                        {
                                            WikiPageSection autoresult = subsection.Subsections.FirstOrDefault(s => s.Title.Trim() == "Автоматический итог");
                                            if (autoresult != null &&
                                                autoresult.SectionText.Contains("Страница была создана заново"))
                                            {
                                                result = " ''(создана заново)''";
                                            }
                                            else
                                            {
                                                result = " ''(восстановлена)''";
                                            }
                                        }
                                        else
                                        {
                                            result = " ''(не восстановлена)''";
                                        }
                                    }
                                }
                            }
                            filler = "";
                            for (int i = 0; i < subsection.Level - 1; ++i)
                            {
                                filler += "*";
                            }
                            titles.Add(filler + " " + subsection.Title.Trim());
                        }
                    }
                    if (titles.Count(s => s.Contains("=")) > 0)
                    {
                        titles[0] = "2=<div>" + titles[0].Substring(2);
                    }
                    sw.Write(string.Join("\n", titles.ConvertAll(c => c).ToArray()));
                    if (titles.Count(s => s.Contains("=")) > 0)
                    {
                        sw.Write("</div>");
                    }
                    sw.Write("}}\n\n");
                }

                sw.WriteLine("{{/Подвал}}");
            }
        }
Ejemplo n.º 11
0
        static int Main(string[] args)
        {
            Wiki wiki = new Wiki("http://ru.wikipedia.org/w/");
            wiki.SleepBetweenQueries = 2;
            if (string.IsNullOrEmpty(Settings.Default.Login) ||
                string.IsNullOrEmpty(Settings.Default.Password))
            {
                Console.Out.WriteLine("Please add login and password to the configuration file.");
                return 0;
            }

            Console.Out.WriteLine("Logging in as " + Settings.Default.Login + "...");
            try
            {
                WikiCache.Login(wiki, Settings.Default.Login, Settings.Default.Password);
            }
            catch (WikiException e)
            {
                Console.Out.WriteLine(e.Message);
                return 0;
            }
            Console.Out.WriteLine("Logged in as " + Settings.Default.Login + ".");

            string listText;
            try
            {
                listText = wiki.LoadText("Шаблон:Список подводящих итоги");
            }
            catch (WikiException e)
            {
                Console.Out.WriteLine(e.Message);
                return -1;
            }

            StringReader reader = new StringReader(listText);
            HashSet<string> users = new HashSet<string>();
            Regex userRE = new Regex(@"^\*\s*\[\[(User|Участник):(.+)\|.+\]\]\s*$", RegexOptions.IgnoreCase);
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                Match m = userRE.Match(line);
                if (m.Success)
                {
                    users.Add(m.Groups[2].Value.ToLower());
                }
            }

            ParameterCollection parameters = new ParameterCollection
            {
                { "generator", "embeddedin" },
                { "geititle", "Template:Db-discussion" },
                { "geilimit", "max"},
                { "prop", "info" },
                { "intoken", "delete" },
                { "inprop", "talkid" }
            };

            Regex templateRE = new Regex(@"\{\{db-discussion\|(\d+)\|(дата|причина)\s*=\s*(.+?)\}\}", RegexOptions.IgnoreCase);
            XmlDocument doc;
            try
            {
                doc = wiki.Enumerate(parameters, true);
            }
            catch (WikiException e)
            {
                Console.Out.WriteLine(e.Message);
                return -1;
            }

            bool failed = false;
            foreach (XmlNode page in doc.SelectNodes("//page"))
            {
                string title = page.Attributes["title"].Value;
                parameters = new ParameterCollection
                {
                    { "prop", "revisions" },
                    { "rvprop", "content" },
                    { "rvlimit", "1" }
                };

                XmlDocument xml;
                try
                {
                    xml = wiki.Query(QueryBy.Titles, parameters, title);
                }
                catch (WikiException e)
                {
                    Console.Out.WriteLine(e.Message);
                    failed = true;
                    continue;
                }
                XmlNode node = xml.SelectSingleNode("//rev");
                if (node == null || node.FirstChild == null)
                {
                    failed = true;
                    continue;
                }
                string content = node.FirstChild.Value;

                Match m = templateRE.Match(content);
                if (m.Success)
                {
                    string timestamp = m.Groups[1].Value;

                    parameters = new ParameterCollection
                    {
                        { "prop", "revisions" },
                        { "rvprop", "timestamp|user|content" },
                        { "rvlimit", "1" },
                        { "rvstart", timestamp },
                        { "rvdir", "newer" },
                    };

                    try
                    {
                        xml = wiki.Query(QueryBy.Titles, parameters, new string[] { title }, 500, false);
                    }
                    catch (WikiException e)
                    {
                        Console.Out.WriteLine(e.Message);
                        failed = true;
                        continue;
                    }
                    node = xml.SelectSingleNode("//rev");
                    if (node != null && users.Contains(node.Attributes["user"].Value.ToLower()))
                    {
                        content = node.FirstChild.Value;
                        m = templateRE.Match(content);
                        if (m.Success && m.Groups[1].Value == timestamp)
                        {
                            string comment;
                            if (m.Groups[2].Value.ToLower() == "причина")
                            {
                                comment = m.Groups[3].Value;
                            }
                            else
                            {
                                DateTime talkDate;
                                if (DateTime.TryParse(m.Groups[3].Value, null, System.Globalization.DateTimeStyles.AssumeUniversal,
                                    out talkDate))
                                {
                                    comment = string.Format("согласно итогу обсуждения [[ВП:К удалению/{0}#{1}]]",
                                        talkDate.ToUniversalTime().ToString("d MMMM yyyy"),
                                        title);
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            string reason = string.Format("удалил [[User:{0}|{0}]]: {1}",
                                node.Attributes["user"].Value,
                                comment);

                            string token = page.Attributes["deletetoken"].Value;
                            try
                            {
                                wiki.Delete(title, reason, token);
                            }
                            catch (WikiException e)
                            {
                                Console.Out.WriteLine(e.Message);
                                failed = true;
                                continue;
                            }

                            parameters = new ParameterCollection
                            {
                                { "list", "backlinks" },
                                { "blfilterredir", "redirects" },
                                { "bllimit", "max" },
                                { "bltitle", title },
                            };

                            try
                            {
                                xml = wiki.Enumerate(parameters, true);
                            }
                            catch (WikiException e)
                            {
                                Console.Out.WriteLine(e.Message);
                                failed = true;
                                continue;
                            }

                            foreach (XmlNode backlink in xml.SelectNodes("//bl"))
                            {
                                try
                                {
                                    wiki.Delete(backlink.Attributes["title"].Value,
                                       "[[ВП:КБУ#П1|П1]]: перенаправление в никуда",
                                      wiki.Token);
                                }
                                 catch (WikiException e)
                                 {
                                     Console.Out.WriteLine(e.Message);
                                     failed = true;
                                     continue;
                                 }
                            }
                        }
                    }
                }
            }
            return failed ? -1 : 0;
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            Wiki wiki = new Wiki("http://ru.wikipedia.org/w/");
            if (string.IsNullOrEmpty(Settings.Default.Login) ||
                string.IsNullOrEmpty(Settings.Default.Password))
            {
                Console.Out.WriteLine("Please add login and password to the configuration file.");
                return;
            }
            Console.Out.WriteLine("Logging in as " + Settings.Default.Login + "...");
            try
            {
                wiki.Login(Settings.Default.Login, Settings.Default.Password);
            }
            catch (WikiException e)
            {
                Console.Out.WriteLine(e.Message);
                return;
            }
            Console.Out.WriteLine("Logged in as " + Settings.Default.Login + ".");
            DateTime time = new DateTime(2009, 1, 1);
            Dictionary<string, DateTime> pages = new Dictionary<string, DateTime>();
            pages.Add("Википедия:Сообщения об ошибках", new DateTime());
            while (time <= DateTime.Now)
            {
                string name = time.ToString("MMMM yyyy");
                pages.Add("Википедия:Сообщения об ошибках/Архив/" + name, new DateTime());
                time = time.AddMonths(1);
            }
            string timeString = DateTime.Now.ToUniversalTime().ToString("HH:mm, d MMMM yyyy (UTC)");
            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("prop", "info");
            XmlDocument doc;
            try
            {
                doc = wiki.Query(QueryBy.Titles, parameters, pages.Keys);
            }
            catch (WikiException e)
            {
                Console.Out.WriteLine(e.Message);
                return;
            }
            XmlNodeList nodes = doc.SelectNodes("//page");
            foreach (XmlNode node in nodes)
            {
                if (node.Attributes["missing"] != null)
                {
                    continue;
                }
                string title = node.Attributes["title"].Value;
                string timestamp = node.Attributes["touched"].Value;
                pages[title] = DateTime.Parse(timestamp, null, DateTimeStyles.AssumeUniversal);
            }
            bool changed = false;
            List<Message> messages = new List<Message>();
            Console.Out.WriteLine("Downloading statistic data...");
            foreach (string s in pages.Keys)
            {
                string timestamp = pages[s].ToString("yyyyMMddHHmmss");
                string filename = s.Replace(':', '-').Replace('/', '-') + ".bin";
                string text = null;
                if (File.Exists(filename))
                {
                    using (FileStream fs = new FileStream(filename, FileMode.Open))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
                    using (TextReader sr = new StreamReader(gs))
                    {
                        string ts = sr.ReadLine();
                        if (ts == timestamp)
                        {
                            text = sr.ReadToEnd();
                        }
                    }
                }
                if (text == null)
                {
                    using (FileStream fs = new FileStream(filename, FileMode.Create))
                    using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                    using (StreamWriter sw = new StreamWriter(gs))
                    {
                        try
                        {
                            text = wiki.LoadText(s);
                        }
                        catch (WikiException e)
                        {
                            Console.Out.WriteLine(e.Message);
                            return;
                        }
                        sw.WriteLine(timestamp);
                        sw.Write(text);
                        changed = true;
                    }
                }
                messages.AddRange(GetMessagesFromPage(text, s.Contains("Архив")));
            }
            Console.Out.WriteLine("Data downloaded.");
            Console.Out.WriteLine("Processing data...");
            int newCount = messages.Count(m => m.Opened &&
                ((m.Archived && m.Closed) || !m.Archived));
            int closedCount = messages.Count(m => m.Opened && m.Closed);
            using (StreamWriter sw =
                        new StreamWriter("output.txt"))
            {
                sw.Write("На этой странице приводится статистика обработки [[ВП:СО|сообщений об ошибках]]");
                sw.Write(" по состоянию на " + timeString + ".");
                sw.Write(" Всего было подано " + Messages(messages.Count));
                sw.Write(", из них не учтено " + Messages(messages.Count - newCount));
                sw.Write(", учтено " + Messages(newCount));
                sw.Write(" (" + closedCount.ToString() + " распознаны как закрытые).");
                sw.Write("{{.ref|<ref>На [[ВП:СО]] учитываются сообщения, которые включают в себя строчку «Автор сообщения» с датой добавления. На страницах архивов учитываются сообщения, которые содержат строчку «Автор сообщения» или «Сообщил» и больше одной даты, при этом новейшая из них считается датой закрытия.</ref>}}\n\n");

                PrintWeeks(sw, messages);
                PrintMonths(sw, messages);
                PrintYears(sw, messages);
                PrintPeriods(sw, messages);
                sw.WriteLine("== Примечания ==");
                sw.WriteLine("{{примечания}}");
            }
            Console.Out.WriteLine("Results are ready.");
            if (changed)
            {
                Console.Out.WriteLine("Updating the wiki page...");
                using (TextReader sr =
                            new StreamReader("output.txt"))
                {
                    string text = sr.ReadToEnd();
                    try
                    {
                        wiki.Save("User:LEMeZza/Статистика ВП:СО",
                            text,
                            "обновление статистики обработки [[ВП:СО|сообщений об ошибках]]");
                    }
                    catch (WikiException e)
                    {
                        Console.Out.WriteLine(e.Message);
                        return;
                    }
                }
            }
            else
            {
                Console.Out.WriteLine("No changes, skipping the update.");
            }
            wiki.Logout();
            Console.Out.WriteLine("Done.");
        }
Ejemplo n.º 13
0
        internal void AddNavigationTemplate(Wiki wiki)
        {
            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("list", "embeddedin");
            parameters.Add("eititle", "Template:" + _l10i.NavigationTemplate);
            parameters.Add("eilimit", "max");
            parameters.Add("einamespace", "4");
            parameters.Add("eifilterredir", "nonredirects");

            XmlDocument doc = wiki.Enumerate(parameters, true);

            List<string> titles = new List<string>();
            DateTime end = DateTime.Today;
            DateTime start = end.AddDays(-7);
            while (start <= end)
            {
                string pageDate = start.ToString("d MMMM yyyy", _l10i.Culture);
                string prefix = _l10i.MainPage + "/";
                string pageName = prefix + pageDate;
                if (doc.SelectSingleNode("//ei[@title='" + pageName + "']") == null)
                {
                    titles.Add(pageName);
                }
                start = start.AddDays(1);
            }

            parameters.Clear();
            parameters.Add("prop", "info");
            XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, titles);
            foreach (XmlNode node in xml.SelectNodes("//page"))
            {
                if (node.Attributes["missing"] == null)
                {
                    Console.Out.WriteLine("Updating " + node.Attributes["title"].Value + "...");
                    wiki.Prepend(node.Attributes["title"].Value,
                        "{{" + _l10i.NavigationTemplate + "}}\n",
                        _l10i.MainPageUpdateComment);
                }
            }
        }
Ejemplo n.º 14
0
        public void UpdateArchivePages(Wiki wiki)
        {
            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("generator", "categorymembers");
            parameters.Add("gcmtitle", _l10i.Category);
            parameters.Add("gcmlimit", "max");
            parameters.Add("gcmnamespace", "4");
            parameters.Add("prop", "info");

            XmlDocument doc = wiki.Enumerate(parameters, true);
            XmlNodeList pages = doc.SelectNodes("//page");

            DateTime minDate = DateTime.Now;
            foreach (XmlNode page in pages)
            {
                string prefix = _l10i.MainPage + "/";
                string pageName = page.Attributes["title"].Value;
                string date = pageName.Substring(prefix.Length);
                DateTime day;
                if (DateTime.TryParse(date,
                        _l10i.Culture,
                        DateTimeStyles.AssumeUniversal, out day))
                {
                    if (day < minDate)
                    {
                        minDate = day;
                    }
                }
                else
                {
                    continue;
                }
            }

            List<string> titles = new List<string>();
            minDate = new DateTime(minDate.Year, minDate.Month, 1);
            DateTime currentMonth = new DateTime(DateTime.Today.Year,
                DateTime.Today.Month, 1);
            DateTime start = minDate;
            while (start <= currentMonth)
            {
                string date = start.ToString("yyyy-MM");
                string pageName = _l10i.ArchivePage + date;
                titles.Add(pageName);
                start = start.AddMonths(1);
            }

            parameters.Clear();
            parameters.Add("prop", "info");

            XmlDocument archivesDoc = wiki.Query(QueryBy.Titles, parameters, titles);
            pages = archivesDoc.SelectNodes("//page");
            foreach (XmlNode archivePage in pages)
            {
                string archiveName = archivePage.Attributes["title"].Value;
                string date = archiveName.Substring(_l10i.ArchivePage.Length);
                DateTime archiveDate;
                if (!DateTime.TryParse(date,
                        _l10i.Culture,
                        DateTimeStyles.AssumeUniversal, out archiveDate))
                {
                    continue;
                }

                string fileName = _cacheDir + "Archive-" + date + ".txt";
                start = archiveDate;
                DateTime end = start.AddMonths(1);
                titles.Clear();
                while (start < end)
                {
                    string pageDate = start.ToString("d MMMM yyyy",
                        _l10i.Culture);
                    string prefix = _l10i.MainPage + "/";
                    string pageName = prefix + pageDate;
                    titles.Add(pageName);

                    start = start.AddDays(1);
                }

                parameters.Clear();
                parameters.Add("prop", "info");

                List<Day> days = new List<Day>();
                XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, titles);
                XmlNodeList archives = xml.SelectNodes("//page");
                foreach (XmlNode page in archives)
                {
                    string prefix = _l10i.MainPage + "/";
                    string pageName = page.Attributes["title"].Value;
                    string dateString = pageName.Substring(prefix.Length);

                    string pageFileName = _cacheDir + dateString + ".bin";
                    Day day = new Day();
                    day.Archived = doc.SelectSingleNode("//page[@title=\"" + pageName + "\"]") == null;

                    if (!DateTime.TryParse(dateString,
                        _l10i.Culture,
                        DateTimeStyles.AssumeUniversal, out day.Date))
                    {
                        continue;
                    }

                    if (page.Attributes["missing"] != null)
                    {
                        day.Exists = false;
                        days.Add(day);
                        continue;
                    }

                    string text = LoadPageFromCache(pageFileName,
                        page.Attributes["lastrevid"].Value, pageName);

                    if (string.IsNullOrEmpty(text))
                    {
                        Console.Out.WriteLine("Downloading " + pageName + "...");
                        text = wiki.LoadText(pageName);
                        CachePage(pageFileName, page.Attributes["lastrevid"].Value, text);
                    }

                    day.Exists = true;
                    day.Page = WikiPage.Parse(pageName, text);
                    days.Add(day);
                }

                days.Sort(CompareDays);

                StringBuilder textBuilder = new StringBuilder();
                textBuilder.AppendLine(_l10i.ArchiveHeader);

                StringBuilder sb = new StringBuilder();
                foreach (Day day in days)
                {
                    sb.Append("{{" + _l10i.Template + "|" + day.Date.ToString("yyyy-M-d") + "|\n");
                    if (!day.Exists)
                    {
                        sb.Append("''" + _l10i.EmptyArchive + "''}}\n\n");
                        continue;
                    }

                    List<string> sectionTitles = new List<string>();
                    foreach (WikiPageSection section in day.Page.Sections)
                    {
                        string result = section.Reduce("", SubsectionsList);
                        if (result.Length > 0)
                        {
                            result = " • <small>" + result.Substring(3) + "</small>";
                        }
                        string title;
                        if (_l10i.Processor != null)
                        {
                            title = _l10i.Processor(section).Trim();
                        }
                        else
                        {
                            title = section.Title.Trim();
                        }
                        sectionTitles.Add(title + result);
                    }
                    sb.Append(string.Join(" • ", sectionTitles.ConvertAll(c => c).ToArray()));
                    sb.Append("}}\n\n");
                }
                sb.Replace("<s>", "");
                sb.Replace("</s>", "");
                sb.Replace("<strike>", "");
                sb.Replace("</strike>", "");

                textBuilder.Append(sb.ToString());
                textBuilder.AppendLine(_l10i.ArchiveFooter);

                if (File.Exists(fileName))
                {
                    using (TextReader sr = new StreamReader(fileName))
                    {
                        string text = sr.ReadToEnd();
                        if (text == textBuilder.ToString())
                        {
                            continue;
                        }
                    }
                }

                Console.Out.WriteLine("Updating " + archiveName + "...");
                wiki.Save(archiveName,
                    textBuilder.ToString(),
                    _l10i.MainPageUpdateComment);
                using (StreamWriter sw =
                        new StreamWriter(fileName))
                {
                    sw.Write(textBuilder.ToString());
                }
            }
        }
Ejemplo n.º 15
0
        public override Dictionary<string, string> Process(Wiki wiki, WikiPage page, ref int diffSize, ref int topicsArchived)
        {
            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 = DateTime.Today;
                    DateTime lastReply = DateTime.MinValue;
                    foreach (Match match in ms)
                    {
                        string value = match.Groups[1].Value;
                        DateTime time = DateTime.Parse(value, L10i.Culture,
                            DateTimeStyles.AssumeUniversal);
                        if (time < published)
                        {
                            published = time;
                        }
                        if (time > lastReply)
                        {
                            lastReply = time;
                        }
                    }
                    if (lastReply != DateTime.MinValue &&
                        ((forceArchivation && (DateTime.Today - lastReply).TotalHours >= ForcedArchivationDelay) ||
                        (DateTime.Today - lastReply).TotalHours >= Delay))
                    {
                        archivedSections.Add(section);
                    }
                }
                if (IsMovedSection(section))
                {
                    section.SectionText = section.SectionText.Trim(new char[] { ' ', '\t', '\n' }) + "\n~~~~\n";
                    archivedSections.Add(section);
                }
            }
            Dictionary<string, string> archiveTexts = new Dictionary<string, string>();

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

            var parameters = new ParameterCollection()
            {
                { "prop", "info" },
            };
            XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, MainPage);
            int ns = int.Parse(xml.SelectSingleNode("//page").Attributes["ns"].Value);
            string prefix = wiki.GetNamespace(ns) + ":";
            parameters = new ParameterCollection()
            {
                { "list", "allpages" },
                { "apprefix", MainPage.Substring(prefix.Length) },
                { "apnamespace", ns.ToString() }
            };
            xml = wiki.Enumerate(parameters, true);

            int maxNumber = 1;
            foreach (XmlNode p in xml.SelectNodes("//p"))
            {
                string title = p.Attributes["title"].Value;
                string format = Format.Replace("{0}", "");
                if (title.StartsWith(format))
                {
                    int number;
                    if (int.TryParse(title.Substring(format.Length), out number))
                    {
                        if (number > maxNumber)
                        {
                            maxNumber = number;
                        }
                    }
                }
            }

            int index = 0;
            string pageName = string.Format(Format, maxNumber);
            parameters.Clear();
            parameters.Add("prop", "info");
            xml = wiki.Query(QueryBy.Titles, parameters, new string[] { pageName });
            XmlNode node = xml.SelectSingleNode("//page");
            if (node.Attributes["missing"] == null)
            {
                string pageFileName = _cacheDir + Cache.GenerateCachePath(pageName);
                string text = Cache.LoadPageFromCache(pageFileName,
                                node.Attributes["lastrevid"].Value, pageName);

                if (string.IsNullOrEmpty(text))
                {
                    Console.Out.WriteLine("Downloading " + pageName + "...");
                    text = wiki.LoadText(pageName);
                    Cache.CachePage(pageName, _cacheDir, node.Attributes["lastrevid"].Value, text);
                }
                WikiPage archivePage = WikiPage.Parse(pageName, text);
                if (archivePage.Sections.Count < Topics)
                {
                    int topics = Topics - archivePage.Sections.Count;
                    for (int i = 0; i < topics && index < archivedSections.Count; ++i, ++index)
                    {
                        WikiPageSection section = archivedSections[index];
                        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, "");
                    }
                    archiveTexts.Add(pageName, archivePage.Text);
                }
            }
            if (index < archivedSections.Count)
            {
                string text = Header;
                pageName = string.Format(Format, maxNumber + 1);
                WikiPage archivePage = WikiPage.Parse(pageName, text);
                for (; index < archivedSections.Count; ++index)
                {
                    WikiPageSection section = archivedSections[index];
                    section.Title = ProcessSectionTitle(section.Title);
                    archivePage.Sections.Add(section);
                }
                archivePage.Sections.Sort(SectionsUp);
                if (!string.IsNullOrEmpty(RemoveFromText))
                {
                    archivePage.Text = archivePage.Text.Replace(RemoveFromText, "");
                }
                archiveTexts.Add(pageName, archivePage.Text);
            }

            topicsArchived = 0;
            diffSize = 0;
            foreach (var section in archivedSections)
            {
                diffSize += Encoding.UTF8.GetByteCount(section.Text);
                ++topicsArchived;
                page.Sections.Remove(section);
            }
            return archiveTexts;
        }
Ejemplo n.º 16
0
        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;
        }
Ejemplo n.º 17
0
        static int Main(string[] args)
        {
            Wiki wiki = new Wiki("http://ru.wikipedia.org/w/");
            wiki.SleepBetweenQueries = 2;
            if (string.IsNullOrEmpty(Settings.Default.Login) ||
                string.IsNullOrEmpty(Settings.Default.Password))
            {
                Console.Out.WriteLine("Please add login and password to the configuration file.");
                return 0;
            }

            Console.Out.WriteLine("Logging in as " + Settings.Default.Login + "...");
            try
            {
                WikiCache.Login(wiki, Settings.Default.Login, Settings.Default.Password);
            }
            catch (WikiException e)
            {
                Console.Out.WriteLine(e.Message);
                return 0;
            }
            Console.Out.WriteLine("Logged in as " + wiki.User + ".");

            ParameterCollection parameters = new ParameterCollection
            {
                { "list", "embeddedin" },
                { "eititle", "Шаблон:Редактирую" },
                { "eilimit", "max"},
                { "einamespace", "0" },
            };

            XmlDocument doc;
            try
            {
                doc = wiki.Enumerate(parameters, true);
            }
            catch (WikiException e)
            {
                Console.Out.WriteLine(e.Message);
                return -1;
            }

            Regex templateRE = new Regex(@"\{\{(Редактирую|Перерабатываю|Inuse-by|Пишу|Inuse|Правлю|Перевожу|In-use|Processing|Process|Редактирую раздел)\|1=(.+?)\|2=.+?}}\s?",
                RegexOptions.IgnoreCase);

            Regex templateRE2 = new Regex(@"\{\{(Редактирую|Перерабатываю|Inuse-by|Пишу|Inuse|Правлю|Перевожу|In-use|Processing|Process|Редактирую раздел)\|(.*?\[\[.+?]].*?)(\|.+?)?}}\s?",
                RegexOptions.IgnoreCase);

            Regex templateRE3 = new Regex(@"\{\{(Редактирую|Перерабатываю|Inuse-by|Пишу|Inuse|Правлю|Перевожу|In-use|Processing|Process|Редактирую раздел)}}\s?",
                RegexOptions.IgnoreCase);

            Regex userRE = new Regex(@"\[\[(User:|Участник:|special:contributions/|Служебная:contributions/)(.+?)(\|.+?)*?\]\]",
                RegexOptions.IgnoreCase);

            bool failed = false;
            foreach (XmlNode page in doc.SelectNodes("//ei"))
            {
                string title = page.Attributes["title"].Value;
                parameters = new ParameterCollection
                {
                    { "prop", "revisions" },
                    { "rvprop", "timestamp|content" },
                    { "rvlimit", "1" }
                };

                XmlDocument xml;
                try
                {
                    xml = wiki.Query(QueryBy.Titles, parameters, new string[] { title }, 500, false);
                }
                catch (WikiException e)
                {
                    Console.Out.WriteLine(e.Message);
                    failed = true;
                    continue;
                }
                XmlNode node = xml.SelectSingleNode("//rev");
                if (node == null || node.FirstChild == null)
                {
                    failed = true;
                    continue;
                }

                string content = node.FirstChild.Value;
                DateTime timestamp = DateTime.Parse(node.Attributes["timestamp"].Value,
                    null,
                    System.Globalization.DateTimeStyles.AssumeUniversal);
                if ((DateTime.Now - timestamp).TotalDays >= 3)
                {
                    Match m = templateRE.Match(content);
                    string text = content;
                    string user = "";
                    if (m.Success)
                    {
                        text = templateRE.Replace(content, "");
                        user = m.Groups[2].Value;
                    }
                    else
                    {
                        m = templateRE2.Match(content);
                        if (m.Success)
                        {
                            text = templateRE2.Replace(content, "");
                            user = m.Groups[2].Value;
                        }
                        else
                        {
                            m = templateRE3.Match(content);
                            if (m.Success)
                            {
                                text = templateRE3.Replace(content, "");
                            }
                        }
                    }
                    if (text == content)
                    {
                        continue;
                    }

                    try
                    {
                        Console.WriteLine("Updating " + title + "...");
                        wiki.Save(title,
                            text,
                            "автоматическое удаление просроченного шаблона [[Шаблон:Редактирую|редактирую]]");
                    }
                    catch (WikiException e)
                    {
                        Console.Out.WriteLine(e.Message);
                        failed = true;
                        continue;
                    }

                    Match um = userRE.Match(user);
                    if (um.Success)
                    {
                        string notification = "\n== Уведомление ==\nЗдравствуйте! Пожалуйста, обратите внимание, что в статье [[" +
                            title + "]] был автоматически удалён установленный вами шаблон {{tl|Редактирую}}. ~~~~";
                        string userTalk = "Обсуждение участника:" + um.Groups[2].Value;
                        try
                        {
                            Console.WriteLine("Creating a topic on " + userTalk + "...");
                            wiki.Append(userTalk,
                                notification,
                                "/* Уведомление */ новая тема: автоматическое удаление просроченного шаблона",
                                MinorFlags.NotMinor,
                                false);
                        }
                        catch (WikiException e)
                        {
                            Console.Out.WriteLine(e.Message);
                            failed = true;
                        }
                    }
                }
            }

            return failed ? -1 : 0;
        }
Ejemplo n.º 18
0
        public void UpdateArchivePages(Wiki wiki, int year, int monthNumber)
        {
            Regex wikiLinkRE = new Regex(@"\[{2}(.+?)(\|.+?)?]{2}");

            DateTime month = new DateTime(year, monthNumber, 1);
            DateTime end = month.AddMonths(1);
            using (StreamWriter archiveSW =
                        new StreamWriter(_cacheDir + "Archive-" +
                            year.ToString() + "-" + monthNumber.ToString() + ".txt"))
                while (month < end && month < DateTime.Now)
                {
                    DateTime start = month;
                    DateTime nextMonth = start.AddMonths(1);
                    List<string> titles = new List<string>();
                    while (start < nextMonth)
                    {
                        string pageDate = start.ToString("d MMMM yyyy",
                            CultureInfo.CreateSpecificCulture("ru-RU"));
                        string prefix = "Википедия:К переименованию/";
                        string pageName = prefix + pageDate;
                        titles.Add(pageName);

                        start = start.AddDays(1);
                    }

                    ParameterCollection parameters = new ParameterCollection();
                    parameters.Add("prop", "info");

                    List<Day> days = new List<Day>();
                    XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, titles);
                    XmlNodeList archives = xml.SelectNodes("//page");
                    foreach (XmlNode page in archives)
                    {
                        string pageName = page.Attributes["title"].Value;
                        string dateString = pageName.Substring("Википедия:К переименованию/".Length);

                        string pageFileName = _cacheDir + dateString + ".bin";
                        Day day = new Day();

                        try
                        {
                            day.Date = DateTime.Parse(dateString,
                                CultureInfo.CreateSpecificCulture("ru-RU"),
                                DateTimeStyles.AssumeUniversal);
                        }
                        catch (FormatException)
                        {
                            continue;
                        }

                        if (page.Attributes["missing"] != null)
                        {
                            day.Exists = false;
                            days.Add(day);
                            continue;
                        }

                        string text = "";
                        if (File.Exists(pageFileName))
                        {
                            using (FileStream fs = new FileStream(pageFileName, FileMode.Open))
                            using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
                            using (TextReader sr = new StreamReader(gs))
                            {
                                string revid = sr.ReadLine();
                                if (revid == page.Attributes["lastrevid"].Value)
                                {
                                    Console.Out.WriteLine("Loading " + pageName + "...");
                                    text = sr.ReadToEnd();
                                }
                            }
                        }
                        if (string.IsNullOrEmpty(text))
                        {
                            Console.Out.WriteLine("Downloading " + pageName + "...");
                            text = wiki.LoadText(pageName);
                            using (FileStream fs = new FileStream(pageFileName, FileMode.Create))
                            using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                            using (StreamWriter sw = new StreamWriter(gs))
                            {
                                sw.WriteLine(page.Attributes["lastrevid"].Value);
                                sw.Write(text);
                            }
                        }
                        day.Exists = true;
                        day.Page = WikiPage.Parse(pageName, text);
                        days.Add(day);
                    }

                    days.Sort(CompareDays);

                    StringBuilder textBuilder = new StringBuilder();
                    textBuilder.AppendLine("{{Навигация по архиву КПМ}}\n{{Переименование статей/Начало}}");

                    StringBuilder sb = new StringBuilder();
                    foreach (Day day in days)
                    {

                        sb.Append("{{Переименование статей/День|" + day.Date.ToString("yyyy-M-d") + "|\n");
                        if (!day.Exists)
                        {
                            sb.Append("''нет обсуждений''}}\n\n");
                            continue;
                        }
                        titles.Clear();
                        Console.Out.WriteLine("Analyzing " + day.Date.ToString("d MMMM yyyy") + "...");
                        foreach (WikiPageSection section in day.Page.Sections)
                        {
                            string filler = "";
                            string result = "";
                            bool hasVerdict = section.Subsections.Count(s => s.Title.ToLower().Trim() == "итог") > 0;
                            bool hasAutoVerdict = section.Subsections.Count(s => s.Title.Trim() == "Автоматический итог") > 0;
                            if (hasVerdict || hasAutoVerdict || section.Title.Contains("<s>"))
                            {
                                Match m = wikiLinkRE.Match(section.Title);
                                if (m.Success && !m.Groups[1].Value.StartsWith(":Категория:"))
                                {
                                    string link = m.Groups[1].Value;
                                    string movedTo;
                                    bool moved = MovedTo(wiki, link, day.Date, out movedTo);

                                    if (moved && string.IsNullOrEmpty(movedTo))
                                    {
                                        result = " ''(переименовано)''";
                                    }
                                    else if (moved)
                                    {
                                        result = string.Format(" ''({1}переименовано в «[[{0}]]»)''",
                                                movedTo.StartsWith("Файл:") ? ":" + movedTo : movedTo, hasAutoVerdict && !hasVerdict ? "де-факто " : "");
                                    }
                                    else
                                    {
                                        result = " ''(не переименовано)''";
                                    }
                                }
                            }

                            for (int i = 0; i < section.Level - 1; ++i)
                            {
                                filler += "*";
                            }
                            titles.Add(filler + " " + section.Title.Trim() + result);

                            List<WikiPageSection> sections = new List<WikiPageSection>();
                            section.Reduce(sections, SubsectionsList);
                            foreach (WikiPageSection subsection in sections)
                            {
                                result = "";
                                hasVerdict = subsection.Subsections.Count(s => s.Title.ToLower().Trim() == "итог") > 0;
                                hasAutoVerdict = subsection.Subsections.Count(s => s.Title.Trim() == "Автоматический итог") > 0;
                                if (hasVerdict || hasAutoVerdict || subsection.Title.Contains("<s>"))
                                {
                                    Match m = wikiLinkRE.Match(subsection.Title);
                                    if (m.Success && !m.Groups[1].Value.StartsWith(":Категория:"))
                                    {
                                        string link = m.Groups[1].Value;
                                        string movedTo;
                                        bool moved = MovedTo(wiki, link, day.Date, out movedTo);

                                        if (moved && string.IsNullOrEmpty(movedTo))
                                        {
                                            result = " ''(переименовано)''";
                                        }
                                        else if (moved)
                                        {
                                            result = string.Format(" ''({1}переименовано в «[[{0}]]»)''",
                                                movedTo.StartsWith("Файл:") ? ":" + movedTo : movedTo, hasAutoVerdict && !hasVerdict ? "де-факто " : "");
                                        }
                                        else
                                        {
                                            result = " ''(не переименовано)''";
                                        }
                                    }
                                }
                                filler = "";
                                for (int i = 0; i < subsection.Level - 1; ++i)
                                {
                                    filler += "*";
                                }
                                titles.Add(filler + " " + subsection.Title.Trim() + result);
                            }
                        }
                        if (titles.Count(s => s.Contains("=")) > 0)
                        {
                            titles[0] = "2=<li>" + titles[0].Substring(2) + "</li>";
                        }
                        sb.Append(string.Join("\n", titles.ConvertAll(c => c).ToArray()));
                        sb.Append("}}\n\n");
                    }
                    sb.Replace("<s>", "");
                    sb.Replace("</s>", "");
                    sb.Replace("<strike>", "");
                    sb.Replace("</strike>", "");

                    textBuilder.Append(sb.ToString());
                    textBuilder.AppendLine("{{Переименование статей/Конец}}");

                    archiveSW.WriteLine(textBuilder.ToString());

                    month = month.AddMonths(1);
                }

            string archiveName = string.Format("Википедия:Архив запросов на переименование/{0}-{1:00}",
                year, monthNumber);
            Console.Out.WriteLine("Updating " + archiveName + "...");
            using (TextReader sr =
                        new StreamReader(_cacheDir + "Archive-" +
                            year.ToString() + "-" + monthNumber.ToString() + ".txt"))
            {
                string text = sr.ReadToEnd();
                wiki.Save(archiveName, text, "обновление");
            }
        }
Ejemplo n.º 19
0
        public void LoadEx(Wiki wiki)
        {
            ParameterCollection parameters = new ParameterCollection
            {
                { "prop", "info|revisions" },
                { "intoken", "edit" },
                { "rvprop", "timestamp|content|ids" },
            };
            XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, Title);
            XmlNode node = xml.SelectSingleNode("//rev");
            string baseTimeStamp = "";
            string text = "";
            string revid = "";
            if (node != null && node.Attributes["timestamp"] != null)
            {
                baseTimeStamp = node.Attributes["timestamp"].Value;
            }
            if (node != null && node.Attributes["content"] != null)
            {
                text = node.Attributes["content"].Value;
            }
            if (node != null && node.Attributes["id"] != null)
            {
                revid = node.Attributes["id"].Value;
            }
            node = xml.SelectSingleNode("//page");
            string editToken = node.Attributes["edittoken"].Value;

            BaseTimestamp = baseTimeStamp;
            Token = editToken;
            LastRevisionId = revid;
            Parse(text);
        }
Ejemplo n.º 20
0
        private void PutNotification(Wiki wiki, TalkResult result, DateTime date)
        {
            string talkPageTemplate;
            string dateString = date.ToString("d MMMM yyyy");
            if (!result.Moved)
            {
                talkPageTemplate = "{{Не переименовано|" + dateString + "|" + result.Title + "}}\n";
            }
            else
            {
                talkPageTemplate = "{{Переименовано|" + dateString + "|" + result.Title +
                    "|" + result.MovedTo + "}}\n";
            }

            string talkPage = "Обсуждение:" + (result.Moved ? result.MovedTo : result.Title);
            Console.Out.WriteLine("Updating " + talkPage + "...");
            try
            {
                ParameterCollection parameters = new ParameterCollection();
                parameters.Add("rvprop", "content");
                parameters.Add("rvsection", "0)");
                parameters.Add("prop", "revisions");
                XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, new string[] { talkPage });
                string content;
                XmlNode node = xml.SelectSingleNode("//rev");
                if (node != null)
                {
                    content = node.FirstChild != null ? node.FirstChild.Value : "";
                }
                else
                {
                    content = "";
                }

                int index = content.IndexOf("{{talkheader", StringComparison.CurrentCultureIgnoreCase);
                if (index != -1)
                {
                    int endIndex = content.IndexOf("}}", index);
                    if (endIndex != -1)
                    {
                        content = content.Insert(endIndex + 2, "\n" + talkPageTemplate);
                    }
                }
                else
                {
                    index = content.IndexOf("{{заголовок обсуждения", StringComparison.CurrentCultureIgnoreCase);
                    if (index != -1)
                    {
                        int endIndex = content.IndexOf("}}", index);
                        if (endIndex != -1)
                        {
                            content = content.Insert(endIndex + 2, "\n" + talkPageTemplate);
                        }
                    }
                    else
                    {
                        content = content.Insert(0, talkPageTemplate);
                    }
                }

                wiki.SaveSection(talkPage,
                    "0",
                    content,
                    "итог");
            }
            catch (WikiException e)
            {
                Console.Out.WriteLine("Failed to update " + talkPage + ":" + e.Message);
            }
        }
Ejemplo n.º 21
0
        public void Run(Wiki wiki)
        {
            Directory.CreateDirectory(_cacheDir);

            Console.Out.WriteLine("Updating " + _page);
            Regex wikiLinkRE = new Regex(@"\[{2}(.+?)(\|.+?)?]{2}");
            Regex timeRE = new Regex(@"(\d{2}:\d{2}\, \d\d? [а-я]+ \d{4}) \(UTC\)");

            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("prop", "info|revisions");
            parameters.Add("intoken", "edit");
            XmlDocument doc = wiki.Query(QueryBy.Titles, parameters, _page);
            XmlNode page = doc.SelectSingleNode("//page");
            string queryTimestamp = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");
            string pageName = page.Attributes["title"].Value;
            string basetimestamp = page.FirstChild.FirstChild.Attributes["timestamp"].Value;
            string editToken = page.Attributes["edittoken"].Value;
            string starttimestamp = queryTimestamp;

            string text = "";
            string fileName = _cacheDir + WikiCache.EscapePath(_page);
            if (File.Exists(fileName))
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
                using (TextReader sr = new StreamReader(gs))
                {
                    string revid = sr.ReadLine();
                    if (revid == page.Attributes["lastrevid"].Value)
                    {
                        Console.Out.WriteLine("Loading " + pageName + "...");
                        text = sr.ReadToEnd();
                    }
                }
            }
            if (string.IsNullOrEmpty(text))
            {
                Console.Out.WriteLine("Downloading " + pageName + "...");
                text = wiki.LoadText(pageName);
                starttimestamp = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");

                using (FileStream fs = new FileStream(fileName, FileMode.Create))
                using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
                using (StreamWriter sw = new StreamWriter(gs))
                {
                    sw.WriteLine(page.Attributes["lastrevid"].Value);
                    sw.Write(text);
                }
            }

            Dictionary<string, List<WikiPageSection>> titles = new Dictionary<string, List<WikiPageSection>>();
            WikiPage wikiPage = WikiPage.Parse(pageName, text);
            foreach (WikiPageSection section in wikiPage.Sections)
            {
                if (section.Subsections.Count(s => s.Title.ToLower(CultureInfo.CreateSpecificCulture("ru-RU")).Trim() == "итог") == 0)
                {
                    Match m = wikiLinkRE.Match(section.Title);
                    if (m.Success)
                    {
                        string title = m.Groups[1].Value.Trim();
                        if (titles.ContainsKey(title))
                        {
                            titles[title].Add(section);
                        }
                        else
                        {
                            titles.Add(title, new List<WikiPageSection>());
                            titles[title].Add(section);
                        }
                    }
                }
            }

            parameters.Clear();
            parameters.Add("prop", "info");
            Dictionary<string, string> normalizedTitles = new Dictionary<string, string>();
            XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, titles.Keys);
            foreach (XmlNode node in xml.SelectNodes("//n"))
            {
                normalizedTitles.Add(node.Attributes["to"].Value,
                    node.Attributes["from"].Value);
            }
            List<string> notificationList = new List<string>();
            XmlNodeList missingTitles = xml.SelectNodes("//page");
            foreach (XmlNode node in missingTitles)
            {
                string title = node.Attributes["title"].Value;

                IEnumerable<WikiPageSection> sections;
                if (titles.ContainsKey(title))
                {
                    sections = titles[title];
                }
                else
                {
                    sections = titles[normalizedTitles[title]];
                }

                if (node.Attributes["missing"] != null)
                {
                    parameters.Clear();
                    parameters.Add("list", "logevents");
                    parameters.Add("letype", "delete");
                    parameters.Add("lemlimit", "max");
                    parameters.Add("ledir", "newer");
                    parameters.Add("letitle", title);
                    parameters.Add("leprop", "comment|type|user|timestamp");
                    XmlDocument log = wiki.Enumerate(parameters, true);
                    XmlNodeList items = log.SelectNodes("//item");
                    List<DeleteLogEvent> events = new List<DeleteLogEvent>();
                    foreach (XmlNode item in items)
                    {
                        DeleteLogEvent ev = new DeleteLogEvent();
                        ev.Comment = item.Attributes["comment"].Value;
                        ev.Deleted = item.Attributes["action"].Value == "delete";
                        ev.User = item.Attributes["user"].Value;
                        ev.Timestamp = DateTime.Parse(item.Attributes["timestamp"].Value,
                            null,
                            DateTimeStyles.AssumeUniversal);
                        events.Add(ev);
                    }
                    events.Sort(CompareDeleteLogEvents);
                    if (events.Count > 0 && events[0].Deleted)
                    {
                        string comment = FilterWikiMarkup(events[0].Comment);
                        string message = string.Format("Страница была удалена {1} администратором [[User:{0}|]]. Была указана следующая причина: «{2}». Данное сообщение было автоматически сгенерировано ботом ~~~~.\n",
                            events[0].User,
                            events[0].Timestamp.ToUniversalTime().ToString("d MMMM yyyy в HH:mm (UTC)",
                                CultureInfo.CreateSpecificCulture("ru-RU")),
                                comment);
                        var pageSections = titles.ContainsKey(title)
                            ? titles[title]
                            : titles[normalizedTitles[title]];
                        foreach (WikiPageSection section in pageSections)
                        {
                            WikiPageSection verdict = new WikiPageSection(" Итог ",
                                section.Level + 1,
                                message);
                            section.AddSubsection(verdict);
                        }
                    }
                }
            }

            string newText = wikiPage.Text;
            if (newText.Trim() == text.Trim())
            {
                return;
            }
            Console.Out.WriteLine("Updating " + pageName + "...");
            string id = wiki.Save(pageName,
                "",
                newText,
                "автоматическое подведение итогов",
                MinorFlags.Minor,
                CreateFlags.NoCreate,
                WatchFlags.None,
                SaveFlags.Replace,
                true,
                basetimestamp,
                "",
                editToken);

            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
            using (StreamWriter sw = new StreamWriter(gs))
            {
                sw.WriteLine(id);
                sw.Write(newText);
            }
        }
Ejemplo n.º 22
0
        internal void AddNavigationTemplate(Wiki wiki)
        {
            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("list", "embeddedin");
            parameters.Add("eititle", "Template:ВПКОБ-Навигация");
            parameters.Add("eilimit", "max");
            parameters.Add("einamespace", "4");
            parameters.Add("eifilterredir", "nonredirects");

            XmlDocument doc = wiki.Enumerate(parameters, true);

            List<string> titles = new List<string>();
            DateTime end = DateTime.Today;
            DateTime start = end.AddDays(-7);
            while (start <= end)
            {
                string pageDate = start.ToString("d MMMM yyyy",
                        CultureInfo.CreateSpecificCulture("ru-RU"));
                string prefix = "Википедия:К объединению/";
                string pageName = prefix + pageDate;
                if (doc.SelectSingleNode("//ei[@title='" + pageName + "']") == null)
                {
                    titles.Add(pageName);
                }
                start = start.AddDays(1);
            }

            parameters.Clear();
            parameters.Add("prop", "info");
            XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, titles);
            foreach (XmlNode node in xml.SelectNodes("//page"))
            {
                if (node.Attributes["missing"] == null)
                {
                    Console.Out.WriteLine("Updating " + node.Attributes["title"].Value + "...");
                    wiki.Prepend(node.Attributes["title"].Value,
                        "{{ВПКОБ-Навигация}}\n",
                        "добавление навигационного шаблона");
                }
            }
        }
Ejemplo n.º 23
0
        public override string ProcessCategory(Wiki wiki, string text)
        {
            var result = new List<string>();
            var pages = new HashSet<string>();

            HashSet<string> ignore = new HashSet<string>();
            foreach (var category in CategoriesToIgnore)
            {
                string fileName = "Cache\\" + Module.Language + "\\NewPages\\" + Cache.EscapePath(category) + ".txt";
                using (TextReader streamReader = new StreamReader(fileName))
                {
                    string line;
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        string[] groups = line.Split(new char[] { '\t' });
                        if (groups[0] == Namespace.ToString())
                        {
                            string title = groups[1].Replace('_', ' ');
                            ignore.Add(title);
                        }
                    }
                }
            }

            ParameterCollection parameters = new ParameterCollection();
            parameters.Add("redirects");
            parameters.Add("prop", "revisions");
            parameters.Add("rvprop", "content");
            parameters.Add("rvsection", "0");

            string file = "Cache\\" + Module.Language + "\\NewPages\\" + Cache.EscapePath(Categories[0]) + ".txt";
            Console.Out.WriteLine("Processing data of " + Categories[0]);
            using (TextReader streamReader = new StreamReader(file))
            {
                string line;
                while ((line = streamReader.ReadLine()) != null)
                {
                    string[] groups = line.Split(new char[] { '\t' });
                    if (groups[0] == Namespace.ToString())
                    {
                        string title = groups[1].Replace('_', ' ');
                        if (ignore.Contains(title))
                        {
                            continue;
                        }
                        string fullTitle = title;
                        if (Namespace != 0)
                        {
                            fullTitle = wiki.GetNamespace(Namespace) + ":" + title;
                        }
                        XmlDocument xml = wiki.Query(QueryBy.Titles, parameters, new string[] { fullTitle });
                        XmlNode node = xml.SelectSingleNode("//rev");
                        if (node != null)
                        {
                            fullTitle = xml.SelectSingleNode("//page").Attributes["title"].Value;
                            string content = node.FirstChild == null ? "" : node.FirstChild.Value;
                            Match m = _regex.Match(content);
                            if (m.Success)
                            {
                                string fileName = m.Groups["fileName"].Value.Trim();
                                if (string.IsNullOrEmpty(fileName))
                                {
                                    continue;
                                }
                                result.Add(string.Format(Format,
                                    Namespace != 0 ? fullTitle.Substring(wiki.GetNamespace(Namespace).Length + 1) : fullTitle,
                                    fileName));
                            }
                        }
                    }
                    if (result.Count == MaxItems)
                    {
                        break;
                    }
                }
            }

            if (result.Count < MaxItems)
            {
                string oldText = text;
                if (!string.IsNullOrEmpty(Header) && text.StartsWith(Header))
                {
                    oldText = oldText.Substring(Header.Length);
                }
                if (!string.IsNullOrEmpty(Footer) && oldText.EndsWith(Footer))
                {
                    oldText = oldText.Substring(0, oldText.Length - Footer.Length);
                }
                string[] items = oldText.Split(new string[] { Delimeter },
                       StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < items.Length && result.Count < MaxItems; ++i)
                {
                    if (!result.Exists(l => l == items[i]))
                    {
                        result.Add(items[i]);
                    }
                }
            }

            return Header + string.Join(Delimeter, result.ToArray()) + Footer;
        }