User:Robobot/User Page Redirect

From Robowiki
Jump to navigation Jump to search

Hi all (mainly Voidious), do you guys want me to perform a user page redirect replace task now? I've finished the code, converting all redirects it get from Category:User Page Redirect, anyway the current version cannot handler the [[Page|Title]] format yet. Now it is only command line comment (a java class that only do a thing in its main method) because the main GUI is still under development. Here is a code (quite messy since it is my first testing version of code, and edit into this pieces) Using sightly modified version wikipedia:User:MER-C/Wiki.java.

package net.robowiki.robobot;

import java.io.*;
import java.text.*;
import java.util.*

import javax.security.auth.login.LoginException;

import org.wikipedia.en.wiki.user.mer_c.Wiki;

public class BotTest {
	public static Wiki wiki = new Wiki("testwiki.roborumble.org");
	public static Wiki local = new Wiki("localhost");

	public static final String talkPage = "<div style=\"text-align:center;display:block;border:2px red solid;background:#fcc;color:#c00;margin:0.5em;padding:0.8em\"><big>'''You MUST NOT edit this page. Any change to this page will STOP a whole bot operations. If you want to talk about this bot, visit [[User:Robobot/talk]] instead.'''</big><p>If you want to cease the bot operations for any reasonable reasons, please leave comment on this page with the reasons. Robot will cease its operations while there are other messages on this page.</p></div>";

	public static void main(String[] args) throws IOException {
		try {
			wiki.login("Robobot", "<CENSORED>".toCharArray());
			local.login("WikiSysop", "1234".toCharArray());
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		try {
			String talkPageText = wiki.getPageText("User_talk:Robobot");

			if (!talkPageText.trim().equals(talkPage.trim())) {
				System.err.println("ERROR: Robot operation had been ceased by its talk page!");
				return;
			} else {
				System.out.println("Passed talk page check.");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		// Getting lists of all user page redirect available
		String categoryName = "User_Page_Redirect";
		String[] pagesname = wiki.getCategoryMembers(categoryName);

		HashMap<String, String> redirectsList = new HashMap<String, String>();

		for (String page : pagesname) {
			String content = wiki.getPageText(page);

			String[] contents = content.trim().split("[\r\n]+");

			// Quick hack to get the target page
			String target = contents[0].replaceAll("#REDIRECT +\\[\\[(.*)\\]\\]", "$1");

			redirectsList.put(page, target);
		}

		try {
			// It is manual now, but I can make it auto in no time
			String pagetext = wiki.getPageText("GuessFactor/Archived_Talk_20071112");

			for (String name : redirectsList.keySet()) {
				// Quick replacing hack again
				pagetext = pagetext.replace("[[" + name + "]]", "[["
						+ redirectsList.get(name) + "|" + name + "]]");
			}
			
			// saving to my local test wiki now, change local to wiki to work with real wiki
			local.edit("Test User Link Fix", pagetext, "", false);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LoginException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		updateLastRunning();
	}

	public static void updateLastRunning() {
		System.out.println("Updating last running time...");
		DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,
				DateFormat.FULL, Locale.US);
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"),
				Locale.US);
		df.setCalendar(c);
		Date dateToFormat = c.getTime();
		System.out.println("Time: " + df.format(dateToFormat));
		try {
			wiki.edit("Template:Robobot/Status", genStatusTemplate("Pending",
					"green", "Last run: " + df.format(dateToFormat)),
					"update last running time", false);
		} catch (LoginException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static String genStatusTemplate(String status, String color,
			String comment) {
		StringBuilder aa = new StringBuilder();
		aa.append("<noinclude><!--\n");
		aa.append("\n");
		aa
				.append("**************************************************************\n");
		aa.append("\n");
		aa
				.append("** **  This template will get updated by automatic bot.  ** **\n");
		aa
				.append("** **             DO NOT EDIT THIS PAGE                  ** **\n");
		aa.append("\n");
		aa
				.append("**************************************************************\n");
		aa.append("\n");
		aa
				.append("--></noinclude>{| style=\"clear: right; width:270px; float:right; margin:0em 0em 0em 1em; border:1px solid #87CEEB; text-align:center; background-color:#FFFFFF;\"\n");
		aa
				.append("| style=\"background-color:#f0ffff; text-align:center;\" colspan=\"2\" |'''[[User:Robobot|Robobot]] Status'''\n");
		aa.append("|-\n");
		aa
				.append("| style=\"background-color:#FFFFFF; font-size: 40px; text-align:center; padding-top: 20px; padding-bottom: 5px;\"  | <div style=\"color:"
						+ color
						+ "\">"
						+ status
						+ "</div><span style=\"font-size:12px\"><br />"
						+ comment + "</span>\n");
		aa.append("|}\n");
		return aa.toString();
	}
}

Quite buggy, huh? Anyway, the GUI is developing, and I'll publish .jar when done (and I need to write new Ant script =() » Nat | Talk » 07:40, 21 May 2009 (UTC)

Ahhh... letters casing is what I've forgot (nano and rozu) Just adding dirty hack about casing. » Nat | Talk » 07:45, 21 May 2009 (UTC)

The GuessFactor/Archived Talk 20071112 had been updated using bot, it look rather fine so if no one comment here, I'll start working around archived talk with above code. Log can be found in Recent Changes page here » Nat | Talk » 15:01, 21 May 2009 (UTC)

I've only briefly examined the code, but it looks OK to me and the test run on GuessFactor/Archived Talk 20071112 worked fine, so I say fire it up. Nice work. --Voidious 15:05, 21 May 2009 (UTC)

There is still a problem with aliases (aka nick). Anyway, when more aliases added to user page redirect category I'll fire it up again. Now I'm firing on the Category:Archived Talk category. » Nat | Talk » 15:19, 21 May 2009 (UTC)

And the edit finished. Randomly checks told me that it have no errors, but if ther are any feel free to rollback. Voidious, asking for your permission, can I fire my bot at full speed? Normally, wiki(pedia) bots will have some throttle delayed to prevent server overload, but I don't think server here will overloaded so I'm asking here. It took me exactly 11 minutes to perform things I just performed.

Just realize that I forgot to enter edit summary for the bot. » Nat | Talk » 15:34, 21 May 2009 (UTC)

Well, it looks like it only made 4-5 edits per minute, so I don't think you need to add any additional delay. If we ever run this bot (or another) on the server itself, I'd probably want to add a small delay (like 1 second or less) between edits. --Voidious 15:46, 21 May 2009 (UTC)

Actually it is throttled to the most of 6 edits per minute. (current throttle delay is Thread.sleep(10000 - System.currentTimeMillis() + startTimeMillis), it comes with the framework) I ask to reducing it to perhaps 5000 or 3000? » Nat | Talk » 15:52, 21 May 2009 (UTC)
Oh, I misunderstood. In that case, full speed (no throttling) would make me a little nervous, but I think 1000 ms (1 second) should be more than enough (so long as we have only one bot, anyway). --Voidious 16:02, 21 May 2009 (UTC)
Thanks. I fired my bot again, now set throttle to 1000 ms (which make the most of 60 edits per minute, but actually it need a time to fetch the page content/replace text to so it around 30 edits). I now added check if there are any changes before saved. Now it modified all talk pages, archived talk category and discussions category (around 180 pages all) » Nat | Talk » 07:40, 22 May 2009 (UTC)
Last run rook 14:54.8941 minutes with 119 edits » Nat | Talk » 08:13, 22 May 2009 (UTC)