Jump to content

User:Tom.Bot/Task5 code

From Wikipedia, the free encyclopedia

Source

[edit]
public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
{
	Skip = false;
	Summary = "[[Wikipedia:Bots/Requests for approval/Tom.Bot 5|Task 5]]: +{{WikiProject Olympics|class=<inherit>|importance=Low}}; [[WP:GenFixes]] on,";
//	Summary = "+{{WikiProject Olympics|class=<inherit>|importance=Low}}; [[WP:GenFixes]] on,";
	
	// skip conditions
	
	bool NotTalk = !Regex.IsMatch(ArticleTitle, @"^Talk:");
	if (NotTalk)
	{
		Summary = "Convert to talks!";
		Skip = true;
	}
	
	bool ListArticle = Regex.IsMatch(ArticleTitle, @"^Talk:List of");
	if (ListArticle)
	{
		Summary = "List article; do manually";
		Skip = true;
	}
	
	string WP_Olympics_Regex = @"\{\{\s*(?:[Oo]lympicsWikiProject|[Ww]ikiProjectOlympics|[Ww]ikiProject[ _]+Olympics|[Ww]POlympics|WPOLY|[Ww]P[ _]+Olympics)(?=\s*[\|\}]|\s*\<\!--)";
	bool HasWP_Olympics = Regex.IsMatch(ArticleText, WP_Olympics_Regex, RegexOptions.IgnoreCase);
	if (HasWP_Olympics)
	{
		Summary = "Already has {{WP Olympics}}";
		Skip = true;
	}
	
	string WP_Bio_Regex = @"\{\{\s*(?:[Bb]io|[Ww]ikiProject[ _]+Biographies|[Ww]ikiProject[ _]+Biography|[Ww]ikiProject[ _]+biography|[Ww]ikiproject[ _]+Biography|[Ww]ikiproject[ _]+biography|[Ww]ikiProject[ _]+Bio|WPBIOGRAPHY|[Ww]PBiography|[Ww]Pbiography|WPBIO|[Ww]PBio|[Ww]pbio|[Ww]P[ _]+Biography|[Ww]P[ _]+biography|[Ww]P[ _]+Bio)(?=\s*[\|\}]|\s*\<\!--)";
	bool HasWP_Bio = Regex.IsMatch(ArticleText, WP_Bio_Regex, RegexOptions.IgnoreCase);
	if (!HasWP_Bio)
	{
		Summary = "Doesn't have {{WP Bio}}";
		Skip = true;
	}
	
	if (!Skip)
	{
		// standardize {{WP Bio}}
		ArticleText = Regex.Replace(ArticleText, WP_Bio_Regex, "{{WikiProject Biography", RegexOptions.IgnoreCase);
		
		string WP_BioAll_Regex = @"\{\{WikiProject Biography\s*([^\{\}]*(\}\})?)";
		string WP_BioAll = Regex.Match(ArticleText, WP_BioAll_Regex).Groups[1].Value;
		
		string WP_BioClassAll_Regex   = @"(\s*\|\s*)class(\s*=\s*)(List|FL|Stub|Start|C|B|GA|A|FA)\b(?=(\s*)\|)?"; // 4 grps
		Match mClassAll    = Regex.Match(WP_BioAll, WP_BioClassAll_Regex, RegexOptions.IgnoreCase);
		string WP_BioClass = mClassAll.Groups[3].Value;
		string Pipe   = mClassAll.Groups[1].Value;
				 Pipe   = (string.IsNullOrEmpty(Pipe) || Regex.IsMatch(Pipe, @"[\r\n]")) ? "|" : mClassAll.Groups[1].Value; // i.e. Talk:Loredana Auletta
		string Eq     = mClassAll.Groups[2].Value;
				 Eq     = (string.IsNullOrEmpty(Eq) || Regex.IsMatch(Eq, @"[\r\n]")) ? "=" : mClassAll.Groups[2].Value;
		string Class  = (string.IsNullOrEmpty(WP_BioClass)) ? "Stub" : WP_BioClass; // default to Stub per botreq
		string Sep    = mClassAll.Groups[4].Value; // add ws if needed, but not too much (Talk:Agneta Engström, Talk:Estha Essombe, Talk:Annalisa Turci, Talk:Nicklas Holm)
				 Sep    = (Regex.IsMatch(Pipe, @"\s+\|") || Regex.IsMatch(Sep, @"[\r\n]")) ? "" : Sep; // avoided problems on Talk:Leslie Malerich
		string Import = (Regex.IsMatch(Class, @"^[A-Z]")) ? "Low" : "low"; // match case; hard-code [Ll]ow per botreq
		// good overall ex: Talk:Jeanne Vaussard, Talk:Alessandra Gorla, Talk:Peter Attrill
		
		string WP_BioEnd = Regex.Match(WP_BioAll, @"(\s*\}\})$",  RegexOptions.IgnoreCase).Groups[1].Value;
		string End = (Regex.IsMatch(WP_BioEnd, @"[\r\n]")) ? "}}" : WP_BioEnd; // some WP_Bios like to end on a NL (Talk:Eddie Dix)
		
		// place after {{WP Bio}}
		string WP_Olympics = @"{{WikiProject Olympics" + Pipe + "class" + Eq + Class + Sep + Pipe + "importance" + Eq + Import + End;
		string PutAfterBio_Regex = @"\{\{WikiProject Biography\s*([^\}]*\}\})";
		bool PutAfterBio = Regex.IsMatch(ArticleText, PutAfterBio_Regex, RegexOptions.IgnoreCase);
		if (PutAfterBio)
		{
			ArticleText = Regex.Replace(ArticleText, PutAfterBio_Regex, @"$0" + "\n" + WP_Olympics, RegexOptions.IgnoreCase);
			if (string.IsNullOrEmpty(WP_BioClass)) Summary = Summary.Replace("<inherit>", Class); // to track via edit summary: inherited or default (Stub)
			Summary = Summary.Replace("Low", Import);
		}
		else
		{
			Summary = "{{WP Bio}} error; do manually";
			Skip = true;
		}
	}
	
	return ArticleText;
}