Jump to content

Wikipedia:Bots/Requests for approval/BHGbot 6/AWB module

From Wikipedia, the free encyclopedia
// AWB module used by BHGbot 6
// v1.01, 29 May 2020
// 
// Yes, the code is ugly and crude.  But it's good enough to do the job
// Changes: v1.01 adds some safety checks

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

    if (wikiNamespace != 0) {
		// This page is not in article space, so skip it
	    Skip = true;
		return ArticleText;
	}

	
	string nuText = ArticleText;
	string fixedText = "";
	
	string escapedArticleTitle = Regex.Escape(ArticleTitle);
	
	string startEponCatPattern = @"\[\[Category[_ ]*:[_ ]*" + escapedArticleTitle;
	string startEponCatText = "[[Category:" + ArticleTitle;
	string fullEponCatFixedText = startEponCatText + "| ]]";
	string fullEponCatPattern = startEponCatPattern + @"[_ ]*(\|[^\]]*)?\]\]";
	
    Match fullEponCatMatch = Regex.Match(ArticleText, fullEponCatPattern);
    if (!fullEponCatMatch.Success) {
		// We haven't found an eponymous category, so skip this article
	    Skip = true;
		return ArticleText;
	}
	
	string foundCat = fullEponCatMatch.Value;
	
	string catBeforeEponCatPattern = @"(?<othercat>\[\[Category:[^\]]*\]\].*)(?<eponcat>" + Regex.Escape(foundCat) + @")";

	fixedText = Regex.Replace(nuText, catBeforeEponCatPattern, "${eponcat}\n${othercat}", RegexOptions.Singleline | RegexOptions.ExplicitCapture);
	if (fixedText != nuText) {
		Summary = Summary + "eponymous category first, per [[MOS:CATORDER]]; ";
		nuText = fixedText;
	}

	fixedText = Regex.Replace(nuText, fullEponCatPattern, fullEponCatFixedText);
	
	
	if (fixedText != nuText) {
		Summary = Summary + "fixed sort key; ";
		nuText = fixedText;
	}

	if (nuText == "") {
		// This is a safety check.  Don't return a blank page
	    Skip = true;
		return ArticleText;
	}

	if (nuText != ArticleText) {
		Summary = Summary + "[[WP:GENFIXES]]";
		return nuText;
	}
	
	// If we get here, there was no replacement, so skip
    Skip = true;
	return ArticleText;

}