WikiFunctions.Parse.Parsers.HasRefAfterReflist C# (CSharp) Method

HasRefAfterReflist() public static method

Check if the article contains a <ref>...</ref> reference after the {{reflist}} to show them
public static HasRefAfterReflist ( string articleText ) : bool
articleText string
return bool
        public static bool HasRefAfterReflist(string articleText)
        {
            if (!Variables.LangCode.Equals("en") || articleText.Length == 0)
                return false;

            int refstemplateindex = 0, reflength = 0;
            int maxlen = Math.Min(articleText.Length, 1000);
            string articleTextoriginal = articleText;

            // Performance: as {{reflist}} normally at end of page, proces page in batches of last 1000 characters
            // Should be that last 1000 or 2000 will contain {{reflist}} and no <ref> so we can avoid processing rest of article
            for(int i = maxlen; i <= articleTextoriginal.Length; i += maxlen)
            {
                articleText = articleTextoriginal.Substring(articleTextoriginal.Length - i, i);
                foreach(Match m in WikiRegexes.ReferencesTemplate.Matches(articleText))
                {
                    if(refstemplateindex > 0)
                        return false; // multiple {{reflist}} etc. in page, not supported for check

                    refstemplateindex = m.Index;
                    reflength = m.Length;
                }
                articleText = articleText.Substring(refstemplateindex + reflength);
                articleText = WikiRegexes.Comments.Replace(articleText, "");

                if(refstemplateindex > 0)
                    return Regex.IsMatch(articleText, WikiRegexes.ReferenceEnd);
            }

            return false;
        }

Usage Example

Example #1
0
        // http://en.wikipedia.org/wiki/Wikipedia_talk:AutoWikiBrowser/Feature_requests#Place_.22External_links.22_section_after_.22References.22
        // TODO: only works when there is another section following the references section
        /// <summary>
        /// Ensures the external links section of an article is after the references section
        /// </summary>
        /// <param name="articleText">The wiki text of the article.</param>
        /// <returns>Article text with external links section below the references section</returns>
        public static string MoveExternalLinks(string articleText)
        {
            string articleTextAtStart = articleText;
            // is external links section above references?
            string externalLinks = ExternalLinksSection.Match(articleText).Groups[1].Value;
            string references    = ReferencesSection.Match(articleText).Groups[1].Value;

            // references may be last section
            if (references.Length == 0)
            {
                references = ReferencesToEnd.Match(articleText).Value;
            }

            if (articleText.IndexOf(externalLinks) < articleText.IndexOf(references) && references.Length > 0 && externalLinks.Length > 0)
            {
                articleText = articleText.Replace(externalLinks, "");
                articleText = articleText.Replace(references, references + externalLinks);
            }

            // newlines are fixed by later logic; validate no <ref> in external links section
            if (!Parsers.HasRefAfterReflist(articleText))
            {
                return(articleText);
            }
            else
            {
                return(articleTextAtStart);
            }
        }
Parsers