User:KingAragorn Bot/Scripts: Difference between revisions

From Tolkien Gateway
m (changing link to Of Beleriand and its Realms)
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__''Note: This code may need to be updated/amended before it can be used on different Wikis.''
==Common scripts==
=Tolkien Gateway=
===Changing categories===
 
== Re-linking ==
<pre>
<pre>
using System;
using System;
Line 8: Line 6:
class MyBot : Bot  
class MyBot : Bot  
{  
{  
        public static void Main()  
public static void Main()  
        {  
{
                Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD");  
Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD");  


                PageList p = new PageList(site);
PageList p = new PageList(site);
                p.FillFromCustomSpecialPage("Whatlinkshere/Peter Jackson's The Lord of the Rings", 500);
p.FillAllFromCategory("Vinyar Tengwar issues");
                p.Load();
p.Load();
                foreach (Page i in p)
p.RemoveFromCategory("Vinyar Tengwar issues");
                        i.text = i.text.Replace("Peter Jackson's The Lord of the Rings|", "The Lord of the Rings: The Motion Picture Trilogy|");
p.AddToCategory("Vinyar Tengwar");
                p.SaveSmoothly(3, "changing link to [[The Lord of the Rings: The Motion Picture Trilogy]]", true);
p.SaveSmoothly(1, "Bot comment: changed category.", true);
        }  
}  


}
}
</pre>
</pre>
Task history:
*''Peter Jackson's The Lord of the Rings'' to [[The Lord of the Rings: The Motion Picture Trilogy]] (2 July 2010)
*''J.R.R. Tolkien's War in Middle-Earth (1988 video game)'' to [[J.R.R. Tolkien's War in Middle Earth]] (2 July 2010)
*''Greek Tolkien Society'' to [[Ellinikós Sýllogos Philon Tólkin]] (2 July 2010)
*''Birthday Party'' to [[Bilbo's Farewell Party]] (3 July 2010)
*''Of Beleriand and its Realms'' to [[Of Beleriand and its Realms]] (3 July 2010)
*


== Changing Categories ==
===Re-linking===
<pre>
<pre>
using System;
using System;
Line 36: Line 27:
class MyBot : Bot  
class MyBot : Bot  
{  
{  
public static void Main()  
public static void Main()  
{  
{  
Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD");  
Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD");  
 
PageList p = new PageList(site);
p.FillFromLinksToPage("Erebor");
p.Load();
foreach (Page i in p) {
i.text = i.text.Replace("[[Erebor]]", "[[Lonely Mountain|Erebor]]");
}
 
foreach (Page j in p) {
j.text = j.text.Replace("[[Erebor|Lonely Mountain]]", "[[Lonely Mountain]]");
}


PageList p = new PageList(site);
p.SaveSmoothly(1, "Bot message: re-linked.", true);
  p.FillAllFromCategory("Images of The Lord of the Rings: The White Council");
}  
  p.Load();
  p.RemoveFromCategory("Images of The Lord of the Rings: The White Council");
  p.AddToCategory("Images from The Lord of the Rings: The White Council");
  p.SaveSmoothly(5, "Bot Comment: Changing Categories", true);
}  


}
}
</pre>
</pre>


=One Wiki to Rule them All=
To avoid certain namespaces, add the following before p.Load():
<pre>
p.RemoveNamespaces(new int[] {1,3});
</pre>
 
To include specific namespaces only, add the following before p.Load():
<pre>
p.FilterNamespaces(new int[] {1,3});
</pre>
List of built-in namespaces and their corresponding integers:
*0 - Main
*1 - Talk
*2 - User
*3 - User talk
*4 - Tolkien Gateway
*5 - Tolkien Gateway talk
*6 - File
*7 - File talk
*8 - MediaWiki
*9 - MediaWiki talk
*10 - Template
*11 - Template talk
*12 - Help
*13 - Help talk
*14 - Category
*15 - Category talk


== Add Category to Recently Uploaded Images ==
==Irregular scripts==
===Change image categories and automatically insert proper sort key===
<pre>
<pre>
using System;
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Xml;
using DotNetWikiBot;
using DotNetWikiBot;
using System.Threading;
using System.Net;
using System.Web;
class MyBot : Bot  
class MyBot : Bot  
{  
{  
public static void Main()  
public static void Main()  
{  
{
Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD");  
Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD");  
PageList p = new PageList(site);
p.FillSubsFromCategoryTree("Images");
p.FilterNamespaces(new int[] {14});
foreach(Page myPage in p) {
String pagename1 = myPage.title;
String pagename2 =  pagename1.Replace("Category:", "");
String pagename3 =  pagename2.Replace("Maps of ", "");
myPage.Load();
myPage.RemoveTemplate("DEFAULTSORT");
myPage.AddTemplate("{{DEFAULTSORT:" + pagename3 + "}}");
}
p.SaveSmoothly(1, "Bot comment: fixed DEFAULTSORT.", true);
}


PageList p = new PageList(site);
        p.FillFromCustomSpecialPage("Log/Upload", 29);
        p.Load();
        p.AddToCategory("Images of Tengwar");
        p.SaveSmoothly(3, "Bot Comment: adding category", true);
}
}
}
</pre>
</pre>


== Removal of Spoiler Template from every page which has it ==
===Mass re-linking===
<pre>
<pre>
using System;
using System;
Line 79: Line 120:
class MyBot : Bot  
class MyBot : Bot  
{  
{  
public static void Main()  
        public static void Main()  
{  
        {  
Site site = new Site("http://lotr.wikia.com", "KingAragorn Bot", "PASSWORD");  
                Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD");  
PageList p = new PageList(site);
        p.FillFromCustomSpecialPage("Whatlinkshere/Template:Spoiler", 500);
        p.Load();
        p.RemoveTemplate("Spoiler");
        p.SaveSmoothly(3, "Bot Comment: removing spoiler template", true);
}
}
</pre>


== Reverting a Vandal Accounts Contributions (not tested + needs rollback rights) ==
                PageList b = new PageList(site);
<pre>
                b.FillFromFile("PageList.txt");
using System;
                foreach (Page c in b) {
using DotNetWikiBot;
                PageList i = new PageList(site);
                i.FillFromLinksToPage(c.title);
            i.LoadEx3();  
                foreach (Page p in i) {


class MyBot : Bot
                      p.text = p.text.Replace("[[Rankin/Bass' The Hobbit]]", "[[The Hobbit (1977 film)]]");
{
                      p.text = p.text.Replace("Rankin/Bass' The Hobbit|", "The Hobbit (1977 film)|");
    public static void Main()
                      p.text = p.text.Replace("[[Ralph Bakshi's The Lord of the Rings]]", "[[The Lord of the Rings (1978 film)]]");
    {
                      p.text = p.text.Replace("Ralph Bakshi's The Lord of the Rings|", "The Lord of the Rings (1978 film)|");
        Site enLOTR = new Site("http://lotr.wikia.com/wiki/", "KingAragorn Bot", "PASSWORD")
                      p.text = p.text.Replace("[[Rankin/Bass' The Return of the King]]", "[[The Return of the King (1980 film)]]");
        p.FillFromUserContributions ("VANDAL USERNAME" "500",);
                      p.text = p.text.Replace("Rankin/Bass' The Return of the King|", "The Return of the King (1980 film)|");
        p.Load();
                p.Save("Bot comment: fixing links", true);
        p.Revert("Bot Comment: Automatic Reverting of a vandal accounts contributions", false);
}
    }
}
}
</pre>
        }


== Adding the Stub template to all pages in [[Special:Shortpages]] ==
}
<pre>
using System;
using DotNetWikiBot;


class MyBot : Bot
{
    public static void Main()
    {
        Site enLOTR = new Site("http://lotr.wikia.com/wiki/", "KingAragorn Bot", "PASSWORD");
        PageList p = new PageList(enLOTR);
        p.FillFromCustomSpecialPage("Shortpages", 1000);
        p.Load();
        p.RemoveTemplate("stub");
        p.AddTemplate("{{stub}}");
        p.SaveSmoothly(5, "Bot Comment: Adding stub template - article is listed under [[Special:Shortpages]]", true);
    }
}
</pre>
</pre>


== Changing Categories ==
===Get list of article names and save to file===
<pre>
<pre>
using System;
using System;
using DotNetWikiBot;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Xml;
using DotNetWikiBot;


class MyBot : Bot
class MyBot : Bot
{
public static void Main()
{
{
  Site enLOTR = new Site("http://lotr.wikia.com/wiki/", "KingAragorn Bot", "PASSWORD");
public static void Main()
  PageList p = new PageList(enLOTR);
{
  p.FillAllFromCategory("Mountains of Middle-earth");
Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "********");  
  p.load();
  p.RemoveFromCategory("Mountains of Middle-earth ")
site.fetchRate = 5000;
  p.AddToCategory("Mountains")
  p.SaveSmoothly(5, "Bot Comment: Changing Categories", true)
PageList p = new PageList(site);
  }
p.FillFromAllPages("The Tolkien Calendar 1993", 0 , false, 5000);
p.SaveTitlesToFile("tg.txt");
}
}
}
</pre>
</pre>
__NOTOC__

Latest revision as of 17:35, 24 October 2017

Common scripts[edit | edit source]

Changing categories[edit | edit source]

using System;
using DotNetWikiBot;
class MyBot : Bot 
{ 
	public static void Main() 
	{
		Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD"); 

		PageList p = new PageList(site);
		p.FillAllFromCategory("Vinyar Tengwar issues");
		p.Load();
		p.RemoveFromCategory("Vinyar Tengwar issues");
		p.AddToCategory("Vinyar Tengwar");
		p.SaveSmoothly(1, "Bot comment: changed category.", true);
} 

}

Re-linking[edit | edit source]

using System;
using DotNetWikiBot;
class MyBot : Bot 
{ 
	public static void Main() 
	{ 
		Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD"); 

		PageList p = new PageList(site);
 		p.FillFromLinksToPage("Erebor");
		p.Load();
		foreach (Page i in p) {
			i.text = i.text.Replace("[[Erebor]]", "[[Lonely Mountain|Erebor]]");
		}

		foreach (Page j in p) {
			j.text = j.text.Replace("[[Erebor|Lonely Mountain]]", "[[Lonely Mountain]]");
		}

		p.SaveSmoothly(1, "Bot message: re-linked.", true);
	} 

}

To avoid certain namespaces, add the following before p.Load():

		p.RemoveNamespaces(new int[] {1,3});

To include specific namespaces only, add the following before p.Load():

		p.FilterNamespaces(new int[] {1,3});

List of built-in namespaces and their corresponding integers:

  • 0 - Main
  • 1 - Talk
  • 2 - User
  • 3 - User talk
  • 4 - Tolkien Gateway
  • 5 - Tolkien Gateway talk
  • 6 - File
  • 7 - File talk
  • 8 - MediaWiki
  • 9 - MediaWiki talk
  • 10 - Template
  • 11 - Template talk
  • 12 - Help
  • 13 - Help talk
  • 14 - Category
  • 15 - Category talk

Irregular scripts[edit | edit source]

Change image categories and automatically insert proper sort key[edit | edit source]

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

class MyBot : Bot 
{ 
	public static void Main() 
	{
		Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD"); 
		
		PageList p = new PageList(site);
		p.FillSubsFromCategoryTree("Images");
		p.FilterNamespaces(new int[] {14});
		
		foreach(Page myPage in p) {
			String pagename1 = myPage.title;
			String pagename2 =  pagename1.Replace("Category:", "");
			String pagename3 =  pagename2.Replace("Maps of ", "");
			myPage.Load();
			myPage.RemoveTemplate("DEFAULTSORT");
			myPage.AddTemplate("{{DEFAULTSORT:" + pagename3 + "}}");			
			
		}
		p.SaveSmoothly(1, "Bot comment: fixed DEFAULTSORT.", true);
} 

}

Mass re-linking[edit | edit source]

using System;
using DotNetWikiBot;
class MyBot : Bot 
{ 
        public static void Main() 
        { 
                Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD"); 

                PageList b = new PageList(site);
                b.FillFromFile("PageList.txt");
                foreach (Page c in b) {
                PageList i = new PageList(site);
                i.FillFromLinksToPage(c.title);
            i.LoadEx3();    
                foreach (Page p in i) {

                      p.text = p.text.Replace("[[Rankin/Bass' The Hobbit]]", "[[The Hobbit (1977 film)]]");
                      p.text = p.text.Replace("Rankin/Bass' The Hobbit|", "The Hobbit (1977 film)|");
                      p.text = p.text.Replace("[[Ralph Bakshi's The Lord of the Rings]]", "[[The Lord of the Rings (1978 film)]]");
                      p.text = p.text.Replace("Ralph Bakshi's The Lord of the Rings|", "The Lord of the Rings (1978 film)|");
                      p.text = p.text.Replace("[[Rankin/Bass' The Return of the King]]", "[[The Return of the King (1980 film)]]");
                      p.text = p.text.Replace("Rankin/Bass' The Return of the King|", "The Return of the King (1980 film)|");
                p.Save("Bot comment: fixing links", true);
}
}
        } 

}

Get list of article names and save to file[edit | edit source]

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

class MyBot : Bot
{
	public static void Main()
	{
		Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "********"); 
		
		site.fetchRate = 5000;
		
		PageList p = new PageList(site);
		
		p.FillFromAllPages("The Tolkien Calendar 1993", 0 , false, 5000);
		
		p.SaveTitlesToFile("tg.txt");
	}
}