void getNamespaces(bool snamespacesAlreadySet) { if (!snamespacesAlreadySet) { snamespaces = CVNBotUtils.getRawDocument(rooturl + "w/api.php?action=query&meta=siteinfo&siprop=namespaces&format=xml"); if (snamespaces == "") { throw new Exception("Can't load list of namespaces from " + rooturl); } } namespaces = new Hashtable(); XmlDocument doc = new XmlDocument(); doc.LoadXml(snamespaces); string namespacesLogline = ""; XmlNode namespacesNode = doc.GetElementsByTagName("namespaces")[0]; for (int i = 0; i < namespacesNode.ChildNodes.Count; i++) { namespaces.Add(namespacesNode.ChildNodes[i].Attributes["id"].Value, namespacesNode.ChildNodes[i].InnerText); namespacesLogline += "id[" + namespacesNode.ChildNodes[i].Attributes["id"].Value + "]=" + namespacesNode.ChildNodes[i].InnerText + "; "; } logger.Info("getNamespaces: " + namespacesLogline); }
/* * Equivalent to function getre in RCParser.py */ void generateRegex(string mwMessageTitle, int reqCount, ref string destRegex, bool nonStrict) { //Get raw wikitext string mwMessage = CVNBotUtils.getRawDocument(rooturl + "w/index.php?title=" + mwMessageTitle + "&action=raw&usemsgcache=yes"); //Now gently coax that into a regex foreach (char c in rechars) { mwMessage = mwMessage.Replace(c.ToString(), @"\" + c); } mwMessage = mwMessage.Replace("$1", "(?<item1>.+?)"); mwMessage = mwMessage.Replace("$2", "(?<item2>.+?)"); mwMessage = mwMessage.Replace("$3", "(?<item3>.+?)"); mwMessage = mwMessage.Replace("$1", "(?:.+?)"); mwMessage = mwMessage.Replace("$2", "(?:.+?)"); mwMessage = mwMessage.Replace("$3", "(?:.+?)"); mwMessage = mwMessage.Replace("$", @"\$"); mwMessage = "^" + mwMessage + @"(?:: (?<comment>.*?))?$"; //Special:Log comments are preceded by a colon //Dirty code: Block log exceptions! if (mwMessageTitle == "MediaWiki:Blocklogentry") { mwMessage = mwMessage.Replace("(?<item3>.+?)", "\\((?<item3>.+?)\\)"); mwMessage = mwMessage.Replace(@"(?<item2>.+?)(?:: (?<comment>.*?))?$", "(?<item2>.+?)$"); } try { Regex r = new Regex(mwMessage); } catch (Exception e) { throw new Exception("Failed to test-generate regex " + mwMessage + " for " + mwMessageTitle + "; " + e.Message); } if (reqCount > 0) { if (reqCount >= 1) { if (!mwMessage.Contains(@"(?<item1>.+?)") && !nonStrict) { throw new Exception("Regex " + mwMessageTitle + " requires one or more items but item1 not found in " + mwMessage); } if (reqCount >= 2) { if (!mwMessage.Contains(@"(?<item2>.+?)") && !nonStrict) { throw new Exception("Regex " + mwMessageTitle + " requires two or more items but item2 not found in " + mwMessage); } } } } destRegex = mwMessage; }
/// <summary> /// Downloads a list of admins/bots from wiki and adds them to the database (Run this in a separate thread) /// </summary> void addGroupToList() { string projectName = currentGetThreadWiki; currentGetThreadWiki = ""; string getGroup = currentGetThreadMode; currentGetThreadMode = ""; Thread.CurrentThread.Name = "Get" + getGroup + "@" + projectName; UserType getGroupUT; if (getGroup == "sysop") { getGroupUT = UserType.admin; } else if (getGroup == "bot") { getGroupUT = UserType.bot; } else { throw new Exception("Undefined group: " + getGroup); } logger.Info("Downloading list of " + getGroup + "s from " + projectName); IDbConnection ourdbcon = null; try { lock (dbtoken) { //Open a new DB connection for this thread ourdbcon = (IDbConnection) new SqliteConnection(connectionString); ourdbcon.Open(); string list = CVNBotUtils.getRawDocument("http://" + projectName + ".org/w/index.php?title=Special:Listusers&group=" + getGroup + "&limit=5000&offset=0"); //Now parse the list: /* _1568: FIX: MW error */ string sr = list.Substring(list.IndexOf("<ul>") + 4); Match lusers = adminLine.Match(sr.Substring(0, sr.IndexOf("</ul>"))); while (lusers.Success) { addUserToList(lusers.Groups[1].Captures[0].Value, projectName, getGroupUT, "CVNBot" , "Auto-download from wiki", 0, ref ourdbcon); //Add the user to the list, using our own DB connection to write lusers = lusers.NextMatch(); } logger.Info("Added all " + getGroup + "s from " + projectName); } } catch (Exception e) { logger.Error("Unable to get list of " + getGroup + "s from " + projectName + ": " + e.Message); } finally { //Clean up our own DB connection ourdbcon.Close(); ourdbcon = null; } }