User:Polygnotus/Scripts/SortByEditcount.js

This is an old revision of this page, as edited by Polygnotus (talk | contribs) at 14:08, 8 July 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
async function processUsersForEditCounts(input, checkButton, progressBar, statusMessage, outputField, copyButton) {
    const allUsers = parseUsers(input);
    const users = deduplicateUsers(allUsers);
    
    if (users.length === 0) {
        mw.notify('No valid usernames found in the input.', { type: 'error' });
        return;
    }

    // Show deduplication info
    const duplicateCount = allUsers.length - users.length;
    if (duplicateCount > 0) {
        statusMessage.setLabel(`Found ${allUsers.length} usernames, removed ${duplicateCount} duplicates. Checking ${users.length} unique users...`);
        console.log(`Found ${allUsers.length} usernames, removed ${duplicateCount} duplicates.`);
    } else {
        statusMessage.setLabel(`Checking ${users.length} users for edit counts...`);
    }

    // Show progress bar and disable check button
    progressBar.setProgress(0);
    progressBar.toggle(true);
    checkButton.setDisabled(true);
    checkButton.setLabel('Checking...');

    const results = [];
    const errors = [];

    console.log(`Checking ${users.length} users for edit counts...`);

    for (let i = 0; i < users.length; i++) {
        const userInfo = users[i];
        const progress = Math.round(((i + 1) / users.length) * 100);
        
        progressBar.setProgress(progress);
        statusMessage.setLabel(`[${i + 1}/${users.length}] Checking ${userInfo.username}...`);
        
        console.log(`[${i + 1}/${users.length}] Checking edit count for: ${userInfo.username}`);

        try {
            const editCount = await getUserEditCount(userInfo.username);
            results.push({
                original: userInfo.original,
                username: userInfo.username,
                editCount: editCount
            });
            console.log(`✓ ${userInfo.username}: ${editCount.toLocaleString()} edits`);
        } catch (error) {
            console.error(`Failed to get edit count for ${userInfo.username}:`, error);
            results.push({
                original: userInfo.original,
                username: userInfo.username,
                editCount: -1 // Use -1 to indicate error, will sort to bottom
            });
            errors.push(userInfo.username);
        }

        // Add delay between requests to avoid hammering the API
        if (i < users.length - 1) {
            await sleep(1000);
        }
    }

    // Sort results by edit count (descending order)
    results.sort((a, b) => {
        // Put errors at the bottom
        if (a.editCount === -1 && b.editCount !== -1) return 1;
        if (b.editCount === -1 && a.editCount !== -1) return -1;
        if (a.editCount === -1 && b.editCount === -1) return 0;
        
        // Sort by edit count (highest first)
        return b.editCount - a.editCount;
    });

    // Update UI with completion
    progressBar.setProgress(100);
    statusMessage.setLabel(`✓ Completed! Checked ${users.length} users. ${errors.length > 0 ? `${errors.length} errors.` : 'No errors.'}`);
    checkButton.setDisabled(false);
    checkButton.setLabel('Check Edit Counts');
    copyButton.setDisabled(false);

    // Display results with edit counts
    const formattedResults = results.map(result => {
        if (result.editCount === -1) {
            return `${result.original} - ERROR`;
        }
        return `${result.original} - ${result.editCount.toLocaleString()} edits`;
    });
    
    outputField.setValue(formattedResults.join('\n'));

    console.log("\n=== EDIT COUNT RESULTS (sorted by edit count) ===");
    formattedResults.forEach(result => console.log(result));
    
    if (errors.length > 0) {
        console.log(`\nErrors occurred for: ${errors.join(', ')}`);
    }
}