Wikipedia:Projekt DotNetWikiBot Framework/Innocent bot/Kategoriomdirigering

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Xml;
using System.Threading;
using DotNetWikiBot;

class MyBot : Bot
{
	public void MyFunction1()
	{
		// Write your own function here
	}

	/// The entry point function. Start coding here.
	public static void Main()
	{
		Site site = new Site("http://sv.wikipedia.org", "Innocent bot", "********");
		
		//Page myPage2;
		Page myPage3;
		PageList pl = new PageList(site);
		PageList pl2 = new PageList(site);
		
		//Hämtar lista över kategorier med mall
		pl.FillFromLinksToPage("Mall:Kategoriomdirigering");	
		pl.FillFromLinksToPage("Mall:Omdirigering kategori");
		//Bara intresserad av ns-14 (Kategori:)
		pl.RemoveNamespaces(new int[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,15,100,101}); 
		string tmpStr = "";
		string tmpStr2 = "";
		int start = 0;
		int end   = 0;
		int i     = 0;
		int antal = 0;
		int type  = -1;
		

		foreach (Page myPage in pl)
		{
			myPage.Load();
			//Console.WriteLine("Text1: " + myPage.title);
			type = -1;
			tmpStr = myPage.text;
			start = tmpStr.IndexOf("{{Kategoriomdirigering|");
			if(start != -1)
				type = 0;
			if(start == -1)
			{
				start = tmpStr.IndexOf("{{kategoriomdirigering|");
				if(start != -1)
					type = 1;
			}
			if(start == -1)
			{
				start = tmpStr.IndexOf("{{Omdirigering kategori|");
				if(start != -1)
					type = 2;
			}
			if(start == -1)
			{
				start = tmpStr.IndexOf("{{omdirigering kategori|");
				if(start != -1)
					type = 3;
			}
			if(type == -1)
			{
				Console.WriteLine("Mallen inte rätt angiven i: " + myPage.title);
				//Console.WriteLine(tmpStr);
			}
			else
			{
				tmpStr = tmpStr.Remove(0, start);
				end = tmpStr.IndexOf("|") + 1;
				tmpStr = tmpStr.Remove(0, end);
				if(tmpStr.IndexOf("|") == -1)
					antal = tmpStr.IndexOf("}");
				else
				{
					if(tmpStr.IndexOf("|")<tmpStr.IndexOf("}"))
						antal = tmpStr.IndexOf("|");
					else
						antal = tmpStr.IndexOf("}");
				}
				for(i = 0; i < tmpStr.Length; i++)
				{
					if(tmpStr[0] == ' ')
						{
							tmpStr = tmpStr.Remove(0 , 1);
							antal--;
						}
				}
				tmpStr = tmpStr.Remove(antal , tmpStr.Length - antal);
				//Console.WriteLine("Text2: " + tmpStr);
				if(tmpStr.IndexOf("Kategori:") != -1)
					tmpStr=tmpStr.Remove(0, 9);
				if(tmpStr.IndexOf("kategori:") != -1)
					tmpStr=tmpStr.Remove(0, 9);
				if(tmpStr.IndexOf("Category:") != -1)
					tmpStr=tmpStr.Remove(0, 9);
				if(tmpStr.IndexOf("category:") != -1)
					tmpStr=tmpStr.Remove(0, 9);
				tmpStr2 = myPage.title;
				tmpStr2 = tmpStr2.Remove(0,9);
				tmpStr2 = char.ToUpper(tmpStr2[0]) + tmpStr2.Substring(1);
				tmpStr  = char.ToUpper(tmpStr[0]) + tmpStr.Substring(1);
				Page px = new Page(site, "Kategori:"+tmpStr);
				px.LoadEx();
				if (px.Exists())
				{
					//Console.WriteLine("Text3: " + tmpStr);
					//Console.WriteLine("Text: " + tmpStr2);
					pl2.FillAllFromCategory( tmpStr2 );
					pl.RemoveNamespaces(new int[] {10});
					foreach (Page myPage2 in pl2)
					{
						myPage2.Load();
						myPage2.text = myPage2.text.Replace("[[Category:", "[[Kategori:");
						myPage2.text = myPage2.text.Replace("[[category:", "[[Kategori:");
						myPage2.text = myPage2.text.Replace("[[kategori:", "[[Kategori:");
						myPage2.text = myPage2.text.Replace("[[Kategori: ", "[[Kategori:");
						myPage2.text = myPage2.text.Replace("[[Kategori:a", "[[Kategori:A");
						myPage2.text = myPage2.text.Replace("[[Kategori:b", "[[Kategori:B");
						myPage2.text = myPage2.text.Replace("[[Kategori:c", "[[Kategori:C");
						myPage2.text = myPage2.text.Replace("[[Kategori:" + tmpStr2 + "]]", "[[Kategori:" + tmpStr +"]]");
						myPage2.text = myPage2.text.Replace("[[Kategori:" + tmpStr2 + "|", "[[Kategori:" + tmpStr +"|");
						myPage2.Save(myPage2.text, "Kategoribyte " + tmpStr2 + " -> " + tmpStr, true);
						Console.WriteLine(myPage2.title+" "+ tmpStr2 + " -> " + tmpStr);
						Thread.Sleep(7000);
					}
				}
			}
		}
	}
}