Jump to content

Wikipedia:AutoWikiBrowser/Custom Modules

From Wikipedia, the free encyclopedia

Using multiple Custom Modules

[edit]
See AWB Manual: Tools: Make Module for how to include a custom module

By default, you can't actually use multiple custom modules; by using simple coding techniques, you can use multiple.

Just be aware, you may need to tweak your summary and skip changes (if you're not using skip, it's easier). Summary, just use +=, to append onto the summary

public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
{
    Summary = "";
    Skip = false;
    ArticleText = CustomModule1(ArticleText, ArticleTitle, wikiNamespace, ref Summary, ref Skip);
    ArticleText = CustomModule2(ArticleText, ArticleTitle, wikiNamespace, ref Summary, ref Skip);

    return ArticleText;
}

public string CustomModule1(string ArticleText, string ArticleTitle, int wikiNamespace, ref string Summary, ref bool Skip)
{
    if (!Skip)
    {
        Summary += "do foo"; 

        //Blah, code goes here (and it can update Skip if required)
    }
    return ArticleText;
}

public string CustomModule2(string ArticleText, string ArticleTitle, int wikiNamespace, ref string Summary, ref bool Skip)
{
    if (!Skip)
    {
        Summary += "do bar"; 

        //Blah, code goes here (and it can update Skip if required)
    }
    return ArticleText;
}

Mass AfD tagging

[edit]

Go to "Tools" → "Make module" and paste the following code:

         public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
         {
             Skip = false;
             Summary = "EDIT_SUMMARY_HERE";
 
             ArticleText = "TAG_TO_ADD_HERE\r\n" + ArticleText;
 
             return ArticleText;
         }

Replacing EDIT_SUMMARY_HERE with the desired edit summary (keep the quotes around the summary; if you want actual quotes in the summary write them as \") and TAG_TO_ADD_HERE with the tag you want to add to the top of the page (again keep the quotes around the summary and the \r\n after it). If you want the tag to be at the end of the page replace the line

             ArticleText = "TAG_TO_ADD_HERE\r\n" + ArticleText;

with

            ArticleText = ArticleText + "\r\nTAG_TO_ADD_HERE";

Then check the "Enabled" box, press "Make module", and then "Done". Keep the "edit summary" box in the main AWB form empty.

Passing text to external program for processing

[edit]

Note: An interface now exists in AWB for this

        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            string OrigText = ArticleText;
            Skip = false;
            Summary = "";

            try
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
                psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                psi.WorkingDirectory = "C:\\Path";
                psi.FileName = "script.ext";
                psi.Arguments = "";

                using (System.IO.StreamWriter writer = new System.IO.StreamWriter("input.txt"))
                    writer.Write(ArticleText);

                System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
                p.WaitForExit();

                if (System.IO.File.Exists(psi.WorkingDirectory + "\\output.txt"))
                {
                    using (System.IO.StreamReader reader = System.IO.File.OpenText(psi.WorkingDirectory + "\\output.txt"))
                        ArticleText = reader.ReadToEnd();

                    if (ArticleText == OrigText)
                        Skip = true;
                }
                else
                    Skip = true;

                return ArticleText;
            }
            catch
            {
                Skip = true;
                return OrigText;
            }
        }
psi.WorkingDirectory = "C:\\Path";
psi.FileName = "script.ext";
psi.Arguments = "";

So, working directory, is where the file you want to run is. File name is obvious, arguments, is probably how you want to pass it the article text or similar.

Inserting text just before categories

[edit]

Could be easily adapted for inserting into other parts of the page.

        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "";

            Match m = Regex.Match(ArticleText, @"\[\[[Cc]ategory:");

            if (m.Success) ArticleText = ArticleText.Insert(m.Index, "foo\r\n");
            else ArticleText += "\r\nfoo";

            return ArticleText;
        }

Add template if not already on talk page

[edit]

This example will add {{Talk header}} if it doesn't exist on the page already

        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Regex header = new Regex(@"\{\{Talk header}}", RegexOptions.IgnoreCase);
            Summary = "Adding {{Talk header}}";

            Skip = (header.Match(ArticleText).Success || !Namespace.IsTalk(ArticleTitle));
            if (!Skip)
                ArticleText = "{{Talk header}} \r\n\r\n" + ArticleText;

            return ArticleText;
        }

