Jump to content

User:GeneralNotability/arcwordcount.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
// <nowiki>

const awc_SKIP_SECTION_RE = /(:?Involved parties|.*: Clerk notes|.*: Arbitrators' opinion.*|{Non-party})/i;
const awc_DIFF_RE = /(:?\[\[(:?Special:Diff|Special:Permalink).*?\]\]|oldid=)/ig;

async function awc_getSectionWordCount(sectionId) {
	'use strict';
	const api = new mw.Api();

	const getRequest = {
		action: 'query',
		prop: 'revisions',
		rvprop: 'content',
		rvslots: 'main',
		indexpageids: true,
		titles: mw.config.get( "wgPageName" ),
		rvsection: sectionId
	};

	let sectionText = '';
	try {
		const response = await api.get(getRequest);
		const pageid = response.query.pageids[0];
		sectionText = response.query.pages[pageid].revisions[0].slots.main['*'];
	} catch (error) {
		console.error('Error fetching text for section ' + sectionId + ': ' + error);
		return 0;
	}

	sectionText = sectionText.replace(/={3,}.*={3,}\n/, '');
	const splitSectionText = sectionText.split(/\s+/);
	return splitSectionText.length;
}

async function awc_getSectionDiffCount(sectionId) {
	'use strict';
	const api = new mw.Api();

	const getRequest = {
		action: 'query',
		prop: 'revisions',
		rvprop: 'content',
		rvslots: 'main',
		indexpageids: true,
		titles: mw.config.get( "wgPageName" ),
		rvsection: sectionId
	};

	let sectionText = '';
	try {
		const response = await api.get(getRequest);
		const pageid = response.query.pageids[0];
		sectionText = response.query.pages[pageid].revisions[0].slots.main['*'];
	} catch (error) {
		console.error('Error fetching text for section ' + sectionId + ': ' + error);
		return 0;
	}

	return (sectionText.match(awc_DIFF_RE) || []).length;
}


$(async function () {
	if( mw.config.get( "wgPageName" ) === "Wikipedia:Arbitration/Requests/Case" ) {
		const api = new mw.Api();
		const response = await api.get({
			action: 'parse',
			prop: 'sections',
			page: mw.config.get( "wgPageName" )
		});
		response.parse.sections.forEach(async(section) => {
			if (section.level === '3') {
				if (section.line.match(awc_SKIP_SECTION_RE)) {
					// Don't care about these administrative sections
					return;
				}
				const sectionLength = await awc_getSectionWordCount(section.index);
				const diffCount = await awc_getSectionDiffCount(section.index);
				const selector = 'span#' + $.escapeSelector(section.anchor) + '.mw-headline';
				$element = $(selector, document)[0];
				$element.after(' (' + sectionLength + ' words, ' + diffCount + ' diffs)');
			}
		});
	} else if( mw.config.get( "wgPageName" ) === "Wikipedia:Arbitration/Requests/Enforcement") {
		const api = new mw.Api();
		const response = await api.get({
			action: 'parse',
			prop: 'sections',
			page: mw.config.get( "wgPageName" )
		});
		response.parse.sections.forEach(async(section) => {
			if (section.level === '4') {
				if (section.line.match(awc_SKIP_SECTION_RE)) {
					// Don't care about these administrative sections
					return;
				}
				const sectionLength = await awc_getSectionWordCount(section.index);
				const diffCount = await awc_getSectionDiffCount(section.index);
				const selector = 'span#' + $.escapeSelector(section.anchor) + '.mw-headline';
				$element = $(selector, document)[0];
				$element.after(' (' + sectionLength + ' words, ' + diffCount + ' diffs)');
			}
		});
	} else if (mw.config.get("wgPageName").match(/Wikipedia:Arbitration\/Requests\/Case\/.*?\/Evidence/)) {
		const api = new mw.Api();
		const response = await api.get({
			action: 'parse',
			prop: 'sections',
			page: mw.config.get( "wgPageName" )
		});
		const caseSections = [];
		response.parse.sections.forEach(async(section) => {
			if (section.level === '2' && section.line.match(/Evidence presented by .*/i)) {
				const sectionLength = await awc_getSectionWordCount(section.index);
				const diffCount = await awc_getSectionDiffCount(section.index);
				const selector = 'span#' + $.escapeSelector(section.anchor) + '.mw-headline';
				$element = $(selector, document)[0];
				$element.after(' (' + sectionLength + ' words, ' + diffCount + ' diffs)');
			}
		});

	}
});
// </nowiki>