User:A2569875-bot/Code/WikiCasCheckTool.cs

using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using WikiTemplateArgUtil;

namespace DotNetWikiBot
{
   public class wikitext_util
    {
        public static WikiTemplate FindTemplate(string TemplateName, WikiText source)
        {
            string find_name = TemplateName.Replace(" ", "");
            find_name = find_name.Replace("_", "").Replace("\n", "").Replace("\r", "").Replace("\t", "")
                .Replace("{", "").Replace("}", "").Replace("|", "").Replace("[", "").Replace("]", "")
                .Replace("<", "").Replace(">", "").ToLower();
            Stack<WikiEntity> find_stack = new Stack<WikiEntity>();
            find_stack.Push(source);
            while (find_stack.Any())
            {
                WikiEntity temp = find_stack.Pop();
                if(temp is WikiTemplate) {
                    WikiTemplate template = (WikiTemplate)temp;
                    string check_name = template.Name.genCode().Replace(" ", "");
                    check_name = check_name.Replace("_", "").Replace("\n", "").Replace("\r", "").Replace("\t", "")
                        .Replace("{", "").Replace("}", "").Replace("|", "").Replace("[", "").Replace("]", "")
                        .Replace("<", "").Replace(">", "").ToLower();
                    if (find_name == check_name) return template;
                    for (int i = template.Name.WikiEntityList.Count - 1; i >= 0; --i) find_stack.Push(template.Name.WikiEntityList[i]);
                    for (int i = template.arg_list.Count - 1; i >= 0; --i) {
                        if(template.arg_list[i] is WikiTemplateValue) {
                            WikiTemplateValue arg = template.arg_list[i] as WikiTemplateValue;
                            for (int j = arg.Value.WikiEntityList.Count - 1; j >= 0; --j) find_stack.Push(arg.Value.WikiEntityList[j]);
                        }else if (template.arg_list[i] is WikiTemplateArg) {
                            WikiTemplateArg arg = template.arg_list[i] as WikiTemplateArg;
                            for (int j = arg.Value.WikiEntityList.Count - 1; j >= 0; --j) find_stack.Push(arg.Value.WikiEntityList[j]);
                            for (int j = arg.Name.WikiEntityList.Count - 1; j >= 0; --j) find_stack.Push(arg.Name.WikiEntityList[j]);
                        }
                    }
                } else if (temp is WikiLink) {
                    WikiLink link = temp as WikiLink;
                    for (int i = link.Name.WikiEntityList.Count - 1; i >= 0; --i) find_stack.Push(link.Name.WikiEntityList[i]);
                    for (int i = link.arg_list.Count - 1; i >= 0; --i) {
                        if(link.arg_list[i] is WikiTemplateValue) {
                            WikiTemplateValue arg = link.arg_list[i] as WikiTemplateValue;
                            for (int j = arg.Value.WikiEntityList.Count - 1; j >= 0; --j) find_stack.Push(arg.Value.WikiEntityList[j]);
                        }else if (link.arg_list[i] is WikiTemplateArg) {
                            WikiTemplateArg arg = link.arg_list[i] as WikiTemplateArg;
                            for (int j = arg.Value.WikiEntityList.Count - 1; j >= 0; --j) find_stack.Push(arg.Value.WikiEntityList[j]);
                            for (int j = arg.Name.WikiEntityList.Count - 1; j >= 0; --j) find_stack.Push(arg.Name.WikiEntityList[j]);
                        }
                    }
                } else if (temp is WikiVariable) {
                    WikiVariable vari = temp as WikiVariable;
                    for (int i = vari.Name.WikiEntityList.Count - 1; i >= 0; --i) find_stack.Push(vari.Name.WikiEntityList[i]);
                    for (int i = vari.arg_list.Count - 1; i >= 0; --i) {
                        if(vari.arg_list[i] is WikiTemplateValue) {
                            WikiTemplateValue arg = vari.arg_list[i] as WikiTemplateValue;
                            for (int j = arg.Value.WikiEntityList.Count - 1; j >= 0; --j) find_stack.Push(arg.Value.WikiEntityList[j]);
                        }else if (vari.arg_list[i] is WikiTemplateArg) {
                            WikiTemplateArg arg = vari.arg_list[i] as WikiTemplateArg;
                            for (int j = arg.Value.WikiEntityList.Count - 1; j >= 0; --j) find_stack.Push(arg.Value.WikiEntityList[j]);
                            for (int j = arg.Name.WikiEntityList.Count - 1; j >= 0; --j) find_stack.Push(arg.Name.WikiEntityList[j]);
                        }
                    }
                } else if (temp is WikiXml) {
                    WikiXml xmldata = temp as WikiXml;
                    for (int i = xmldata.InnerXml.WikiEntityList.Count - 1; i >= 0; --i)
                    {
                        find_stack.Push(xmldata.InnerXml.WikiEntityList[i]);
                    }
                } else if (temp is WikiText) {
                    WikiText subtree = (WikiText)temp;
                    for (int i = subtree.WikiEntityList.Count - 1; i >= 0; --i)
                    {
                        find_stack.Push(subtree.WikiEntityList[i]);
                    }
                }
            }
            return null;
        }

