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

Content deleted Content added
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1:
//https://en.wikipedia.org/wiki/Wikipedia_talk:Twinkle#Twinkle_feature_request_--_look_for_previous_XfDs
 
// Add AfD search functionality to Wikipedia
$(document).ready(function() {
Line 50 ⟶ 52:
aplimit: 50,
format: 'json'
}).donethen(function(data) {
var exactMatches = data.query.allpages || [];
Line 61 ⟶ 63:
srlimit: 50,
format: 'json'
}).donethen(function(searchData) {
var searchMatches = searchData.query.search || [];
Line 90 ⟶ 92:
displayAfDResults(allMatches, articleTitle);
}
}).failcatch(function() {
// If search fails, just show exact matches
displayAfDResults(exactMatches, articleTitle);
});
}).failcatch(function() {
$('#afd-search-btn').text('Search AfD').css('color', '#0645ad');
mw.notify('Error searching for AfD discussions', { type: 'error' });
Line 102 ⟶ 104:
function getAfDDates(pages, articleTitle) {
var api = new mw.Api();
var pagesWithDates = [];
var pageTitles = pages.map(function(page) { return page.title; });
var mostRecentDate = null;
var mostRecentTimestamp = 0;
var completedRequests = 0;
// Get creationconsole.log('Fetching dates for all:', pages.map(function(p) { return p.title; }));
api.get({
// If no pages, just display empty results
action: 'query',
if (pages.length === 0) {
titles: pageTitles.join('|'),
displayAfDResults(pages, articleTitle);
prop: 'revisions',
rvlimit: 1,return;
}
rvdir: 'newer', // Get the first revision (creation)
rvprop: 'timestamp',
// Fetch date for each page individually
format: 'json'
})pages.doneforEach(function(datapage) {
var pagesWithDates = [];api.get({
var mostRecentDate = null; action: 'query',
var mostRecentTimestamp = 0; format: 'json',
prop: 'revisions',
// Process each page andtitles: add creation datepage.title,
rvlimit: 1,
Object.keys(data.query.pages).forEach(function(pageId) {
varrvdir: pageData = data.query.pages[pageId];'newer',
rvprop: 'timestamp'
if (pageData.revisions && pageData.revisions.length > 0) {
}).then(function(data) {
var timestamp = pageData.revisions[0].timestamp;
var datepageData = new Date(timestamp)null;
var timestampMshasDate = date.getTime()false;
if (data.query && data.query.pages) {
var pageId = Object.keys(data.query.pages)[0];
pageData = data.query.pages[pageId];
if (pageData && !pageData.missing && pageData.revisions && pageData.revisions.length > 0) {
var timestamp = pageData.revisions[0].timestamp;
var date = new Date(timestamp);
var timestampMs = date.getTime();
console.log('Page:', pageData.title, 'Date:', date.toDateString());
pagesWithDates.push({
title: pageData.title,
date: date,
timestamp: timestampMs
});
// Track most recent
if (timestampMs > mostRecentTimestamp) {
mostRecentTimestamp = timestampMs;
mostRecentDate = date;
}
hasDate = true;
}
}
// If no date found, add page without date
if (!hasDate) {
console.log('No date found for:', page.title);
pagesWithDates.push({
title: pageDatapage.title,
date: datenull,
timestamp: timestampMs0
});
}
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 (!a.timestamp) return 1;
if (!b.timestamp) return -1;
return b.timestamp - a.timestamp;
});
// TrackCheck if most recent is within 3 months
ifvar (timestampMsthreeMonthsAgo >= mostRecentTimestamp)new {Date();
threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
mostRecentTimestamp = timestampMs;
var isRecent = mostRecentDate && mostRecentDate => datethreeMonthsAgo;
}
console.log('Most recent date:', mostRecentDate ? mostRecentDate.toDateString() : 'none', 'Is recent:', isRecent);
displayAfDResults(pagesWithDates, articleTitle, mostRecentDate, isRecent);
}
}).catch(function(error) {
console.log('API error for', page.title, ':', error);
// Add page without date on error
pagesWithDates.push({
title: page.title,
date: null,
timestamp: 0
});
completedRequests++;
// When all requests are complete (including errors), display results
if (completedRequests === pages.length) {
displayAfDResults(pagesWithDates, articleTitle, mostRecentDate, false);
}
});
// Sort by date (most recent first)
pagesWithDates.sort(function(a, b) { 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;
displayAfDResults(pagesWithDates, articleTitle, mostRecentDate, isRecent);
}).fail(function() {
// If date fetching fails, display without dates
displayAfDResults(pages, articleTitle);
});
}