Rough archiving template insert after last template on page

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "Adding archiving templates";

            int insertPos = ArticleText.LastIndexOf("}}") + 2;

            if (insertPos < 2)
                insertPos = 0;

            ArticleText = ArticleText.Insert(insertPos, @"{{autoarchivingnotice|bot=MiszaBot I|age=28}}
{{User:MiszaBot/config
|archiveheader = {{talkarchivenav}}
|maxarchivesize = 31K
|counter = 3
|algo = old(28d)
|archive = Talk:{{subst:FULLPAGENAME}}/Archive %(counter)d
}}
");

            return ArticleText;
        }

How to write initialization code that's run just once

[edit]

AWB will run your ProcessArticle() method many times, once for each page processed. Sometimes you want to put initialization code into the Custom Module which will be executed only once (as in the example below, reading "words to unlink" from a text file). To do this, put the initialization code into a static constructor and store values into one or more static fields like this:

        // static field to hold data
        static HashSet<string> wordsToUnlink = new HashSet<string>(); // or List<string>, etc

        // static constructor for the CustomModule class
        static CustomModule()
        {
            // put whatever initialization code you want into here in place of my three lines
            using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\MyAwb\unlinkwords.txt")) // for example
            {
                while (!sr.EndOfStream)
                {
                    wordsToUnlink.Add(sr.ReadLine());
                }
            }
        }

Customised "General Fixes"

[edit]

A way was requested to run the general fixes, but to disable certain specific ones. Therefore, the code has basically just been duplicated as a Custom Module, and the lines with "//" specify the lines where a general fix has been disabled.

  1. Paste the following into the "Make Module" window at "Tools". Leave language at "C# 2.0", click "enabled" and "make module". The below sample has the insertion of &nbsp; disabled.
  2. In the "Options" tab, disable "Apply general fixes"
AWB version 5.4.0.1 / SVN
WikiFunctions.Parse.Parsers parsers = new WikiFunctions.Parse.Parsers();
WikiFunctions.Parse.HideText removeText = new WikiFunctions.Parse.HideText(false, true, false);
WikiFunctions.Parse.HideText HiderHideExtLinksImages = new WikiFunctions.Parse.HideText(true, true, true);
 
