Jump to content

User:DannyS712 test/sandbox.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by DannyS712 test (talk | contribs) at 04:40, 30 May 2019 (testing). 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.
//<nowiki>
importScript( 'User:DannyS712 test/JSON.js' );

mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () { 
    	mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'Get all redirects', 'ca-getAllRedirects');
    	$('#ca-getAllRedirects').on('click', function() {
        	getAllRedirects();
    	} );
    } );
} );
async function getAllRedirects(){
	var initial = await getAll();
	var data = initial.data;
	var offset = initial.offset;
	var iter = {};
	for (var iii = 0; iii < 15; iii++){
		iter = await getAll( offset );
		data = data.concat( iter.data );
		offset = iter.offset;
	}
	if (redirects_config.debug) console.log( data );
	var stringy = JSON.stringify( data, null, 2 );
	set_JSON ( 'User:DannyS712 test/sandbox.json', stringy, 'All redirects');
}
function getAll( 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: page.pageid,
				title: page.title,
				snippet: page.snippet,
			});
//			}
		}
		console.log( data );
		var next_offset = 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, '');
	if (target === page.title.replace( / \(disambiguation\)/i, '')) return true;
	if (target.localeCompare(page.title, 'en', {sensitivity: 'base'}) === 0) return true;
	return false;
}
});
//</nowiki>