/* Adds buttons, using ], to perform various cleanups including replacing uder with more specific templates */
/* jshint boss: true, esversion: 6, eqeqeq: true, varstmt: true, unused: true, undef: true */
/* globals $, CleanupButtons, mw */
/* jshint esversion: 8 */
// <nowiki>
if ( .includes(mw.config.get("wgAction"))
&& mw.config.get("wgPageContentModel") === "wikitext"
// Not in edit conflict view.
&& !document.querySelector(".mw-twocolconflict-changes-col")) {
$.when(
$.getScript("//en.wiktionary.orghttps://en.wiktionary.org/w/index.php?title=User:Erutuon/scripts/CleanupButtons.js&action=raw&ctype=text/javascript"),
$.ready
).done(function () {
"use strict";
const cleanupFunctions = [
{
textBoxIncludes: `uder`,
button: { text: "to bor" },
minorEdit: true,
func:
function(content) {
const oldContent = content;
content = content.replace("{{uder", "{{bor");
CleanupButtons.addSummary(oldContent !== content, "uder > bor");
return content;
}
},
{
textBoxIncludes: `uder`,
button: { text: "to der" },
minorEdit: true,
func:
function(content) {
const oldContent = content;
content = content.replace("{{uder", "{{der");
CleanupButtons.addSummary(oldContent !== content, "uder > der");
return content;
}
},
{
textBoxIncludes: `uder`,
button: { text: "to lbor" },
minorEdit: true,
func:
function(content) {
const oldContent = content;
content = content.replace(/{{uder(+)}}/, "{{lbor$1|notext=1}}");
CleanupButtons.addSummary(oldContent !== content, "uder > lbor");
return content;
}
},
{
textBoxIncludes: `uder`,
button: { text: "to ubor" },
minorEdit: true,
func:
function(content) {
const oldContent = content;
content = content.replace(/{{uder(+)}}/, "{{ubor$1|notext=1}}");
CleanupButtons.addSummary(oldContent !== content, "uder > ubor");
return content;
}
},
{
textBoxIncludes: /(?<====Etymology===\n)(?!from)(+)/i,
button: { text: "missing from" },
minorEdit: true,
func:
function(content) {
const oldContent = content;
content = content.replace(/(?<====Etymology===\n)(?!from)(+)/i, "From $1.");
content = content.replace("..", ".");
CleanupButtons.addSummary(oldContent !== content, "add missing from");
return content;
}
},
{
textBoxIncludes: `uder`,
button: { text: "to translit" },
minorEdit: true,
func:
function(content) {
const oldContent = content;
content = content.replace(/{{uder(+)}}/, "{{translit$1|notext=1}}");
CleanupButtons.addSummary(oldContent !== content, "uder > translit");
return content;
}
},
{
textBoxIncludes: /(\|]+)(?:\|\1)?\]\]|\+)\|(]+)\]\])/,
button: { text: "templatise w" },
minorEdit: true,
func:
function(content) {
const oldContent = content;
content = content.replace(/\|]+)(?:\|\1)?\]\]/g, "{{w|$1}}");
content = content.replace(/\+)\|(]+)\]\]/g, "{{w|$1|$2}}");
CleanupButtons.addSummary(oldContent !== content, "templatise wikipedia links");
return content;
}
},
{
textBoxIncludes: /\+\]\]/i,
button: { text: "templatise categories" },
minorEdit: true,
func: function (content) {
const oldContent = content;
function getLanguageCodeToCanonicalName() {
const CACHE_DURATION = 24 * 60 * 60; // 24 hours
const KEY = "enwiktLanguageCodeToCanonicalNameJson";
let timeNow = new Date().getTime() * 1e-3;
try {
let cachedData = JSON.parse(localStorage.getItem(KEY));
if (timeNow - cachedData.timestamp < CACHE_DURATION)
return Promise.resolve(cachedData.data);
} catch (e) { }
const actionAPI = new mw.Api({ ajax: { headers: { "Api-User-Agent": "]" } } });
return actionAPI.get({
"action": "parse",
"page": "Module:languages/code to canonical name.json",
"prop": "wikitext",
"formatversion": "2",
"format": "json"
})
.then(response => {
let languageData = JSON.parse(response.parse.wikitext);
localStorage.setItem(KEY, JSON.stringify({
timestamp: timeNow,
data: languageData
}));
return languageData;
});
}
function getCode(canonicalName) {
if (window.cachedCodeToLangname) {
return Promise.resolve(cachedCodeToLangname.get(canonicalName));
}
// reuse the same browser cache as that used by Surjection's gadget
return getLanguageCodeToCanonicalName().then(languageData => {
// invert the map, this is O(n) but only happens once
let inverted = new Map(Object.entries(languageData).map(() => ));
window.cachedCodeToLangname = inverted;
return window.cachedCodeToLangname.get(canonicalName);
});
}
let categories = :]+)/gi)];
if (categories.length === 0) {
console.error("cleanup.js found no raw category links");
return;
}
let promises = categories.map(category => {
let fullMatch = category; // the whole ]
let langName = category; // just the langname
return getCode(langName).then(langCode => ({ fullMatch, langName, langCode }));
});
return Promise.all(promises).then(results => {
results.forEach(({ fullMatch, langName, langCode }) => {
if (langCode) {
let clnTemplate = `{{cln|${langCode}|...}}`;
content = content.replace(fullMatch, clnTemplate);
} else {
console.error("cleanup.js could not find a language code for ", langName);
}
});
CleanupButtons.addSummary(oldContent !== content, "templatise category links");
return content;
});
}
},
{
condition: mw.config.get('wgNamespaceNumber') === 118,
button: { text: "add {{reconstructed}}" },
minorEdit: true,
func: function(content) {
const oldContent = content;
content = content.replace(/^/, "{{reconstructed}}\n");
CleanupButtons.addSummary(oldContent !== content, "add {{reconstructed}}")
return content;
}
},
];
const buttons = new CleanupButtons();
for ( const buttonInfo of cleanupFunctions )
if (CleanupButtons.evaluateConditions(buttonInfo.condition, buttonInfo.textBoxIncludes))
buttons.addButton(buttonInfo);
});
}
// </nowiki>