User:Phe/Works about.js

// when acting on an Author: page try to find works about this author. // // An associative array of basename prefix associated with a two entry array, // array[0] describe how to build a page title, array[1] describe how to build // a link to such page. // Parameters substitution is done this way: // %0 is the target base pagename // %A: lastname, firstname // %B: firstname lastname // %1: the title matching the subpage name // Don't add more than 50 entry. // // Only one request for the full set of name is done, adding more entry doesn't // require more request. // // Only article following the base_pagename/article_name convention can // go in this array, other special case, like DBN00, are hard-coded. var searched_pages = { "1911 Encyclopædia Britannica"      : [ "%A", "" ], "The New International Encyclopædia" : [ "%A", "%1" ], "A Short Biographical Dictionary of English Literature" : [ "%A", "" ], "Collier's New Encyclopedia (1921)" : [ "%A", "" ], "The New Student's Reference Work"  : [ "%A", "" ], "Cyclopædia of American Biographies (1903)"  : [ "%A", "" ], "The Encyclopedia Americana (1906)" : [ "%A", "" ], "The Encyclopedia Americana (1920)" : [ "%A", "" ], "Appletons' Cyclopædia of American Biography"  : [ "%A", "" ], "Catholic Encyclopedia (1913)"      : [ "%B", "" ], }

function works_about_cb(data) {   var wpTextbox1 = document.getElementById("wpTextbox1"); if (!wpTextbox1) return;

var result = '';

for (var page in data.query.pages) { if (Number(page) > 0) { var title = data.query.pages[page].title; title = title.split('/', 2); if (searched_pages[title[0]]) { var link = searched_pages[title[0]][1]; link = link.replace(/%0/g, title[0]); link = link.replace(/%1/g, title[1]); result += '* ' + link + '\n'; } else { var pos = title[0].search(' \\(DNB00\\)$'); if (pos != -1) { result += '* \n'; } else if (mw.config.get('wgUserName') == 'Phe' || mw.config.get('wgUserName') == 'WikiSysop') { // testbed alert('unknown title type: ' + title[0]); }           }        }    }

// Add the results at start of the text, placing it at end is prone error if (result.length) { var lastname = get_last_name(split_title); wpTextbox1.value = '==Works about ' + lastname + '==\n' + result + wpTextbox1.value; } }

function create_script_obj(url) {   var scriptObj = document.createElement("script"); scriptObj.setAttribute("type", "text/javascript"); scriptObj.setAttribute("src", url); document.body.appendChild(scriptObj); }

function format_pagename(firstname, lastname, key) {  var result = key + '/'; if (searched_pages[key][0] == '%A') result += lastname + ', ' + firstname; else if (searched_pages[key][0] == '%B') result += firstname + ' ' + lastname; return encodeURIComponent(result); }

function works_about {   if (mw.config.get('wgNamespaceNumber') != 102) return;

// Doesn't work as expected, is importScript asynchronous? //if (typeof(window['split_title']) == 'undefined') //   importScript('User:WikiSysop/Author fill.js');

var words = split_title; var lastname = get_last_name(words); var firstname = get_first_name(words); var author_title = lastname + ', ' + firstname;

if (mw.config.get('wgUserName') != 'WikiSysop') var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath'); else var url = 'http://en.wikisource.org' + mw.config.get('wgScriptPath');   // testbed

url += "/api.php?format=json&callback=works_about_cb&action=query&prop=info&titles=";

var items = [] for (var srch in searched_pages) items.push(format_pagename(firstname, lastname, srch)); items.push(encodeURIComponent(author_title + ' (DNB00)')); url += items.join('|');

create_script_obj(url);

$('#wpSummary').val('Works about'); }