User:Robobot/User Page Redirect
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)