WikiCrawler/Tasks/Commons/CommonsCreatorFromWikidata.cs
/// <summary>
/// Fixes formatting issues with Information/Artwork/Book templates.
/// </summary>
/// <param name="article">The already-downloaded article.</param>
Fixes formatting issues with Information/Artwork/Book templates.
Fixes formatting issues with Information / Artwork / Book templates .
public static void FixInformationTemplates(Article article, HashSet<string> removeEmptyParams = null)
		{
			if (MediaWiki.Article.IsNullOrEmpty(article))
			{
				Console.WriteLine("FixInformationTemplates: FATAL: article missing");
				return;
			}
			string text = article.revisions[0].text;
			bool mainTemplate = false;
			bool artTemplate = false;
			bool artTemplateHasFilledAuthor = false;
			bool nowiki = false;
			bool comment = false;
			Stack<string> openTemplates = new Stack<string>();
			int templateStartIndex = -1;
			int paramNameStartIndex = -1;
			bool hasError = false;
			for (int c = 0; c < text.Length - 1; c++)
			{
				if (StringAt(text, "<!--", c))
				{
					comment = true;
				}
				else if (StringAt(text, "-->", c))
				{
					comment = false;
				}
				if (!comment)
				{
					if (StringAt(text, "<nowiki>", c))
					{
						nowiki = true;
					}
					else if (StringAt(text, "</nowiki>", c))
					{
						nowiki = false;
					}
					if (!nowiki)
					{
						if (StringAt(text, "[[", c))
						{
							templateStartIndex = c;
							paramNameStartIndex = -1;
							c += 1;
						}
						else if (StringAt(text, "]]", c))
						{
							if (templateStartIndex >= 0)
							{
								templateStartIndex = -1;
							}
							else
							{
								if (openTemplates.Count == 0)
								{
									hasError = true;
								}
								else
								{
									openTemplates.Pop();
								}
								mainTemplate = openTemplates.Count > 0 && IsMainTemplate(openTemplates.Peek());
								artTemplate = openTemplates.Count > 0 && IsArtTemplate(openTemplates.Peek());
							}
							paramNameStartIndex = -1;
							c += 1;
						}
						else if (StringAt(text, "{{", c))
						{
							templateStartIndex = c;
							paramNameStartIndex = -1;
							c += 1;
						}
						else if (StringAt(text, "}}", c))
						{
							if (templateStartIndex >= 0)
							{
								templateStartIndex = -1;
							}
							else
							{
								if (openTemplates.Count == 0)
								{
									hasError = true;
								}
								else
								{
									openTemplates.Pop();
								}
								mainTemplate = openTemplates.Count > 0 && IsMainTemplate(openTemplates.Peek());
								artTemplate = openTemplates.Count > 0 && IsArtTemplate(openTemplates.Peek());
							}
							paramNameStartIndex = -1;
							c += 1;
						}
						if (templateStartIndex >= 0 && text[c] == '|')
						{
							openTemplates.Push(text.Substring(templateStartIndex, c - templateStartIndex).Trim());
							mainTemplate = openTemplates.Count > 0 && IsMainTemplate(openTemplates.Peek());
							artTemplate = openTemplates.Count > 0 && IsArtTemplate(openTemplates.Peek());
							templateStartIndex = -1;
						}
						else if (mainTemplate && text[c] == '|')
						{
							bool alreadyHasNewline = false;
							int backtrack = c - 1;
							for (; backtrack >= 0; backtrack--)
							{
								if (text[backtrack] == '\n')
								{
									alreadyHasNewline = true;
									break;
								}
								else if (!char.IsWhiteSpace(text[backtrack]))
								{
									backtrack++;
									break;
								}
							}
							if (!alreadyHasNewline)
							{
								text = text.Substring(0, backtrack) + "\n" + text.Substring(backtrack, text.Length - backtrack);
								c++;
							}
							paramNameStartIndex = c + 1;
						}
						if (text[c] == '=' && paramNameStartIndex >= 0)
						{
							string paramName = text.Substring(paramNameStartIndex, c - paramNameStartIndex).Trim();
							for (int lookahead = c + 1; lookahead < text.Length; lookahead++)
							{
								if (!char.IsWhiteSpace(text[lookahead]))
								{
									if (text[lookahead] == '|' || StringAt(text, "}}", lookahead))
									{
										if (removeEmptyParams != null && removeEmptyParams.Contains(paramName))
										{
											text = text.Substring(0, paramNameStartIndex - 1) + text.Substring(lookahead, text.Length - lookahead);
											c = paramNameStartIndex - 1;
										}
									}
									else
									{
										if (paramName == "author" || paramName == "artist")
										{
											artTemplateHasFilledAuthor = true;
										}
									}
									break;
								}
							}
							paramNameStartIndex = -1;
						}
					}
				}
			}
			if (hasError)
			{
				text = MediaWiki.WikiUtils.AddCategory("Category:Pages with mismatched parentheses", text);
			}
			else if (artTemplateHasFilledAuthor && removeEmptyParams == null)
			{
				FixInformationTemplates(article, artistOrAuthor);
			}
			if (text != article.revisions[0].text)
			{
				article.revisions[0].text = text;
				article.Dirty = true;
			}
		}
