MediaWiki:Gadget-imageForeignUseCheck.js

From PUBG Wiki
Jump to navigation Jump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// Adds a link on "File:" pages that lists the file's usage on foreign wikis (which use this wiki's shared media repository)

$(function() {
    var wgNamespaceNumber = mw.config.get('wgNamespaceNumber'),
        wgPageName = mw.config.get('wgPageName'),
        wgTitle = mw.config.get('wgTitle');

	// Only continue if the page is in the "File:" namespace
    if (wgNamespaceNumber !== 6) return;

	// Message strings
    var msgForeignUses = 'File usage (foreign)', // header for all foreign use links
        msgImageLink = 'file page', // link to foreign "File:" page of the file
        msgNoUses = 'No foreign uses of this file were detected.',
        msgListLink = 'List foreign uses', // link text
        msgLocalFileListHeader = 'File usage (local)'; // changed header for local uses

	// List of foreign wikis that will be checked
    var foreignWikis = {
    	// 'display_name': 'base_url',
        'Chinese': 'pubg.wiki.gg/zh',
        'French': 'pubg.wiki.gg/fr',
        'German': 'pubg.wiki.gg/de',
        'Hungarian': 'pubg.wiki.gg/hu',
        'Korean': 'pubg.wiki.gg/ko',
        'Polish': 'pubg.wiki.gg/pl',
        'Portuguese': 'pubg.wiki.gg/pt',
        'Russian': 'pubg.wiki.gg/ru',
        'Ukrainian': 'pubg.wiki.gg/uk',
    };

    function getForeignApiUrl(foreignWiki) {
        return 'https://' + foreignWiki + '/api.php?format=json&callback=?';
    }

    function getForeignPageUrl(foreignWiki, pageName) {
        return 'https://' + foreignWiki + '/wiki/' + pageName;
    }

    // Create link that checks the foreign wikis when clicked
	$('#mw-imagepage-reupload-link').after(
		$('<p></p>').append(
			$('<a></a>', {
                id: 'imageForeignUseCheck-link',
                href: '#ddd'
            }).text(msgListLink)
		)
	);

    // Add functionality to the link
    $('#imageForeignUseCheck-link').click(function() {
        // Clear all existing DOM elements added by this gadget
        $('ul.imageForeignUseCheck-foreignUsesList').remove();
        $('h2#imageForeignUseCheck-header').remove();
        $('li#imageForeignUseCheck-toclink').remove();

        // Change the text of the existing "File usage" header and
        // create new header for the foreign uses
        $('h2#filelinks')
            .text(msgLocalFileListHeader)
            .before(
                $('<h2></h2>', { id: 'imageForeignUseCheck-header' }).text(msgForeignUses),
                $('<ul></ul>', { 'class': 'imageForeignUseCheck-foreignUsesList' })
            );

        // Add TOC link
        $('ul#filetoc li a[href="#filelinks"]')
            .text(msgLocalFileListHeader)
            .parent('li').after(
                $('<li></li>', { id: 'imageForeignUseCheck-toclink' }).append(
                    $('<a></a>', { href: '#imageForeignUseCheck-header' }).text(msgForeignUses)
                )
            );

        // Create notice about no foreign uses
        var $foreignUsesList = $('.imageForeignUseCheck-foreignUsesList');
        $foreignUsesList.append(
            $('<h3></h3>', { 'class': 'imageForeignUseCheck-noForeignUses' }).text(msgNoUses)
        );

        // Prepare API requests on the foreign wikis
        var getImageInfo = { action: 'query', list: 'allimages', ailimit: '1', aifrom: wgTitle };
        var getImageUsage = { action: 'query', list: 'imageusage', iutitle: wgPageName };

        // Iterate over all foreign wikis
        $.each(foreignWikis, function(foreignWikiName, foreignWikiBaseUrl) {
            var foreignWikiApi = getForeignApiUrl(foreignWikiBaseUrl);
            // On the foreign wiki, check if the image exists
            $.getJSON(foreignWikiApi, getImageInfo, function(imageInfoResponse) {
                if (
                    imageInfoResponse.query.allimages.length === 0 ||
                    imageInfoResponse.query.allimages[0].name === wgTitle.replace(/ /g, '_')
                ) return;
                // On the foreign wiki, get the pages that use the image
                $.getJSON(foreignWikiApi, getImageUsage, function(imageUsageResponse) {
                    var pagesUsingTheImage = imageUsageResponse.query.imageusage;
                    if (pagesUsingTheImage.length > 0) {
                        addPageListFromOneForeignWiki(
                            $foreignUsesList,
                            { name: foreignWikiName, baseUrl: foreignWikiBaseUrl },
                            pagesUsingTheImage
                        );
                    }
                });
            });
        });
    });

    function addPageListFromOneForeignWiki($foreignUsesList, foreignWiki, pagesUsingTheImage) {
        // Remove the "No foreign uses" text that was created at the start
        $('.imageForeignUseCheck-noForeignUses').remove();
        // Add header for this foreign wiki
        $foreignUsesList.append(
            $('<h3></h3>', { style: 'margin-left:-20px;', text: foreignWiki.name }).append(
                $('<span></span>', { style: 'font-size:85%;' }).append(
                    ' (',
                    $('<a></a>', { href: getForeignPageUrl(foreignWiki.baseUrl, wgPageName) }).text(msgImageLink),
                    ')'
                )
            )
        );
        // Add list of pages that use the image
        $.each(pagesUsingTheImage, function(_, pageInfo) {
            var urlUse = getForeignPageUrl(foreignWiki.baseUrl, pageInfo.title);
            $foreignUsesList.append(
                $('<li></li>').append(
                    $('<a></a>', { href: urlUse }).text(pageInfo.title)
                )
            );
        });
    }
});