User:WJ-Bot/HWTemplUnify

维基百科,自由的百科全书
private void HighwayTemplateUnifier()
{
    Site zhwiki = new Site("zh.wikipedia.org", "WJ-Bot", "PASSWORD");
    BotWorker bw = new BotWorker();
    bw.PageLoaded += OnWorkerStatusChange;
    bw.PageUpdated += OnWorkerStatusChange;

    PageList pl = new PageList(zhwiki);

    PageList pl1 = new PageList(zhwiki);
    pl.FillFromLinksToPage("Template:中国国家高速公路信息框");
    foreach (Page pg in pl1) pl.Add(pg);

    PageList pl2 = new PageList(zhwiki);
    pl2.FillFromLinksToPage("Template:中国省级高速公路信息框");
    foreach (Page pg in pl2) pl.Add(pg);

    bw.Start(pl, t => !t.Contains(':'), pg =>
    {
        string new_page = pg.text;
        string pname = string.Empty;

        foreach (string tl in pg.GetTemplatesWithParams())
        {
            Regex prov_expwy_regex = new Regex(@"^ *(\S*)高速公路 *$");
            if (prov_expwy_regex.IsMatch(tl))
            {
                Match mt = prov_expwy_regex.Match(tl);
                pname = mt.Groups[1].Value;
            }
        }

        foreach (string tl in pg.GetTemplatesWithParams())
        {
            string tl_text = string.Empty;
            bool is_national = true;
            if (tl.Contains("Infobox 中國國家高速公路") ||
                tl.Contains("Infobox 中国国家高速公路") ||
                tl.Contains("中国国家高速公路信息框"))
                tl_text = tl;
            else if (tl.Contains("Infobox 中國省级高速公路") ||
                tl.Contains("Infobox 中国省级高速公路") ||
                tl.Contains("中国省级高速公路信息框"))
            {
                tl_text = tl;
                is_national = false;
            }
            if (tl_text != string.Empty)
            {
                string[] para_replace = new string[]{
                "國家高速編號:编号",
                "國家高速名稱:名称",
                "省高速編號:编号",
                "省高速名稱:名称",
                "附加頭部圖片:自定头部"};
                string tl_new = tl_text;
                Regex header_regex = new Regex(@"^[\S ]*高速公路\S*\n");
                if (!is_national && pname != string.Empty)
                {
                    if (!tl_new.Contains("省名"))
                        tl_new = header_regex.Replace(tl_new, "中国高速公路信息框\n| 省名 = " + pname + "\n");
                    else
                        tl_new = header_regex.Replace(tl_new, "中国高速公路信息框\n");
                }
                else
                {
                    Regex code_regex = new Regex(@"高速編號 *= *\S+");
                    if (!code_regex.IsMatch(tl_new))
                        tl_new = header_regex.Replace(tl_new, "中国高速公路信息框\n| 级别 = 国家\n");
                    else
                        tl_new = header_regex.Replace(tl_new, "中国高速公路信息框\n");
                }

                Regex param2_regex = new Regex(@" *\n+\| *(\S*) *= *");
                tl_new = param2_regex.Replace(tl_new, "\n| $1 = ");

                foreach (var pstr in para_replace)
                {
                    string[] sparts = pstr.Split(':');
                    tl_new = tl_new.Replace(sparts[0], sparts[1]);
                }

                new_page = new_page.Replace(tl_text, tl_new);
            }
        }
        if (new_page != pg.text)
            return new_page;
        else
            return null;
    }, false);
}