Jump to content

Wikipedia:Bots/Requests for approval/BHGbot 8/BHGbot-8-AWB-module

From Wikipedia, the free encyclopedia
// AWB custom module to replace {{tl|Bare URL inline}} with {{tl|Cleanup bare URLs}}
// v 1.00, 3 August 2021
// -- BHG

public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
{
    Skip = false;
    Summary = "[[WP:BHGbot 8]]: ";

    string nuArticleText = "";
	int bareURLinlineTagCount = 0;
	int bareURLinlineTagRemovedCount = 0;

	string bareURLinlineTagMatcher = @"\s*\{\{ *([tT]emplate *: *)?([Bb]are[_ ]+URL[\- ]inline|[Ll]inkrot-inline|[Bb]are-inline|[Bb]are[_ ]+inline|[Bb]are[_ ]+url[_ ]+inline|[Bb]are-url[_ ]+inline|[Bb]are[_ ]+link[_ ]+inline|[Bb]are-link-inline|[Bb]are-url-inline|[Bb]are[_ ]+url) *(\|[^\}]*)?\}\}";

	// first, check if we have any {{Bare URL inline}} tags
	MatchCollection bareURLinlineTagmatches = Regex.Matches(ArticleText, bareURLinlineTagMatcher, RegexOptions.Singleline);
	bareURLinlineTagCount = bareURLinlineTagmatches.Count;
	if (bareURLinlineTagCount == 0) {
		// No {{Bare URL inline}} tags, so skip this page
	    Skip = false;
		return "no matches"; // ArticleText;
	} 
	
	// So we have {{Bare URL inline}} tags
	// Now remove them all
	nuArticleText = Regex.Replace(ArticleText, bareURLinlineTagMatcher, "", RegexOptions.Singleline);
	MatchCollection remainingbareURLinlineTagmatches = Regex.Matches(nuArticleText, bareURLinlineTagMatcher, RegexOptions.Singleline);
	bareURLinlineTagRemovedCount = bareURLinlineTagCount - remainingbareURLinlineTagmatches.Count;
	Summary = Summary + "removed " + bareURLinlineTagRemovedCount + " of " + bareURLinlineTagCount + " {{[[Template:Bare URL inline|Template:Bare URL inline]]}}. ";
	
	// Now see if we have an existing {{Cleanup bare URLs}} tag
	
	string cleanupBareURLsTagMatcher = @"(?<templatename>\{\{ *([tT]emplate *: *)?([Cc]leanup[_ ]+bare[_ ]+URLs|[Ll]inkrot|[Bb]are[_ ]+URLs|[Ll]ink[_ ]+rot|[Bb]are[_ ]+links|[Bb]arelinks|[Bb]areurls|[Bb]are[_ ]+urls|[Cc]leanup-linkrot|[Bb]are[_ ]+URL|[Bb]areURLs|[Bb]are-URLs|[Cc]leanup[_ ]+link-rot|[Cc]leanup[_ ]+link[_ ]+rot|[Bb]are[_ ]+refs|[Bb]are[_ ]+references|[Bb]areurl|[Bb]areURL|[Bb]are|[Cc]leanup-link-rot|[Cc]leanup-link[_ ]+rot|[Bb]are[_ ]+linkname|[Cc]leanup-Bare[_ ]+URLs|[Cc]leanup-barelinks|[Bb]are[_ ]+link|[Ll]R|[Ll]r|[Ll]INKROT|[Cc]leanup-bare[_ ]+URLs|[Cc]leanup[_ ]+bare-URLs|[Cc]UBURL\s*))(?<params>(\|[^\}]*)?\}\})";

	MatchCollection cleanupBareURLsTagMatches = Regex.Matches(ArticleText, cleanupBareURLsTagMatcher, RegexOptions.Singleline);
	if (cleanupBareURLsTagMatches.Count == 0) {
		// No existing {{Cleanup bare URLs}} tag, so add one
		nuArticleText = "{{Cleanup bare URLs|date={{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}}}\n" + nuArticleText;
		Summary = Summary + "Added {{[[Template:Cleanup bare URLs|Cleanup bare URLs]]}}";
	}
	else {
		// We have an existing {{Cleanup bare URLs}} tag
		Summary = Summary + "Already tagged with {{[[Template:Cleanup bare URLs|Cleanup bare URLs]]}}";
		nuArticleText = Regex.Replace(ArticleText, cleanupBareURLsTagMatcher, "{{Cleanup bare URLs${params}");
	}

    return nuArticleText;
}