MediaWiki:DYK-nomination-wizard.js: Difference between revisions

Content deleted Content added
Refactored User:SD0001/DYK-helper.js for loading on a project using URL parameter
 
per tper
 
(9 intermediate revisions by 5 users not shown)
Line 1:
/**
* DYK-nomination-wizard
* Form loaded on [[Wikipedia:Did you know/Create new nomination]]
*
* Wizard to easily create DYK nominations
*
* Loaded on [[Wikipedia:Did you know/Create new nomination]]
* using [[mw:Snippets/Load JS and CSS by URL]]
*
* Author: [[User:SD0001]]
* 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
*/
 
Line 28 ⟶ 29:
var NOMINATIONS_PAGE = 'Template talk:Did you know';
 
dyk.advert = ' ([[UserWikipedia:SD0001Did you know/DYK-helperNomination wizard|DYK-helperwizard]])';
 
// Calculating prose character count, code based on [[User:Shubinator/DYKcheck.js]] and [[User:Dr pda/prosesize.js]]
Line 44 ⟶ 45:
}
return charCount;
};
 
dyk.proseCharCount = function ($html) {
var charCount = 0, readable_text = '';
$html.find('> p').each(function(i, el) {
charCount += dyk.proseCharCountInNode(el);
readable_text += el.textContent.trim();
});
var wordCount = readable_text.split(/\s/).length;
 
var prosesizediv = document.getElementById('dyk-prosesize');
prosesizediv.textContent = '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';
}
};
 
dyk.updateProseSize = function (form, article) {
if (!article) return;
// Speedy prose size calculation faster without API call if we were invoked from the article
try { // Just in case of issues with window.opener use
if (window.opener && opener.document.title === article + ' - Wikipedia') { // ideally should compare with MediaWiki:Pagetitle
var $html = $(opener.document.body).find('.mw-parser-output');
dyk.proseCharCount($html);
return;
}
} catch(e) { console.log(e); }
$('#dyk-prosesize').text('Prose size: calculating...').css('color', 'black');
new mw.Api().get({
Line 56 ⟶ 85:
}).then(function (json) {
if (article !== form.article.value) {
$('#dyk-prosesize').text('');
return; // input was changed, ignore this response
}
var $html = $(json.parse.text);
dyk.proseCharCount($html);
 
}).catch(function (code, error) {
var charCount = 0, readable_text = '';
if (code === 'missingtitle') {
$html.find('> p').each(function(i, el) {
$('#dyk-prosesize').text('Article does not exist!').css('color', 'red');
charCount += dyk.proseCharCountInNode(el);
readable_text += el.textContent.trim();
});
var wordCount = readable_text.split(/\s/).length;
 
var prosesizediv = document.getElementById('dyk-prosesize');
prosesizediv.textContent = '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 {
$('#dyk-prosesize').text(''); // empty it
prosesizediv.style.color = 'green';
console.error(error);
}
});
};
 
dyk.callback = function dykMainCallback(e) {
if (e) e.preventDefault();
 
var form = new Morebits.quickForm( dyk.evaluate );
 
form.append({
type: 'div',
style: 'float: right; font-style: italic;',
id: 'dyk-prosesize',
label: 'Prose size: calculating...'
});
 
Line 117 ⟶ 135:
label: 'on date: ',
name: 'date',
tooltip: 'The date as ofon which creation/expansion has been completedbegan. Must be within the past week. ',
value: new Date().toISOString().slice(0, 10), // YYYY-MM-DD format
event: dyk.dateCheck // for the benefit of browsers that don't support a datepicker for date fields
});
Line 290 ⟶ 307:
name: 'qpq',
label: 'Reviewed: ',
tooltip: 'DYK nomination(s) you reviewed. This is compulsorymandatory for editors with 5+ DYKprior creditsnominations (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',
Line 319 ⟶ 343:
type: 'div',
style: 'float: right; font-size: smaller;', //text-decoration: italic;',
label: $('<ulspan>')
.addClass('hlist')
.append(
$('<li>').append(Morebits.createHtml([
'[[w:WP:DYKRULES|DYK rules]]')),
$('<li>').append(Morebits.createHtml( '[[Userw:Wikipedia talk:SD0001Did you know/DYK-helperNomination wizard|Give feedback]]'))
].join(' &bull; '))
).get()
});
Line 329 ⟶ 354:
var result = form.render();
 
// Attach to the page, #dyk-helperwizard-container is provided by the wikitext
$('#dyk-helperwizard-container').empty().append(result);
 
dyk.updateProseSize(result, mw.util.getParamValue('article'));
Line 365 ⟶ 390:
'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; }'
);
 
Line 420 ⟶ 441:
 
$(Morebits.quickForm.getElementContainer(result.multiarticle)).css('margin-top', '10px');
 
// 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').map(num => parseInt(num));
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 if (numQpqsNeeded === 0) {
$('#dyk-qpq-count').text('0, as you have fewer than 5 past nominations');
} else {
$('#dyk-qpq-count').text('failed to calculate');
}
}).catch(function(err) {
$('#dyk-qpq-count').text('failed to calculate');
console.log(err);
});
 
};
Line 429 ⟶ 474:
var diff = curDate.getTime() - date.getTime();
if (date.toString() === 'Invalid date' || diff < 0) {
checkElem.textContent = 'Invalid date' + (diff < 0 ? '. Back from the future, are you?' : '');
checkElem.style.color = 'red';
return;
}
var diffdays = diff/(1000*60*60*24);
if (diffdays >= 1012) {
$(checkElem).html(Morebits.createHtml('Date must be within the past week, see [[WP:DYK#New]]'));
checkElem.style.color = '#fa3800e8red';
} else if (diffdays >= 8) {
checkElem.textContent = 'Possibly ineligible as date is not within the past week';
Line 449 ⟶ 494:
 
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';
Line 518 ⟶ 561:
 
addTemplateParam('status', params.status);
addTemplateParam('hook', params.hook + (params.source ? ('\n{{smalldiv|1= \n* <small>Source: ' + params.source + '</small>}}') : ''));
 
Object.keys(params).filter(function (field) {
Line 597 ⟶ 640:
}
var diffdays = diff/(1000*60*60*24);
if (diffdays >= 1012) {
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 afewer readablethan prose1500 sizecharacters of lessreadable than 1500 charactersprose. \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;
}
Line 621 ⟶ 667:
Morebits.wiki.actionCompleted.redirect = NOMINATIONS_PAGE + '#' + dyk.articles.join(', ');
Morebits.wiki.actionCompleted.notice = 'Completed';
Morebits.wiki.api.setApiUserAgent('[[w:Useren:MediaWiki:SD0001/DYK-helpernomination-wizard.js|DYK-helper]]');
 
var nompage = new Morebits.wiki.page(NOMPAGE_PREFIX + article, 'Creating nomination page');
Line 638 ⟶ 684:
if (pageText === newPageText) {
var linknode = document.createElement('a');
linknode.setAttribute("href", mw.util.getUrl("UserWikipedia:SD0001Did you know/DYK-helperNomination wizard/Fixing nomination"));
linknode.appendChild(document.createTextNode('Repair nomination'));
dykpage.getStatusElement().error(['Could not find the target spot for the nomination. Please see: ', linknode, '.']);