User:Asked42/QuickCat.js
Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins
(function() {
if (typeof createCategories === 'undefined') {
var createCategories = {};
}
createCategories.api = new mw.Api();
$(document).ready(function() {
var formHtml = `
<div style="margin-top: 20px; padding: 20px; border: 1px solid #ccc; border-radius: 5px; max-width: 500px; margin: 0 auto;">
<h2 style="margin-bottom: 10px; text-align: center;">Create Category and Lists Pages</h2>
<div style="margin-bottom: 10px;">
<label for="year" style="display: block; margin-bottom: 5px;">Year:</label>
<input id="year" type="text" required style="width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 5px;">
</div>
<div style="margin-bottom: 10px;">
<label style="display: block; margin-bottom: 5px;">Select Month(s):</label>
<div id="month-checkboxes" style="padding: 8px; border: 1px solid #ccc; border-radius: 5px;">
<label><input type="checkbox" value="1"> January</label><br>
<label><input type="checkbox" value="2"> February</label><br>
<label><input type="checkbox" value="3"> March</label><br>
<label><input type="checkbox" value="4"> April</label><br>
<label><input type="checkbox" value="5"> May</label><br>
<label><input type="checkbox" value="6"> June</label><br>
<label><input type="checkbox" value="7"> July</label><br>
<label><input type="checkbox" value="8"> August</label><br>
<label><input type="checkbox" value="9"> September</label><br>
<label><input type="checkbox" value="10"> October</label><br>
<label><input type="checkbox" value="11"> November</label><br>
<label><input type="checkbox" value="12"> December</label><br>
</div>
</div>
<div style="margin-bottom: 10px;">
<label for="category-type" style="display: block; margin-bottom: 5px;">Select Type:</label>
<select id="category-type" style="width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 5px;">
<option value="date">Date Category</option>
<option value="month">Month Category</option>
<option value="wikinews-month">Wikinews Month List</option>
<option value="wikinews-date">Wikinews Date List</option>
</select>
</div>
<button id="preview-categories-button" style="background-color: #3366cc; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer;">Preview Pages</button>
</div>
<div id="preview-section" style="display: none; margin-top: 20px; padding: 20px; border: 1px solid #ccc; border-radius: 5px; max-width: 500px; margin: 20px auto;">
<h2 style="margin-bottom: 10px; text-align: center;">Preview Targeted Pages</h2>
<div id="preview-list" style="margin-bottom: 10px;"></div>
<button id="create-categories-button" style="background-color: #3366cc; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer;">Good to go</button>
</div>
`;
$('#category-create-form').html(formHtml);
$('#preview-categories-button').click(function() {
var year = $('#year').val().trim();
var months = $('#month-checkboxes input:checked').map(function() { return $(this).val(); }).get();
var categoryType = $('#category-type').val();
if (year && months.length > 0) {
$('#preview-list').empty();
var previewPromises = [];
if (categoryType === 'date') {
previewPromises = getPreviewDateCategories(year, months);
} else if (categoryType === 'month') {
previewPromises = getPreviewMonthCategories(year, months);
} else if (categoryType === 'wikinews-month') {
previewPromises = getPreviewWikinewsMonthList(year, months);
} else if (categoryType === 'wikinews-date') {
previewPromises = getPreviewWikinewsDateList(year, months);
}
Promise.all(previewPromises).then(function(previewItems) {
previewItems.forEach(function(monthItems) {
monthItems.forEach(function(item) {
var content = item.content;
var checkbox = `
<div style="margin-bottom: 10px;">
<label><input type="checkbox" value="${item.title}" checked> ${item.title}</label>
<textarea style="width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 5px;" data-title="${item.title}">${content}</textarea>
</div>
`;
$('#preview-list').append(checkbox);
});
});
$('#preview-section').show();
}).catch(function(error) {
console.error('Error generating preview:', error);
alert('An error occurred while generating the preview.');
});
} else {
alert('Please fill in both Year and Month fields.');
}
});
$('#create-categories-button').click(function() {
var year = $('#year').val().trim();
var selectedTitles = $('#preview-list input:checked').map(function() { return $(this).val(); }).get();
var categoryType = $('#category-type').val();
if (selectedTitles.length > 0) {
var promises = selectedTitles.map(function(title) {
return checkCategoryExists(title).then(function(exists) {
var content = $(`textarea[data-title="${title}"]`).val();
return { title: title, exists: exists, content: content };
});
});
handlePromises(promises, categoryType);
} else {
alert('Please select at least one page to create/update.');
}
});
});
function getPreviewDateCategories(year, months) {
var previewPromises = [];
months.forEach(function(month) {
var promise = new Promise(function(resolve, reject) {
var daysInMonth = getDaysInMonth(month, year);
var previewItems = [];
var monthName = getMonthName(month);
for (var day = 1; day <= daysInMonth; day++) {
var title = `Category:${monthName} ${day}, ${year}`;
var content = '{{Datecategory}}';
previewItems.push({ title: title, content: content });
}
resolve(previewItems);
});
previewPromises.push(promise);
});
return previewPromises;
}
function getPreviewMonthCategories(year, months) {
var previewPromises = [];
months.forEach(function(month) {
var promise = new Promise(function(resolve, reject) {
var monthName = getMonthName(month);
var title = `Category:${monthName} ${year}`;
var content = '{{monthcategory}}';
resolve([{ title: title, content: content }]);
});
previewPromises.push(promise);
});
return previewPromises;
}
function getPreviewWikinewsMonthList(year, months) {
var previewPromises = [];
months.forEach(function(month) {
var promise = new Promise(function(resolve, reject) {
var monthName = getMonthName(month);
var title = `Wikinews:${year}/${monthName}`;
var content = '{{Monthpage}}';
resolve([{ title: title, content: content }]);
});
previewPromises.push(promise);
});
return previewPromises;
}
function getPreviewWikinewsDateList(year, months) {
var previewPromises = [];
months.forEach(function(month) {
var promise = new Promise(function(resolve, reject) {
var daysInMonth = getDaysInMonth(month, year);
var previewItems = [];
var monthName = getMonthName(month);
for (var day = 1; day <= daysInMonth; day++) {
var title = `Wikinews:${year}/${monthName}/${day}`;
var content = `{{DateDPL|${day}|${monthName}|${year}}}`;
previewItems.push({ title: title, content: content });
}
resolve(previewItems);
});
previewPromises.push(promise);
});
return previewPromises;
}
function getDaysInMonth(month, year) {
return new Date(year, month, 0).getDate();
}
function checkCategoryExists(title) {
return createCategories.api.get({
action: 'query',
format: 'json',
prop: 'revisions',
titles: title,
formatversion: '2',
rvprop: 'content',
rvslots: '*'
}).then(function(data) {
var page = data.query.pages[0];
return page.missing === '' ? false : true;
});
}
function handlePromises(promises, categoryType) {
Promise.all(promises).then(function(results) {
var totalPages = results.length;
var message = `There are total ${totalPages} pages targeted. Do you want to create or update them?`;
if (confirm(message)) {
updateCategories(results, categoryType);
} else {
alert('Operation cancelled.');
}
}).catch(function(error) {
console.error('Error checking categories:', error);
alert('An error occurred while checking the categories.');
});
}
function updateCategories(results, categoryType) {
var summary;
if (categoryType === 'date' || categoryType === 'month') {
summary = 'Creating/updating category, using [[User:Asked42/QuickCat.js|QuickCat]].';
} else {
summary = 'Creating/updating list page, using [[User:Asked42/QuickCat.js|QuickCat]].';
}
var updatePromises = results.map(function(result) {
return createCategories.api.postWithToken('csrf', {
action: 'edit',
format: 'json',
title: result.title,
formatversion: '2',
text: result.content,
summary: summary
}).then(function(response) {
console.log(`Processed ${result.title}`);
}).catch(function(error) {
console.error(`Error processing ${result.title}:`, error);
});
});
Promise.all(updatePromises).then(function() {
alert(`${categoryType === 'date' || categoryType === 'month' ? 'Categories' : 'List pages'} created/updated successfully.`);
}).catch(function(error) {
console.error('Error creating/updating categories:', error);
alert(`An error occurred while creating/updating the ${categoryType === 'date' || categoryType === 'month' ? 'categories' : 'list pages'}.`);
});
}
function getMonthName(month) {
var monthNames = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
return monthNames[month - 1];
}
})();