User:Dr pda/persondata.js: Difference between revisions

Content deleted Content added
updating parameters to handle {{Infobox tennis biography}}
Maintenance: mw:RL/MGU - Remove use of redundant module
 
(7 intermediate revisions by 3 users not shown)
Line 1:
// This script used to populate the Persondata template with fields from
//<pre>
// infoboxes. However, it is now deprecated, and should no longer be used.
//This function adds a tab which, when clicked, extracts data from an infobox and
//populates the fields of theSee [[Wikipedia:Persondata template. See the talk page]] for more details.
//
//To use this function add {{subst:js|User:Dr pda/persondata.js}} to your monobook.js
//
function format_name(x){
NAME = x.substr(x.indexOf('=')+1);
NAME = NAME.replace(/'''?/g,'');
NAME = NAME.replace(/^\s*/g,'');
NAME = NAME.replace(/\s*$/g,'');
var comma = NAME.indexOf(',');
var start = (comma == -1) ? NAME.length : comma;
var lastSpace = NAME.lastIndexOf(' ',start);
if(lastSpace != -1){
NAME = NAME.substring(lastSpace,start)+', '+NAME.substring(0,lastSpace)+NAME.substring(start);
}
return NAME;
}
 
mw.loader.using( [
function suggestPersonData(){
'mediawiki.jqueryMsg',
var text = document.getElementById('wpTextbox1').value;
'mediawiki.util',
], function () {
var label, tooltip, portletLink;
 
// Find the label and tooltip the same way as the old script did.
//do nothing if article already contains persondata
if (text !document.matchforms.editform && $(/ '#persondata/i' ).length ) return;{
label = 'show/hide persondata';
tooltip = 'Show/hide persondata metadata';
} else if (
mw.config.get( 'wgNamespaceNumber' ) === 0 &&
$( '#wpTextbox1' ).length &&
!$( '#wpTextbox1' ).text().match( /persondata/i )
) {
label = 'add persondata';
tooltip = 'Add persondata metadata';
}
 
// Add the portlet link. When clicked, this pops up a notification saying
var template = '';
// that the script is deprecated.
var NAME = '';
if ( label && tooltip ) {
var ALTERNATIVE_NAMES = '';
portletLink = mw.util.addPortletLink(
var SHORT_DESCRIPTION = '';
'p-cactions',
var PLACE_OF_BIRTH = '';
'#',
var DATE_OF_BIRTH = '';
label,
var PLACE_OF_DEATH = '';
'ca-pdata',
var DATE_OF_DEATH = '';
tooltip
 
);
//handle cases where the template name doesn't contain infobox
mw.messages.set( {
text = text.replace(/{{NFL player/i,'{{NFL player infobox');
'pdata-deprecated': "[[User:Dr pda/persondata.js]] is " +
if(text.match(/Infobox/i)){
"[[Wikipedia:Persondata|deprecated]]. Please remove it " +
start = text.lastIndexOf('{{',text.indexOf('nfobox'));
"from your [[Wikipedia:User scripts|user script]] page."
stop = text.indexOf('}}',start);
} );
next = text.indexOf('{{',start+1)
$( portletLink ).click( function ( e ) {
//Correctly handle other templates used within the infobox
e.preventDefault();
while (next < stop && text.indexOf('{{',start+1)!= -1){
mw.notify(
next = text.indexOf('{{',stop+1);
mw.message( 'pdata-deprecated' ),
stop = text.indexOf('}}',stop+1);
{ title: 'Script deprecated' }
}
);
template = text.substring(start,stop);
} );
 
}
//Remove references, birth/death date and age templates
template = template.replace(/<ref.*(\/ref>|$)/gm,'');
template = template.replace(/{{(?:[Bb]irth|[Dd]eath) date(?: and age)?\|\s?(\d{1,4})\|\s?(\d{1,2})\|\s?(\d{1,2}).*}}/g,"$1-$2-$3");
template = template.replace(/\[\[Image:Flag.*px\]\]/ig,'');
template = template.replace(/{{flagicon.*}}/ig,'');
template = template.replace(/<\/?small>/ig,'');
 
var firstpar = template.indexOf('|');
template = template.substr(firstpar+1);
 
var birthplace_in_born = false;
var array = template.split(/(\n\s*\||\|\s*\n)/);
for (x=0;x<array.length;x++){
if(array[x].match(/subject_name/i)||array[x].match(/^\s*\bname\b/i)||array[x].match(/fullname/i)){
NAME = format_name(array[x]);
}
if(array[x].match(/playername/i)||array[x].match(/birth_?name/i)){
ALTERNATIVE_NAMES = (ALTERNATIVE_NAMES == '') ? format_name(array[x]):ALTERNATIVE_NAMES + ';' + format_name(array[x]);
}
else if(array[x].match(/date_of_birth/i)||array[x].match(/dateofbirth/i)||array[x].match(/birthdate/i)||array[x].match(/birth_date/i)||array[x].match(/date of birth/i)||array[x].match(/datebirth/i)){
DATE_OF_BIRTH = array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/place_of_birth/i)||(array[x].match(/origin/i)&&birthplace_in_born==false)||array[x].match(/birthplace/i)||array[x].match(/placebirth/i)||array[x].match(/___location/i)||array[x].match(/placeofbirth/i)||array[x].match(/birth_place/i)||array[x].match(/place of birth/i)){
PLACE_OF_BIRTH = array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/born/i)){
var temp = array[x].indexOf('<br');
if(temp != -1){
DATE_OF_BIRTH = array[x].substring(array[x].indexOf('=')+1,temp);
PLACE_OF_BIRTH = array[x].substr(array[x].indexOf('>',temp)+1);
birthplace_in_born = true;
}
else{
DATE_OF_BIRTH = array[x].substring(array[x].indexOf('=')+1);
}
}
else if(array[x].match(/lived/i)){
var temp = array[x].substr(array[x].indexOf('=')+1);
temp = temp.replace(/born\s*/i,'');
var dash = temp.search(/&ndash;\s*|-\s*/);
if(dash >=0){
DATE_OF_BIRTH = temp.substring(0,dash);
DATE_OF_DEATH = temp.substring(temp.indexOf(' ',dash)+1);
}
else{
DATE_OF_BIRTH = temp;
}
}
else if(array[x].match(/cityofbirth/i)){
var temp = array[x].substr(array[x].indexOf('=')+1);
temp = temp.match(/.*/);
PLACE_OF_BIRTH = (PLACE_OF_BIRTH == '') ? temp : temp + ' ,' + PLACE_OF_BIRTH;
}
else if(array[x].match(/countryofbirth/i)){
PLACE_OF_BIRTH = (PLACE_OF_BIRTH == '') ? array[x].substr(array[x].indexOf('=')+1) : PLACE_OF_BIRTH + ',' + array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/date_of_death/i)||array[x].match(/dateofdeath/i)||array[x].match(/deathdate/i)||array[x].match(/death_date/i)||array[x].match(/date of death/i)||array[x].match(/datedeath/i)){
DATE_OF_DEATH = array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/place_of_death/i)||array[x].match(/deathplace/i)||array[x].match(/placeofdeath/i)||array[x].match(/death_place/i)||array[x].match(/place of death/i)||array[x].match(/placedeath/i)){
PLACE_OF_DEATH = array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/died/i)){
var temp = array[x].indexOf('<br');
if(temp != -1){
DATE_OF_DEATH = array[x].substring(array[x].indexOf('=')+1,temp);
PLACE_OF_DEATH = array[x].substr(array[x].indexOf('>',temp)+1);
}
else{
DATE_OF_DEATH = array[x].substring(array[x].indexOf('=')+1);
}
}
else if(array[x].match(/cityofdeath/i)){
var temp = array[x].substr(array[x].indexOf('=')+1);
temp = temp.match(/.*/);
PLACE_OF_DEATH = (PLACE_OF_DEATH == '') ? temp : temp + ' ,' + PLACE_OF_DEATH;
}
else if(array[x].match(/countryofdeath/i)){
PLACE_OF_DEATH = (PLACE_OF_DEATH == '') ? array[x].substr(array[x].indexOf('=')+1) : PLACE_OF_DEATH + ',' + array[x].substr(array[x].indexOf('=')+1);
}
else if(array[x].match(/occupation/i)||array[x].match(/\bfield\b\s*=/i)||array[x].match(/office/i)){
SHORT_DESCRIPTION = array[x].substr(array[x].indexOf('=')+1);
SHORT_DESCRIPTION = SHORT_DESCRIPTION.replace(/\s*<br\s*\/?>\s*/g,', ');
}
}
 
//more tidy up
DATE_OF_BIRTH = DATE_OF_BIRTH.replace(/\(age? \d*\)/i,'');
DATE_OF_DEATH = DATE_OF_DEATH.replace(/\(age? \d*\)/i,'');
}
if(NAME == ''){
var pagename = wgPageName.replace(/_/g,' ');
pagename = pagename.replace(/\(.*\)/g,' ');
NAME = format_name(pagename);
}
if(DATE_OF_BIRTH=='' && text.match(/Category:\d* births/)){
var catbirth = text.search(/Category:\d* births/);
DATE_OF_BIRTH = text.substring(catbirth+9,text.indexOf('births')-1);
}
if(DATE_OF_DEATH=='' && text.match(/Category:\d* deaths/)){
var catdeath = text.search(/Category:\d* deaths/);
DATE_OF_DEATH = text.substring(catdeath+9,text.indexOf('deaths')-1);
}
 
var persondata = "\n\n<!-- Metadata: see [[Wikipedia:Persondata]] -->\n{{Persondata\n|NAME="+NAME+"\n|ALTERNATIVE NAMES="+ALTERNATIVE_NAMES+"\n|SHORT DESCRIPTION="+SHORT_DESCRIPTION+"\n|DATE OF BIRTH="+DATE_OF_BIRTH+"\n|PLACE OF BIRTH="+PLACE_OF_BIRTH+"\n|DATE OF DEATH="+DATE_OF_DEATH+"\n|PLACE OF DEATH="+PLACE_OF_DEATH+"\n}}\n\n";
 
var insertPosition = text.indexOf('[[Category:')-1;
if(text.match('{{DEFAULTSORT')) insertPosition = text.indexOf('{{DEFAULTSORT')-1;
if(insertPosition != -2){
document.getElementById('wpTextbox1').value = text.substr(0,insertPosition)+persondata+text.substr(insertPosition+1);
}
else{
alert('This article does not belong to any categories! Consider adding some.');
document.getElementById('wpTextbox1').value = text+persondata;
}
document.getElementById('wpSummary').value += ' adding [[WP:PDATA|persondata]] using [[User talk:Dr pda/persondata.js|User:Dr pda/persondata.js]]';
document.getElementById('wpDiff').click();
 
}
 
function togglePersondata() {
var element = document.getElementById('persondata');
 
if (element.style.display != 'block'){
element.style.display = 'block';
}
else{
element.style.display = 'none';
}
}
 
addOnloadHook(function () {
if(!document.forms.editform){
if (document.getElementById('persondata') != null){
addPortletLink('p-cactions', 'javascript:togglePersondata()', 'show/hide persondata', 'ca-pdata', 'Show/hide persondata metadata', '', '');
}
}
else{
if (wgNamespaceNumber == 0 && document.getElementById('wpTextbox1').value.match(/persondata/i) == null){
addPortletLink('p-cactions', 'javascript:suggestPersonData()', 'add persondata', 'ca-pdata', 'add persondata metadata', '', '');
}
}
});
//</pre>