MediaWiki:Common.js
Appearance
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.
/* ============================================================
Blunt Wiki – HotCat (loaded from Wikimedia Commons)
============================================================ */
window.hotcat_no_autocommit = false;
if ( mw.config.get( 'wgNamespaceNumber' ) >= 0 &&
mw.config.get( 'wgAction' ) === 'view' &&
mw.config.get( 'wgIsProbablyEditable' ) ) {
mw.loader.load( '//commons.wikimedia.org/w/index.php?title=MediaWiki:Gadget-HotCat.js&action=raw&ctype=text/javascript' );
}
/* Any JavaScript here will be loaded for all users on every page load. */
/* ============================================================
Blunt Wiki – {{Information}} fields on Special:Upload
Fields change with the license chosen in the Licensing menu.
Paste at the END of MediaWiki:Common.js (replace the old version).
============================================================ */
( function () {
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Upload' ) {
return;
}
var MONTHS = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December' ];
var USER = mw.config.get( 'wgUserName' ) || '';
/* ---------- Which license -> which group ---------- */
function groupOf( lic ) {
lic = ( lic || '' ).toLowerCase();
if ( !lic ) { return 'none'; }
if ( lic.indexOf( 'self-' ) === 0 ) { return 'own'; }
if ( lic.indexOf( 'cc' ) === 0 || lic.indexOf( 'godl' ) === 0 ) { return 'free'; }
if ( lic === 'pd-old' ) { return 'pdold'; }
if ( lic === 'pd-india' ) { return 'pdindia'; }
if ( lic.indexOf( 'pd-' ) === 0 ) { return 'pdgov'; }
if ( lic.indexOf( 'permission' ) === 0 ) { return 'perm'; }
if ( lic.indexOf( 'non-free' ) === 0 ) { return 'nonfree'; }
return 'none';
}
var NOTES = {
none: 'Choose a license in the Licensing menu below. The boxes here will change to match it.',
own: 'Own work: you took this photo or made this file yourself. You keep the copyright and release it under the chosen license.',
free: 'Someone else\'s free file: give the exact source link where this license is shown, and the original creator\'s name.',
pdold: 'Public domain (old work): the author must have died more than 60 years ago (India). Give the author and death year.',
pdindia: 'Public domain in India: give the year of first publication (60+ years ago) and the source.',
pdgov: 'Government work: give the government agency and the source link.',
perm: 'Permission: the copyright holder emailed written permission. Fill in the ticket details from the permission email.',
nonfree: 'Non-free: copyrighted file used only to identify the subject. Upload a small, low-resolution copy and use it in one article only.'
};
var PURPOSE = {
'non-free-logo': 'To identify the organisation or brand in the article about it.',
'non-free-promotional': 'To identify the subject of the article; no free photo of the subject is available.',
'non-free-poster': 'To identify the film, show, event or album in the article about it.',
'non-free-screenshot': 'To illustrate commentary on the software, website or broadcast in the article about it.'
};
/* ---------- Field list: [id, label, type, groups that show it, groups that require it, hint] ---------- */
var ALL = 'none own free pdold pdindia pdgov perm nonfree';
var FIELDS = [
[ 'desc', 'Description', 'textarea', ALL, ALL, 'Who / what is in the file, where and when.' ],
[ 'date', 'Date', 'date', 'none own free pdold pdgov perm nonfree', '', 'Date the photo was taken or the work was created.' ],
[ 'source', 'Source link / details', 'text', 'none free pdold pdindia pdgov perm nonfree', 'free pdold pdindia pdgov nonfree', 'URL or where the file came from (website, press kit, book, archive).' ],
[ 'author', 'Author / photographer', 'text', ALL, ALL, 'The person who must be credited.' ],
[ 'deathyear', 'Author\'s year of death', 'text', 'pdold', 'pdold', 'Example: 1948' ],
[ 'pubyear', 'Year first published', 'text', 'pdold pdindia', 'pdindia', 'Example: 1950' ],
[ 'holder', 'Copyright holder', 'text', 'perm nonfree', 'perm nonfree', 'Person or company who owns the copyright.' ],
[ 'ticket', 'Permission ticket no.', 'text', 'perm', 'perm', 'Your permission-archive number, e.g. 2026102110000001' ],
[ 'permdate', 'Permission received on', 'date', 'perm', 'perm', 'Date the permission email arrived.' ],
[ 'usedin', 'Used in article', 'text', 'nonfree', 'nonfree', 'The one article this file is for, e.g. Example Film' ],
[ 'purpose', 'Purpose of use', 'textarea', 'nonfree', 'nonfree', 'Why this non-free file is needed.' ],
[ 'lowres', 'Low resolution', 'checkbox', 'nonfree', 'nonfree', 'I confirm this is a small, low-resolution copy (about 300px wide or less).' ],
[ 'lat', 'Camera latitude', 'text', 'none own free perm', '', 'Optional, decimal. Example: 26.790611' ],
[ 'lon', 'Camera longitude', 'text', 'none own free perm', '', 'Optional, decimal. Example: 75.824056' ]
];
function has( list, g ) {
return ( ' ' + list + ' ' ).indexOf( ' ' + g + ' ' ) !== -1;
}
function el( tag, attrs, text ) {
var e = document.createElement( tag );
var k;
for ( k in ( attrs || {} ) ) { e.setAttribute( k, attrs[ k ] ); }
if ( text ) { e.textContent = text; }
return e;
}
function val( id ) {
var e = document.getElementById( 'bw-' + id );
if ( !e ) { return ''; }
if ( e.type === 'checkbox' ) { return e.checked ? 'yes' : ''; }
return e.value.replace( /^\s+|\s+$/g, '' );
}
function safe( s ) {
return s.replace( /\|/g, '{{!}}' );
}
function niceDate( iso ) {
var m = /^(\d{4})-(\d{2})-(\d{2})$/.exec( iso );
return m ? ( parseInt( m[ 3 ], 10 ) + ' ' + MONTHS[ parseInt( m[ 2 ], 10 ) - 1 ] + ' ' + m[ 1 ] ) : iso;
}
$( function () {
var summary = document.getElementById( 'wpUploadDescription' );
var form = document.getElementById( 'mw-upload-form' );
var licSel = document.getElementById( 'wpLicense' );
if ( !summary || !form ) { return; }
if ( /\{\{\s*Information/i.test( summary.value ) ) { return; }
var anchorRow = summary.closest( 'tr' ) || summary.closest( '.mw-htmlform-field-HTMLTextAreaField' ) || summary.parentNode;
var box = el( 'div', { id: 'bw-info-box', style: 'border:1px solid #a2a9b1;background:#f8f9fa;padding:10px 14px;margin:8px 0 12px;border-radius:4px;' } );
box.appendChild( el( 'div', { style: 'font-weight:bold;margin-bottom:4px;' }, 'File information (shown on the file page)' ) );
var note = el( 'div', { id: 'bw-note', style: 'font-size:90%;background:#eaf3ff;border-left:4px solid #36c;padding:6px 8px;margin-bottom:8px;' } );
box.appendChild( note );
var table = el( 'table', { style: 'width:100%;border-collapse:collapse;' } );
FIELDS.forEach( function ( f ) {
var tr = el( 'tr', { id: 'bw-row-' + f[ 0 ] } );
var th = el( 'td', { style: 'width:28%;vertical-align:top;padding:4px 8px 4px 0;font-weight:bold;' } );
th.appendChild( el( 'label', { 'for': 'bw-' + f[ 0 ] }, f[ 1 ] ) );
th.appendChild( el( 'span', { 'class': 'bw-star', style: 'color:#d33;' }, ' *' ) );
var td = el( 'td', { style: 'vertical-align:top;padding:4px 0;' } );
var input;
if ( f[ 2 ] === 'textarea' ) {
input = el( 'textarea', { id: 'bw-' + f[ 0 ], rows: '3', style: 'width:100%;box-sizing:border-box;' } );
} else if ( f[ 2 ] === 'checkbox' ) {
input = el( 'input', { id: 'bw-' + f[ 0 ], type: 'checkbox' } );
} else {
input = el( 'input', { id: 'bw-' + f[ 0 ], type: f[ 2 ], style: 'width:100%;box-sizing:border-box;' } );
}
td.appendChild( input );
td.appendChild( el( 'div', { style: 'font-size:85%;color:#54595d;' }, f[ 5 ] ) );
tr.appendChild( th );
tr.appendChild( td );
table.appendChild( tr );
} );
box.appendChild( table );
var err = el( 'div', { style: 'display:none;color:#d33;font-weight:bold;margin-top:6px;' } );
box.appendChild( err );
if ( anchorRow.tagName === 'TR' ) {
var holder = el( 'tr' );
var cell = el( 'td', { colspan: '2' } );
cell.appendChild( box );
holder.appendChild( cell );
anchorRow.parentNode.insertBefore( holder, anchorRow );
} else {
anchorRow.parentNode.insertBefore( box, anchorRow );
}
/* ---------- Show / hide fields for the chosen license ---------- */
var lastGroup = null;
function currentLicense() {
return licSel ? licSel.value : '';
}
function refresh() {
var lic = currentLicense();
var g = groupOf( lic );
note.textContent = NOTES[ g ];
FIELDS.forEach( function ( f ) {
document.getElementById( 'bw-row-' + f[ 0 ] ).style.display = has( f[ 3 ], g ) ? '' : 'none';
document.getElementById( 'bw-row-' + f[ 0 ] ).querySelector( '.bw-star' ).style.display = has( f[ 4 ], g ) ? '' : 'none';
} );
var author = document.getElementById( 'bw-author' );
if ( g === 'own' && !author.value && USER ) {
author.value = '[[User:' + USER + '|' + USER + ']]';
}
if ( lastGroup === 'own' && g !== 'own' && author.value === '[[User:' + USER + '|' + USER + ']]' ) {
author.value = '';
}
var purpose = document.getElementById( 'bw-purpose' );
if ( g === 'nonfree' && ( !purpose.value || purpose.getAttribute( 'data-auto' ) ) ) {
purpose.value = PURPOSE[ lic.toLowerCase() ] || '';
purpose.setAttribute( 'data-auto', '1' );
}
err.style.display = 'none';
lastGroup = g;
}
document.getElementById( 'bw-purpose' ).addEventListener( 'input', function () {
this.removeAttribute( 'data-auto' );
} );
if ( licSel ) {
licSel.addEventListener( 'change', refresh );
$( licSel ).on( 'change', refresh );
}
refresh();
/* ---------- Build {{Information}} on submit ---------- */
form.addEventListener( 'submit', function ( e ) {
var lic = currentLicense();
var g = groupOf( lic );
var missing = [];
FIELDS.forEach( function ( f ) {
if ( has( f[ 3 ], g ) && has( f[ 4 ], g ) && !val( f[ 0 ] ) ) {
missing.push( f[ 1 ] );
}
} );
if ( g === 'none' && !val( 'source' ) ) {
missing.push( 'Source link / details' );
}
if ( missing.length ) {
e.preventDefault();
err.textContent = 'Please fill in: ' + missing.join( ', ' );
err.style.display = 'block';
box.scrollIntoView( { behavior: 'smooth', block: 'center' } );
return;
}
var source = g === 'own' ? 'Own work' : safe( val( 'source' ) );
var permission = '';
var otherLabel = '';
var other = '';
if ( g === 'perm' ) {
permission = '{{Permission ticket|ticket=' + safe( val( 'ticket' ) ) +
'|date=' + safe( niceDate( val( 'permdate' ) ) ) +
'|holder=' + safe( val( 'holder' ) ) +
( USER ? '|verified_by=' + USER : '' ) + '}}';
}
if ( g === 'pdold' || g === 'pdindia' ) {
otherLabel = 'Public domain details';
other = ( val( 'deathyear' ) ? 'Author died: ' + safe( val( 'deathyear' ) ) + '. ' : '' ) +
( val( 'pubyear' ) ? 'First published: ' + safe( val( 'pubyear' ) ) + '.' : '' );
}
if ( g === 'nonfree' ) {
otherLabel = 'Non-free use rationale';
other = 'Copyright holder: ' + safe( val( 'holder' ) ) +
'<br />Used in: [[' + safe( val( 'usedin' ) ) + ']]' +
'<br />Purpose: ' + safe( val( 'purpose' ) ) +
'<br />Low resolution: Yes. Not replaceable with a free file.';
}
var lat = val( 'lat' );
var lon = val( 'lon' );
var showLoc = has( 'none own free perm', g ) && lat && lon;
var dateVal = has( 'pdindia', g ) ? '' : niceDate( val( 'date' ) );
var info = '== Summary ==\n{{Information\n' +
'|description = ' + safe( val( 'desc' ) ) + '\n' +
'|date = ' + safe( dateVal ) + '\n' +
'|source = ' + source + '\n' +
'|author = ' + val( 'author' ) + '\n' +
'|permission = ' + permission + '\n' +
'|location = ' + ( showLoc ? '{{Location|' + lat + '|' + lon + '}}' : '' ) + '\n' +
( other ? '|other_fields_label = ' + otherLabel + '\n|other_fields = ' + other + '\n' : '' ) +
'}}';
var extra = summary.value.replace( /^\s+|\s+$/g, '' );
summary.value = info + ( extra ? '\n\n' + extra : '' );
} );
} );
}() );
/* Blunt Wiki – show the short description under the page title
Paste at the END of MediaWiki:Common.js */
$( function () {
var sd = document.querySelector( '#mw-content-text .shortdescription' );
var heading = document.getElementById( 'firstHeading' );
if ( !sd || !heading || !sd.textContent.trim() ) {
return;
}
var line = document.createElement( 'div' );
line.className = 'bw-shortdesc';
line.textContent = sd.textContent.trim();
line.setAttribute( 'style', 'font-size:90%;color:#54595d;margin:-0.2em 0 0.6em;font-family:sans-serif;' );
heading.parentNode.insertBefore( line, heading.nextSibling );
} );