User:TLULbot/Source/SourceMgr.java

/* * tLULbot - Java bot for editing the RuneScape Wiki * Copyright (c) 2009-2010 The last username left and others. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;. * * This program was based on AmauriceBot, created by Maurice Abraham. * * Contributors: * Maurice Abraham * The last username left *--*/

package tLULbot_v3;

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import jBot.*; import jBot.Core.*;

public class SourceMgr extends Task {   public void setWikiSession(WikiSession wiki) {       w = wiki; }   protected int getPrinterVersion {       return 1; }   protected int getMainVersion {       return 2; }   private Runnable r = new Runnable {       public void run {           try {               if (!w.isLoggedIn) {                   w.p.errString("You must be logged in to upload source."); return; }               saveSource("C:\\Users\\TLUL\\Documents\\Eclipse\\com.gmail.tlul.gm\\src\\tLULbot_v3", "User:" + w.getCurrentUser + "/Source"); }           catch (IOException e1) {               w.p.errString("IOException: " + e1.getMessage); }           catch (jBotException e1) {               w.p.errString("Error: " + e1.getMessage); }       }    };

public Runnable getRunnable {       return r;    } public String getTaskCommand {       return "sourcemgr"; }   public String getTaskName {       return "Manage source of TLULbot - Runescape Wiki"; }   protected String getWiki {       return "runescape"; }   private WikiSession w = null;

public void saveSource(String sourcePath, String wikiPath) throws IOException, jBotException {       File sourceDir = new File(sourcePath); if (!sourceDir.exists || !sourceDir.isDirectory) {           Utils.log("ERROR: No such source directory as " + sourceDir.getCanonicalPath); return; }

File files[] = sourceDir.listFiles; for (int i = 0; i &lt; files.length; i ++) {           if (files[i].getName.endsWith(".java")) {               String wikiPage = wikiPath + "/" + files[i].getName; saveSourceFile(files[i], wikiPage); }       }    }

private void saveSourceFile(File sourceFile, String wikiPage) throws IOException, jBotException {       StringBuffer sourceCode = new StringBuffer; BufferedReader in = new BufferedReader(new FileReader(sourceFile));

int c;       while ((c = in.read) != -1) {           sourceCode.append((char)c); }       in.close;

String pageText = sourceCode.toString .replaceAll("\\&amp;", "\\&amp;amp;") .replaceAll("&lt;", "\\&amp;lt;") .replaceAll("&gt;", "\\&amp;gt;");

pageText = "&lt;pre&gt;\n" + pageText + "&lt;/pre&gt;";

String oldText = w.getText(wikiPage); if (oldText == null) {           Utils.log("ERROR: Could not get text for " + wikiPage); return; }

if (!pageText.equals(oldText)) {           if (w.editText(wikiPage, pageText, "Updated source", true)) {               w.p.outString("Updated " + wikiPage); Utils.log("Updated " + wikiPage + ""); }           else {               w.p.errString("Failed to update " + wikiPage); Utils.log("ERROR: Failed to update " + wikiPage + " (conflict)"); }       }    } }