User:Phe/Works about.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

// 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", "{{EB1911 Link|%1}}" ],
   "The New International Encyclopædia" : [ "%A", "[[%0/%1|%1]]" ],
   "A Short Biographical Dictionary of English Literature" : [ "%A", "{{SBDEL link|%1}}" ],
   "Collier's New Encyclopedia (1921)"  : [ "%A", "{{Collier's Link|%1}}" ],
   "The New Student's Reference Work"   : [ "%A", "{{NSRW link|%1}}" ],
   "Cyclopædia of American Biographies (1903)"   : [ "%A", "{{CAB03 link|%1}}" ],
   "The Encyclopedia Americana (1906)"  : [ "%A", "{{Americana link|%1|year=1906}}" ],
   "The Encyclopedia Americana (1920)"  : [ "%A", "{{Americana link|%1|year=1920}}" ],
   "Appletons' Cyclopædia of American Biography"   : [ "%A", "{{Appletons' link|%1}}" ],
   "Catholic Encyclopedia (1913)"       : [ "%B", "{{CE link|%1}}" ],
}

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 += '* {{DNB link|' + title[0].slice(0, pos) + '}}\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');
}