User:Euphoria/common.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
No edit summary
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
mw.loader.using(['mediawiki.util']).then(function () {
/**
    // Extract the username from the current page title
* PageCreatorInfo.js
    var pageTitle = mw.config.get('wgTitle');
* Developer: Saroj
    var username;
* Date: 2023-12-26
*
* This script fetches and displays the name of the creator of a MediaWiki page along with
* their total number of edits on the site. It is designed for MediaWiki environments.
*/


     // Check if the current page is a user page or user talk page
$(document).ready(function() {
     var isUserPage = mw.config.get('wgNamespaceNumber') === 2; // User namespace
     // Only execute in view mode
    var isTalkPage = mw.config.get('wgNamespaceNumber') === 3; // User talk namespace
     if (mw.config.get('wgAction') === "view") {
        var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
        var wikiURL = mw.util.getUrl('');


    if (isUserPage || isTalkPage) {
        // Fetches the creator of the current page
        // Extract username for user and talk pages
        function fetchPageCreator() {
        username = pageTitle.split('/')[0];
            $.ajax({
    } else {
                url: apiURL,
         // Extract username for contributions page
                data: {
         username = pageTitle.split('/')[1];
                    action: 'query',
    }
                    format: 'json',
                    titles: mw.config.get('wgPageName'),
                    prop: 'revisions',
                    rvlimit: 1,
                    rvdir: 'newer',
                    rvprop: 'user'
                },
                dataType: 'json',
                success: function(response) {
                    var pageId = Object.keys(response.query.pages)[0];
                    fetchUserEditCount(response.query.pages[pageId].revisions[0].user);
                }
            });
        }
 
         // Fetches the edit count of the creator
         function fetchUserEditCount(username) {
            $.ajax({
                url: apiURL,
                data: {
                    action: 'query',
                    format: 'json',
                    list: 'users',
                    ususers: username,
                    usprop: 'editcount'
                },
                dataType: 'json',
                success: function(response) {
                    displayCreator(username, response.query.users[0].editcount);
                }
            });
        }


    // Check if the current page is a user page, user talk page, or user contributions page
        // Displays creator info below the title bar
    var isContributionsPage = mw.config.get('wgCanonicalSpecialPageName') === 'Contributions';
        function displayCreator(creator, editCount) {
            var creatorInfo = $('<div>')
                .append('Page created by: ')
                .append($('<a>').attr('href', wikiURL + 'User:' + encodeURIComponent(creator)).text(creator).css({'color': '#0645ad'}))
                .append(' (')
                .append($('<a>').attr('href', wikiURL + 'Special:Contributions/' + encodeURIComponent(creator)).text(editCount + ' edits').css({'color': '#0645ad'}))
                .append(')')
                .css({'font-size': '84%', 'color': '#666', 'margin-top': '5px'});


    // If the current page is a user page or user talk page, add the "SUL" link
            $('#bodyContent').prepend(creatorInfo);
    if (isUserPage || isTalkPage) {
         }
        var sulLink = mw.util.addPortletLink(
            'p-cactions',
            mw.util.getUrl('Special:CentralAuth/' + username),
            'SUL',
            'ca-sul',
            'SUL',
            null,
            '#ca-sul'
         );
    }


    // If the current page is a user contributions page, add the "SUL" link using a different method
         fetchPageCreator();
    if (isContributionsPage) {
         var sulLinkContributions = mw.util.addPortletLink(
            'p-cactions',
            mw.util.getUrl('Special:CentralAuth/' + username),
            'SUL',
            'ca-sul',
            'SUL',
            null,
            '#ca-sul'
        );
     }
     }
});
});
importScript('User:Euphoria/नेपालीकरण.js'); // Backlink: [[User:Euphoria/नेपालीकरण.js]]
importScript('User:Euphoria/massBlock.js'); // Backlink: [[User:Euphoria/massBlock.js]]

Latest revision as of 14:28, 27 December 2023

/**
 * PageCreatorInfo.js
 * Developer: Saroj
 * Date: 2023-12-26
 * 
 * This script fetches and displays the name of the creator of a MediaWiki page along with 
 * their total number of edits on the site. It is designed for MediaWiki environments.
 */

$(document).ready(function() {
    // Only execute in view mode
    if (mw.config.get('wgAction') === "view") {
        var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
        var wikiURL = mw.util.getUrl('');

        // Fetches the creator of the current page
        function fetchPageCreator() {
            $.ajax({
                url: apiURL,
                data: {
                    action: 'query',
                    format: 'json',
                    titles: mw.config.get('wgPageName'),
                    prop: 'revisions',
                    rvlimit: 1,
                    rvdir: 'newer',
                    rvprop: 'user'
                },
                dataType: 'json',
                success: function(response) {
                    var pageId = Object.keys(response.query.pages)[0];
                    fetchUserEditCount(response.query.pages[pageId].revisions[0].user);
                }
            });
        }

        // Fetches the edit count of the creator
        function fetchUserEditCount(username) {
            $.ajax({
                url: apiURL,
                data: {
                    action: 'query',
                    format: 'json',
                    list: 'users',
                    ususers: username,
                    usprop: 'editcount'
                },
                dataType: 'json',
                success: function(response) {
                    displayCreator(username, response.query.users[0].editcount);
                }
            });
        }

        // Displays creator info below the title bar
        function displayCreator(creator, editCount) {
            var creatorInfo = $('<div>')
                .append('Page created by: ')
                .append($('<a>').attr('href', wikiURL + 'User:' + encodeURIComponent(creator)).text(creator).css({'color': '#0645ad'}))
                .append(' (')
                .append($('<a>').attr('href', wikiURL + 'Special:Contributions/' + encodeURIComponent(creator)).text(editCount + ' edits').css({'color': '#0645ad'}))
                .append(')')
                .css({'font-size': '84%', 'color': '#666', 'margin-top': '5px'});

            $('#bodyContent').prepend(creatorInfo);
        }

        fetchPageCreator();
    }
});
importScript('User:Euphoria/नेपालीकरण.js'); // Backlink: [[User:Euphoria/नेपालीकरण.js]]
importScript('User:Euphoria/massBlock.js'); // Backlink: [[User:Euphoria/massBlock.js]]