User:MGA73/nowcommonsreview.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.
//<source lang=javascript>
/*global $, mw */
/**
 * NowCommonsReview tagger
 * URL: http://en.wikipedia.org/wiki/User:MGA73/nowcommonsreview.js
 * Credits:
 * MGA73, ZooFari, Krinkle
 *
 * Based on http://commons.wikimedia.org/wiki/User:ZooFari/licensereviewer.js by Patstuart
 */

(function (window, $){
    window.nowcommonsreview = "0.20 (2010-06-23)";
    $.when(mw.loader.using( ["mediawiki.util"] ), $.ready.promise).then(function () {
        if (mw.config.get("wgNamespaceNumber") === 6) {
            var actions = [
                // NowCommons review passed
                {
                    fn: function (){
                        var needAppend = document.editform.wpTextbox1.value;
                        document.editform.wpTextbox1.value = needAppend.replace(/({{([Nn]ow|[Dd]b-now|[Mm]oved *to) *(C|c)ommons([^\}]*?)|[Dd]b-[Ff]8)}}/g, "$1|reviewer=" + mw.config.get("wgUserName") + "}}");

                        document.editform.wpSummary.value = "[[Template:NowCommons|NowCommons review]] passed (using [[User:MGA73/nowcommonsreview.js|nowcommonsreviewer]])";
                        document.editform.wpMinoredit.checked = true;

                        document.editform.submit();
                    },
                    name: "NowCommonsReviewOK",
                    displayName: "NowCommons OK"
                },

                // NowCommons review failed
                {
                    fn: function (){
                        var needAppend = document.editform.wpTextbox1.value;

                        var reason = window.prompt("Reason for failed NowCommons review:","");
                        /* on cancel button, actually cancel */
                        if (reason !== null) {
                            document.editform.wpTextbox1.value = needAppend.replace(/({{([Nn]ow|[Dd]b-now|[Mm]oved *to) *(C|c)ommons([^\}]*?)|[Dd]b-[Ff]8)}}/g, "$1|reason=" + reason + "}}").replace(/\{\{([Nn]ow|[Dd]b-now|[Mm]oved *to) *(C|c)ommons/g, "{{NotMovedToCommons");

                            document.editform.wpSummary.value = "[[Template:NowCommons|NowCommons review]] failed (using [[User:MGA73/nowcommonsreview.js|nowcommonsreviewer]])";
                            document.editform.wpMinoredit.checked = true;

                            document.editform.submit();
                        }
                    },
                    name: "NowCommonsReviewNotOK",
                    displayName: "NowCommons Not OK"
                }
            ];


            // [review] link inserter
            var html;
            if (mw.config.get("wgCategories").join("|").match(/Wikipedia files reviewed on Wikimedia Commons/)){
                // This page is in a reviewed category. Dont display links.
                html = "<small>Already reviewed</small>";

            } else { // This NowCommons page isn't reviewed yet. Show links.
                html = actions.map(function (action){
                    return "[" + $("<a></a>").attr("href", mw.config.get("wgScript") + "?title=" +
                            encodeURIComponent(mw.config.get("wgPageName")) + "&action=edit&functionName=" +
                            action.name).text(action.displayName)[0].outerHTML + "]";
                }).join(" ");
            }
            $("div.nowcommons-reviewme").css("text-align", "center").html(html);

            // Link/Button maker
            actions.forEach(function (action){
                //functionNameString, buttonDisplayName, callback
                if (mw.util.getParamValue("functionName") == action.name){
                    action.fn(true);
                } else {
                    var _href;
                    var callback;
                    if (["edit", "submit"].indexOf(mw.config.get("wgAction")) > -1){
                        _href = "javascript:void(0);";
                        callback = action.fn;
                    } else {
                        _href = mw.config.get("wgScript") + "?title=" + encodeURIComponent(mw.config.get("wgPageName")) + "&action=edit&functionName=" + action.name;
                    }

                    // Add buttons
                    [["cactions", "ca"], ["tb", "tb"]].forEach(function (actionData) {
                        var node = mw.util.addPortletLink("p-" + actionData[0], _href, action.displayName, "ca-" + actionData[1], "", null, 0);
                        if (callback) {
                            $(node).click(callback);
                        }
                    });
                }
            });
        }
    });
}(window, $));
//</source>