Wikipedia:Projekt DotNetWikiBot Framework/Lsjbot/Lsj-clean-params2
Utseende
//Botcode to reorder parameters in a template to a standard order, and make sure a standard parameter set is included. //Code is set for Mall:Taxobox at present. Modify templateTitle, mainparamstring, allparamstring to use with other template. // //Originally from [[Wikipedia:Projekt DotNetWikiBot Framework/Innocent bot/Ny parameter i Mall Ishockeyspelare]] //Extensively modified by Lsj. // using System; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Threading; using DotNetWikiBot; // class MyBot : Bot { public static void Main() { Site site = new Site("http://sv.wikipedia.org", "*****", "*****"); PageList pl = new PageList(site); //============================================================================ //Select how to get pages. Uncomment as needed. //============================================================================ //Find articles from a category //pl.FillFromCategoryTree("Kräldjur"); //Find articles from all the links to a template, mostly useful on very small wikis // pl.FillFromLinksToPage("Mall:Taxobox"); //Set specific article: Page pp = new Page(site, "Talgoxe"); pl.Add(pp); //pl.RemoveNamespaces(new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,100,101}); //============================================================================ //Loop over pages: foreach(Page p in pl) { p.Load(); string origtext = p.text; //Reorder parameters and remove superfluous ones //mainparams are those that should always be in the template, even if unused. string mainparamstring = "name|status|image|image_caption|domain_sv|domain|regnum_sv|regnum|phylum_sv|phylum|subphylum_sv|subphylum|classis_sv|classis|ordo_sv|ordo|familia_sv|familia|subfamilia_sv|subfamilia|genus|species_sv|species|taxon|taxon_authority|range_map|range_map_caption|image2|image2_caption"; string[] mainparams = mainparamstring.Split('|'); //allparams are all the params of the template, in the order desired. Those not in mainparams are included only if used. string allparamstring = "name|status|status_ref|sverigestatus|sverigestatus_ref|fossil_range|image|image_caption|image_width|range_map|range_map_caption|range_map_width|image2|image2_caption|image2_width|domain_sv|domain|domain_authority|superregnum_sv|superregnum|superregnum_authority|regnum_sv|regnum|regnum_authority|subregnum_sv|subregnum|subregnum_authority|unranked_phylum_sv|unranked_phylum|unranked_phylum_authority|superdivisio_sv|superdivisio|superdivisio_authority|superphylum_sv|superphylum|superphylum_authority|divisio_sv|divisio|divisio_authority|phylum_sv|phylum|phylum_authority|subdivisio_sv|subdivisio|subdivisio_authority|subphylum_sv|subphylum|subphylum_authority|infraphylum_sv|infraphylum|infraphylum_authority|microphylum_sv|microphylum|microphylum_authority|nanophylum_sv|nanophylum|nanophylum_authority|unranked_classis_sv|unranked_classis|unranked_classis_authority|superclassis_sv|superclassis|superclassis_authority|classis_sv|classis|classis_authority|subclassis_sv|subclassis|subclassis_authority|infraclassis_sv|infraclassis|infraclassis_authority|unranked_ordo_sv|unranked_ordo|unranked_ordo_authority|magnordo_sv|magnordo|magnordo_authority|superordo_sv|superordo|superordo_authority|ordo_sv|ordo|ordo_authority|subordo_sv|subordo|subordo_authority|infraordo_sv|infraordo|infraordo_authority|parvordo_sv|parvordo|parvordo_authority|zoodivisio_sv|zoodivisio|zoodivisio_authority|zoosectio_sv|zoosectio|zoosectio_authority|unranked_familia_sv|unranked_familia|unranked_familia_authority|superfamilia_sv|superfamilia|superfamilia_authority|familia_sv|familia|familia_authority|subfamilia_sv|subfamilia|subfamilia_authority|supertribus_sv|supertribus|supertribus_authority|tribus_sv|tribus|tribus_authority|subtribus_sv|subtribus|subtribus_authority|genus_sv|genus|genus_authority|subgenus_sv|subgenus|subgenus_authority|species_sv|species|species_authority|subspecies_sv|subspecies|subspecies_authority|taxon|taxon_authority|synonyms|subdivision_ranks|subdivision"; string[] allparams = allparamstring.Split('|'); // Dictionary <string, string> oldparameters; // //set template name here: string templateTitle = "Taxobox"; Regex templateTitleRegex = new Regex("^\\s*(" + Bot.Capitalize(Regex.Escape(templateTitle)) + "|" + Bot.Uncapitalize(Regex.Escape(templateTitle)) + ")\\s*\\|"); foreach (string template in p.GetTemplatesWithParams()) { if (templateTitleRegex.IsMatch(template)) { //Ok, found the right template. Now get params: oldparameters = site.ParseTemplate(template); //First clear the template, except one: Console.WriteLine("Clear template"); bool skipfirst = true; foreach (string op in oldparameters.Keys) { //Console.WriteLine(op); if (!skipfirst) p.RemoveTemplateParameter(templateTitle, op, true); else skipfirst = false; } //Then loop through all params and replace the ones we want in the right order: Console.WriteLine("Refill template"); foreach (string ap in allparams) if (( ap!= null)&&(ap!="")) { //Console.WriteLine(ap); bool inold = oldparameters.ContainsKey(ap); string value = ""; if (inold) { value = oldparameters[ap]; inold = (value != ""); } bool inmain = false; foreach (string mp in mainparams) if (ap == mp) inmain = true; if (inold | inmain) { p.SetTemplateParameter("Taxobox", ap, value, true); oldparameters[ap] = ""; } } //Finally, check if any params are left over in oldparameters, and add them at the end: Console.WriteLine("Check leftovers"); foreach (string op in oldparameters.Keys) if (oldparameters[op] != "") p.SetTemplateParameter("Taxobox", op, oldparameters[op], true); } } //DONE! Now save if needed. // Bot.editComment = "Fixar taxobox"; isMinorEdit = true; if (p.text != origtext) p.Save(); Thread.Sleep(4000);//milliseconds } } }