User:Yobot/Task 17

From Wikipedia, the free encyclopedia
// Fixes priority and listas parameter in WikiProject Biography

private static readonly Regex WPBiography = Tools.NestedTemplateRegex(new List<string>("WPBiography,WikiProject Biography,Wikiproject Biography,WP Biography,WPBIO".Split(',')));
private static readonly Regex WorkGroup = new Regex(@"^([\w&]+)\-work\-group\s*=\s*", RegexOptions.Compiled);
public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
{
	// WPBiography priority fixes
	Skip = true;
	Summary = "Set WPBiography work group priorities: ";

	// process each WPBiography
	foreach(Match m in WPBiography.Matches(ArticleText))
	{
		string WPBiographyCall = m.Value, newValue = m.Value;

		// rename old importance field
		newValue = Tools.RenameTemplateParameter(newValue, "importance", "priority");

		string priority = Tools.GetTemplateParameterValue(newValue, "priority");
		string listas = Tools.GetTemplateParameterValue(newValue, "listas");
		//bool removePriority = false;
		bool addListas = false;


		//if(priority.Length == 0)
		//continue;

		int WPBiographyArgs = Tools.GetTemplateArgumentCount(WPBiographyCall);

		// process each parameter for work groups
		for(int a = 1; a <= WPBiographyArgs; a++)
		{
			string param = Tools.GetTemplateArgument(WPBiographyCall, a);
			param = WikiRegexes.Comments.Replace(param, "").Trim();

			if(WorkGroup.IsMatch(param))
			{
				string GroupName = WorkGroup.Match(param).Groups[1].Value;

				// set work group priority if not already set
				if(Tools.GetTemplateParameterValue(newValue, GroupName + "-work-group").Length > 0 &&
					Tools.GetTemplateParameterValue(newValue, GroupName + @"-priority").Length == 0)
				{
					if(Tools.GetTemplateParameterValue(newValue, "priority").Length > 0)
					{
						newValue = Tools.RenameTemplateParameter(newValue, "priority", GroupName + @"-priority");
					}
					else
					{
						newValue = Tools.SetTemplateParameterValue(newValue, GroupName + @"-priority", priority);
					}

					Skip = false;
					Summary += GroupName + @", ";
				}

				// remove empty work groups
				if(Tools.GetTemplateParameterValue(newValue, GroupName + "-work-group").Length == 0)
				{
					newValue = Tools.RemoveTemplateParameter(newValue, GroupName + "-work-group");
				}

				// remove empty priority
				if(Tools.GetTemplateParameterValue(newValue, "priority").Length == 0)
				{
					newValue = Tools.RemoveTemplateParameter(newValue, "priority");
				}
			}

			// add listas if missing
			if(listas.Length == 0)
			{
				string newListas = Tools.MakeHumanCatKey(ArticleTitle.Replace("Talk:", ""), ArticleText);
				listas = newListas;
				newValue = Tools.SetTemplateParameterValue(newValue, "listas", newListas);
				addListas = true;
				Skip = false;
			}

			// move listas to the bottom
			string ListasValue = Tools.GetTemplateParameterValue(newValue,"listas");
            		newValue =  Tools.RemoveTemplateParameter(newValue,"listas");
			newValue = Tools.SetTemplateParameterValue(newValue,"listas",ListasValue);

			// trim whitespace
			newValue=newValue.Replace(" |","|");
			newValue=newValue.Replace("| ","|");
		}

		// merge changes
		if(!m.Value.Equals(newValue))
		{
			ArticleText = ArticleText.Replace(m.Value, newValue);
			if(addListas)
			{
				Summary += "added listas";
			}
		}
	}
	return ArticleText;
}