Git Product home page Git Product logo

Comments (2)

Krinkle avatar Krinkle commented on June 8, 2024 1

I created a quick draft of this for my own bot. See https://gist.github.com/Krinkle/8e1e0e41baaae63f9839d86d918d512c/c1788e5ececff3ddf373944d3dc41ced99be08af#file-wmf-tour-bot-js-L107-L139. Feel free to use it as in any way you like.

    client.edit = function(pageData, content, summary, minor, callback) {
        var params = {
            text: content,
            // Avoid accidentally editing as anonymous user if session expires
            assert: 'user'
        };

        if (typeof minor === 'function') {
            callback = minor;
            minor = undefined;
        }

        if (minor) {
            params.minor = '';
        } else {
            params.notminor = '';
        }

        var title;
        if (typeof pageData === 'object') {
            params.basetimestamp = pageData.revision.timestamp;
            params.starttimestamp = new Date().toISOString();
            // Avoid accidentally creating a new page (e.g. if title string got corrupted,
            // or if page was deleted meanwhile).
            params.nocreate = '';
            title = pageData.title;
        } else {
            title = pageData;
        }

        this.doEdit('edit', title, summary, params, callback);
    };

This protects bots against various problems. The same protections that the MediaWiki web interface uses when you edit via a web browser:

  • Session loss or expiration (assert=user). Avoid making edits as anonymous user if server lost the session.
  • Edit conflict (basetimestamp). Avoid overriding other edits.
  • Re-create (starttimestamp). Avoid re-publishing text that was deleted.
  • Edit type (nocreate/createonly). Avoid creating new pages or edits. Sometimes if the page title is corrupted (e.g. bad encoding) it can happen that the content is fetched from A but saved to B - causing a duplicate page to be created. (E.g. If spaces are cut off and you fetch from "Foo bar" and save to "Foo", or a character encoding problem).

See https://www.mediawiki.org/wiki/API:Edit

from nodemw.

macbre avatar macbre commented on June 8, 2024

@Krinkle, thanks for the code snippet. assert=user can be added automatically when bot operates in logged-in mode.

from nodemw.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.