public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
{
        Skip = false;
        Summary = "";
 
        Article a = new Article(ArticleTitle, ArticleText);
        string zeroth = WikiRegexes.ZerothSection.Match(a.ArticleText).Value;
        bool CircaLink = WikiRegexes.CircaLinkTemplate.IsMatch(a.ArticleText), Floruit = (!zeroth.Contains(@"[[floruit|fl.]]") && WikiRegexes.UnlinkedFloruit.IsMatch(zeroth));

        a.HideMoreText(HiderHideExtLinksImages);

        // The two slashes below are inserted to disable the insertion of non-breaking spaces HTML markup (sample) 
        // a.AWBChangeArticleText("Fix non-breaking spaces", parsers.FixNonBreakingSpaces(a.ArticleText), true);

        a.AWBChangeArticleText("Mdashes", parsers.Mdashes(a.ArticleText, ArticleTitle), true);

        a.AWBChangeArticleText("Fix Date Ordinals/Of", parsers.FixDateOrdinalsAndOf(a.ArticleText, ArticleTitle), true, true);


        a.AWBChangeArticleText("FixBrParagraphs", parsers.FixBrParagraphs(a.ArticleText).Trim(), true);

        if (!Tools.IsRedirect(a.ArticleText))
        {
            a.AWBChangeArticleText("Fix dates 1", parsers.FixDatesB(a.ArticleText, CircaLink, Floruit).Trim(), true);
        }

        a.UnHideMoreText(HiderHideExtLinksImages);

        if (!Tools.IsRedirect(a.ArticleText))
        {
            // FixDates does its own hiding
            a.AWBChangeArticleText("Fix dates 2", parsers.FixDatesA(a.ArticleText).Trim(), true);
        }

        a.HideText(removeText);

        if (Tools.IsRedirect(a.ArticleText))
        {
            a.AWBChangeArticleText("Redirect tagger", WikiFunctions.Parse.Parsers.RedirectTagger(a.ArticleText, ArticleTitle), true);

            a.AWBChangeArticleText("Fix syntax redirects", WikiFunctions.Parse.Parsers.FixSyntaxRedirects(a.ArticleText), true);
        }
        else
        {
            a.AWBChangeArticleText("Template redirects", WikiFunctions.Parse.Parsers.TemplateRedirects(a.ArticleText, WikiRegexes.TemplateRedirects), true);

            a.AWBChangeArticleText("Fixes for {{Multiple issues}}", parsers.MultipleIssues(a.ArticleText), true);

            a.AWBChangeArticleText("Fix whitespace in links", WikiFunctions.Parse.Parsers.FixLinkWhitespace(a.ArticleText, ArticleTitle), true);

            a.AWBChangeArticleText("Fix syntax", WikiFunctions.Parse.Parsers.FixSyntax(a.ArticleText), true, true);

            a.AWBChangeArticleText("Rename Template Parameters", WikiFunctions.Parse.Parsers.RenameTemplateParameters(a.ArticleText, WikiRegexes.RenamedTemplateParameters), true);
            
            a.EmboldenTitles(parsers, false);

            a.AWBChangeArticleText("Conversions", WikiFunctions.Parse.Parsers.Conversions(a.ArticleText), true);
            a.AWBChangeArticleText("FixLivingThingsRelatedDates", WikiFunctions.Parse.Parsers.FixLivingThingsRelatedDates(a.ArticleText), true);
            a.AWBChangeArticleText("FixHeadings", WikiFunctions.Parse.Parsers.FixHeadings(a.ArticleText, ArticleTitle), true);

            a.FixPeopleCategories(parsers, false);

            a.SetDefaultSort(Variables.LangCode, false, false);

            a.AWBChangeArticleText("Fix categories", WikiFunctions.Parse.Parsers.FixCategories(a.ArticleText), true);

            a.AWBChangeArticleText("Fix images", WikiFunctions.Parse.Parsers.FixImages(a.ArticleText), true);

            a.BulletExternalLinks(false);

            a.CiteTemplateDates(parsers, false);

            a.AWBChangeArticleText("Fix citation templates", WikiFunctions.Parse.Parsers.FixCitationTemplates(a.ArticleText), true, true);

            a.AWBChangeArticleText("Fix temperatures", WikiFunctions.Parse.Parsers.FixTemperatures(a.ArticleText), true);

            a.AWBChangeArticleText("Fix main article", WikiFunctions.Parse.Parsers.FixMainArticle(a.ArticleText), true);

            if(a.IsMissingReferencesDisplay && !Variables.LangCode.Equals("de"))
                a.AWBChangeArticleText("Fix reference tags", WikiFunctions.Parse.Parsers.FixReferenceListTags(a.ArticleText), true);

            a.AWBChangeArticleText("Fix empty links and templates", WikiFunctions.Parse.Parsers.FixEmptyLinksAndTemplates(a.ArticleText), true);

            a.AWBChangeArticleText("Fix empty references", WikiFunctions.Parse.Parsers.SimplifyReferenceTags(a.ArticleText), true);

            a.AWBChangeArticleText("DuplicateUnnamedReferences", WikiFunctions.Parse.Parsers.DuplicateUnnamedReferences(a.ArticleText), true);

            a.AWBChangeArticleText("DuplicateNamedReferences", WikiFunctions.Parse.Parsers.DuplicateNamedReferences(a.ArticleText), true);

            a.AWBChangeArticleText("SameRefDifferentName", WikiFunctions.Parse.Parsers.SameRefDifferentName(a.ArticleText), true);
            
            a.AWBChangeArticleText("Refs after punctuation", WikiFunctions.Parse.Parsers.RefsAfterPunctuation(a.ArticleText), true);
            
            if(!Variables.IsWikipediaEN)
                a.AWBChangeArticleText("ReorderReferences", WikiFunctions.Parse.Parsers.ReorderReferences(a.ArticleText), true);

            a.AWBChangeArticleText("FixReferenceTags", WikiFunctions.Parse.Parsers.FixReferenceTags(a.ArticleText), true);

            a.AWBChangeArticleText("Add missing {{reflist}}", WikiFunctions.Parse.Parsers.AddMissingReflist(a.ArticleText), true, true);

            a.AWBChangeArticleText("PersonData", WikiFunctions.Parse.Parsers.PersonData(a.ArticleText, ArticleTitle), true);

            a.FixLinks(false);

            a.AWBChangeArticleText("Simplify links", WikiFunctions.Parse.Parsers.SimplifyLinks(a.ArticleText), true);
        }

        a.UnHideText(removeText);

        a.AWBChangeArticleText("Sort meta data", parsers.SortMetaData(a.ArticleText, ArticleTitle), true);

        return a.ArticleText;
}

