User:Qwerfjkl/scripts/shortdescs-in-category.js

// Fork of User:1234qwer1234qwer4/shortdescs-in-category.js

/** * Shows button on category pages to see short description of all pages in the category. * * Look for a button saying "Show SDs" towards the right, above the list of pages. * * Forking from User:SD0001/shortdescs-in-category.js to allow importing Wikidata descriptions. * Uses code from User:Qwerfjkl/scripts/unusedCategories.js * * Do not use on redirects (yet) * *  */

$.ready.then(function {

if (mw.config.get('wgNamespaceNumber') !== 14) { return; }	$('#mw-pages > p').append(		$(' ').text('Show SDs').attr('id', 'showSD-button').addClass('mw-ui-button').css('float', 'right').click(showsSDs)	);

if (location.href.indexOf('&showSD=y') !== -1) { showsSDs; }

});

function fixLinks(showingSDs) { if (showingSDs) { $('#mw-pages > a').each(function {			if (this.href.indexOf('#') !== -1) {				this.href = this.href.replace(/#/, '&showSD=y#');			} else {				this.href += '&showSD=y';			}		}); } else { $('#mw-pages > a').each(function {			this.href = this.href.replace(/&showSD=y/g, '');		}); } }

function hideSDs(e) { if (e) e.preventDefault; fixLinks(false); $('#showSD-button').text('Show SDs').off('click').on('click', showsSDs); $('.cat-shortdesc-none, .cat-shortdesc-local, .cat-shortdesc-wikidata, .cat-shortdesc-wikidata-import').remove; }

function showsSDs(e) { if (e) e.preventDefault;

fixLinks(true); $('#showSD-button').text('Hide SDs').off('click').on('click', hideSDs); mw.util.addCSS('.cat-shortdesc-none { color: red } .cat-shortdesc-wikidata { color: MediumVioletRed } .cat-shortdesc-wikidata-import {font-size:x-small; display:inline-block}');

var titles = $('#mw-pages li').get.map(function(e) {		var a = e.querySelector('a');		return mw.Title.newFromText(a.textContent).getSubjectPage.toText;	});

var titleSets = arrayChunk(titles, 50); var api = new mw.Api; var wdApi = new mw.ForeignApi('https://www.wikidata.org/w/api.php', { anonymous: true });

var promiseArr = titleSets.map(function(set) {		return api.get({ "action": "query", "format": "json", "prop": "description", "titles": set, "formatversion": "2", });	});	var promiseArrWd = titleSets.map(function(set) {		return wdApi.get({ "action": "wbgetentities", "sites": "enwiki", "titles": set, "props": "descriptions|sitelinks", "languages": "en", "sitefilter": "enwiki" });	});

$.when.apply($, promiseArr.concat(promiseArrWd)).then(function {		var descs = {};		Array.prototype.slice.call(arguments).forEach(function(response) { var json = response[0]; if (json.query) { // enwiki API output json.query.pages.forEach(function(pg) {					descs[pg.title] = {						value: pg.description					};				}); } else if (json.entities) { // wikidata API output $.each(json.entities, function(id, data) {					var pagename = data.sitelinks && data.sitelinks.enwiki && data.sitelinks.enwiki.title;					if (pagename) {						var entry = descs[pagename];						if (entry && !entry.value) {							entry.value = data.descriptions && data.descriptions.en && data.descriptions.en.value;							entry.wd = id;						} 					}				}); }		});			attachDescriptions(descs);	}); }

function attachDescriptions(descs) { var counter = 0; $('#mw-pages li').get.forEach(function(e) {		var a = e.querySelector('a');		var title = mw.Title.newFromText(a.textContent).getSubjectPage.toText;		if (!descs[title] || !descs[title].value) {			$(e).append( $(' ').text(' – no shortdesc') .addClass('cat-shortdesc-none') ).append( $('').text(' – Add') .addClass('cat-shortdesc-wikidata-add') .attr({title: "Add short description.", noPopup: 1, id: counter + "Add"}) );

} else if (!descs[title].wd) { $(e).append(				$(' ').text(' – ' + descs[title].value)					.addClass('cat-shortdesc-local')			); } else { var wd = descs[title].wd; $(e).append(				$(' ').text(' – ' + descs[title].value)					.addClass('cat-shortdesc-wikidata')			).append(				$('').text(' – Import')					.addClass('cat-shortdesc-wikidata-import')					.attr({title: "Import Wikidata description.", noPopup: 1, id: wd + "Import"})			); $("#" + wd + "Import").click({"page":title,"desc":descs[title].value}, importSubmitS); $("#" + counter + "Add").click({"page":title,"desc":''}, importSubmitS); counter += 1; console.log("Title: " + title); }	}); }

function importSubmitS(event){ var page = event.data.page; var desc = event.data.desc; checkPageWikitextS(page,desc); }

function checkPageWikitextS(page,desc) { var send_req = { action: 'expandtemplates', text: "", titles: page, prop: 'wikitext', };	$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', send_req, function( response ) {		window.textToCheckForCategories = $( response ).find( '*' ).text;		importSubmitS2(page,desc);	}); }

function importSubmitS2(page,desc) { console.log("Title: " + page);

if (window.textToCheckForCategories.match(/Category:articles with short description/) != null) { // There's a match mw.notify( page+" contains a short description, aborted."); return; }	desc1=desc.charAt(0).toUpperCase+desc.slice(1); desc=prompt("Change short description to:", desc1); if (!desc) { mw.notify("Aborted."); return; }	var requestDataS = {		minor: false, prependtext: "\n", summary: (desc===desc1?"Importing Wikidata":"Adding")+" short description: "+desc+" (shortdescs-in-category)" };	new mw.Api.edit(page, function {		return requestDataS;	}).done(function(data) {			if (data && data.result && data.result === 'Success')			{				mw.notify("Wikidata description imported for "+page);			}			else			{				alert('There was an error tagging "'+page+'". Code: ' + data);			}	}).fail(function(data) {			alert('There was an error using AJAX to tag "'+page+'". Error: ' + data);	}); }

function arrayChunk(arr, size) { var numChunks = Math.ceil(arr.length / size); var result = new Array(numChunks); for (var i = 0; i < numChunks; i++) { result[i] = arr.slice(i * size, (i + 1) * size); }	return result; }

//