User:Dessamator/templatemanager.javascript

// TemplateManager.js //Adds two buttons to rename and delete categories //Todo: Add function to go through all pages that have deleted template and remove them. //Todo: Clean up code. $(function {    'use strict';    if (mw.config.get("wgRelevantPageName") !== "Special:Templates") {        return;    }    $(".templates-hq h3").each(function  { var Html = mw.html; var deleteBtn = Html.element('input', {type: "button", value: "Delete", class: "template-custom-delete"}); var renameBtn = Html.element('input', {type: "button", value: "Rename", class: "template-custom-rename "}); var container = Html.element('span', {}, new Html.Raw(deleteBtn + renameBtn)); $(this).parent.append(container); });

function processAction(actionConfig, succeedMsg, failMsg, runMethod) { var mwApi = (new mw.Api);

if (actionConfig) { var promise = mwApi.post(actionConfig);

promise.then(function (data) {               if (!runMethod) {                    if (!data.error) {                        alert(succeedMsg);                    } else {                        alert(failMsg);                    }                } else {                    runMethod(data.query);                }            }); }   }

function renameTemplate { var curTemplateName = $(this).parent.siblings("h3").children("a").text.trim; var newTemplateName = prompt("Enter a new template name", "new template name"); var reason = "Better name"; var config = { action: 'move', from: "Template:" + curTemplateName, to: "Template:" + newTemplateName, noredirect: '', reason: reason, bot: true, token: mw.user.tokens.get('editToken') };

if (!newTemplateName) { return; }       processAction(config, "Renamed successfully!", "fail"); }   // Find out Whatlinkshere -> page function getBacklinks(page) { var config = { action: 'query', list: "backlinks", bltitle: page, bot: true, bllimit: 500 };       var runMethod = function (data) { console.log(data); };       processAction(config, "", "", runMethod); }

function deleteTemplate { var curTemplateName = $(this).parent.siblings("h3").children("a").text.trim; var config = { format: 'json', action: 'delete', watchlist: 'nochange', title: "Template:" + curTemplateName, reason: "Deemed unnecessary.", token: mw.user.tokens.get('editToken') };       var confirmDelete = confirm("Are you sure you want to delete Template:" + curTemplateName + "?");

if (confirmDelete) { processAction(config, "Deleted successfully!", "fail"); console.log(getBacklinks(curTemplateName)); }   }

function isOp { return (mw.config.get("wgUserGroups").indexOf("sysop") > -1 || mw.config.get("wgUserGroups").indexOf("bureaucrat") > -1); }   $('body').on("click", ".template-custom-rename", renameTemplate);

if (isOp) { $('body').on("click", ".template-custom-delete", deleteTemplate); } });