/**
* Script to easily create DYK nominations
* Automates:
* - Creation of the nomination page
* - Transcluding the nomination at T:DYKT
* - Transcluding the nomination at article talk pages
*/
/* jshint maxerr: 999 */
// <nowiki>
if (mw.config.get('wgPageName') === 'Wikipedia:Did_you_know/Create_new_nomination') {
// Do nothing on this page, to avoid conflict with [[MediaWiki:DYK-nomination-wizard.js]] (both use the same global)
} else {
var dyk = {};
window.dyk = dyk;
$.when(
mw.loader.using('ext.gadget.morebits'),
$.ready
).then(function() {
if (mw.config.get('wgNamespaceNumber') === 0 && mw.config.get('wgCurRevisionId')) {
$(mw.util.addPortletLink(window.DYKH_portlet || 'p-cactions', '#', 'DYK', 'dyk-portlet', 'Nominate for DYK')).click(dyk.callback);
}
});
var NOMPAGE_PREFIX = 'Template:Did you know nominations/';
var NOMINATIONS_PAGE = 'Template talk:Did you know';
dyk.advert = ' ([[User:SD0001/DYK-helper|DYK-helper]])';
dyk.callback = function dykMainCallback(e) {
e.preventDefault();
var Window = new Morebits.simpleWindow(600, 470);
Window.setTitle( "Nominate article for DYK" );
Window.setScriptName( "DYK-helper" );
Window.addFooterLink( "DYK rules", "WP:DYKRULES" );
Window.addFooterLink( "Script help", "User:SD0001/DYK-helper" );
var form = new Morebits.quickForm( dyk.evaluate );
if (mw.config.get('wgAction') === 'view' && mw.config.get('wgRevisionId') === mw.config.get('wgCurRevisionId')) {
// Calculating prose character count, code based on [[User:Shubinator/DYKcheck.js]] and [[User:Dr pda/prosesize.js]]
var getReadableCount = function(el) {
var charCount = 0;
for (var i = 0; i < el.childNodes.length; i ) {
if (el.childNodes[i].nodeName === '#text') {
charCount = el.childNodes[i].nodeValue.length;
} else if (el.childNodes[i].className !== 'reference' && // exclude references [1], [2], etc
el.childNodes[i].className.indexOf('emplate') === -1 && // exclude inline templates
el.childNodes[i].id !== 'coordinates' //exclude geocoords
) {
charCount = getReadableCount(el.childNodes[i]);
}
}
return charCount;
};
var charCount = 0, readable_text = '';
$('.mw-parser-output > p').each(function(i, el) {
charCount = getReadableCount(el);
readable_text = el.textContent.trim();
});
var wordCount = readable_text.split(/\s/).length;
var prosesizediv = document.createElement('div');
prosesizediv.id = 'dyk-prosesize';
prosesizediv.innerHTML = 'Prose size: ' wordCount ' words, ' charCount ' characters';
if (charCount < 1500) {
prosesizediv.style.color = 'red';
Morebits.quickForm.element.generateTooltip(prosesizediv, {
tooltip: 'Article must have at least 1500 characters of prose to be eligible for DYK'
});
} else {
prosesizediv.style.color = 'green';
}
form.append({
type: 'div',
style: 'float: right',
label: prosesizediv
});
}
form.append({
type: 'select',
name: 'status',
label: 'Status: ',
list: [
{ type: 'option', label: 'Created', value: 'new', selected: true },
{ type: 'option', label: '5x expanded', value: 'expanded' },
{ type: 'option', label: 'Converted from redirect', value: 'redirect' },
{ type: 'option', label: 'Moved to mainspace', value: 'mainspace' },
{ type: 'option', label: 'Improved to GA', value: 'GA' }
]
});
form.append({
type: 'input', // converted to date input below
label: 'Created/expanded on: ',
name: 'date',
tooltip: 'The date as of which creation/expansion has been completed. Must be within the past week. ',
event: dyk.dateCheck // for the benefit of browsers that don't support a datepicker for date fields
});
form.append({
type: 'checkbox',
list: [ {
name: 'multiarticle',
label: 'Multi-article nomination',
subgroup: {
type: 'input',
label: 'Article 2: ',
name: 'article2'
},
event: function addPlusbuttonArticle() {
if (document.getElementById('dyk-plusarticle') === null) { // happens only the first time
var plusbutton = dyk.createPlusButton('dyk-plusarticle');
plusbutton.addEventListener('click', function(e) {
var anchor = e.target.parentElement;
var num = parseInt(anchor.previousElementSibling.previousElementSibling.textContent.slice('Article '.length)) 1;
var newDiv = new Morebits.quickForm.element({
type: 'input',
label: 'Article ' num ': ',
name: 'multiarticle.article' num
}).render();
$(anchor).parent().after(newDiv);
newDiv.append(plusbutton);
newDiv.querySelector('input').focus();
});
$(result).find('[name="multiarticle.article2"]').after(plusbutton);
}
}
} ]
});
form.append({
type: 'textarea',
name: 'hook',
label: 'Hook: ',
tooltip: 'Should be concise, not more than 200 characters. See WP:DYKHOOK for guidelines. Do wikilink words in the hook and bold the link to the DYK article(s).',
value: '... that ',
className: 'dyk-hook'
});
form.append({
type: 'textarea',
name: 'source',
label: 'Source: ',
className: 'dyk-source',
tooltip: 'Source for the hook. You are strongly encouraged to quote the source text supporting the hook" (and [link] the source, or cite it briefly without using citation templates)'
});
form.append({
type: 'button',
label: 'Add ALT hook',
name: 'anotherhook',
event: function addAnotherHook(e) {
var span = e.target.parentElement;
var prevnum = parseInt($(span).prev().find('textarea').attr('name').slice('source'.length));
var num = isNaN(prevnum) ? 1 : prevnum 1;
var xh = new Morebits.quickForm.element({
type: 'textarea',
name: 'ALT' num,
label: 'ALT' num ' hook: ',
value: '... that ',
className: 'dyk-hook'
});
var xs = new Morebits.quickForm.element({
type: 'textarea',
name: 'source' num,
label: 'Source: ',
className: 'dyk-source'
});
$(span).before(xh.render(), xs.render());
dyk.txtareaModifications(result['ALT' num], 'hook');
dyk.txtareaModifications(result['source' num], 'source');
}
});
form.append({
type: 'input',
name: 'author',
label: 'Author: ',
value: mw.config.get('wgUserName'),
tooltip: 'If nominating an article created by another editor, change this value',
style: 'margin-top: 8px'
});
form.append({
type: 'checkbox',
list: [ {
name: 'multiauthor',
label: 'Add additional authors',
subgroup: {
type: 'input',
label: 'Author 2: ',
name: 'author2'
},
event: function addPlusbuttonAuthor() {
if (document.getElementById('dyk-plusauthor') === null) {
var plusbutton = dyk.createPlusButton('dyk-plusauthor');
plusbutton.addEventListener('click', function(e) {
var anchor = e.target.parentElement;
var num = parseInt(anchor.previousElementSibling.previousElementSibling.textContent.slice('Author '.length)) 1;
var newDiv = new Morebits.quickForm.element({
type: 'input',
label: 'Author ' num ': ',
name: 'multiauthor.author' num
}).render();
$(anchor).parent().after(newDiv);
newDiv.append(plusbutton);
newDiv.querySelector('input').focus();
});
$(result).find('[name="multiauthor.author2"]').after(plusbutton);
}
}
} ]
});
form.append({
type: 'checkbox',
list: [ {
name: 'img',
label: 'Include image',
tooltip: 'Images must be free, and used in the article. See WP:DYKIMG',
subgroup: [ {
type: 'input',
label: 'Image name: ',
name: 'name',
size: '50px'
}, {
type: 'input',
label: 'Image caption: ',
name: 'caption',
size: '60px'
} ]
} ],
event: function(e) {
if (e.target.checked) {
// Add a datalist for image field populated with images used in article
$(result['img.name']).attr('list', 'dyk-img-list').after(
$('<datalist>').attr('id', 'dyk-img-list')
);
new mw.Api().get({
"action": "query",
"format": "json",
"prop": "images",
"titles": mw.config.get('wgPageName'),
"formatversion": "2"
}).then(function(data) {
data.query.pages[0].images.forEach(function(img) {
$('#dyk-img-list').append($('<option>').attr('value', img.title));
});
});
}
}
});
form.append({
type: 'input',
name: 'qpq',
label: 'Reviewed: ',
tooltip: 'DYK nomination(s) you reviewed. This is mandatory for editors with 5 prior nominations (QPQ requirement). You can fill this after you make the nomination as well. When the unreviewed backlog mode is active, a 2nd QPQ is also required for editors with 20 past nominations.',
size: '50px',
value: NOMPAGE_PREFIX
});
form.append({
type: 'div',
name: 'qpq-required',
label: 'Number of QPQs required: <span id=dyk-qpq-count>calculating ...</span>'
});
form.append({
type: 'button',
label: 'find articles to review',
event: function() {
window.open('//en.wikipedia.org/wiki/' mw.util.wikiUrlencode(NOMINATIONS_PAGE) '#Nominations');
}
});
form.append({
type: 'textarea',
name: 'comments',
label: 'Comments ',
tooltip: 'Any additional comments (optional). Do not include signature.',
className: 'dyk-comments'
});
var previewlink = Morebits.htmlNode('a', 'Preview');
previewlink.style.cursor = "pointer";
$(previewlink).click(function() {
result.previewer.beginRender(dyk.getDiscussionWikitext(result), "API"); // |result| is defined below
});
form.append( { type: 'div', id: 'dykpreview', label: [ previewlink ] } );
form.append( { type: 'div', id: 'dyk-previewbox', style: 'display: none' } );
form.append( { type: 'submit', label: 'Submit' } );
var result = form.render();
Window.setContent( result );
Window.display();
result.previewer = new Morebits.wiki.preview(document.getElementById('dyk-previewbox'));
dyk.txtareaModifications(result.hook, 'hook');
dyk.txtareaModifications(result.source, 'source');
dyk.txtareaModifications(result.comments, 'comments');
// morebits should really allow a postRender hook for quickform elements ...
Morebits.quickForm.getElementContainer(result.img).style.margin = '5px 0';
// remove the awkward lack of alignment of text input fields with other fields
Morebits.quickForm.getElementContainer(result.author).style.marginLeft = '4px';
Morebits.quickForm.getElementContainer(result.qpq).style.marginLeft = '4px';
mw.util.addCSS(
'form.quickform div textarea.dyk-hook { font-size: 125%; height: 38px; }'
'form.quickform div textarea.dyk-source { font-size: 110%; height: 35px; }'
'form.quickform div textarea.dyk-comments { font-size: 125%; height: 35px; }'
//'form.quickform div.dyk-source { display: table-row; }'
//'form.quickform div.dyk-source label { display: table-cell; vertical-align: middle; }'
//'div.dyk-source > textarea { font-size: 110%; height: 19px; }'
//'html form.quickform div textarea.dyk-source { font-size: 110%; height: 35px; }'
);
// Add date check element
var d = document.createElement('span');
d.style.color = 'red';
d.style.paddingLeft = '5px';
$('.quickform [name=date]').parent().append(d);
$('.quickform [name=date]')[0].type = 'date';
$('.quickform [name=date]').on('change', dyk.dateCheck);
// Show number of QPQs required
mw.loader.using('ext.gadget.libLua').then(function() {
return mw.libs.lua.call({
module: 'NewDYKnomination',
func: 'getRequiredQpqCount',
args: [mw.config.get('wgUserName')]
});
}).then(function(output) {
var [numQpqsNeeded, numPriorNoms] = output.split('\t');
dyk.numQpqsRequiredPerArticle = numQpqsNeeded;
if (numQpqsNeeded === '2') {
$('#dyk-qpq-count').text('2, as DYK is currently in backlog mode and you have ' numPriorNoms ' past nominations');
} else if (numQpqsNeeded === '1') {
$('#dyk-qpq-count').text('1, as you have ' numPriorNoms ' past nominations');
} else {
$('#dyk-qpq-count').text('0, as you have fewer than 5 past nominations');
}
}).catch(function(err) {
$('#dyk-qpq-count').text('failed to calculate');
console.log(err);
});
};
dyk.dateCheck = function dykDateCheck(e) {
var checkElem = e.target.nextElementSibling;
var date = new Date(e.target.value);
var curDate = new Date();
var diff = curDate.getTime() - date.getTime();
if (date.toString() === 'Invalid date' || diff < 0) {
checkElem.textContent = 'Invalid date';
checkElem.style.color = 'red';
return;
}
var diffdays = diff/(1000*60*60*24);
if (diffdays >= 10) {
checkElem.textContent = 'Must be within the past week, see WP:DYK#New';
checkElem.style.color = '#fa3800e8';
} else if (diffdays >= 8) {
checkElem.textContent = 'Possibly ineligible as date is not within the past week';
checkElem.style.color = '#8f8946';
} else {
checkElem.textContent = '';
}
};
dyk.txtareaModifications = function dykTxtareaModifications(txtarea, type) {
var $txtarea = $(txtarea);
if (type === 'source') {
// var width = txtarea.parentElement.previousElementSibling.offsetWidth - txtarea.previousElementSibling.offsetWidth;
// txtarea.style.width = width 'px';
txtarea.previousElementSibling.style.borderTop = 'none';
txtarea.previousElementSibling.style.marginTop = '0';
}
else if (type === 'comments') {
txtarea.previousElementSibling.style.borderTop = 'none';
}
else if (type === 'hook') {
// Add character counter
var stdiv = document.createElement('div');
stdiv.style.float = 'right';
stdiv.style.fontWeight = 'normal';
$txtarea.prev().append(stdiv);
$txtarea.on('keyup', function updateCharCount() {
var len = this.value
.replace(/^\.\.\. ?/, '') // remove ... in the beginning
.replace(/'''/g, '') // remove bold syntax
.replace(/''(\(.*?\))'' /, '') // remove italic text in brackets
.replace(/''/g, '') // remove any remaining italic syntax
.replace(/\[/g, '\1').replace(/\]/g, '\2') // remove link syntax and piped part - step 1
.replace(/\2\2/g, '') // step 2
.replace(/\1\1(?:[^\1\2]*?\|)?/g, '') // step 3
.length;
stdiv.textContent = len ' characters';
if (len > 200) {
stdiv.style.color = 'red';
} else {
stdiv.style.color = '#222222'; // default morebits color
}
});
$txtarea.trigger('keyup');
}
};
dyk.createPlusButton = function(id) {
var a = document.createElement('a');
a.id = id;
a.style.paddingLeft = '5px';
var img = document.createElement('img');
img.setAttribute('src', '//upload.wikimedia.org/wikipedia/commons/thumb/b/b9/Nuvola_action_edit_add.svg/20px-Nuvola_action_edit_add.svg.png');
img.setAttribute('alt', 'add another');
img.setAttribute('title', 'Add another');
a.append(img);
return a;
};
dyk.getDiscussionWikitext = function dykGetDiscussionWikitext(form) {
var params = {};
for (var i in form.elements) {
var el = form.elements[i];
if (el.type) {
if (el.type === 'checkbox') {
params[el.name] = el.checked;
} else if (el.type === 'select-one' || el.type === 'text' || el.type === 'textarea') {
params[el.name] = el.value;
}
}
}
var templatetext =
'{{subst:NewDYKnomination'
' | article = ' Morebits.pageNameNorm;
dyk.articles = [Morebits.pageNameNorm];
$('input[name^="multiarticle.article"]').each(function(i, el) {
if (params[el.name]) { // skip if empty
templatetext = ' | ' el.name.slice('multiarticle.'.length) ' = ' params[el.name];
dyk.articles.push(params[el.name]);
}
});
templatetext =
' | status = ' params.status
' | hook = ' params.hook (params.source ? (' <small>Source: ' params.source '</small>') : '');
var ALTgiven = function(n) {
return params['ALT' n] !== '' && !/^\.\.\. ?that ?$/.test(params['ALT' n]);
};
$('textarea[name^="ALT"]').each(function(i, el) {
var n = el.name.slice('ALT'.length); // string form
if (ALTgiven(n)) {
templatetext = ' | ' el.name ' = ' params[el.name]
(params['source' n] ? (' <small>Source: ' params['source' n] '</small>') : '');
}
});
templatetext =
' | author = ' params.author;
$('input[name^="multiauthor.author"]').each(function(i, el) {
if (params[el.name]) {
templatetext = ' | ' el.name.slice('multiauthor.'.length) ' = ' params[el.name];
}
});
templatetext =
' | image = ' (params.img ? params['img.name'] : '')
' | caption = ' (params.img ? params['img.caption'] : '')
' | comment = ' params.comments
' | reviewed = ' (params.qpq !== NOMPAGE_PREFIX ? '[[' params.qpq ']]' : '')
'}}';
return templatetext;
};
dyk.evaluate = function dykEvaluate(e) {
var form = e.target;
var date = form.date.value;
// Validation
if (!date) {
alert('Please specify the date as of which creation/expansion has been completed');
return;
}
if (form.hook.value === '') {
alert('Please specify the hook for DYK nomination');
return;
}
var broken = false; // flag
var problem; // start or end
var problemhook;
var sourcewarning = false;
$(form).find('.dyk-hook').each(function(i, e) {
var isGiven = e.value !== '' && !/^\.\.\. ?that ?$/.test(e.value);
if (!isGiven) return true; // continue
e.value = e.value.trim();
if (e.value.indexOf('... that ') !== 0) {
problem = 'start';
problemhook = e.name;
broken = true;
return false; // break
}
if (e.value.slice(-1) !== '?') {
problem = 'end';
problemhook = e.name;
broken = true;
return false;
}
if ($(e).parent().next().find('textarea')[0].value === '') {
sourcewarning = true;
}
});
if (broken) {
alert('Hook ' (problemhook === 'hook' ? '' : problemhook) ' must ' (problem === 'start' ? 'start with "... that "' : 'end with a question mark (?)'));
return;
}
// Date error handling, also done above for on keyup in input field
date = new Morebits.date(date);
var curDate = new Morebits.date();
var diff = curDate.getTime() - date.getTime();
if (date.toString() === 'Invalid date' || diff < 0) {
alert("Please specify a valid date");
return;
}
var diffdays = diff/(1000*60*60*24);
if (diffdays >= 10) {
alert('The date specified is well outside the past week, and hence the article is ineligible for DYK, see WP:DYK#New');
return;
}
if (diffdays >= 8 && !confirm('The date specified is not within the past week, see WP:DYK#New. Are you sure you want to continue?')) {
return;
}
var prosesizewarn = $('#dyk-prosesize').css('color') === "rgb(255, 0, 0)";
if (prosesizewarn && !confirm('This article has fewer than 1500 characters of readable prose. \n\nWhile you may still nominate it for DYK, it may be rejected unless you expand it to more than 1500 characters after the nomination. \n\nClick OK to continue with the nomination.' )) {
return;
}
if (sourcewarning && !confirm('You have not specified the source for each hook. Are you sure you want to continue?')) {
return;
}
if (dyk.numQpqsRequiredPerArticle > 0 &&
!/Template:Did you know nominations\/\w /.test(form.qpq.value) &&
!confirm('You have not specified a QPQ. The nomination may be rejected unless you provide a QPQ soon after the nomination. Are you sure you want to continue?')) {
return;
}
var templatetext = dyk.getDiscussionWikitext(form);
Morebits.simpleWindow.setButtonsEnabled( false );
Morebits.status.init(form);
Morebits.wiki.actionCompleted.redirect = NOMINATIONS_PAGE '#' dyk.articles.join(', ');
Morebits.wiki.actionCompleted.notice = 'Completed';
Morebits.wiki.api.setApiUserAgent('[[w:User:SD0001/DYK-helper.js|DYK-helper]]');
var nompage = new Morebits.wiki.page(NOMPAGE_PREFIX Morebits.pageNameNorm, 'Creating nomination page');
nompage.setAppendText(templatetext);
nompage.setCreateOption('createonly');
nompage.setWatchlist(true);
nompage.setEditSummary('Creating DYK nomination for [[' dyk.articles.join(']], [[') ']]' dyk.advert);
nompage.append(function onNominationSuccess() {
var dykpage = new Morebits.wiki.page(NOMINATIONS_PAGE, 'Adding nomination to ' NOMINATIONS_PAGE);
dykpage.load(function addNomToTTDYK(dykpage) {
var pageText = dykpage.getPageText();
var todaysHeader = 'Articles created/expanded on ' date.getUTCMonthName() ' ' date.getUTCDate();
var re = new RegExp('===' todaysHeader '===\n<!--.*?-->');
var newPageText = pageText.replace(re, '$&\n{{' NOMPAGE_PREFIX Morebits.pageNameNorm '}}');
if (pageText === newPageText) {
var linknode = document.createElement('a');
linknode.setAttribute("href", mw.util.getUrl("User:SD0001/DYK-helper/Fixing nomination"));
linknode.appendChild(document.createTextNode('Repair nomination'));
dykpage.getStatusElement().error(['Could not find the target spot for the nomination. Please see: ', linknode, '.']);
return;
}
dykpage.setPageText(newPageText);
dykpage.setEditSummary('/* ' todaysHeader ' */ ' 'Adding [[' NOMPAGE_PREFIX Morebits.pageNameNorm ']]' dyk.advert);
dykpage.setMaxConflictRetries(3);
dykpage.save();
});
dyk.articles.forEach(function transcludeOnTalk(page) {
var talkpagename = 'Talk:' page;
var talkpage = new Morebits.wiki.page(talkpagename, 'Transcluding nomination on ' talkpagename);
talkpage.setAppendText('\n\n==Did you know nomination==\n{{' NOMPAGE_PREFIX Morebits.pageNameNorm '}}\n');
talkpage.setEditSummary('Nominated for DYK, see [[' NOMPAGE_PREFIX Morebits.pageNameNorm ']]' dyk.advert);
talkpage.setCreateOption('recreate');
if (window.DYKH_watchlistTalkPage === undefined) {
talkpage.setWatchlistFromPreferences(true);
} else {
talkpage.setWatchlist(window.DYKH_watchlistTalkPage);
}
talkpage.append();
});
}, function onNominationFailure() {
Morebits.status.printUserText(templatetext, 'Arrgh :( Something bad happened. Your DYK template is provided below, which you can copy and use to create ' nompage.getPageName() ' manually.');
});
};
}
// </nowiki>