Jump to content

User:Ahecht/Scripts/searchandcheck.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(document).ready( function()
{
	mw.loader.using(["mediawiki.api"]).then(function ()
	{
		async function searchAndCheck() {
			let needle = prompt("Text to search for");
			if (!needle) return;
			console.log(`Starting scan for "${needle}"...`);
			
			const api = new mw.Api();
			const checkedRevisions = [];

			const checkboxes = document.querySelectorAll('input[type="checkbox"][name^="ids["]');
			const elementsToScan = [];

			checkboxes.forEach(cb => {
				const nameAttr = cb.name;
				const cleanIdStr = nameAttr.replace('ids[', '').replace(']', '');
				if (cleanIdStr && !isNaN(cleanIdStr)) {
					elementsToScan.push({ id: cleanIdStr, element: cb });
				}
			});

			const revIds = elementsToScan.map(item => item.id);

			if (revIds.length === 0) {
				console.log("No valid revision checkboxes found on this page.");
				return;
			}

			console.log(`Found ${revIds.length} revisions to inspect on the page.`);

			const batchSize = 50;

			for (let i = 0; i < revIds.length; i += batchSize) {
				const batch = revIds.slice(i, i + batchSize);
				
				try {
					const response = await api.get({
						action: 'query',
						prop: 'revisions',
						revids: batch.join('|'),
						rvprop: 'content|ids',
						rvslots: 'main',
						formatversion: 2
					});

					const pages = response?.query?.pages || [];
					for (const page of pages) {
						const revisions = page.revisions || [];
						for (const rev of revisions) {
							if (!rev) {
								continue;
							} else if (!rev.revid) {
								console.log("No revid found in");console.log(rev);
								continue;
							}

							const haystack = rev.slots?.main?.content || rev.content || '';
							const revIdStr = rev.revid.toString();

							if (haystack.includes(needle)) {
								checkedRevisions.push(revIdStr);
							}
						}
					}
				} catch (error) {
					console.error("Error fetching revision batch:", error);
				}
			}

			let matchCount = 0;
			elementsToScan.forEach(item => {
				if (checkedRevisions.includes(item.id)) {
					item.element.checked = true;
					matchCount++;
					item.element.dispatchEvent(new Event('change', { bubbles: true })); 
				}
			});

			alert(`Scan complete. Successfully checked ${matchCount} revision boxes matching "${needle}".`);
		}
		
		
		document.querySelector('.mw-history-revisionactions')?.insertAdjacentHTML('afterbegin', ' <button type="submit" name="title" id="searchAndCheck" value="Special:RevisionDelete" class="cdx-button" onclick="event.preventDefault();">Check revs based on text</button> ');
		document.getElementById('searchAndCheck').addEventListener('click', searchAndCheck);
	});
});