Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var origCheckPageText =
                Tools.GetHTML(
                    "https://en.wikipedia.org/w/index.php?title=Wikipedia:AutoWikiBrowser/CheckPage&action=raw");

            var enabledUsers = Tools.StringBetween(origCheckPageText, "<!--enabledusersbegins-->",
                                                   "<!--enabledusersends-->");

            var botUsers = Tools.StringBetween(enabledUsers, "<!--enabledbots-->", "<!--enabledbotsends-->");

            var normalUsers = enabledUsers.Replace("<!--enabledbots-->" + botUsers + "<!--enabledbotsends-->", "");

            Regex username = new Regex(@"^\*\s*(.*?)\s*$", RegexOptions.Multiline | RegexOptions.Compiled);

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

            foreach (Match m in username.Matches(normalUsers))
            {
                users.Add(m.Groups[1].Value.Trim());
            }

            users.Sort();

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

            foreach (Match m in username.Matches(botUsers))
            {
                bots.Add(m.Groups[1].Value.Trim());
            }

            bots.Sort();

            Dictionary <string, List <string> > checkPageOutput = new Dictionary <string, List <string> > {
                { "enabledusers", users },
                { "enabledbots", bots }
            };

            ApiEdit edit    = new ApiEdit("https://en.wikipedia.org/w/");
            var     profile = AWBProfiles.GetProfile(1);

            edit.Login(profile.Username, profile.Password);

            edit.Open("Project:AutoWikiBrowser/CheckPageJSON");
            edit.Save(JsonConvert.SerializeObject(checkPageOutput, Formatting.Indented), "Converting from non json page", false, WatchOptions.NoChange);

            // Site Config stuff
            Dictionary <string, object> configOutput = new Dictionary <string, object>();

            Match typoLink = Regex.Match(origCheckPageText, "<!--[Tt]ypos:(.*?)-->");

            configOutput.Add("typolink", typoLink.Success && typoLink.Groups[1].Value.Trim().Length > 0 ? typoLink.Groups[1].Value.Trim() : "");

            configOutput.Add("allusersenabled", origCheckPageText.Contains("<!--All users enabled-->"));

            configOutput.Add("allusersenabledusermode", origCheckPageText.Contains("<!--All users enabled user mode-->"));

            List <Dictionary <string, string> > awbMessages = new List <Dictionary <string, string> >();

            // see if there is a message
            foreach (Match m in Message.Matches(origCheckPageText))
            {
                if (m.Groups[1].Value.Trim().Length == 0)
                {
                    continue;
                }

                awbMessages.Add(new Dictionary <string, string> {
                    { "version", "*" },
                    { "message", m.Groups[1].Value.Trim() }
                });
            }

            // see if there is a version-specific message
            foreach (Match m in VersionMessage.Matches(origCheckPageText))
            {
                if (m.Groups[2].Value.Trim().Length == 0 || m.Groups[1].Value == "x.x.x.x")
                {
                    continue;
                }

                awbMessages.Add(new Dictionary <string, string> {
                    { "version", m.Groups[1].Value },
                    { "message", m.Groups[2].Value.Trim() }
                });
            }

            configOutput.Add("messages", awbMessages);

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

            foreach (Match underscore in Underscores.Matches(origCheckPageText))
            {
                if (underscore.Success && underscore.Groups[1].Value.Trim().Length > 0)
                {
                    us.Add(underscore.Groups[1].Value.Trim());
                }
            }

            us.Sort();
            configOutput.Add("underscoretitles", us);

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

            // Get list of articles not to apply general fixes to.
            Match noGenFix = WikiRegexes.NoGeneralFixes.Match(origCheckPageText);

            if (noGenFix.Success)
            {
                foreach (Match link in WikiRegexes.UnPipedWikiLink.Matches(noGenFix.Value))
                {
                    if (!NoParse.Contains(link.Groups[1].Value))
                    {
                        NoParse.Add(link.Groups[1].Value);
                    }
                }
            }

            NoParse.Sort();
            configOutput.Add("nogenfixes", NoParse);

            List <string> NoRetf = new List <string>();
            // Get list of articles not to apply RETF to.
            Match noRETF = WikiRegexes.NoRETF.Match(origCheckPageText);

            if (noRETF.Success)
            {
                foreach (Match link in WikiRegexes.UnPipedWikiLink.Matches(noRETF.Value))
                {
                    if (!NoRetf.Contains(link.Groups[1].Value))
                    {
                        NoRetf.Add(link.Groups[1].Value);
                    }
                }
            }

            NoRetf.Sort();
            configOutput.Add("noregextypofix", NoRetf);

            edit.Open("Project:AutoWikiBrowser/Config");
            edit.Save(JsonConvert.SerializeObject(configOutput, Formatting.Indented), "Converting from non json page", false, WatchOptions.NoChange);
        }
        /// <summary>
        /// Main function that retrieves the list from API, including paging
        /// </summary>
        /// <param name="url">URL of API request</param>
        /// <param name="haveSoFar">Number of pages already retrieved, for upper limit control</param>
        /// <returns>List of pages</returns>
        public List <Article> ApiMakeList(string url, int haveSoFar)
        {
            if (Globals.UnitTestMode)
            {
                throw new Exception("You shouldn't access Wikipedia from unit tests");
            }

            // TODO: error handling
            List <Article> list    = new List <Article>();
            string         postfix = "";

            string newUrl = url;

            ApiEdit editor = Variables.MainForm.TheSession.Editor.SynchronousEditor;

            while (list.Count + haveSoFar < Limit)
            {
                // API continuation needs updating https://phabricator.wikimedia.org/T104684
                string text = editor.QueryApi(newUrl + "&rawcontinue=1" + postfix); // HACK: Hacky hack hack

                XmlTextReader xml = new XmlTextReader(new StringReader(text));
                xml.MoveToContent();
                postfix = "";

                while (xml.Read())
                {
                    if (xml.Name == "query-continue")
                    {
                        XmlReader r = xml.ReadSubtree();

                        r.Read();

                        while (r.Read())
                        {
                            if (!r.IsStartElement())
                            {
                                continue;
                            }
                            if (!r.MoveToFirstAttribute())
                            {
                                throw new FormatException("Malformed element '" + r.Name + "' in <query-continue>");
                            }
                            postfix += "&" + r.Name + "=" + HttpUtility.UrlEncode(r.Value);
                        }
                    }
                    else if (PageElements.Contains(xml.Name) && xml.IsStartElement())
                    {
                        if (!EvaluateXmlElement(xml))
                        {
                            continue;
                        }

                        int ns;
                        int.TryParse(xml.GetAttribute("ns"), out ns);
                        string name = xml.GetAttribute(WantedAttribute);

                        if (string.IsNullOrEmpty(name))
                        {
                            System.Windows.Forms.MessageBox.Show(xml.ReadInnerXml());
                            break;
                        }

                        list.Add(ns >= 0 ? new Article(name, ns) : new Article(name));
                    }
                }
                if (string.IsNullOrEmpty(postfix))
                {
                    break;
                }
            }

            return(list);
        }