User:Chieftainalex/API javascripts

(nothing is loading until you paste a script from below into your javascript scratchpad)

Wiki - API - all pages in NS
/* GWPVX API query tool for all pages */

// Setup var redirectfilter = 'nonredirects'; // all, redirects var languagelinkfilter = 'all'; // withlanglinks, withoutlanglinks var nsfilter = 104; // 100 var baseurl = 'https://gwpvx.gamepedia.com/api.php?action=query&format=json&list=allpages&aplimit=500&apfilterredir='+redirectfilter+'&apnamespace='+nsfilter+'&apfilterlanglinks='+languagelinkfilter; var datadrop = [];

// Function to concatenate all data function processdata (datadrop) { var contribs = []; $.each(datadrop, function(k,v){       $.each(v, function(kk,vv){ contribs.push(vv); });   });    $('#mw-content-text').html(makeTableHTML(contribs)); }

// Function to convert an object variable into a table function makeTableHTML( data ) { var result = ' ";       return result; }

// Fetch all the pages function apiQuery (token,startkey) { var url = baseurl; if (startkey) { url += '&apfrom=' + startkey; }   if (token) { url += '&apcontinue=' + token; }   // console.log(url); $.getJSON(url).done(function(data){       datadrop.push(data['query']['allpages']);

if (data['continue']) { apiQuery(data['continue']['apcontinue']); } else { console.log('Query complete.'); processdata(datadrop); }   }); }

console.log('User pressed run. Starting queries for Special:AllPages'); //apiQuery(null,'Z'); apiQuery;

Wiki - API - Category listings
/* GW2W API query tool for all categories as the generator with additional properties */ console.log('User pressed run. Starting queries for Special:Categories'); var continue_param = '';

// Wrapper function to return the results once complete function apiWrapper (config, callbackfunction) { // Fetch all the pages function apiQuery (config, token) { var url = config.baseurl; if (token) { url += '&' + config.continue + '=' + token; }       $.getJSON(url).done(function(data){            // Add results to stack            config.tempresultsarray.push(data['query'][config.output]);

// Get further results if available if (data['continue']) { if (continue_param == data['continue'][config.continue]) { // Quit if its making two queries in a row with the same continue offset... probably indicates wrong limits set. return; }               continue_param = data['continue'][config.continue]; console.log(continue_param);

// Make next query apiQuery(config, data['continue'][config.continue]); } else { // Flatten data from nested arrays to one array of result objects config.resultsarray = $.map(config.tempresultsarray, function(v) {                   return v;                }); console.log('Query complete.');

// Call the next function callbackfunction(config); }       });    }    apiQuery(config); }

// Function to convert an object variable into a table function makeTableHTML(config) { var data = config.resultsarray; var result = ' '; $('#mw-content-text').html('' + result + ''); }

// Notes --- // We're using a generator here, so all the parameters usually used from action=query have a "g" on the front of it. // Generators allow you to extract additional properties not normally available from "list=allpages". // The output format is dumb from generators, you need to set "formatversion=2" to make it use an array like every other endpoint. // Generators are dumb; the limits need to be set for each of the individual props too. These limits should be higher than that //  set for the actual generator. 4x higher seems to be the sweetspot. (e.g. gaplimit=1000, cllimit=4000) // Admins and bots have limits of 5000 as well as no throttling. Regular users have limits of 500.

// Make the request apiWrapper({ baseurl: 'https://gwpvx.gamepedia.com/api.php?format=json&formatversion=2' + '&action=query' + '&generator=allcategories&gaclimit=1000' + '&prop=categories&cllimit=5000',  output: 'pages',  continue: 'gaccontinue',  tempresultsarray: [],  resultsarray: [] }, makeTableHTML);

Wiki - API - Interwikis from namespace
/* PvXWiki API query tool for language links. */ /* GW2W API query tool for all pages as the generator with additional properties */ console.log('User pressed run. Starting queries for Special:AllPages'); var continue_param = '';

// Wrapper function to return the results once complete function apiWrapper (config, callbackfunction) { // Fetch all the pages function apiQuery (config, token) { var url = config.baseurl; if (token) { url += '&' + $.map(token, function(v, k){ return k + '=' + v }).join('&'); }       $.getJSON(url).done(function(data){            // Add results to stack          console.log(data);            config.tempresultsarray.push(data['query'][config.output]);

// Get further results if available if (data['continue']) { // Remove stupidly named distraction key delete data['continue'].continue; if (continue_param == JSON.stringify(data['continue'])) { // Quit if its making two queries in a row with the same continue offset... probably indicates wrong limits set. console.log('same offset applied twice in a row, probable bug in the code. quitting.') return; }               continue_param = JSON.stringify(data['continue']); console.log(continue_param);

// Make next query apiQuery(config, data['continue']); } else { // Flatten data from nested arrays to one array of result objects config.resultsarray = $.map(config.tempresultsarray, function(v) {                   return v;                }); console.log('Query complete.');

// Call the next function callbackfunction(config); }       });    }    apiQuery(config); }

// Function to convert an object variable into a table function makeTableHTML(config) { var data = config.resultsarray; var result = ' '; $('#mw-content-text').html(result); }

// Notes --- // We're using a generator here, so all the parameters usually used from action=query have a "g" on the front of it. // Generators allow you to extract additional properties not normally available from "list=allpages". // The output format is dumb from generators, you need to set "formatversion=2" to make it use an array like every other endpoint. // Generators are dumb; the limits need to be set for each of the individual props too. These limits should be higher than that //  set for the actual generator. 4x higher seems to be the sweetspot. (e.g. gaplimit=1000, lllimit=4000, cllimit=4000) // Admins and bots have limits of 5000 as well as no throttling. Regular users have limits of 500.

// Make the request apiWrapper({ baseurl: 'https://gwpvx.gamepedia.com/api.php?format=json&formatversion=2' + '&action=query' + '&generator=allpages&gaplimit=500' + '&gapnamespace=100' + '&prop=iwlinks&iwlimit=5000',  output: 'pages',  tempresultsarray: [],  resultsarray: [] }, makeTableHTML);

GWW - API - Pages in category
/* GWW API query tool for all pages */ console.log('User pressed run. Starting queries for Special:Category'); var CURRENTPAGE = mw.config.get('wgPageName');

// Wrapper function to return the results once complete function apiWrapper (config, callbackfunction) { // Fetch all the pages function apiQuery (config, token) { var url = config.baseurl; if (token) { url += '&' + config.continue + '=' + token; }       $.getJSON(url).done(function(data){            // Add results to stack            config.tempresultsarray.push(data['query'][config.output]);

// Get further results if available if (data['continue']) { apiQuery(config, data['continue'][config.continue]); } else { // Flatten data from nested arrays to one array of result objects config.resultsarray = $.map(config.tempresultsarray, function(v) {                   return v;                }); console.log('Query complete.');

// Call the next function callbackfunction(config); }       });    }    apiQuery(config); }

// Function to convert an object variable into a table function makeTableHTML (config) { var data = config.resultsarray;

// Generate table html var result = ' '; $('#mw-content-text').html(result); }

// Make request apiWrapper({ baseurl: 'https://wiki.guildwars.com/api.php?format=json&action=query&list=categorymembers&cmlimit=500&cmtitle=' + CURRENTPAGE,  output: 'categorymembers',  continue: 'cmcontinue',  tempresultsarray: [],  resultsarray: [] }, makeTableHTML);