Удзельнік:Хомелка/для бота

З Вікіпедыі, свабоднай энцыклапедыі

using System; using System.Collections.Generic; using System.Linq; using System.IO; using DotNetWikiBot; using System.Xml; using MySql.Data.MySqlClient;

class record {

   public int all, main, user, templ, file, cat, porproj, meta, tech, main_edits_index;
   public bool globalbot;

} class Program { static void Main(string[] args)

   {
       string[] falsebots = { "Alex_Smotrov", "Wind", "Tutaishy" };
       Site site = new Site("https://ru.wikipedia.org", "MBHbot", new StreamReader("p").ReadToEnd());
       var bots = new Dictionary<string, record>();
       var users = new Dictionary<string, record>();
       var ruconnect = new MySqlConnection("Server=ruwiki.labsdb;Database=ruwiki_p;Uid=;Pwd=;CharacterSet=utf8;SslMode=none;");
       ruconnect.Open();
       var metaconnect = new MySqlConnection("Server=metawiki.labsdb;Database=metawiki_p;Uid=;Pwd=;CharacterSet=utf8;SslMode=none;");
       metaconnect.Open();
       MySqlCommand rucommand, metacommand;
       MySqlDataReader sr;
       rucommand = new MySqlCommand("select cast(user_name as char) user from user where user_editcount >= 10000;", ruconnect); //rucommand.CommandTimeout = 9999;
       sr = rucommand.ExecuteReader();
       while (sr.Read())
           users.Add(sr.GetString(0), new record());
       sr.Close();
       rucommand.CommandText = "select distinct cast(log_title as char) bot from logging where log_type=\"rights\" and log_params like '%bot%';";
       sr = rucommand.ExecuteReader();
       while (sr.Read())
       {
           string bot = sr.GetString(0);
           if (!falsebots.Contains(bot))
           {
               bots.Add(bot, new record() { globalbot = false });
               users.Remove(bot.Replace("_", " "));
           }
       }
       sr.Close();
       metacommand = new MySqlCommand("select distinct cast(log_title as char) bot from logging where log_type='gblrights' and (log_params like '%lobal-bot%' or log_params like '%lobal_bot%');", metaconnect);
       sr = metacommand.ExecuteReader();
       while (sr.Read())
       {
           string bot = sr.GetString(0);
           if (!bots.ContainsKey(bot))
           {
               bots.Add(bot, new record() { globalbot = true });
               users.Remove(bot.Replace("_", " "));
           }
       }
       foreach (var type in new Dictionary<string, record>[] { users, bots })
           foreach(var k in type.Keys)
           {
               bool end = false; string cont = "", query = "/w/api.php?action=query&format=xml&list=usercontribs&uclimit=5000&ucprop=title&ucuser=" + Uri.EscapeDataString(k);
               while (end == false)
               {
                   string apiout = (cont == "" ? site.GetWebPage(query) : site.GetWebPage(query + "&uccontinue=" + Uri.EscapeDataString(cont)));
                   using (var r = new XmlTextReader(new StringReader(apiout)))
                   {
                       r.WhitespaceHandling = WhitespaceHandling.None;
                       r.Read(); r.Read(); r.Read(); cont = r.GetAttribute("uccontinue");
                       if (cont == null) end = true;
                       while (r.Read())
                           if (r.Name == "item")
                           {
                               int ns = Convert.ToInt16(r.GetAttribute("ns"));
                               type[k].all++;
                               if (ns == 0 || ns == 1)
                                   type[k].main++;
                               else if (ns == 2 || ns == 3)
                                   type[k].user++;
                               else if (ns == 4 || ns == 5 || ns == 12 || ns == 13 || ns == 106 || ns == 107)
                                   type[k].meta++;
                               else if (ns == 100 || ns == 101 || ns == 104 || ns == 105)
                                   type[k].porproj++;
                               else if (ns == 10 || ns == 11)
                                   type[k].templ++;
                               else if (ns == 6 || ns == 7)
                                   type[k].file++;
                               else if (ns ==  8 || ns == 9 || ns == 828 || ns == 829)
                                   type[k].tech++;
                               else if (ns == 14 || ns == 15)
                                   type[k].cat++;
                           }
                   }
               }
           }
       string header = "
Самыя актыўныя ўдзельнікі
Па колькасці правак (ботына вікіскане) • статыстыка догляду
\nУ кожным слупку прыведзена сума правак у паказанай прасторы і яе абмеркаванні. Першасна адсартавана і пранумеравана па агульнай колькасці правак.%globalbots%\n{|class=\"standard sortable\"\n!№!!№ п/с!!Удзельнік!!Усяго правак!!У артыкулах!!шаблонах!!файлах!!катэгорыях!!парталах і праектах!!модулях і MediaWiki!!старонках удзельнікаў!!метапедычных страницах";
       int main_edits_index = 0;
       foreach (var bot in bots.OrderByDescending(bot => bot.Value.main))
       {
           if (bot.Value.all == 0)
               bots.Remove(bot.Key);
           else bot.Value.main_edits_index = ++main_edits_index;
       }
       main_edits_index = 0;
       foreach (var user in users.OrderByDescending(user => user.Value.main))
           user.Value.main_edits_index = ++main_edits_index;
       site = new Site("https://ru.wikipedia.org", "MBHbot", new StreamReader("p").ReadToEnd());
       string botresult = "
Кароткая спасылка-перанакіраванне ВП:САБ
" + header.Replace("%globalbots%", " Блакітным паказаны глабальныя боты.");
       int all_edits_index = 0;
       foreach (var s in bots.OrderByDescending(s => s.Value.all))
       {
           string color = "";
           if (s.Value.globalbot)
               color = "style=\"background-color:#bbf\"";
           botresult += "\n|-" + color + "\n|" + ++all_edits_index + "||" + s.Value.main_edits_index + "||" + s.Key + "||" + s.Value.all + "||" + s.Value.main + "||" + s.Value.templ + "||" + s.Value.file + "||" + s.Value.cat + "||" + s.Value.porproj + "||" + s.Value.tech + "||" + s.Value.user + "||" + s.Value.meta;
       }
       botresult += "\n|}";
       var b = new Page("Вікіпедыя:Самыя актыўныя боты");
       b.Save(botresult, "абнаўленне", false);
       all_edits_index = 0;
       string userresult = "
Кароткая спасылка-перанакіраванне ВП:САУ
" + header.Replace("%globalbots%", "");
       foreach (var s in users.OrderByDescending(s => s.Value.all))
           userresult += "\n|-\n|" + ++all_edits_index + "||" + s.Value.main_edits_index + "||" + s.Key + "||" + s.Value.all + "||" + s.Value.main + "||" + s.Value.templ + "||" + s.Value.file + "||" + s.Value.cat + "||" + s.Value.porproj + "||" + s.Value.tech + "||" + s.Value.user + "||" + s.Value.meta;
       userresult += "\n|}";
       var u = new Page("Вікіпедыя:Удзельнікі з найбольшай колькасцю правак");
       u.Save(userresult, "абнаўленне", false);
   }

}