User:DannyS712 test/links.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/links. |
//<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>