Jump to content

User:Base/Scripts/HaveWikibaseLabelLowercased.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
function haveLabelLowercased(language) {
    var api = new mw.Api();

    var token = mw.user.tokens.values.csrfToken;
    api.get({
        action: 'wbgetentities',
        ids: mw.config.get('wgTitle'),
        props: ['aliases', 'labels'],
        languages: language,
        formatversion: 2
    }).done(function (data) {
        $.each(data.entities, function () {
            var entity = this;
            entity.labels[language].value = entity.labels[language].value[0].toLowerCase() + entity.labels[language].value.substring(1);
            if (Object.keys(entity.aliases).length !== 0) {
                entity.aliases[language].forEach(alias => {
                    if (alias.value === entity.labels[language].value) {
                        alias.remove = '';
                    }
                });
            }

            api.post({
                action: 'wbeditentity',
                id: entity.id,
                lang: language,
                summary: 'Lowercasing ' + language + ' label',
                data: JSON.stringify(entity),
                token: token,
                formatversion: 2
            }).done(function (data) {
                console.log(entity.id + ' must have had its ' + language + ' label lowercased');
                location.reload();
            });
        });
    });
}

mw.loader.using(['oojs-ui-core', 'oojs-ui.styles.icons-editing-styling']).then(function () {
    var haveLabelLowercasedIcon = new OO.ui.IconWidget({
        icon: 'smaller',
        title: 'з малої',
        id: 'u-base-havelabellowercased'
    });

    $('.wikibase-title').append(haveLabelLowercasedIcon.$element.bind({
        dblclick: function (e) {
            haveLabelLowercased(mw.config.get('wgUserLanguage'));
        },
        touchstart: function (e) {
            if (confirm('Зменшити першу літеру назви?')) {
                haveLabelLowercased(mw.config.get('wgUserLanguage'));
            }
        },
    }));
});