Use HideMore function to hide templates, images and text in quotes

[edit]
//...
	WikiFunctions.Parse.HideText htmore = new WikiFunctions.Parse.HideText();

	ArticleText = htmore.HideMore(ArticleText);

	// make your changes to ArticleText here

	ArticleText = htmore.AddBackMore(ArticleText);
//...

Getting hold of the status of tagger, RETF etc.

[edit]
//...
bool TyposEnabled = awb.RegexTypoFix.Checked;
bool TaggerEnabled = awb.AutoTagCheckBox.Checked; 
//...

Skip if no Persondata changes

[edit]
WikiFunctions.Parse.Parsers parser = new WikiFunctions.Parse.Parsers();
WikiFunctions.Parse.HideText removeText = new WikiFunctions.Parse.HideText(false, true, false);
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = " ";
   
string old = ArticleText;
Article a = new Article(ArticleTitle, ArticleText);
 
 a.HideText(removeText);
 
 a.AWBChangeArticleText("Persondata", WikiFunctions.Parse.Parsers.PersonData(a.ArticleText, ArticleTitle), true);
            
a.UnHideText(removeText);
if(old.Equals(a.ArticleText))
Skip = true;
 
 return a.ArticleText;
        }

Apply a regular expression replacement only n times

[edit]
    public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            // define your regular expression
	    Regex R1 = new Regex(@"foo");
            // define how many times to replace the regular expression
            int n = 1;
            
            Skip = false;
            Summary = " ";
 
            // run the replace
	    ArticleText = R1.Replace(ArticleText, "bar", n);

            return ArticleText;
        }

Read template parameters from Infobox and write to text file

[edit]
private static readonly Regex InfoboxUKplace = Tools.NestedTemplateRegex(new List<string>("infobox UK place".Split(',')));
public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
{
          string country="", static_image="", static_image_caption="";
          string latitude="", longitude="", official_name="";
          string population="", population_ref="", civil_parish="";
          string unitary_england="", lieutenancy_england="", region="";
          string constituency_westminster="", post_town="", postcode_district="";
          string dial_code="", os_grid_reference="";

          Skip = true;
          Summary = "";

          foreach(Match m in InfoboxUKplace.Matches(ArticleText))
          {
                string InfoboxUKplaceCall = m.Value;
                country = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"country");
                static_image = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"static_image");
                static_image_caption = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"static_image_caption");
                latitude = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"latitude");
                longitude = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"longitude");
                official_name = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"official_name");
                population = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"population");
                population_ref = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"population_ref");
                civil_parish = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"civil_parish");
                unitary_england = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"unitary_england");
                lieutenancy_england = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"lieutenancy_england");
                region = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"region");
                constituency_westminster = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"constituency_westminster");
                post_town = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"post_town");
                postcode_district = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"postcode_district");
                dial_code = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"dial_code");
                os_grid_reference = WikiFunctions.Tools.GetTemplateParameterValue(InfoboxUKplaceCall,"os_grid_reference");
          }

          System.IO.StreamWriter sw = System.IO.File.AppendText("C:/Projects/Infodata.txt");
          sw.WriteLine(ArticleTitle + "~" + country + "~" + static_image + "~" + static_image_caption + "~" + latitude + "~" + longitude + "~" + official_name 
          + "~" + population + "~" + population_ref + "~" + civil_parish + "~" + unitary_england + "~" + lieutenancy_england + "~" + region + "~" + constituency_westminster
          + "~" + post_town + "~" + postcode_district + "~" + dial_code + "~" + os_grid_reference);
          sw.Close();
          return ArticleText;
}

Remove a parameter from a template

[edit]
	public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
	{
		Skip = false;
        Summary = "";
        
        string templateName = "infobox example";
        
        ArticleText = Tools.NestedTemplateRegex(templateName).Replace(ArticleText, m => parameterRemoval(m.Value));
       	return ArticleText;
	}
    public static string parameterRemoval(string templateCall)
	{
		// Set up parameters to remove
		List<string> paramsToRemove = new List<string>();
		string[] array2 = new string[]{"example1", "deprecatedParam", "etc"};
		paramsToRemove.AddRange(array2);
		foreach(string param in paramsToRemove)
		{
			templateCall = WikiFunctions.Tools.RemoveTemplateParameter(templateCall, param, false);
		}
		return templateCall;
	}

See also

[edit]