User:Suyash.dwivedi/common.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
importScript('User:Kanonkas/twinkle.js');
importScript('User:Majora/LicenseReview.js');
window.hotcat_single_changes_are_minor = false; // Or true
importScript('User:Perhelion/justReplace.js');
if (mw.config.get('wgNamespaceNumber') === 6 && /SVG/i.test(mw.config.get('wgTitle').slice(-3)))
	importScript('User:Perhelion/simpleSVGcheck.js');
mw.loader.load( 'ext.gadget.VisualFileChange' );
window.vFC_PortletText = 'Suyash-Visual File Change';
importScript('https://de.wikipedia.org/w/index.php?title=Benutzer:DerHexer/massendiskus.js');
////////////////////////
$(document).ready(function() {
    // Function to fetch total files by a specific user and calculate pagination
    function fetchTotalFiles(username, itemsPerPage) {
        $.getJSON(
            mw.util.wikiScript('api'), 
            {
                action: 'query',
                list: 'usercontribs',
                ucuser: username,
                uclimit: 'max', // Fetch max items to get the count
                ucnamespace: 6, // Namespace for files
                format: 'json'
            },
            function(data) {
                if (data.query && data.query.usercontribs) {
                    const totalItems = data.query.usercontribs.length;
                    const totalPages = Math.ceil(totalItems / itemsPerPage);
                    createPagination(totalItems, currentPage, totalPages);
                } else {
                    // Handle case where no files are found
                    createPagination(0, currentPage, 0);
                }
            }
        ).fail(function() {
            console.error('Error fetching data from MediaWiki API');
            createPagination(0, currentPage, 0);
        });
    }

    // Function to create and display pagination
    function createPagination(totalItems, currentPage, totalPages) {
        const paginationContainer = $('#pagination-container');
        paginationContainer.empty(); // Clear previous pagination

        // Display a message if no files are found
        if (totalItems === 0) {
            paginationContainer.append('<span>No files found.</span>');
            return;
        }

        // Create Previous Page Link
        if (currentPage > 1) {
            $('<a>', {
                text: 'Previous',
                href: `?page=${currentPage - 1}`,
                class: '',
            }).appendTo(paginationContainer);
        }

        // Create Page Number List
        for (let i = 1; i <= totalPages; i++) {
            $('<a>', {
                text: i,
                href: `?page=${i}`,
                class: i === currentPage ? 'active' : '',
            }).appendTo(paginationContainer);
        }

        // Create Next Page Link
        if (currentPage < totalPages) {
            $('<a>', {
                text: 'Next',
                href: `?page=${currentPage + 1}`,
                class: '',
            }).appendTo(paginationContainer);
        }
    }

    // Example dynamic values (these should be set based on your actual pagination)
    const currentPage = parseInt(new URLSearchParams(window.location.search).get('page')) || 1; // Get the current page number from the URL
    const itemsPerPage = 20; // Set this based on user selection or default

    // Extract the user ID from the URL
    const title = decodeURIComponent(window.location.pathname.split('/').pop());
    const username = title.split(':')[1]; // Get the username from the title

    // Fetch total files for the specific user and create pagination
    if (username) {
        fetchTotalFiles(username, itemsPerPage);
    }
});

////////////////////////////////////