Search in sources :

Example 1 with XMLDocument

use of com.laytonsmith.PureUtilities.XMLDocument in project CommandHelper by EngineHub.

the class DocGenUIHandler method doUpload.

/**
 * Uploads a file to a page. If protect is null, the protections are left as is. If protect is false, protections
 * are removed if they are present. If protect is true, protections are added if they are not present.
 *
 * @param wikiMarkup
 * @param page
 * @param protect
 * @throws XPathExpressionException
 */
void doUpload(String wikiMarkup, String page, Boolean protect) throws XPathExpressionException {
    checkStop();
    if (page.startsWith("/")) {
        // The prefix already has this
        page = page.substring(1);
    }
    wikiMarkup = pagePrefix + wikiMarkup;
    // The full path
    String fullPath = prefix + page;
    progress.setStatus("Uploading " + fullPath);
    // First we need to get the edit token
    XMLDocument content = getXML(endpoint, mapCreator("action", "query", "titles", fullPath, "prop", "revisions", "rvprop", "sha1", "format", "xml"));
    checkStop();
    String sha1 = content.getNode("/api/query/pages/page/revisions/rev/@sha1");
    String sha1local = getSha1(wikiMarkup);
    if (!sha1.equals(sha1local)) {
        XMLDocument query = getXML(endpoint, mapCreator("action", "query", "titles", fullPath, "prop", "info", "intoken", "edit", "format", "xml"));
        checkStop();
        String edittoken = query.getNode("/api/query/pages/page/@edittoken");
        XMLDocument edit = getXML(endpoint, mapCreator("action", "edit", "title", fullPath, "text", wikiMarkup, "summary", "Automatic documentation update. (This is a bot edit)", "bot", "true", "format", "xml", // This must always come last
        "token", edittoken), false);
    }
    if (protect != null) {
        XMLDocument query = getXML(endpoint, mapCreator("action", "query", "titles", fullPath, "prop", "info", "inprop", "protection", "intoken", "protect", "format", "xml"));
        checkStop();
        String protectToken = query.getNode("/api/query/pages/page/@protecttoken");
        boolean isProtectedEdit = false;
        boolean isProtectedMove = false;
        if (query.nodeExists("/api/query/pages/page/protection/pr")) {
            for (int i = 1; i <= query.countChildren("/api/query/pages/page/protection"); i++) {
                // If only sysops can edit and move
                if (query.getNode("/api/query/pages/page/protection/pr[" + i + "]/@level").equals("sysop") && query.getNode("/api/query/pages/page/protection/pr[" + i + "]/@type").equals("edit")) {
                    isProtectedEdit = true;
                }
                if (query.getNode("/api/query/pages/page/protection/pr[" + i + "]/@level").equals("sysop") && query.getNode("/api/query/pages/page/protection/pr[" + i + "]/@type").equals("move")) {
                    isProtectedMove = true;
                }
            }
        }
        boolean isProtected = false;
        if (isProtectedEdit && isProtectedMove) {
            isProtected = true;
        }
        if (protect && !isProtected) {
            // Protect it
            getXML(endpoint, mapCreator("action", "protect", "title", fullPath, "token", protectToken, "protections", "edit=sysop|move=sysop", "expiry", "infinite", "reason", "Autoprotecting page (This is a bot edit)"));
        } else if (!protect && isProtected) {
            // Unprotect it
            getXML(endpoint, mapCreator("action", "protect", "title", fullPath, "token", protectToken, "protections", "edit=autoconfirmed|move=autoconfirmed", "expiry", "infinite", "reason", "Autoprotecting page (This is a bot edit)"));
        }
    }
    incProgress();
}
Also used : XMLDocument(com.laytonsmith.PureUtilities.XMLDocument)

Example 2 with XMLDocument

use of com.laytonsmith.PureUtilities.XMLDocument in project CommandHelper by EngineHub.

the class DocGenUIHandler method doLogin.

private void doLogin() throws MalformedURLException, XPathExpressionException {
    checkStop();
    XMLDocument login = getXML(endpoint, mapCreator("format", "xml", "action", "login", "lgname", username, "lgpassword", password));
    if ("NeedToken".equals(login.getNode("/api/login/@result"))) {
        XMLDocument login2 = getXML(endpoint, mapCreator("format", "xml", "action", "login", "lgname", username, "lgpassword", password, "lgtoken", login.getNode("/api/login/@token")));
        if (!"Success".equals(login2.getNode("/api/login/@result"))) {
            if ("WrongPass".equals(login2.getNode("/api/login/@result"))) {
                throw new APIException("Wrong password.");
            }
            throw new APIException("Could not log in successfully.");
        }
    }
    progress.setStatus("Logged in");
    password = null;
}
Also used : XMLDocument(com.laytonsmith.PureUtilities.XMLDocument)

