Hoppa till innehållet

Wikipedia:Projekt DotNetWikiBot Framework/Innocent bot/DotNetKompl

Från Wikipedia
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Xml;
using System.Net;
using System.Web;
using DotNetWikiBot;


class DotNetKompl
{

	// Gör så att en sida med namnet "page" hamnar i bevakningslistan.
	public static void Watch(Site site, string page)
	{
			if (string.IsNullOrEmpty(page))
				throw new WikiBotException(Bot.Msg("No title specified for page to watch."));
		
			XmlTextReader reader;
			string tmpStr = site.PostDataAndGetResultHTM(site.site+"/w/api.php", "action=query&prop=info&format=xml&intoken=watch&titles="+HttpUtility.UrlEncode(page));
			System.IO.File.WriteAllText("watch.xml", tmpStr);
			reader = new XmlTextReader ("watch.xml");
			string s = "";
			string s1 = "";
			while (reader.Read()) {
			if (reader.IsEmptyElement && reader["watchtoken"] != null)
				s = HttpUtility.HtmlDecode(reader["watchtoken"]) ;
				if(s != "")
				{
					s1 = s;
				}
			}
			reader.Close();
			if (string.IsNullOrEmpty(s1))
				throw new WikiBotException(Bot.Msg("Not allowed to watch this page."));
			
			site.PostDataAndGetResultHTM(site.site+"/w/api.php", "action=watch&format=xml&title="+HttpUtility.UrlEncode(page)+"&token="+HttpUtility.UrlEncode(s1));
	}
	//initierar en fil som beskriver vilken interwikisortering som gäller
	public static void Interwikiinit(Site site)
	{
		Page iwso = new Page(site, "MediaWiki:Interwiki config-sorting order");
		iwso.Load();
		if(!iwso.Exists()) //Om sidan saknas så är det efter koden som gäller. Typexempel: dewiki
		{
			string matrix = "";
			string tmpStr = site.GetPageHTM(site.site+"/w/api.php?action=sitematrix");
			do
			{
				tmpStr = tmpStr.Substring(tmpStr.IndexOf("<language code="")+24);
				matrix += tmpStr.Substring(0,tmpStr.IndexOf("""))+"\n";
			}while(tmpStr.IndexOf("<language code="") != -1);
			File.WriteAllText("Interwikisortingorder.txt", matrix);
			
		}
		else if(iwso.text.Trim() == "auto-languagecode") //Som ovan med med sidan skapad 
		{
			string matrix = "";
			string tmpStr = site.GetPageHTM(site.site+"/w/api.php?action=sitematrix");
			do
			{
				tmpStr = tmpStr.Substring(tmpStr.IndexOf("<language code="")+24);
				matrix += tmpStr.Substring(0,tmpStr.IndexOf("""))+"\n";
			}while(tmpStr.IndexOf("<language code="") != -1);
			File.WriteAllText("Interwikisortingorder.txt", matrix);
			
		}
		else
		{
			if(iwso.text.Trim() == "meta-native-languagename")//Efter namnet på språket typexempel enwiki, svwiki
			{
				Site sitemeta = new Site("http://meta.wikimedia.org", "Innocent bot", "********");
				Page pmw = new Page(sitemeta, "MediaWiki:Interwiki config-sorting order-native-languagename");
				pmw.Load();
				File.WriteAllText("Interwikisortingorder.txt", pmw.text);
			}
			else if(iwso.text.Trim() == "meta-native-languagename-firstword")//Efter namnet på språket, första ordet, typexempel: fiwiki
			{
				Site sitemeta = new Site("http://meta.wikimedia.org", "Innocent bot", "********");
				Page pmw = new Page(sitemeta, "MediaWiki:Interwiki config-sorting order-native-languagename-firstword");
				pmw.Load();
				File.WriteAllText("Interwikisortingorder.txt", pmw.text);
			}
			else if(iwso.text.IndexOf("meta-native-languagename") != -1)//Efter språknamn, men med vissa undantag. Typexempel nnwiki
			{
				Site sitemeta = new Site("http://meta.wikimedia.org", "Innocent bot", "********");
				Page pmw = new Page(sitemeta, "MediaWiki:Interwiki config-sorting order-native-languagename");
				pmw.Load();
				string matrix = pmw.text;
				string matrix2 = "";
				iwso.text = iwso.text.Trim();
				do
				{
					string tmp = iwso.text.Substring(0,iwso.text.IndexOf("\n"));
					iwso.text = iwso.text.Substring(iwso.text.IndexOf("\n")+1);
					matrix = matrix.Replace(tmp+"\n", "");
					matrix2 += tmp+"\n";
				}while(iwso.text.IndexOf("\n") != -1);
				File.WriteAllText("Interwikisortingorder.txt", matrix2+matrix);
			}
			else if(iwso.text.IndexOf("auto-languagecode") != -1)//Efter kod, men med undantag, typexempel huwiki
			{
				string matrix = "";
				string tmpStr = site.GetPageHTM(site.site+"/w/api.php?action=sitematrix");
				do
				{
					tmpStr = tmpStr.Substring(tmpStr.IndexOf("<language code="")+24);
					matrix += tmpStr.Substring(0,tmpStr.IndexOf("""))+"\n";
				}while(tmpStr.IndexOf("<language code="") != -1);
				string matrix2 = "";
				iwso.text = iwso.text.Trim();
				do
				{
					string tmp = iwso.text.Substring(0,iwso.text.IndexOf("\n"));
					iwso.text = iwso.text.Substring(iwso.text.IndexOf("\n")+1);
					matrix = matrix.Replace(tmp+"\n", "");
					matrix2 += tmp+"\n";
				}while(iwso.text.IndexOf("\n") != -1);
				File.WriteAllText("Interwikisortingorder.txt", matrix2+matrix);
			}
			else
			{
				//Har en helt egen struktur. Typexempel srwiki
				File.WriteAllText("Interwikisortingorder.txt", iwso.text);
			}
			
		}
		
	}
}