WikiFunctions.Updater.CheckForUpdates C# (CSharp) Method

CheckForUpdates() public static method

Background request to check enabled state of AWB
public static CheckForUpdates ( ) : void
return void
        public static void CheckForUpdates()
        {
            if (_request != null) return;

            _request = new BackgroundRequest();
            _request.Execute(UpdateFunc);
        }

Usage Example

Example #1
0
        /// <summary>
        /// Checks log in status, registered and version.
        /// </summary>
        private WikiStatusResult UpdateWikiStatus()
        {
            try
            {
                IsBot = false;

                Site = new SiteInfo(Editor.SynchronousEditor);

                // load version check page if no status set
                if (Updater.Result == Updater.AWBEnabledStatus.None)
                {
                    Updater.CheckForUpdates();
                }

                // load check page
                string url = Variables.URLIndex + "?title=Project:AutoWikiBrowser/CheckPage&action=raw";

                string checkPageText = Editor.SynchronousEditor.HttpGet(url);

                // remove U+200E LEFT-TO-RIGHT MARK, U+200F RIGHT-TO-LEFT MARK as on RTL wikis these can get typed in by accident
                checkPageText = DirectionMarks.Replace(checkPageText, "");

                Variables.RTL = Site.IsRightToLeft;
                Variables.CapitalizeFirstLetter = Site.CapitalizeFirstLetter;

                // the wgCategoryCollation list uses wiki rather than wikipedia so convert that
                string siteName = (Variables.Project.ToString() == "wikipedia" ? "wiki" : Variables.Project.ToString());
                Variables.UnicodeCategoryCollation = !Variables.IsCustomProject && Site.UcaCategoryCollation.Contains(Site.Language + siteName);

                if (Variables.IsCustomProject || Variables.IsWikia)
                {
                    Variables.LangCode = Site.Language;
                }

                Variables.TagEdits = Site.IsAWBTagDefined;

                Updater.WaitForCompletion();
                Updater.AWBEnabledStatus versionStatus = Updater.Result;
                VersionCheckPage = Updater.GlobalVersionPage;

                // see if this version is enabled
                if ((versionStatus & Updater.AWBEnabledStatus.Disabled) == Updater.AWBEnabledStatus.Disabled)
                {
                    return(WikiStatusResult.OldVersion);
                }

                CheckPageText = checkPageText;

                if (!User.IsLoggedIn)
                {
                    return(WikiStatusResult.NotLoggedIn);
                }

                if (!User.HasRight("writeapi"))
                {
                    return(WikiStatusResult.NoRights);
                }

                // TODO: assess the impact on servers later
                Editor.Maxlag = /*User.IsBot ? 5 : 20*/ -1;

                var versionJson = JObject.Parse(Updater.GlobalVersionPage);
                // check if username is globally blacklisted
                foreach (string badName in versionJson["badnames"])
                {
                    if (!string.IsNullOrEmpty(User.Name) &&
                        Regex.IsMatch(User.Name, badName,
                                      RegexOptions.IgnoreCase | RegexOptions.Multiline))
                    {
                        return(WikiStatusResult.NotRegistered);
                    }
                }

                // See if there's any messages on the Version page
                foreach (var message in versionJson["messages"])
                {
                    var version = message["version"].ToString();
                    // TODO: Semver version checking
                    if ((version == "*" || version == AWBVersion) && message["text"] != null)
                    {
                        if (message["enabled"] != null && !(bool)message["enabled"])
                        {
                            continue;
                        }
                        MessageBox.Show(message["text"].ToString(), "Automated message", MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }
                }

                // CheckPage Messages per wiki are still in scary HTML comments.... TBC!

                // see if there is a message
                foreach (Match m in Message.Matches(checkPageText))
                {
                    if (m.Groups[1].Value.Trim().Length > 0)
                    {
                        continue;
                    }
                    MessageBox.Show(m.Groups[1].Value, "Automated message", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                }

                // see if there is a version-specific message
                foreach (Match m in VersionMessage.Matches(checkPageText))
                {
                    if (m.Groups[1].Value.Trim().Length == 0 ||
                        m.Groups[1].Value != AWBVersion)
                    {
                        continue;
                    }
                    MessageBox.Show(m.Groups[2].Value, "Automated message", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                }

                HasTypoLink(checkPageText);

                List <string> us = new List <string>();
                foreach (Match underscore in Underscores.Matches(checkPageText))
                {
                    if (underscore.Success && underscore.Groups[1].Value.Trim().Length > 0)
                    {
                        us.Add(underscore.Groups[1].Value.Trim());
                    }
                }
                if (us.Count > 0)
                {
                    Variables.LoadUnderscores(us.ToArray());
                }

                // don't require approval if checkpage does not exist
                // Or it has the special text...
                if (checkPageText.Length < 1 || checkPageText.Contains("<!--All users enabled-->"))
                {
                    IsBot = true;
                    return(WikiStatusResult.Registered);
                }

                // see if we are allowed to use this software
                checkPageText = Tools.StringBetween(checkPageText, "<!--enabledusersbegins-->",
                                                    "<!--enabledusersends-->");

                // Checkpage option: <!--All users enabled user mode--> will enable all users for user mode,
                // and enable bots only when in <!--enabledbots--> section
                if (checkPageText.Contains("<!--All users enabled user mode-->") ||
                    (IsSysop && Variables.Project != ProjectEnum.wikia) ||
                    UserNameInText(User.Name, checkPageText))
                {
                    string botUsers = Tools.StringBetween(checkPageText, "<!--enabledbots-->", "<!--enabledbotsends-->");

                    // enable bot mode if in bots section
                    IsBot = UserNameInText(User.Name, botUsers);

                    return(WikiStatusResult.Registered);
                }

                if (Variables.Project != ProjectEnum.custom)
                {
                    var globalUsers = versionJson["globalusers"];
                    foreach (string s in globalUsers)
                    {
                        if (User.Name == s)
                        {
                            return(WikiStatusResult.Registered);
                        }
                    }
                }
                return(WikiStatusResult.NotRegistered);
            }
            catch (Exception ex)
            {
                Tools.WriteDebug(ToString(), ex.Message);
                Tools.WriteDebug(ToString(), ex.StackTrace);
                IsBot = false;
                return(WikiStatusResult.Error);
            }
        }
All Usage Examples Of WikiFunctions.Updater::CheckForUpdates