Example 3 with XMLDocument

use of com.laytonsmith.PureUtilities.XMLDocument in project CommandHelper by EngineHub.

the class Static method getMSObject.

/**
 * Given a java object, returns a MethodScript object.
 *
 * @param object
 * @param t
 * @return
 */
public static Construct getMSObject(Object object, Target t) {
    if (object == null) {
        return CNull.NULL;
    } else if (object instanceof Boolean) {
        return CBoolean.get((boolean) object);
    } else if ((object instanceof Byte) || (object instanceof Short) || (object instanceof Integer) || (object instanceof Long)) {
        return new CInt((long) object, t);
    } else if ((object instanceof Float) || (object instanceof Double)) {
        return new CDouble((double) object, t);
    } else if (object instanceof Character) {
        return new CString((char) object, t);
    } else if (object instanceof String) {
        return new CString((String) object, t);
    } else if (object instanceof StringBuffer) {
        return new CResource<>((StringBuffer) object, new CResource.ResourceToString() {

            @Override
            public String getString(CResource res) {
                return res.getResource().toString();
            }
        }, t);
    } else if (object instanceof XMLDocument) {
        return new CResource<>((XMLDocument) object, t);
    } else if (object instanceof Construct) {
        return (Construct) object;
    } else if (object instanceof boolean[]) {
        boolean[] array = (boolean[]) object;
        CArray r = new CArray(t);
        for (boolean b : array) {
            r.push(CBoolean.get(b), t);
        }
        return r;
    } else if (object instanceof byte[]) {
        return CByteArray.wrap((byte[]) object, t);
    } else if (object instanceof char[]) {
        char[] array = (char[]) object;
        CArray r = new CArray(t);
        for (char c : array) {
            r.push(new CString(c, t), t);
        }
        return r;
    } else if (object instanceof short[]) {
        short[] array = (short[]) object;
        CArray r = new CArray(t);
        for (short s : array) {
            r.push(new CInt(s, t), t);
        }
        return r;
    } else if (object instanceof int[]) {
        int[] array = (int[]) object;
        CArray r = new CArray(t);
        for (int i : array) {
            r.push(new CInt(i, t), t);
        }
        return r;
    } else if (object instanceof long[]) {
        long[] array = (long[]) object;
        CArray r = new CArray(t);
        for (long l : array) {
            r.push(new CInt(l, t), t);
        }
        return r;
    } else if (object instanceof float[]) {
        float[] array = (float[]) object;
        CArray r = new CArray(t);
        for (float f : array) {
            r.push(new CDouble(f, t), t);
        }
        return r;
    } else if (object instanceof double[]) {
        double[] array = (double[]) object;
        CArray r = new CArray(t);
        for (double d : array) {
            r.push(new CDouble(d, t), t);
        }
        return r;
    } else if (object instanceof Object[]) {
        CArray r = new CArray(t);
        for (Object o : (Object[]) object) {
            r.push((o == object) ? r : getMSObject(o, t), t);
        }
        return r;
    } else if (object instanceof Collection) {
        return getMSObject(((Collection) object).toArray(), t);
    } else if (object instanceof Map) {
        Map map = ((Map) object);
        CArray r = new CArray(t);
        for (Object key : map.keySet()) {
            Object o = map.get(key);
            r.set(key.toString(), (o == object) ? r : getMSObject(o, t), t);
        }
        return r;
    } else {
        return new CString(object.toString(), t);
    }
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString) XMLDocument(com.laytonsmith.PureUtilities.XMLDocument) CString(com.laytonsmith.core.constructs.CString) CBoolean(com.laytonsmith.core.constructs.CBoolean) CResource(com.laytonsmith.core.constructs.CResource) CDouble(com.laytonsmith.core.constructs.CDouble) CDouble(com.laytonsmith.core.constructs.CDouble) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

XMLDocument (com.laytonsmith.PureUtilities.XMLDocument)3 CArray (com.laytonsmith.core.constructs.CArray)1 CBoolean (com.laytonsmith.core.constructs.CBoolean)1 CDouble (com.laytonsmith.core.constructs.CDouble)1 CInt (com.laytonsmith.core.constructs.CInt)1 CResource (com.laytonsmith.core.constructs.CResource)1 CString (com.laytonsmith.core.constructs.CString)1 Construct (com.laytonsmith.core.constructs.Construct)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1