Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
* Citoid for Bharatpedia (BharatpediaNeo)
* Generates a CS1 <ref>{{cite …}}</ref> from a URL via Wikimedia Citoid.
*
* Adapted from User:Salix alba/Citoid.js (enwiki)
* and User:Mvolz/veCiteFromURL.js
*
* Install in User:WikiDwarf/common.js:
* mw.loader.load('/w/index.php?title=User:WikiDwarf/Citoid.js&action=raw&ctype=text/javascript');
*
* @version 1.1-bpneo
*/
( function ( $, mw ) {
'use strict';
var CITOID_ENDPOINT = 'https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/';
var lang = mw.config.get( 'wgUserLanguage' ) || 'en';
var messages = {
en: {
'toolbox-label': 'Citoid',
'toolbox-tooltip': 'Generate a Bharatpedia reference from a URL',
title: 'Citoid – Bharatpedia',
close: 'Close',
generate: 'Generate citation',
urlLabel: 'URL',
dateLabel: 'Date format',
codeLabel: 'Template code',
rawLabel: 'Raw Citoid data',
monthnames: [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
]
}
};
if ( !messages[ lang ] ) {
lang = 'en';
}
var msg = messages[ lang ];
var lastUrl = '';
function escapePipes( val ) {
if ( val === undefined || val === null ) {
return '';
}
return String( val ).replace( /\|/g, '-' );
}
function formatCS1Date( isodate ) {
if ( !isodate ) {
return '';
}
var style = $( 'input:radio[name=citoid-datestyle]:checked' ).val() || 'DMY';
var day = '', month = 0, year = '', daynum = '', monthnum = '';
var raw = String( isodate );
var test1 = raw.match( /(\d{4})-(\d{2})-(\d{2})/ );
var test2 = raw.match( /([A-Z][a-z]+) (\d+), (\d{4})/ );
var test3 = raw.match( /(\d{4})-(\d{2})(?:$|[^\d])/ );
if ( test1 ) {
year = test1[ 1 ];
monthnum = test1[ 2 ];
daynum = test1[ 3 ];
day = parseInt( daynum, 10 );
month = parseInt( monthnum, 10 ) - 1;
} else if ( test2 ) {
year = test2[ 3 ];
day = parseInt( test2[ 2 ], 10 );
daynum = day < 10 ? '0' + day : String( day );
var index = messages.en.monthnames.indexOf( test2[ 1 ] ) + 1;
if ( index < 1 ) {
return raw;
}
monthnum = index < 10 ? '0' + index : String( index );
month = index - 1;
} else if ( test3 ) {
year = test3[ 1 ];
monthnum = test3[ 2 ];
month = parseInt( monthnum, 10 ) - 1;
if ( style === 'MDY' ) {
return msg.monthnames[ month ] + ', ' + year;
}
if ( style === 'YMD' ) {
return year + '-' + monthnum;
}
return msg.monthnames[ month ] + ' ' + year;
} else {
return raw;
}
if ( style === 'MDY' ) {
return msg.monthnames[ month ] + ' ' + day + ', ' + year;
}
if ( style === 'YMD' ) {
return year + '-' + monthnum + '-' + daynum;
}
return day + ' ' + msg.monthnames[ month ] + ' ' + year;
}
var templateTypeMap = {
book: 'Cite book',
bookSection: 'Cite book',
journalArticle: 'Cite journal',
magazineArticle: 'Cite news',
newspaperArticle: 'Cite news',
thesis: 'Cite journal',
letter: 'Citation',
manuscript: 'Cite book',
interview: 'Citation',
film: 'Citation',
artwork: 'Citation',
webpage: 'Cite web',
report: 'Cite journal',
bill: 'Citation',
hearing: 'Citation',
patent: 'Citation',
statute: 'Citation',
email: 'Cite web',
map: 'Citation',
blogPost: 'Cite web',
instantMessage: 'Citation',
forumPost: 'Cite web',
audioRecording: 'Citation',
presentation: 'Cite journal',
videoRecording: 'Citation',
tvBroadcast: 'Citation',
radioBroadcast: 'Citation',
podcast: 'Citation',
computerProgram: 'Citation',
conferencePaper: 'Cite journal',
document: 'Citation',
encyclopediaArticle: 'Cite journal',
dictionaryEntry: 'Cite journal'
};
var citationParams = {
first1: 'author1-first', last1: 'author1-last',
first2: 'author2-first', last2: 'author2-last',
first3: 'author3-first', last3: 'author3-last',
first4: 'author4-first', last4: 'author4-last',
title: 'title', url: 'url', publisher: 'publisher',
journal: 'publicationTitle', date: 'date', location: 'place',
issn: 'ISSN', isbn: 'ISBN', pages: 'pages', volume: 'volume',
series: 'series', issue: 'issue', via: 'libraryCatalog',
pmid: 'PMID', pmc: 'PMCID', doi: 'DOI'
};
var webParams = {
first1: 'author1-first', last1: 'author1-last',
first2: 'author2-first', last2: 'author2-last',
first3: 'author3-first', last3: 'author3-last',
first4: 'author4-first', last4: 'author4-last',
'access-date': 'accessDate',
title: 'title', url: 'url', date: 'date',
publisher: 'publisher', via: 'libraryCatalog',
website: 'publicationTitle'
};
var newsParams = {
first1: 'author1-first', last1: 'author1-last',
first2: 'author2-first', last2: 'author2-last',
first3: 'author3-first', last3: 'author3-last',
first4: 'author4-first', last4: 'author4-last',
'access-date': 'accessDate',
title: 'title', url: 'url', publisher: 'publisher',
newspaper: 'publicationTitle', date: 'date', location: 'place',
issn: 'ISSN', isbn: 'ISBN', pages: 'pages', volume: 'volume',
series: 'series', issue: 'issue', via: 'libraryCatalog', doi: 'DOI'
};
var bookParams = {
first1: 'author1-first', last1: 'author1-last',
first2: 'author2-first', last2: 'author2-last',
first3: 'author3-first', last3: 'author3-last',
first4: 'author4-first', last4: 'author4-last',
title: 'title', url: 'url', publisher: 'publisher',
journal: 'publicationTitle', date: 'date', location: 'place',
issn: 'ISSN', isbn: 'ISBN', pages: 'pages', volume: 'volume',
series: 'series', issue: 'issue', via: 'libraryCatalog', doi: 'DOI'
};
var journalParams = {
first1: 'author1-first', last1: 'author1-last',
first2: 'author2-first', last2: 'author2-last',
first3: 'author3-first', last3: 'author3-last',
first4: 'author4-first', last4: 'author4-last',
title: 'title', url: 'url', publisher: 'publisher',
journal: 'publicationTitle', date: 'date', location: 'place',
issn: 'ISSN', isbn: 'ISBN', pages: 'pages', volume: 'volume',
series: 'series', issue: 'issue',
pmid: 'PMID', pmc: 'PMCID', doi: 'DOI'
};
var templateParamMap = {
Citation: citationParams,
'Cite web': webParams,
'Cite news': newsParams,
'Cite journal': journalParams,
'Cite book': bookParams
};
function getPlainObject( url, searchResults ) {
var citation = ( searchResults && searchResults[ 0 ] ) ? searchResults[ 0 ] : {};
var templateName = templateTypeMap[ citation.itemType ] || 'Cite web';
var paramMap = templateParamMap[ templateName ] || webParams;
var paramObj = {};
if ( !citation.url ) {
citation.url = url;
}
if ( citation.PMCID ) {
citation.url = null;
}
if ( citation.websiteTitle ) {
citation.publicationTitle = citation.websiteTitle;
}
$.each( paramMap, function ( key, value ) {
paramObj[ key ] = { wt: escapePipes( citation[ value ] ) };
} );
var authors = citation.author;
if ( $.isArray( authors ) ) {
for ( var i = 0; i < authors.length; i++ ) {
var n = i + 1;
var pair = authors[ i ] || [];
paramObj[ 'first' + n ] = { wt: escapePipes( pair[ 0 ] ) };
paramObj[ 'last' + n ] = { wt: escapePipes( pair[ 1 ] ) };
}
}
if ( paramObj.date && paramObj.date.wt ) {
paramObj.date.wt = formatCS1Date( paramObj.date.wt );
}
if ( paramObj[ 'access-date' ] && paramObj[ 'access-date' ].wt ) {
paramObj[ 'access-date' ].wt = formatCS1Date( paramObj[ 'access-date' ].wt );
}
return {
templateName: templateName.toLowerCase(),
href: 'Template:' + templateName,
params: paramObj
};
}
function buildTemplateCode( obj ) {
var res = '<ref>{{' + obj.templateName;
$.each( obj.params, function ( key, value ) {
var wt = value && value.wt;
if ( wt ) {
res += '|' + key + '=' + wt;
}
} );
res += '}}</ref>';
return res;
}
function callCitoid() {
var url = $.trim( $( '#citoid-url' ).val() );
if ( !url ) {
mw.notify( 'Enter a URL first.' );
return;
}
lastUrl = url;
$( '#citoid-code' ).val( 'Loading…' );
$( '#citoid-full' ).val( '' );
$.ajax( {
url: CITOID_ENDPOINT + encodeURIComponent( url ),
type: 'GET',
dataType: 'json',
success: function ( result ) {
var plain = getPlainObject( lastUrl, result );
$( '#citoid-code' ).val( buildTemplateCode( plain ) );
$( '#citoid-full' ).val( JSON.stringify( result, null, 2 ) );
},
error: function ( xhr, textStatus, errorThrown ) {
var detail = textStatus + ( errorThrown ? ': ' + errorThrown : '' );
mw.notify( 'Citoid failed — ' + detail );
$( '#citoid-code' ).val( '' );
$( '#citoid-full' ).val( detail );
}
} );
}
function openDialog() {
$( '#citoid-cont' ).fadeIn( 'fast' );
$( '#citoid-url' ).trigger( 'focus' );
}
function closeDialog() {
$( '#citoid-cont' ).fadeOut( 'fast' );
}
function injectCSS() {
mw.util.addCSS(
'#citoid-cont{position:fixed;z-index:2000;inset:8% 10%;' +
'background:var(--color-surface-1,#fff);color:var(--color-base,#202122);' +
'border:1px solid var(--border-color-base,#a2a9b1);' +
'box-shadow:0 2px 16px rgba(0,0,0,.25);padding:12px 16px;overflow:auto;}' +
'#citoid-dialog h2{margin:0 32px 8px 0;font-size:1.25em;}' +
'#citoid-close{position:absolute;top:10px;right:12px;font-size:22px;line-height:1;text-decoration:none;}' +
'#citoid-url{width:98%;max-width:100%;}' +
'#citoid-code,#citoid-full{width:98%;min-height:8em;font-family:monospace;font-size:12px;}' +
'#citoid-button{margin:8px 0;}' +
'#citoid-floating{position:fixed;right:16px;bottom:16px;z-index:2001;}'
);
}
function buildHTML() {
var $button = $( '<button>' )
.attr( { id: 'citoid-button', type: 'button' } )
.text( msg.generate )
.on( 'click', callCitoid );
var $body = $( '<div>' ).attr( 'id', 'citoid-body' )
.append( $( '<p>' ).append(
document.createTextNode( msg.urlLabel + ': ' ),
$( '<input>' ).attr( { type: 'text', id: 'citoid-url', size: 80 } )
) )
.append( $( '<p>' ).append(
document.createTextNode( msg.dateLabel + ': ' ),
$( '<label>' ).append(
$( '<input>' ).attr( { type: 'radio', name: 'citoid-datestyle', value: 'DMY', checked: true } ),
' 1 September 2014'
),
' ',
$( '<label>' ).append(
$( '<input>' ).attr( { type: 'radio', name: 'citoid-datestyle', value: 'MDY' } ),
' September 1, 2014'
),
' ',
$( '<label>' ).append(
$( '<input>' ).attr( { type: 'radio', name: 'citoid-datestyle', value: 'YMD' } ),
' 2014-09-01'
)
) )
.append( $button )
.append( $( '<p>' ).text( msg.codeLabel ) )
.append( $( '<textarea>' ).attr( { id: 'citoid-code' } ) )
.append( $( '<p>' ).text( msg.rawLabel ) )
.append( $( '<textarea>' ).attr( { id: 'citoid-full' } ) );
var $cont = $( '<div>' )
.attr( 'id', 'citoid-cont' )
.append(
$( '<div>' ).attr( 'id', 'citoid-dialog' ).append(
$( '<h2>' ).text( msg.title ),
$( '<a>' ).attr( {
id: 'citoid-close',
href: '#',
title: msg.close
} ).on( 'click', function ( e ) {
e.preventDefault();
closeDialog();
} ).text( '×' ),
$body
)
)
.hide();
$( document.body ).append( $cont );
}
function bindOpener( node ) {
if ( !node ) {
return;
}
$( node ).on( 'click', function ( e ) {
e.preventDefault();
openDialog();
} );
}
function addEntryPoint() {
var $slot = $( '#antivandal-slot' );
if ( $slot.length && !$( '#t-citoid' ).length ) {
var $slotBtn = $( '<button>' )
.attr( { type: 'button', id: 't-citoid', title: msg[ 'toolbox-tooltip' ] } )
.text( msg[ 'toolbox-label' ] );
$slot.append( $slotBtn );
bindOpener( $slotBtn.get( 0 ) );
return;
}
var portlets = [ 'p-cactions', 'p-views', 'p-tb' ];
var node = null;
var i;
for ( i = 0; i < portlets.length; i++ ) {
node = mw.util.addPortletLink(
portlets[ i ],
'#',
msg[ 'toolbox-label' ],
't-citoid',
msg[ 'toolbox-tooltip' ]
);
if ( node ) {
break;
}
}
if ( !node ) {
node = $( '<button>' )
.attr( { type: 'button', id: 'citoid-floating', title: msg[ 'toolbox-tooltip' ] } )
.text( msg[ 'toolbox-label' ] )
.appendTo( document.body )
.get( 0 );
}
bindOpener( node );
}
mw.loader.using( 'mediawiki.util' ).then( function () {
$( function () {
injectCSS();
buildHTML();
addEntryPoint();
} );
} );
}( jQuery, mediaWiki ) );