User:DannyS712 test/redirects.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:DannyS712 test/redirects. |
//<nowiki>
$(function (){
var redirects_config = {
name: '[[User:DannyS712/getRedirects|getRedirects.js]]',
version: 2.6,
debug: false
};
var user = mw.config.get( 'wgUserName' );
console.log( user );
if (user && user === 'DannyS712 bot III') {
redirects_config.logpage = 'User:DannyS712 bot III/Redirects.json';
} else {
redirects_config.logpage = 'User:DannyS712 test/redirects.json';
}
importScript( 'User:DannyS712 test/JSON.js' );
mw.loader.using( 'mediawiki.util', function () {
$(document).ready( function () {
mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'Get redirects', 'ca-getRedirects');
mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'Patrol redirects', 'ca-patrolRedirects');
$('#ca-getRedirects').on('click', function() {
getRedirects();
} );
$('#ca-patrolRedirects').on('click', function() {
patrolRedirects();
} );
} );
} );
async function getRedirects(){
var inTrial = false;
var ignoreOffset = false;
var trialLimit = 0;
var initial = await getFiltered();
var pre = [{
notes: {
script: redirects_config.name,
scriptVersion: redirects_config.version,
logPage: redirects_config.logpage,
time: parseInt(initial.offset),
listBuild: parseInt(new Date().toISOString().replace(/[\-T:]|\.\d{3}Z/g, '')),
}
}];
var data = pre.concat( initial.data );
var offset = initial.offset;
var iter = {};
var shouldRun = true;
var end = getEnd();
while ( shouldRun ){
iter = await getFiltered( offset );
console.log( iter );
data = data.concat( iter.data );
offset = parseInt(iter.offset);
if (inTrial && data.length > trialLimit) {
console.log( "Batch complete" );
shouldRun = false;
}
if (!ignoreOffset && offset && offset < end){
console.log("All remaining redirects have already been analyzed previously");
shouldRun = false;
}
if (!iter.offset) {
console.log( "Done" );
shouldRun = false;
}
}
if (redirects_config.debug) console.log( data );
var stringy = JSON.stringify( data, null, 2 );
console.log( stringy );
var user = mw.config.get( 'wgUserName' );
console.log( user );
if (user && user === 'DannyS712 bot III') {
set_JSON ( 'User:DannyS712 bot III/Redirects.json', stringy, 'Redirects to patrol (bot)');
} else {
set_JSON ( 'User:DannyS712 test/redirects.json', stringy, 'Candidates to patrol');
}
}
function getFiltered( offset ){
return new Promise((resolve) => {
var to_send = {
action: 'pagetriagelist',
showunreviewed: true,
showredirs: true,
limit: 200,
format: 'json',
};
if (offset || false ) to_send.offset = offset;
if (redirects_config.debug) console.log( to_send );
$.post( mw.config.get( 'wgScriptPath' ) + '/api.php', to_send, function( full_data ){
if (redirects_config.debug) console.log( full_data );
var pages = full_data.pagetriagelist.pages;
if (redirects_config.debug) console.log( pages );
var data = [];
var page;
for (var iii = 0; iii < pages.length; iii++){
page = pages[iii];
if (shouldPatrol( page )) {
data.push( {
pageid: parseInt(page.pageid),
title: page.title,
snippet: page.snippet,
});
}
}
console.log( data );
var next_offset = false;
if (pages && pages[pages.length-1] && pages[pages.length-1].creation_date) next_offset = parseInt(pages[pages.length-1].creation_date);
var to_return = {
data: data,
offset: next_offset
};
resolve(to_return);
});
});
}
function shouldPatrol( page ){
var target = page.snippet.replace( /REDIRECT /i, '');
var title = page.title;
///*
if (target === title.replace( / \(disambiguation\)/i, '')) return true;
if (comparePages( target, title )) return true;
if (comparePages( target + 's', title ) ) return true;
if (comparePages( target + 'es', title ) ) return true;
if (comparePages( target.replace( /[’'‘]/g, '\'' ), title.replace( /[’'‘]/g, '\'' ) ) ) return true;
if (comparePages( target, title.replace( /(\w*), (\w*)/, '$2 $1' ) ) ) return true;
if (comparePages( target, 'List of ' + title ) ) return true;
if (comparePages( target.replace( /[ -]/g, '' ), title.replace( /[ -]/g, '' ) ) ) return true;
if (comparePages( target.replace( / vs?\.? /g, 'v.' ), title.replace( / vs?\.? /g, 'v.' ) ) ) return true;
if (comparePages( target.replace( /^The /, '' ), title.replace( /^The /g, '' ) ) ) return true;
if (comparePages( target.replace( /[-‒–—―]/g, '-'), title.replace( /[-‒–—―]/g, '-' ) ) ) return true;
//*/
return false;
}
function comparePages( target, title ){
var comparison = target.localeCompare( title, 'en', {sensitivity: 'base'} );
if (comparison === 0) return true;
return false;
}
function patrolRedirects(){
var list = get_JSON(redirects_config.logpage);
if (redirects_config.debug) console.log( list );
for (var iii = 1; iii < list.length; iii++){ // Start from 1 because 0 is notes
doPatrolFromId( list[iii].pageid);
}
}
function doPatrolFromId( id ){
var to_send = {
action: 'pagetriageaction',
pageid: id,
reviewed: 1,
token: mw.user.tokens.get( 'csrfToken' ),
format: 'json'
};
if (redirects_config.debug) console.log( to_send );
$.post( mw.config.get( 'wgScriptPath' ) + '/api.php', to_send, function( response ){
console.log( response );
} );
}
function getEnd(){
var list = get_JSON(redirects_config.logpage);
//console.log( list );
var end = 0;
if (list && list[0] && list[0].notes && list[0].notes.time) end = parseInt(list[0].notes.time);
console.log( end );
return end;
}
});
//</nowiki>