//TODO:Needs re-write
/// <summary>
/// If necessary, adds/removes wikify or stub tag
/// </summary>
/// <param name="articleText">The wiki text of the article.</param>
/// <param name="articleTitle">The article title.</param>
/// <param name="restrictOrphanTagging"></param>
/// <param name="summary"></param>
/// <returns>The tagged article.</returns>
public string Tagger(string articleText, string articleTitle, bool restrictOrphanTagging, ref string summary)
{
// don't tag redirects/outside article namespace/no tagging changes
if (!Namespace.IsMainSpace(articleTitle) || Tools.IsRedirect(articleText) || WikiRegexes.Wi.IsMatch(articleText))
return articleText;
tagsRemoved.Clear();
tagsAdded.Clear();
string commentsStripped = WikiRegexes.Comments.Replace(articleText, "");
string commentsCategoriesStripped = WikiRegexes.Category.Replace(commentsStripped, "");
Sorter.Interwikis(ref commentsStripped);
// bulleted or indented text should weigh less than simple text.
// for example, actor stubs may contain large filmographies
string crapStripped = WikiRegexes.BulletedText.Replace(commentsCategoriesStripped, "");
int words = (Tools.WordCount(commentsCategoriesStripped) + Tools.WordCount(crapStripped))/2;
// remove stub tags from long articles
if ((words > StubMaxWordCount) && WikiRegexes.Stub.IsMatch(commentsStripped))
{
articleText = WikiRegexes.Stub.Replace(articleText, StubChecker).Trim();
tagsRemoved.Add("stub");
}
// refresh
commentsStripped = WikiRegexes.Comments.Replace(articleText, "");
commentsCategoriesStripped = WikiRegexes.Category.Replace(commentsStripped, "");
// on en wiki, remove expand template when a stub template exists
// http://en.wikipedia.org/wiki/Wikipedia_talk:AutoWikiBrowser/Feature_requests/Archive_5#Remove_.7B.7Bexpand.7D.7D_when_a_stub_template_exists
if (Variables.LangCode == "en" && WikiRegexes.Stub.IsMatch(commentsCategoriesStripped) &&
WikiRegexes.Expand.IsMatch(commentsCategoriesStripped))
{
articleText = WikiRegexes.Expand.Replace(articleText, "");
tagsRemoved.Add("expand");
}
// refresh
commentsStripped = WikiRegexes.Comments.Replace(articleText, "");
commentsCategoriesStripped = WikiRegexes.Category.Replace(commentsStripped, "");
// do orphan tagging before template analysis for categorisation tags
articleText = TagOrphans(articleText, articleTitle, restrictOrphanTagging);
articleText = TagRefsIbid(articleText);
articleText = TagEmptySection(articleText);
int totalCategories;
int linkCount = Tools.LinkCount(commentsStripped);
#if DEBUG || UNITTEST
if (Globals.UnitTestMode)
{
totalCategories = Globals.UnitTestIntValue;
}
else
#endif
{
// stubs add non-hidden stub categories, don't count these in categories count
List<Article> Cats = CategoryProv.MakeList(new[] {articleTitle});
List<Article> CatsNotStubs = new List<Article>();
foreach (Article a in Cats)
{
if (!a.Name.EndsWith(" stubs") && !a.Name.EndsWith(":Stubs"))
CatsNotStubs.Add(a);
}
totalCategories = CatsNotStubs.Count;
}
if (linkCount > 0 && WikiRegexes.DeadEnd.IsMatch(articleText))
{
articleText = WikiRegexes.DeadEnd.Replace(articleText, new MatchEvaluator(SectionTagME));
if(!WikiRegexes.DeadEnd.IsMatch(articleText))
tagsRemoved.Add("deadend");
}
// discount persondata along with comments and categories from wikify and stub evaluation
int length = WikiRegexes.Persondata.Replace(commentsCategoriesStripped, "").Length + 1;
bool underlinked = (linkCount < 0.0025*length);
if (length <= 300 && !WikiRegexes.Stub.IsMatch(commentsCategoriesStripped) &&
!WikiRegexes.Disambigs.IsMatch(commentsCategoriesStripped) && !WikiRegexes.SIAs.IsMatch(commentsCategoriesStripped))
{
// add stub tag
articleText += Tools.Newline("{{stub}}", 3);
tagsAdded.Add("stub");
commentsStripped = WikiRegexes.Comments.Replace(articleText, "");
}
// http://en.wikipedia.org/wiki/Wikipedia_talk:AutoWikiBrowser/Archive_19#AWB_problems
// nl wiki doesn't use {{Uncategorized}} template
// prevent wictionary redirects from being tagged as uncategorised
if (words > 6 && totalCategories == 0
&& !WikiRegexes.Uncat.IsMatch(articleText)
&& Variables.LangCode != "nl"
&& !Tools.NestedTemplateRegex("cat improve").IsMatch(articleText)
// category count is from API; don't add uncat tag if genfixes added person categories
&& !WikiRegexes.DeathsOrLivingCategory.IsMatch(articleText)
&& !WikiRegexes.BirthsCategory.IsMatch(articleText))
{
if (WikiRegexes.Stub.IsMatch(commentsStripped))
{
// add uncategorized stub tag
articleText += Tools.Newline("{{Uncategorized stub|", 2) + WikiRegexes.DateYearMonthParameter +
@"}}";
tagsAdded.Add("[[CAT:UNCATSTUBS|uncategorised]]");
}
else
{
// add uncategorized tag
articleText += Tools.Newline("{{Uncategorized|", 2) + WikiRegexes.DateYearMonthParameter + @"}}";
tagsAdded.Add("[[CAT:UNCAT|uncategorised]]");
}
}
// remove {{Uncategorized}} if > 0 real categories (stub categories not counted)
// rename {{Uncategorized}} to {{Uncategorized stub}} if stub with zero categories (stub categories not counted)
if (WikiRegexes.Uncat.IsMatch(articleText))
{
if (totalCategories > 0)
{
articleText = WikiRegexes.Uncat.Replace(articleText, "");
tagsRemoved.Add("uncategorised");
}
else if (totalCategories == 0 && WikiRegexes.Stub.IsMatch(commentsStripped))
{
string uncatname = WikiRegexes.Uncat.Match(articleText).Groups[1].Value;
if (!uncatname.Contains("stub"))
articleText = Tools.RenameTemplate(articleText, uncatname, "Uncategorized stub");
}
}
if (linkCount == 0 && !WikiRegexes.DeadEnd.IsMatch(articleText) && Variables.LangCode != "sv"
&& !Regex.IsMatch(WikiRegexes.MultipleIssues.Match(articleText).Value.ToLower(), @"\bdead ?end\b"))
{
// add dead-end tag
articleText = "{{dead end|" + WikiRegexes.DateYearMonthParameter + "}}\r\n\r\n" + articleText;
tagsAdded.Add("[[:Category:Dead-end pages|deadend]]");
}
if (linkCount < 3 && underlinked && !WikiRegexes.Wikify.IsMatch(articleText)
&& !WikiRegexes.MultipleIssues.Match(articleText).Value.ToLower().Contains("wikify"))
{
// add wikify tag
articleText = "{{Wikify|" + WikiRegexes.DateYearMonthParameter + "}}\r\n\r\n" + articleText;
tagsAdded.Add("[[WP:WFY|wikify]]");
}
else if (linkCount > 3 && !underlinked &&
WikiRegexes.Wikify.IsMatch(articleText))
{
articleText = WikiRegexes.Wikify.Replace(articleText, new MatchEvaluator(SectionTagME));
if(!WikiRegexes.Wikify.IsMatch(articleText))
tagsRemoved.Add("wikify");
}
// rename unreferenced --> refimprove if has existing refs
if (WikiRegexes.Unreferenced.IsMatch(commentsCategoriesStripped)
&& WikiRegexes.Refs.Matches(commentsCategoriesStripped).Count > 0)
{
articleText = Tools.RenameTemplate(articleText, "unreferenced", "refimprove", true);
Match m = WikiRegexes.MultipleIssues.Match(articleText);
if(m.Success)
{
string newValue = Tools.RenameTemplateParameter(m.Value, "unreferenced", "refimprove");
if(!newValue.Equals(m.Value))
articleText = articleText.Replace(m.Value, newValue);
}
}
if (tagsAdded.Count > 0 || tagsRemoved.Count > 0)
{
Parsers p = new Parsers();
HideText ht = new HideText();
articleText = ht.HideUnformatted(articleText);
articleText = p.MultipleIssues(articleText);
articleText = Conversions(articleText);
articleText = ht.AddBackUnformatted(articleText);
// sort again in case tag removal requires whitespace cleanup
articleText = p.Sorter.Sort(articleText, articleTitle);
}
summary = PrepareTaggerEditSummary();
return articleText;
}