Jump to content

User:DannyS712 test/links.js

From Wikipedia, the free encyclopedia
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>
$(function (){
var link_config = {
	name: '[[User:DannyS712/lk|lk.js]]',
	version: 1.0,
	debug: false
};
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';

mw.loader.using( 'mediawiki.util', function () {
	importScript( 'User:DannyS712 test/page.js ' );
    $(document).ready( function () { 
    	mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'lk', 'get-links', 'TOOLTIP');
    	$('#get-links').on('click', function() {
    		run();
    	} );
    } );
} );
function run(){
	var cat = mw.config.get( 'wgPageName' ).replace(/_/g, ' ');
	var pages = in_cat( cat );
	var to_check = [];
	var this_result = false;
	for (var iii = 0; iii < pages.length; iii++){
		this_result = incoming_links( pages[iii].title );
		if (this_result) to_check.push( this_result );
	}
	console.log( to_check );
	var string = as_table( to_check, cat );
	console.log ( string );
	//set_page ( 'User:DannyS712 test/sandbox', string, 'Updating report');
}
function incoming_links( name ){
	var links = get_links( name );
	var page = { name: name, links: links };
	console.log( page );
	if ( links >= 2 ) return page;
	else return false;
}
function get_links( page ){
    var links_to_get = {
        action: 'query',
        prop: 'linkshere',
        titles: page,
        lhprop: 'title',
        lhnamespace: '0',
        lhshow: '!redirect',
        lhlimit: 10,
        format: 'json',
        formatversion: 2
    };
    var result = 0;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: links_to_get,
		dataType: 'json',
		async: false,
		success: function(links) {
			if (link_config.debug) console.log( links );
			if (links['query']['pages'][0]['linkshere']) result = links['query']['pages'][0]['linkshere'].length;
	    	if (link_config.debug) console.log( result );
		} 
	});
	return result;
}
function in_cat( cat ){
    var cat_to_get = {
        action: 'query',
        list: 'categorymembers',
        cmnamespace: 0,
        cmtitle: cat,
        cmprop: 'title',
        cmlimit: 'max',
        format: 'json',
        formatversion: 2
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: cat_to_get,
		dataType: 'json',
		async: false,
		success: function(cat_got) {
			if (link_config.debug) console.log( cat_got );
			result = cat_got['query']['categorymembers'];
			if (link_config.debug) console.log ( result );
		} 
	});
	return result;
}
function as_table ( to_check, cat ){
	var as_string = "Pages tagged as orpahned in [[:" + cat + "]] with at least 2 incoming links; data as of ~~~~~. Updated by ~~~.\n\n----\n";
	as_string = as_string + '{| class="wikitable sortable"\n|+ Orphans with at least 2 links\n|-\n! scope="col" | Page\n! scope="col" | Links\n|-\n';
	for (var page = 0; page < to_check.length; page++){
		as_string = as_string + '| [[' +  to_check[page].name + ']] || ' + to_check[page].links + '\n|-\n';
	}
	as_string = as_string + '|}';
	return as_string;
}
});
//</nowiki>