User:BrownHairedGirl/AddBUbanner/Module

// AddBuBanner v1.02, 14 June 2022 public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip) {	const int MinimumIfAllBareRefsInlineTagged = 7; const int MinimumIfSomeBareRefsUntagged = 5; bool debugging = false; Skip = false; Summary = ""; int myBUCounter = 0; int myTaggedBUCounter = 0; int myUntaggedBUCounter = 0; int mySkipCounter = 0; string nuArticleText = ArticleText; // Key regexes string CleanupBareURLsTagMatcher = @"\s*\{\{ *([tT]emplate *: *)?([Cc]leanup[_ ]+bare[_ ]+URLs|[Bb]are[_ ]+|[Bb]are|[Bb]are[_ ]+link|[Bb]are[_ ]+linkname|[Bb]are[_ ]+links|[Bb]are[_ ]+references|[Bb]are[_ ]+refs|[Bb]are[_ ]+URL|[Bb]are[_ ]+[uU][rR][lL]s|[Bb]are-URLs|[Bb]arelinks|[Bb]areURL|[Bb]areURLs|[Cc]leanup[_ ]+bare-URLs|[Cc]leanup[_ ]+link[_ ]+rot|[Cc]leanup[_ ]+link-rot|[Cc]leanup-Bare[_ ]+URLs|[Cc]leanup-barelinks|[Cc]leanup-link[_ ]+rot|[Cc]leanup-link-rot|[Cc]leanup-linkrot|[Cc]UBURL|[Ll]ink[_ ]*rot|[Ll]INKROT|[Ll]R) *(\|[^\}]*)?\}\}"; string findBareUrlRegex = @"(?<=]*>)\s*\[?\s*(? https?://[^ \]\[\{\}\|<>]+)\s*\]?[\.,]?\s*(? (\{\{\s*Bare(-|\s*)URL(-|\s*)([a-z]+\s*)?(\|[^\}\{]*)?\}\})?)\s*(?=\s*<\s*/\s*ref\s*>)"; // first, strip out any HTML comments nuArticleText = Regex.Replace(nuArticleText, @").)*-->", "", RegexOptions.Singleline); // STEP 1. check whether the page already contains the banner template, or one of its many aliases. // If so, skip the page MatchCollection CleanupBareURLsTagmatches = Regex.Matches(nuArticleText, CleanupBareURLsTagMatcher, RegexOptions.Singleline); if (CleanupBareURLsTagmatches.Count > 0) { // We already have tag(s), so skip this page if (debugging) { Skip = false; Summary = "debugging"; return "Page already contains a CleanupBareURLsTagMatcher tag" + "\n--\n\n" + ArticleText; }	   Skip = true; return ArticleText; }	nuArticleText = Regex.Replace(nuArticleText, findBareUrlRegex, delegate(Match match)	{		++myBUCounter;		string myURL = match.Groups["url"].Value;		string myBUtag = match.Groups["butag"].Value;		string nuref = myURL;		if (String.IsNullOrEmpty(myBUtag)) {			++myUntaggedBUCounter;		}		else {			++myTaggedBUCounter;			nuref = nuref + " " + myBUtag;		}		/* 		// ref cleanup disabled. Not viable when we have stripped comments		return nuref;		*/		return ""; // OK to strip it, 'cos we are working on a copy of the text, which wil be discarded	}, (RegexOptions.IgnoreCase | RegexOptions.Singleline)); // Now calculate whether we tag the article bool addBanner = false; if (myBUCounter >= MinimumIfAllBareRefsInlineTagged) { addBanner = true; }	else if ((myBUCounter >= MinimumIfSomeBareRefsUntagged) && (myUntaggedBUCounter > 0)) { addBanner = true; }	if (!addBanner) { if (debugging) { Summary = "Debugging"; string retnote = "Total bare URLs = " + myBUCounter + "\nTagged = " + myTaggedBUCounter + "\nUntagged = " + myUntaggedBUCounter + "\n================================================================\n\n"; return retnote + ArticleText; }		Skip = true; return ArticleText; }	Summary = "Add banner . "; Summary = Summary + "This article has " + myBUCounter + " bare URLs: " + myTaggedBUCounter + " inline-tagged, " + myUntaggedBUCounter + " untagged. More info at User:BrownHairedGirl/AddBUbanner"; string returnedArticleText = "\n" + ArticleText; // Some minor fixes returnedArticleText = Regex.Replace(returnedArticleText, " ", " "); returnedArticleText = Regex.Replace(returnedArticleText, " ", " "); return returnedArticleText; }