WikiFunctions.Parse.MetaDataSorter.MoveMaintenanceTags C# (CSharp) Method

MoveMaintenanceTags() public static method

Moves maintenance tags to the top of the article text. Deduplicates identical tags Does not move tags when only non-infobox templates are above the last tag For en-wiki apply this to zeroth section of article only
public static MoveMaintenanceTags ( string articleText ) : string
articleText string the article text
return string
        public static string MoveMaintenanceTags(string articleText)
        {
            string originalArticleText = articleText;
            bool doMove = false;

            // don't pull tags from new-style {{multiple issues}} template
            string articleTextNoMI = Tools.ReplaceWithSpaces(articleText, WikiRegexes.MultipleIssues.Matches(articleText));

            // if all templates removed from articletext before last MaintenanceTemplates match are not infoboxes then do not change anything
            var maintTemplatesFound = (from Match m in WikiRegexes.MaintenanceTemplates.Matches(articleTextNoMI) select m);

            // return if no MaintenanceTemplates to move
            if (!maintTemplatesFound.Any())
                return articleText;

            string articleTextToCheck = articleText.Substring(0, maintTemplatesFound.Select(m => m.Index).Max());

            foreach (Match m in WikiRegexes.NestedTemplates.Matches(articleTextToCheck))
            {
                if (Tools.GetTemplateName(m.Value).ToLower().Contains("infobox"))
                {
                    doMove = true;
                    break;
                }

                articleTextToCheck = articleTextToCheck.Replace(m.Value, "");
            }

            // work to do if tags to move or duplicate tags
            if (articleTextToCheck.Trim().Length > 0 || maintTemplatesFound.Count() > Parsers.DeduplicateMaintenanceTags(maintTemplatesFound.Select(m => m.Value).ToList()).Count())
                doMove = true;

            if (!doMove)
                return articleText;

            List<string> mt = new List<string>();

            // extract maintenance tags, not section ones
            articleText = WikiRegexes.MaintenanceTemplates.Replace(articleText, m => {
                                    if (m.Value.Contains("section"))
                                        return m.Value;

                                    mt.Add(m.Value);
                                    return "";
                                });

            if (mt.Any())
            {
                string strMaintTags = string.Join("\r\n", Parsers.DeduplicateMaintenanceTags(mt).ToArray());
                articleText = strMaintTags + "\r\n" + articleText.TrimStart();

                // don't change commented out tags etc.
                if (!Tools.UnformattedTextNotChanged(originalArticleText, articleText))
                    return originalArticleText;
            }

            return articleText;
        }

Usage Example

        /// <summary>
        /// In the {{Multiple issues}} template renames unref to BLPunref for living person bio articles
        /// </summary>
        /// <param name="articleText">The page text</param>
        /// <returns>The updated page text</returns>
        private string MultipleIssuesBLPUnreferenced(string articleText)
        {
            articleText = MultipleIssuesDate.Replace(articleText, "");

            if (WikiRegexes.MultipleIssues.IsMatch(articleText))
            {
                string aiat = WikiRegexes.MultipleIssues.Match(articleText).Value;

                // unref to BLPunref for living person bio articles
                if (Tools.GetTemplateParameterValue(aiat, "unreferenced").Length > 0 && articleText.Contains(CategoryLivingPeople))
                {
                    articleText = articleText.Replace(aiat, Tools.RenameTemplateParameter(aiat, "unreferenced", "BLP unsourced"));
                }
                else if (Tools.GetTemplateParameterValue(aiat, "unref").Length > 0 && articleText.Contains(CategoryLivingPeople))
                {
                    articleText = articleText.Replace(aiat, Tools.RenameTemplateParameter(aiat, "unref", "BLP unsourced"));
                }

                string zerothSection = Tools.GetZerothSection(articleText);
                string restOfArticle = articleText.Substring(zerothSection.Length);
                articleText = MetaDataSorter.MoveMaintenanceTags(zerothSection) + restOfArticle;
            }

            return(articleText);
        }