Content deleted Content added
fetch user talk pages without using usercontribs API |
improve user talk page fetching by using Special:PrefixIndex |
||
(3 intermediate revisions by the same user not shown) | |||
Line 33:
version: this.version
});
}
enumerate() {
if (this.version != 4) {
throw new Error('can only enumerate IPv4 addresses');
}
const count = 1n << BigInt(32 - this.mask);
let current = this.masked(this.mask).ip;
return Array.from({ length: Number(count) }, () =>
IPAddress.#bigIntToIPv4(current++)
);
}
Line 46 ⟶ 57:
}
return uppercase ? ipString.toUpperCase() : ipString;
}
getRange() {
const size = this.version === 4 ? 32 : 128;
const effectiveMask = this.effectiveMask;
const hostBits = BigInt(size - effectiveMask);
const start = this.ip & (~0n << hostBits);
const end = start | ((1n << hostBits) - 1n);
return {
start: new IPAddress({ ip: start, mask: null, version: this.version }),
end: new IPAddress({ ip: end, mask: null, version: this.version })
};
}
inRange(other) {
if (!(other instanceof IPAddress)) return false;
if (this.version !== other.version) return false;
const { start, end } = this.getRange();
return other.ip >= start.ip && other.ip <= end.ip;
}
Line 241 ⟶ 271:
const heading = document.querySelector('#firstHeading');
if (heading) {
heading.
}
const contentContainer = document.querySelector('#mw-content-text');
Line 284 ⟶ 314:
// display talk pages for IP range
async function displayRangeTalk(ip) {
async function getUserTalkPages(ip, maxPages = 32) {
const userTalk = new Set();
const
const prefix = commonPrefix(start.toString(true), end.toString(true));
const validPrefix = /^\w+[.:]\w+[.:]/.test(prefix);
let pagesFetched = 0;
let errors = false;
url = `/wiki/Special:PrefixIndex?prefix=${encodeURIComponent(prefix)}&namespace=3`;
}
while (url && pagesFetched < maxPages && !errors) {
try {
const html = await fetch(url).then(res => res.text());
const parser = new DOMParser();
const fetched = parser.parseFromString(html, 'text/html');
const links = fetched.querySelectorAll('ul.mw-prefixindex-list > li > a');
for (const link of links) {
const ipText = link.textContent;
const pageIp = IPAddress.from(ipText);
if (pageIp && ip.inRange(pageIp)) {
userTalk.add(`User talk:${ipText}`);
}
}
const nextLink = fetched.querySelector('.mw-prefixindex-nav a');
if (nextLink && nextLink.textContent.includes('Next page') && nextLink.href) {
url = nextLink.href;
} else {
url = null;
}
} catch (error) {
console.error('Error fetching usertalk pages:', error);
errors = true;
break;
}
pagesFetched++;
}
if (!validPrefix || errors || pagesFetched === maxPages) {
url = `/wiki/Special:Contributions/${ip}?limit=500`;
try {
const html = await fetch(url).then(res => res.text());
const parser = new DOMParser();
const fetched = parser.parseFromString(html, 'text/html');
const talkLinks = fetched.querySelectorAll('.mw-contributions-list a.mw-usertoollinks-talk:not(.new)');
for (const link of talkLinks) {
const title = link.title;
if (title) userTalk.add(title);
}
} catch (error) {
console.error('Error fetching usertalk pages:', error);
}
}
return Array.from(userTalk);
}
function timeAgo(timestamp) {
Line 356 ⟶ 422:
blockUser.href = `/wiki/Special:Block/${ip}`;
}
let userTalkMethod;
if (ip.version === 4 && ip.mask >= 24) {
userTalk = ip.enumerate().map(ipString => `User talk:${ipString}`);
userTalkMethod = "enumerate";
} else {
userTalk = await getUserTalkPages(ip);
userTalkMethod = "contributions";
if (!userTalk.length) {
const resultMessage = document.createElement('p');
resultMessage.style.color = 'var(--color-notice, gray)';
resultMessage.textContent = 'No user talk pages found for recent contributions from this IP range.';
contentContainer.appendChild(resultMessage);
return;
}
}
const
batch(userTalk, 50).map(titles => api.get({
action: 'query',
format: 'json',
formatversion: 2
}))
);
const pages = infoResponses
.flatMap(response => response.query.pages)
.filter(page => !page.missing && page.revisions && page.revisions.length > 0)
.map(page => ({
title: page.title,
redirect: !!page.redirect
}))
.sort((a, b) => b.timestamp.localeCompare(a.timestamp));
if (!pages.length) {
const
if (userTalkMethod === "enumerate") {
resultMessage.style.color = 'var(--color-notice, gray)';
resultMessage.textContent = 'No user talk pages found.';
} else {
resultMessage.style.color = 'var(--color-error, red)';
resultMessage.textContent = 'An error occurred while retrieving timestamps for user talk pages in this IP range.';
}
contentContainer.appendChild(resultMessage);
return;
}
Line 386 ⟶ 471:
for (const page of pages) {
const ip = page.title.replace(/^User talk:/, '');
const relativeTime = `${timeAgo(page.
const headerText = `== ${relativeTime}: [[Special:Contributions/${ip}|${ip}]] ([[${page.title}|talk]]) ==`;
const inclusionText = `{{${page.title}}}`;
Line 428 ⟶ 513:
const heading = document.querySelector('#firstHeading');
if (heading) {
heading.
}
const contentContainer = document.querySelector('#mw-content-text');
Line 640 ⟶ 725:
}
return wikitext;
}
function batch(items, maxSize) {
const minBins = Math.ceil(items.length / maxSize);
const bins = Array.from({length: minBins}, () => []);
items.forEach((item, i) => {
bins[i % minBins].push(item);
});
return bins;
}
Line 655 ⟶ 749:
const index = ipList.findIndex(i => i.equals(ip));
if (index !== -1) ipList.splice(index, 1);
}
function commonPrefix(a, b) {
let i = 0;
while (i < a.length && i < b.length && a[i] === b[i]) {
i++;
}
return a.slice(0, i);
}
});
|