Jump to content

User:Zackmann08/scripts/updateUnknowns.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.
async function updateJSONs(query, page){
	mw.notify('Getting JSON');
	var htmlText;
	try {
		var response = await fetch(query);
    
		if (!response.ok) {
			throw new Error(`HTTP error! status: ${response.status}`);
		}
    
		// Get the response body as plain text (HTML string)
		htmlText = await response.text();
    
	} catch (error) {
		console.error('Fetch error:', error);
		mw.notify('Something went wrong. Check log for errors.');
	}
	mw.loader.using("mediawiki.api", function () {
		var api = new mw.Api();
        api.get({
            prop: 'revisions',
            rvprop: 'content',
            rvlimit: 1,
            titles: 'User:Zackmann08/' + page + '.json'
        }).done( function ( data ) {
            if ( !data.query || !data.query.pages ) return;
            var pageid = Object.getOwnPropertyNames( data.query.pages )[0],
                text = data.query.pages[pageid].revisions[0]["*"];
            new_text = htmlText;
            api.postWithEditToken( {
                action: "edit",
                title: 'User:Zackmann08/' + page + '.json',
                summary: 'updating JSON usinng [[User:Zackmann08/scripts/updateUnknowns.js]]',
                text: new_text
            }).done ( function ( data ) {
                if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
                    mw.notify( "JSON updated for "+ page, {type: 'success'});
                }
            });
        });
	});
}

async function updateUnknown(){
	var unknown_query = 'https://petscan.wmcloud.org/?project=wikipedia&common_wiki=auto&namespace_conversion=keep&psid=39322871&cb_labels_yes_l=1&wikidata_item=no&show_redirects=both&sortby=title&cb_labels_any_l=1&interface_language=en&ns%5B14%5D=1&subpage_filter=either&edits%5Bbots%5D=both&show_disambiguation_pages=both&categories=Infoboxes+with+unknown+parameters&active_tab=tab_output&cb_labels_no_l=1&format=json&min_redlink_count=1&language=en&combination=subset';
	var conflicting_query = 'https://petscan.wmcloud.org/?project=wikipedia&common_wiki=auto&namespace_conversion=keep&psid=47180248&cb_labels_yes_l=1&wikidata_item=no&show_redirects=both&sortby=title&cb_labels_any_l=1&interface_language=en&ns%5B14%5D=1&subpage_filter=either&edits%5Bbots%5D=both&show_disambiguation_pages=both&categories=Infoboxes+with+conflicting+parameters&active_tab=tab_output&cb_labels_no_l=1&format=json&min_redlink_count=1&language=en&combination=subset'
	updateJSONs(unknown_query, 'unknowns');
	updateJSONs(conflicting_query, 'conflicting');

	// window.setTimeout( function () {document.location.reload( true ); }, 2000 );
}

jQuery(document).ready(function() {
	if (mw.config.get('wgPageName') == 'User:Zackmann08/conflicting' || mw.config.get('wgPageName') == 'User:Zackmann08/unknown' || mw.config.get('wgPageName') == 'User:Zackmann08/unknown2'){
		mw.util.addPortletLink(
			"p-cactions", 
			"javascript:updateUnknown()", 
			"Update JSON"
		);
    }
});