        public static void MergeTemplate(WikiTemplate t_from, ref WikiTemplate t_to)
        {
            Regex cas_par = new Regex("cas([號\\u53f7]|(no[0-9]*|number))");
            if(t_from != null)
            for (int i = 0; i < t_from.arg_list.Count; ++i)
            {
                if (t_from.arg_list[i] is WikiTemplateArg)
                {
                    WikiTemplateArg the_arg = t_from.arg_list[i] as WikiTemplateArg;
                    string arg_name = the_arg.Name.genCode().Replace(" ", "").Replace("_", "").Replace("\n", "").Replace("\r", "").Replace("\t", "")
                        .Replace("{", "").Replace("}", "").Replace("|", "").Replace("[", "").Replace("]", "")
                        .Replace("<", "").Replace(">", "").ToLower();
                    Match m_result = cas_par.Match(arg_name);
                    if (m_result.Success) continue;
                    bool ToBeContinue = false;
                    for (int j = 0; j < t_to.arg_list.Count; ++j)
                    {
                        if (t_to.arg_list[j] is WikiTemplateArg)
                        {
                            WikiTemplateArg to_arg = t_to.arg_list[j] as WikiTemplateArg;
                            string to_arg_name = to_arg.Name.genCode().Replace(" ", "").Replace("_", "").Replace("\n", "").Replace("\r", "").Replace("\t", "")
                                .Replace("{", "").Replace("}", "").Replace("|", "").Replace("[", "").Replace("]", "")
                                .Replace("<", "").Replace(">", "").ToLower();
                            string to_val = to_arg.Value.genCode().Replace(" ", "").Replace("_", "").Replace("\n", "").Replace("\r", "").Replace("\t", "")
                                .Replace("{", "").Replace("}", "").Replace("|", "").Replace("[", "").Replace("]", "")
                                .Replace("<", "").Replace(">", "").ToLower();
                            if (arg_name == to_arg_name)
                            {
                                //不存在才移動
                                if (to_val == "") to_val = the_arg.Value.genCode();
                                ToBeContinue = true;
                                break;
                            }
                        }
                    }
                    if (ToBeContinue) continue;
                    if (the_arg.Value.genCode().Replace(" ", "").Replace("_", "").Replace("\n", "").Replace("\r", "").Replace("\t", "")
                                .Replace("{", "").Replace("}", "").Replace("|", "").Replace("[", "").Replace("]", "")
                                .Replace("<", "").Replace(">", "").ToLower()
                         == "")
                        //忽略空參數
                        continue;
                    //若中文區連這個參數本身都完全沒有
                    //即加入此參數
                    t_to.arg_list.Add(t_from.arg_list[i]);
                }
            }
            //CAS號在另一個函式合併
            //避免重複,先移除CAS號
            for (int i = 0; i < t_to.arg_list.Count; ++i)
            {
                if (t_to.arg_list[i] is WikiTemplateArg)
                {
                    WikiTemplateArg the_arg = t_to.arg_list[i] as WikiTemplateArg;
                    string arg_name = the_arg.Name.genCode().Replace(" ", "").Replace("_", "").Replace("\n", "").Replace("\r", "").Replace("\t", "")
                        .Replace("{", "").Replace("}", "").Replace("|", "").Replace("[", "").Replace("]", "")
                        .Replace("<", "").Replace(">", "").ToLower();
                    Match m_result = cas_par.Match(arg_name);
                    if (m_result.Success)
                    {
                        t_to.arg_list.Remove(t_to.arg_list[i--]);
                    }
                }
            }
            t_to.arg_list[t_to.arg_list.Count - 1].Value.WikiEntityList.Add(new WikiText(new WikiFlatText("\n")));
        }

