User:Polygnotus/Scripts/PreviousAfDs.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 102:
function getAfDDates(pages, articleTitle) {
var api = new mw.Api();
var if (pagesWithDates.length === 0) {[];
var pageTitles = pages.map(function(page) { return page.title; });
var mostRecentDate = null;
var mostRecentTimestamp = 0;
var completedRequests = 0;
console.log('Fetching dates for:', pageTitlespages.map(function(p); //{ Debugreturn logp.title; }));
// GetIf creationno datespages, forjust alldisplay pagesempty results
if (pages.length === 0) {
api.get({
displayAfDResults(pages, articleTitle);
action: 'query',
titles: pageTitles.join('|'),return;
}
prop: 'revisions',
rvlimit: 1,
// Fetch date for each page individually
rvdir: 'newer', // Get the first revision (creation)
})pages.thenforEach(function(datapage) {
rvprop: 'timestamp',
format: 'json'api.get({
action: 'query',
}).then(function(data) {
console.log('API response:', data); // Debugformat: log'json',
prop: 'revisions',
var pagesWithDates = []; titles: page.title,
var mostRecentDate = null; rvlimit: 1,
var mostRecentTimestamp = 0; rvdir: 'newer',
rvprop: 'timestamp'
}).catchthen(function(errordata) {
// Process each page and add creation date
var pageData = })null;
if (data.query && data.query.pages) {
var hasDate = false;
Object.keys(data.query.pages).forEach(function(pageId) {
var pageData = data.query.pages[pageId];
if (data.query && data.query.pages) {
var pageId = Object.keys(data.query.pages).forEach(function(pageId) {[0];
pageData = data.query.pages[pageId];
// Check if page exists and has revisions
if (pageData && !pageData.missing && pageData.revisions && pageData.revisions.length > 0) {
var timestamp = pageData.revisions[0].timestamp;
Line 133 ⟶ 138:
var timestampMs = date.getTime();
console.log('Page:', pageData.title, 'Date:', date.toDateString()); // Debug log
pagesWithDates.push({
Line 146 ⟶ 151:
mostRecentDate = date;
}
} else { hasDate = true;
console.log('No revisions found for:', pageData ? pageData.title : 'unknown page');
// Still add the page without date
pagesWithDates.push({
title: pageData ? pageData.title : 'Unknown page',
date: null,
timestamp: 0
});
}
}
// If no date // Stillfound, add the page without date
if (!a.timestamphasDate) return 1;{
console.log('No revisionsdate found for:', pageData ? pageDatapage.title : 'unknown page');
pagesWithDates.push({
title: pageData ? pageDatapage.title : 'Unknown page',
date: null,
timestamp: 0
});
}
completedRequests++;
// When all requests are complete, display results
if (completedRequests === pages.length) {
// Sort by date (most recent first, pages without dates last)
pagesWithDates.sort(function(a, b) {
if (!a.timestamp && !b.timestamp) return 0;
if (!ba.timestamp) return -1;
return if (!b.timestamp) -return a.timestamp-1;
return b.timestamp - a.timestamp;
});
// Check if pagemost existsrecent andis haswithin revisions3 months
var threeMonthsAgo = new Date();
threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
var isRecent = mostRecentDate && mostRecentDate > threeMonthsAgo;
console.log('Most recent date:', mostRecentDate ? mostRecentDate.toDateString() : 'none', 'Is recent:', isRecent); // Debug log
displayAfDResults(pagesWithDates, articleTitle, mostRecentDate, isRecent);
}
}).catch(function(error) {
console.log('API error for', page.title, ':', error); // Debug log
// Add page without date on error
pagesWithDates.push({
title: page.title,
date: null,
rvprop: ' timestamp',: 0
});
}
completedRequests++;
// If we couldn't get any dates, fall back to original pages
// When all requests are complete (including errors), display results
if (pagesWithDates.length === 0) {
pagesWithDatesif (completedRequests === pages;.length) {
displayAfDResults(pagesWithDates, articleTitle, mostRecentDate, false);
}
}
// Sort by date (most recent first, pages without dates last)
pagesWithDates.sort(function(a, b) {
if (!a.timestamp && !b.timestamp) return 0;
if (!a.timestamp) return 1;
if (!b.timestamp) return -1;
return b.timestamp - a.timestamp;
});
// Check if most recent is within 3 months
var threeMonthsAgo = new Date();
threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
var isRecent = mostRecentDate && mostRecentDate > threeMonthsAgo;
console.log('Most recent date:', mostRecentDate ? mostRecentDate.toDateString() : 'none', 'Is recent:', isRecent); // Debug log
displayAfDResults(pagesWithDates, articleTitle, mostRecentDate, isRecent);
}).catch(function(error) {
console.log('API error:', error); // Debug log
// If date fetching fails, display without dates
displayAfDResults(pages, articleTitle);
});
}