User:Phe/Author fill.js
< User:Phe
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.
Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the central discussion page, Scriptorium. The code will be executed when previewing this page under some skins, including Monobook. You can in the interim if you wish to refresh the content sooner under another skin. |
Documentation for this script can be added at User:Phe/Author fill. |
// Try to fill field when creating an author page. Fields always filled, are
// lastname, firstname, lastinitial, wikipedia. Birth/Deat year filled iff an
// article is available on wp:en. Try also to avoid duplicate author:
// by creating a redirect if the wikipedia is non empty, its title
// is different from the current Author:title and an Author:new_title
// exists.
author_v2 = {
check_redirect : function (title, text_box)
{
var url = mw.config.get('wgServer') + wgScriptPath
+ "/api.php?format=xml&action=query&prop=info&titles="
+ encodeURIComponent(title);
var request = sajax_init_object();
request.open('GET', url, true);
request.onreadystatechange = function () {
if (request.readyState == 4) {
var xml = request.responseXML;
if (xml == null) return ;
var page = xml.getElementsByTagName( "page" )[0];
if (page.getAttribute("missing") != "")
text_box.value = '#REDIRECT [[' + title + ']]';
}
};
request.send(null);
},
author_fill_callback : function (data)
{
try {
var wpTextbox1 = document.getElementById("wpTextbox1");
if (wpTextbox1 && !data.query.pages["-1"]) {
for (var ids in data.query.pages) {
wpTextbox1.value = wpTextbox1.value.replace(/( *\\| *wikipedia *= *)/, "$1" + data.query.pages[ids].title);
var cats = data.query.pages[ids].categories;
for (var i = 0; i < cats.length; ++i) {
var m = cats[i].title.match(/Category:(\d+) deaths/);
if (m)
wpTextbox1.value = wpTextbox1.value.replace(/( *\\| *deathyear *= *)/, "$1" + m[1]);
m = cats[i].title.match(/Category:(\d+) births/);
if (m)
wpTextbox1.value = wpTextbox1.value.replace(/( *\\| *birthyear *= *)/, "$1" + m[1]);
}
break;
}
// This can undo all the above works by preferring to create a redirect to an existing author:
author_v2.check_redirect('Author:' + data.query.pages[ids].title, wpTextbox1);
}
}
catch (err) { }
},
create_script_obj : function (url)
{
var scriptObj = document.createElement("script");
scriptObj.setAttribute("type", "text/javascript");
scriptObj.setAttribute("src", url);
document.body.appendChild(scriptObj);
},
split_title : function ()
{
return mw.config.get('wgTitle').replace(/ \(.*\)/, "").split(" ");
},
get_special_word_pos : function (words)
{
var i;
for (i = 0; i < words.length; ++i) {
if (words[i] == 'van' || words[i] == 'von' ||
words[i] == 'le' || words[i] == 'de') {
return i;
}
}
return -1;
},
get_first_name : function (words)
{
return words.slice(0, author_v2.get_special_word_pos(words)).join(" ");
},
get_last_name : function(words)
{
return words.slice(author_v2.get_special_word_pos(words)).join(" ");
},
get_last_initial : function(words)
{
// get_last_name() can't be used here, we want the last word or the next
// word following a special word
var lastname = words[words.length - 1];
var pos = author_v2.get_special_word_pos(words);
if (pos != -1 && pos < words.length - 1)
lastname = words[pos + 1];
var last_initial = lastname.slice(0, 2);
// O'Donnel --> Od
if (lastname.length > 2 && last_initial.charAt(1) == "'")
last_initial = last_initial.charAt(0) + lastname.charAt(2).toLowerCase();
return last_initial;
},
/* Preload Template:Author when starting an author page, derived from [[User:Remember the dot]] code */
preloadAuthorTemplate : function()
{
var wpTextbox1 = document.getElementById("wpTextbox1")
if (wpTextbox1.value != "") return;
// try to figure out what value we can fill, broken in some case because
// it's difficult to handle name like Tom Van Moore but Tom van Moore is
// handled correctly.
var words = author_v2.split_title();
var lastname = author_v2.get_last_name(words);
var firstname = author_v2.get_first_name(words);
var last_initial = author_v2.get_last_initial(words);
wpTextbox1.value = "{" + "{author\n" +
" | firstname = " + firstname + "\n" +
" | lastname = " + lastname + "\n" +
" | last_initial = " + last_initial + "\n" +
" | birthyear = \n" +
" | deathyear = \n" +
" | description = \n" +
" | image = \n" +
" | wikipedia = \n" +
" | wikiquote = \n" +
" | commons = \n" +
"}}\n\n" +
"==Works==\n\n";
if (mw.config.get('wgServer') == "https://secure.wikimedia.org")
var base_url = "https://secure.wikimedia.org/wikipedia/en/w";
else
var base_url = "//en.wikipedia.org" + wgScriptPath;
var url = base_url + "/api.php?format=json&redirects"
+ "&callback=author_v2.author_fill_callback&action=query&prop=categories&cllimit=20&titles="
+ encodeURIComponent(mw.config.get('wgTitle'));
author_v2.create_script_obj(url);
}
}
function auto_fill_text() {
switch (mw.config.get('wgNamespaceNumber')) {
case 102: //Author
author_v2.preloadAuthorTemplate();
break;
}
}
jQuery( document ).ready( function( $ ) {
if (wgAction == "edit")
auto_fill_text();
} );