Jump to content

User:Minesweeper.007/auto.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// $Id: autoedit.js 1377 2006-08-23 08:43:24Z quarl $

// autoedit.js - tab menu & framework for automatically editing
// wiki pages

// SYNOPSIS:
//    var myeditor = new $autoedit('myeditor', 'MyEdit', 'ca-myedit', 'Do my edits', 'Edit summary prefix');
//    myeditor.initData = function() { this.foo=5; } /* optional */
//    myeditor.splitText = function(s) { return s.split('\n'); } /* optional */
//    myeditor.buildRegExp = function() { return /foo/ }
//    myeditor.replaceRegExp = function(s) { return + "("+s+")" }

// USER USAGE:
//    myeditor.widgetLoad();

// quarl 2006-02-08 initial version, refactoring *_canonicalize.js scripts

var $autoedit = new Module('autoedit.js');
$autoedit.depend('wikipage.js','wikiedit.js', 'util.js',
                 'wikiwidget.js', 'token.js');

$autoedit.editors = {};

$autoedit.options = { submitButton : 'wpDiff' };

// auto edit if query string asks us to (more commonly this is not necessary;
// user will invoke through tab)
$autoedit.load = function() {
    var v = WikiDocument.queryVars['autoedit'];
    if (!v) return;

    var autoeditor = $autoedit.editors[v];
    if (!autoeditor || !autoeditor.varname) {
        $autoedit.error("$autoedit: invalid autoeditor! (error ae2a3784-7daf-49fb-9c6c-b68a7f28dc63)");
        return;
    }

    if (WikiDocument.queryVars['auth'] != autoeditor._makeAuthToken(wikiPage)) {
        $autoedit.error("$autoedit: invalid auth token! (error a1908c50-620b-4104-bcfa-881ecf094442)");
        return;
    }

    setTimeout(function(){autoeditor.run();}, 50);
}

$autoedit._getMenu = function() {
    if (!$autoedit._menuAutoEdit) {
        $autoedit._menuAutoEdit = $wikiwidget.addTabMenu('AutoEdit', 'mn-autoedit');
    }
    return $autoedit._menuAutoEdit;
}

$autoedit.AutoEditor = function(varname, name, id, title, summaryPrefix) {
    this.varname = varname;
    this.summaryPrefix = summaryPrefix;
    this.name = name;
    this.id = id;
    this.title = title;

    this.widgetLoad = $util.bindThis(this, this.widgetLoad_);

    $autoedit.editors[varname] = this;
};

$autoedit.AutoEditor.prototype = {
    widgetLoad_ : function(location) {
        var me = this;
        if (!(me instanceof $autoedit.AutoEditor)) {
            $autoedit.error("widgetLoad: need AutoEditor instance (error 87bd9bfd-7ff6-4577-a59c-488936fa536c)");
            return;
        }
        $util.addOnloadHook(function() {
                if (wikiPage.nsSpecialP) return;
                if (wikiDoc.protectedP) return;

                this.widget = new WikiWidget({default_location: $autoedit._getMenu,
                                              onclick: $util.bindThis(me, me.run),
                                              // href: me.makeEditUrl(wikiPage),
                                              name: me.name, id: me.id, title: me.title});
                this.widget.add(location);
                });
    },

    _makeAuthToken: function(wp) {
        return $token.makeAuthToken('autoedit', this.varname, wp.page);
    },

    makeEditUrl : function(wp) {
        return (wp.qurl + '&action=edit&autoedit=' + this.varname + '&auth=' +
                this._makeAuthToken(wp));
    },

    run : function() {
        this.initData();
        $util.buttonShowStatus(this.tab);
        wikiPage.getEditorAsync(this._edit0, this);
        return false;
    },

    _edit0 : function(editor, this_) {
        this_._edit1(editor);
    },

    _edit1 : function(editor) {
        var result = '';
        var input = editor.wpTextbox1;
        var changes = [];

        var inputs = this.splitText(input);

        var result = this.editStrings(inputs, changes);

        $util.buttonRestoreStatus(this.tab);
        if (changes.length) {
            editor.wpTextbox1 = result;
            editor.wpSummary = this.summaryPrefix + ': ' + changes.join('; ');
            editor.wpMinoredit = true;
            editor.submit($autoedit.options.submitButton);
        } else {
            alert("No changes to make!");
        }
    },

    initData : function() {
        /* TO BE OVERRIDDEN */
        // default: nothing to initialize
    },

    splitText : function(s) {
        /* TO BE OVERRIDDEN */
        // default: don't split at all
        return [s];
    },

    editStrings : function(inputs, changes) {
        for (var i in inputs) {
            inputs[i] = this.editString(inputs[i], changes);
        }
        return inputs.join('');
    },

    editString : function(input, changes) {
        /* TO BE OVERRIDDEN */
        return this.regexpEditString(input, changes);
    },

    regexpEditString : function(input, changes) {
        var result = '';

        var regexp = this.buildRegExp();
        var m;
        while ((m=input.match(regexp))) {
            result += RegExp.leftContext;

            // This descriptor is passed in for input as well as taken as output.
            //
            // LEFT is the text on the left of the match; TEXT is the text itself;
            // RIGHT is the text on the right of the match.  All 3 can be modified.
            //
            // Return value of replaceRegExp overrides d.text if non-null --
            // it's simply a convenience feature.

            var d = { left: result, text: m[0], right: RegExp.rightContext };

            var dresult = this.replaceRegExp(d, m);
            if (dresult != null) {
                d.text = dresult;
            }

            if (d.text != m[0]) {
                changes.push('"'+m[0]+'"' + ' → ' +'"'+d.text+'"');
            }
            result = d.left + d.text;
            input = d.right;
        }
        result += input;
        return result;
    },

    buildRegExp : function() {
        $autoedit.error("## $autoedit.AutoEditor.buildRegExp() not overridden! (38c826f0-9f24-4ce2-9708-567d8e55a7c5)");
    },

    replaceRegExp : function(d, match) {
        // d.left
        // d.text
        // d.right
        $autoedit.error("## $autoedit.AutoEditor.replaceRegExp() not overridden! (1a791fee-8020-453f-adb1-df3c69dc6828)");
    },

    // $autoedit._findCategoryContentTable = function() {
    //     var tables = document.getElementsByTagName('table');
    //     for (var i = 0; i < tables.length; ++i) {
    //         var table = tables[i];
    //         if (table.previousSibling.textContent.match(/^There are/)) {
    //             return table;
    //         }
    //     }
    //     return false;
    // }

    // prefix [varname] buttons before all wikilinks.  Good for category pages.
    addAutoEditButtons : function() {
        // if (!wikiPage.nsCategoryP) return;

        // var table = $autoedit._findCategoryContentTable();
        // if (!table) return;

        var d = document.getElementById('bodyContent');

        var links = $util.copyArray(d.getElementsByTagName('a'));
        for (var i = 0; i < links.length; ++i) {
            var link = links[i];
            if (!link.href || !link.href.match(/\/wiki\//)) continue;
            if (link.href.match(/#/)) continue;
            if (link.className.match(/external/)) continue;
            var wp = new WikiPage(link);
            var url = this.makeEditUrl(wp);

            var span = document.createElement('span');
            span.innerHTML = '[<a href="'+url+'">'+this.varname+'</a>] ';
            $util.addNodeBefore(link, span);
        }
    }
};

$util.addOnloadHook($autoedit.load);