public static void FixInformationTemplates ( Article article , HashSet < string > removeEmptyParams = null ) { if ( MediaWiki . Article . IsNullOrEmpty ( article ) ) { Console . WriteLine ( " " ) ; return ; } string text = article . revisions [ 0 ] . text ; bool mainTemplate = false ; bool artTemplate = false ; bool artTemplateHasFilledAuthor = false ; bool nowiki = false ; bool comment = false ; Stack < string > openTemplates = new Stack < string > ( ) ; int templateStartIndex = - 1 ; int paramNameStartIndex = - 1 ; bool hasError = false ; for ( int c = 0 ; c < text . Length - 1 ; c ++ ) { if ( StringAt ( text , " " , c ) ) { comment = true ; } else if ( StringAt ( text , " " , c ) ) { comment = false ; } if ( ! comment ) { if ( StringAt ( text , " " , c ) ) { nowiki = true ; } else if ( StringAt ( text , " " , c ) ) { nowiki = false ; } if ( ! nowiki ) { if ( StringAt ( text , " " , c ) ) { templateStartIndex = c ; paramNameStartIndex = - 1 ; c += 1 ; } else if ( StringAt ( text , " " , c ) ) { if ( templateStartIndex >= 0 ) { templateStartIndex = - 1 ; } else { if ( openTemplates . Count == 0 ) { hasError = true ; } else { openTemplates . Pop ( ) ; } mainTemplate = openTemplates . Count > 0 && IsMainTemplate ( openTemplates . Peek ( ) ) ; artTemplate = openTemplates . Count > 0 && IsArtTemplate ( openTemplates . Peek ( ) ) ; } paramNameStartIndex = - 1 ; c += 1 ; } else if ( StringAt ( text , " " , c ) ) { templateStartIndex = c ; paramNameStartIndex = - 1 ; c += 1 ; } else if ( StringAt ( text , " " , c ) ) { if ( templateStartIndex >= 0 ) { templateStartIndex = - 1 ; } else { if ( openTemplates . Count == 0 ) { hasError = true ; } else { openTemplates . Pop ( ) ; } mainTemplate = openTemplates . Count > 0 && IsMainTemplate ( openTemplates . Peek ( ) ) ; artTemplate = openTemplates . Count > 0 && IsArtTemplate ( openTemplates . Peek ( ) ) ; } paramNameStartIndex = - 1 ; c += 1 ; } if ( templateStartIndex >= 0 && text [ c ] == ' ' ) { openTemplates . Push ( text . Substring ( templateStartIndex , c - templateStartIndex ) . Trim ( ) ) ; mainTemplate = openTemplates . Count > 0 && IsMainTemplate ( openTemplates . Peek ( ) ) ; artTemplate = openTemplates . Count > 0 && IsArtTemplate ( openTemplates . Peek ( ) ) ; templateStartIndex = - 1 ; } else if ( mainTemplate && text [ c ] == ' ' ) { bool alreadyHasNewline = false ; int backtrack = c - 1 ; for ( ; backtrack >= 0 ; backtrack -- ) { if ( text [ backtrack ] == ' \n ' ) { alreadyHasNewline = true ; break ; } else if ( ! char . IsWhiteSpace ( text [ backtrack ] ) ) { backtrack ++ ; break ; } } if ( ! alreadyHasNewline ) { text = text . Substring ( 0 , backtrack ) + " \n " + text . Substring ( backtrack , text . Length - backtrack ) ; c ++ ; } paramNameStartIndex = c + 1 ; } if ( text [ c ] == ' ' && paramNameStartIndex >= 0 ) { string paramName = text . Substring ( paramNameStartIndex , c - paramNameStartIndex ) . Trim ( ) ; for ( int lookahead = c + 1 ; lookahead < text . Length ; lookahead ++ ) { if ( ! char . IsWhiteSpace ( text [ lookahead ] ) ) { if ( text [ lookahead ] == ' ' || StringAt ( text , " " , lookahead ) ) { if ( removeEmptyParams != null && removeEmptyParams . Contains ( paramName ) ) { text = text . Substring ( 0 , paramNameStartIndex - 1 ) + text . Substring ( lookahead , text . Length - lookahead ) ; c = paramNameStartIndex - 1 ; } } else { if ( paramName == " " || paramName == " " ) { artTemplateHasFilledAuthor = true ; } } break ; } } paramNameStartIndex = - 1 ; } } } } if ( hasError ) { text = MediaWiki . WikiUtils . AddCategory ( " " , text ) ; } else if ( artTemplateHasFilledAuthor && removeEmptyParams == null ) { FixInformationTemplates ( article , artistOrAuthor ) ; } if ( text != article . revisions [ 0 ] . text ) { article . revisions [ 0 ] . text = text ; article . Dirty = true ; } }
Fixes formatting issues with Information/Artwork/Book templates.
Fixes formatting issues with Information / Artwork / Book templates .
// set if either "artist" or "author" exists and is filled // force line returns before every main template param //1. backtrack through previous non-newline whitespace //2. place a newline //empty parameter // remove it!
article
Article
The already-downloaded article.
The already - downloaded article .
removeEmptyParams
HashSet<string>