Jump to content

MediaWiki:Common.js: Difference between revisions

From Gyaanipedia
No edit summary
Tags: Mobile edit Mobile web edit
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */


/**
// Custom Random Page with Minimum Word Count
* MediaWiki Common.js - Sort Special:AllPages alphabetically A-Z
$(document).ready(function() {
* This script reorganizes the page listing on Special:AllPages to display
     // Override the default random page link behavior
* pages in strict alphabetical order from A to Z
     $('a[href*="Special:Random"]').on('click', function(e) {
*/
         e.preventDefault(); // Prevent default link behavior
 
(function() {
    'use strict';
   
     // Only run on Special:AllPages
     if (mw.config.get('wgCanonicalSpecialPageName') !== 'Allpages') {
        return;
    }
   
    // Wait for the page to fully load
    mw.loader.using(['mediawiki.util'], function() {
         $(document).ready(function() {
            sortAllPages();
        });
    });
   
    function sortAllPages() {
        // Find the content area containing the page list
        var $content = $('.mw-allpages-body, .mw-allpages-chunk');
          
          
         if ($content.length === 0) {
         // Function to count words in text
             // Fallback: try to find the list container
        function countWords(text) {
            $content = $('.mw-content-ltr ul').first();
             return text.replace(/[\s\n]+/g, ' ').trim().split(' ').length;
         }
         }
          
          
         if ($content.length === 0) {
         // Function to fetch a random page and check word count
            console.log('AllPages sort: Could not find page list container');
        function getRandomPageWithMinWords(minWords) {
             return;
            $.ajax({
                url: mw.util.wikiScript('api'),
                data: {
                    action: 'query',
                    format: 'json',
                    list: 'random',
                    rnnamespace: 0, // Main namespace only
                    rnlimit: 1
                },
                dataType: 'json',
                success: function(data) {
                    if (data.query && data.query.random && data.query.random.length > 0) {
                        var pageId = data.query.random[0].id;
                        // Fetch page content
                        $.ajax({
                            url: mw.util.wikiScript('api'),
                            data: {
                                action: 'query',
                                format: 'json',
                                prop: 'revisions',
                                pageids: pageId,
                                rvprop: 'content',
                                rvslots: 'main'
                            },
                            dataType: 'json',
                            success: function(contentData) {
                                var pageContent = contentData.query.pages[pageId].revisions[0].slots.main['*'];
                                var wordCount = countWords(pageContent);
                               
                                if (wordCount >= minWords) {
                                    // Redirect to the page if it meets the word count
                                    window.location = mw.util.getUrl(contentData.query.pages[pageId].title);
                                } else {
                                    // Retry if word count is too low
                                    getRandomPageWithMinWords(minWords);
                                }
                            },
                            error: function() {
                                // Retry on error
                                getRandomPageWithMinWords(minWords);
                            }
                        });
                    } else {
                        // Retry if no page is returned
                        getRandomPageWithMinWords(minWords);
                    }
                },
                error: function() {
                    // Retry on error
                    getRandomPageWithMinWords(minWords);
                }
             });
         }
         }
          
          
         // Get all list items (pages)
         // Start the process with minimum 50 words
         var $lists = $content.find('ul');
         getRandomPageWithMinWords(50);
       
    });
        $lists.each(function() {
});
            var $list = $(this);
            var $items = $list.find('> li').detach();
           
            if ($items.length === 0) {
                return;
            }
           
            // Sort items: letters first (A-Z), then numbers
            $items.sort(function(a, b) {
                var textA = $(a).text().trim().toUpperCase();
                var textB = $(b).text().trim().toUpperCase();
               
                // Remove any leading special characters for sorting
                textA = textA.replace(/^[^A-Z0-9]+/i, '');
                textB = textB.replace(/^[^A-Z0-9]+/i, '');
               
                // Check if strings start with a letter or number
                var startsWithLetterA = /^[A-Z]/i.test(textA);
                var startsWithLetterB = /^[A-Z]/i.test(textB);
               
                // If one starts with letter and other with number, letter comes first
                if (startsWithLetterA && !startsWithLetterB) return -1;
                if (!startsWithLetterA && startsWithLetterB) return 1;
               
                // Both start with letters or both start with numbers - normal sort
                if (textA < textB) return -1;
                if (textA > textB) return 1;
                return 0;
            });
           
            // Re-append sorted items
            $items.each(function() {
                $list.append(this);
            });
        });
       
        console.log('AllPages sort: Pages sorted alphabetically A-Z');
    }
})();

Revision as of 22:53, 27 October 2025

/* Any JavaScript here will be loaded for all users on every page load. */

// Custom Random Page with Minimum Word Count
$(document).ready(function() {
    // Override the default random page link behavior
    $('a[href*="Special:Random"]').on('click', function(e) {
        e.preventDefault(); // Prevent default link behavior
        
        // Function to count words in text
        function countWords(text) {
            return text.replace(/[\s\n]+/g, ' ').trim().split(' ').length;
        }
        
        // Function to fetch a random page and check word count
        function getRandomPageWithMinWords(minWords) {
            $.ajax({
                url: mw.util.wikiScript('api'),
                data: {
                    action: 'query',
                    format: 'json',
                    list: 'random',
                    rnnamespace: 0, // Main namespace only
                    rnlimit: 1
                },
                dataType: 'json',
                success: function(data) {
                    if (data.query && data.query.random && data.query.random.length > 0) {
                        var pageId = data.query.random[0].id;
                        // Fetch page content
                        $.ajax({
                            url: mw.util.wikiScript('api'),
                            data: {
                                action: 'query',
                                format: 'json',
                                prop: 'revisions',
                                pageids: pageId,
                                rvprop: 'content',
                                rvslots: 'main'
                            },
                            dataType: 'json',
                            success: function(contentData) {
                                var pageContent = contentData.query.pages[pageId].revisions[0].slots.main['*'];
                                var wordCount = countWords(pageContent);
                                
                                if (wordCount >= minWords) {
                                    // Redirect to the page if it meets the word count
                                    window.location = mw.util.getUrl(contentData.query.pages[pageId].title);
                                } else {
                                    // Retry if word count is too low
                                    getRandomPageWithMinWords(minWords);
                                }
                            },
                            error: function() {
                                // Retry on error
                                getRandomPageWithMinWords(minWords);
                            }
                        });
                    } else {
                        // Retry if no page is returned
                        getRandomPageWithMinWords(minWords);
                    }
                },
                error: function() {
                    // Retry on error
                    getRandomPageWithMinWords(minWords);
                }
            });
        }
        
        // Start the process with minimum 50 words
        getRandomPageWithMinWords(50);
    });
});