// Only run on AfD edit pages
if (mw.config.get('wgPageName').startsWith('Wikipedia:Articles_for_deletion/') && mw.config.get('wgAction') === 'edit') {
$(function() {
// Add button after the h1#firstHeading
$('<button>')
.attr('id', 'generateSourceTableButton')
.text('Generate Source Table')
.css({
'margin-left': '10px',
'margin-bottom': '10px'
})
.click(generateSourceTable)
.insertAfter('#firstHeading');
});
}
function generateSourceTable() {
// Extract article name from AfD page title
var afdPageName = mw.config.get('wgPageName');
var articleName = extractArticleName(afdPageName);
// Fetch article content
new mw.Api().get({
action: 'query',
prop: 'revisions',
rvprop: 'content',
titles: articleName,
formatversion: 2
}).done(function(data) {
var pageContent = data.query.pages[0].revisions[0].content;
// Extract references
var references = extractReferences(pageContent);
if (references.length === 0) {
// If there are no references, notify the user and remove the button
mw.notify('No references found in the article.', {type: 'warning'});
$('#generateSourceTableButton').remove();
return;
}
var tableWikicode = '\n\n{{static row numbers}}\n';
tableWikicode += '{| class="wikitable mw-collapsible static-row-numbers"\n';
tableWikicode += `|+ class="nowrap" | Source review by ~~` + `~\n`;
tableWikicode += '|-\n';
tableWikicode += '! Source !! Comments\n';
references.forEach(function(ref) {
tableWikicode += '|-\n| ' + ref + ' \n|| \n';
});
tableWikicode += '|}';
// Get the edit textarea
var editTextarea = document.getElementById('wpTextbox1');
if (editTextarea) {
// Insert the table at the end of the current content
editTextarea.value += tableWikicode;
// Trigger a change event to update the preview if it's open
$(editTextarea).trigger('change');
mw.notify('Source table added to the edit area. Please review and save your changes when ready.');
} else {
mw.notify('Failed to find the edit textarea.', {type: 'error'});
}
}).fail(function() {
mw.notify('Failed to fetch article content. Please try again.', {type: 'error'});
});
}
function extractArticleName(afdPageName) {
var parts = afdPageName.replace('Wikipedia:Articles_for_deletion/', '').split('(');
if (parts.length > 1) {
// Check if the last part contains a nomination number
var lastPart = parts[parts.length - 1];
if (/^\d+(st|nd|rd|th)\s+nomination\)$/.test(lastPart.trim())) {
// It's a nomination, so we remove the last part
return parts.slice(0, -1).join('(').trim();
}
}
// It's not a nomination, so we return the full name
return afdPageName.replace('Wikipedia:Articles_for_deletion/', '').trim();
}
function extractReferences(content) {
var references = [];
var index = 0;
while (true) {
var startIndex = content.indexOf('<ref', index);
if (startIndex === -1) break;
var endIndex = content.indexOf('>', startIndex);
if (endIndex === -1) break;
var tagContent = content.substring(startIndex, endIndex + 1);
if (tagContent.endsWith('/>')) {
// This is a self-closing tag, skip it
index = endIndex + 1;
continue;
}
var refEndIndex = content.indexOf('</ref>', endIndex);
if (refEndIndex === -1) break;
var refContent = content.substring(endIndex + 1, refEndIndex).trim();
// Replace {{|}} with |
refContent = refContent.replace(/\{\{\!\}\}/g, '|');
references.push(refContent);
index = refEndIndex + 6; // Move past '</ref>'
}
return references;
}