        public static void AddCasNoToChemboxIdentifiers(ref WikiTemplate to_add, List<cas_checker> cas_list)
        {

            for (int i = cas_list.Count - 1; i > 0; --i)
            {
                to_add.arg_list.Insert(0, WikiTemplateArgBase.Create("CASNo" + i.ToString() + "_Ref",
                    cas_list[i].cas_ref));
                if (cas_list[i].cas_comment != "")
                    to_add.arg_list.Insert(0, WikiTemplateArgBase.Create("CASNo" + i.ToString() + "_Comment",
                        cas_list[i].cas_comment));
                to_add.arg_list.Insert(0, WikiTemplateArgBase.Create("CASNo" + i.ToString(), cas_list[i].cas_no));
            }
            to_add.arg_list.Insert(0, WikiTemplateArgBase.Create("CASNo_Ref", cas_list[0].cas_ref));
            if (cas_list[0].cas_comment != "") to_add.arg_list.Insert(0, WikiTemplateArgBase.Create("CASNo_Comment",
                    cas_list[0].cas_comment));
            to_add.arg_list.Insert(0, WikiTemplateArgBase.Create("CASNo", cas_list[0].cas_no));
        }

        public static List<cas_checker> getCasNos(WikiTemplate chem_template)
        {
            //TreeNode<ParsingTreeNode> parsing_tree = input.parsing_tree;

            //WikiText ParsedWikiEntity = input.parsed_result;
            //WikiTemplate chem_template = FindTemplate("Chembox Identifiers", ParsedWikiEntity);

            Regex cas_par = new Regex("cas([號\\u53f7]|(no[0-9]*|number))");
            List<cas_checker> cas_list = new List<cas_checker>();
            List<string> arg_name_list = new List<string>();
            for (int i=0; i< chem_template.arg_list.Count; ++i)
            {
                if(chem_template.arg_list[i] is WikiTemplateArg)
                {
                    WikiTemplateArg the_arg = chem_template.arg_list[i] as WikiTemplateArg;
                    string arg_name = the_arg.Name.genCode().Replace(" ", "").Replace("_", "").Replace("\n", "").Replace("\r", "").Replace("\t", "")
                        .Replace("{", "").Replace("}", "").Replace("|", "").Replace("[", "").Replace("]", "")
                        .Replace("<", "").Replace(">", "").ToLower();
                    Match m_result = cas_par.Match(arg_name);
                    if (m_result.Success)
                    {
                        cas_checker temp_cas;
                        if(arg_name == m_result.Value)
                        {
                            temp_cas = new cas_checker(the_arg.Value.genCode().Trim(' ')
                                .Trim('\n').Trim('\r').Trim('\t'), arg_name);
                            arg_name_list.Add(arg_name);
                            cas_list.Add(temp_cas);
                        }
                    }
                }
            }
            for (int i = 0; i < cas_list.Count; ++i){
                bool ref_found = false;
                bool comment_found = false;
                for (int j = 0; j < chem_template.arg_list.Count; ++j){
                    if (chem_template.arg_list[j] is WikiTemplateArg)
                    {
                        WikiTemplateArg the_arg = chem_template.arg_list[j] as WikiTemplateArg;
                        string arg_name = the_arg.Name.genCode().Replace(" ", "").Replace("_", "").Replace("\n", "").Replace("\r", "").Replace("\t", "")
                            .Replace("{", "").Replace("}", "").Replace("|", "").Replace("[", "").Replace("]", "")
                            .Replace("<", "").Replace(">", "").ToLower();
                        if ((arg_name_list[i] + "ref") == arg_name)
                        {
                            ref_found = true;
                            cas_list[i].cas_ref = the_arg.Value.genCode().Trim(' ')
                                .Trim('\n').Trim('\r').Trim('\t');
                        }//supplemental
                        if ((arg_name_list[i] + "comment") == arg_name || (arg_name_list[i] + "supplemental") == arg_name)
                        {
                            comment_found = true;
                            cas_list[i].cas_comment = the_arg.Value.genCode();
                        }
                        if (ref_found && comment_found) break;
                    }
                }
                if (!ref_found) cas_list[i].cas_ref = "";
                if (!comment_found) cas_list[i].cas_comment = "";
            }
            return cas_list;
        }
    }
}