Jump to content

User:DannyS712/Tag.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.
/*  _____________________________________________________________________________
 * |                                                                             |
 * |                    === WARNING: GLOBAL GADGET FILE ===                      |
 * |                  Changes to this page affect many users.                    |
 * | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
 * |_____________________________________________________________________________|
 *
 * Easily tag a page with a specific template, and send a message to the page's creator
 * Revision: 2.1
 * Author: [[:w:en:User:DannyS712]]
 */
//<nowiki>
$(function (){

//Script-wide configuration information
var Tag_config = {
	name: '[[:w:en:User:DannyS712/Tag.js|Tag]]',
	version: 2.1,
	//Only change the debug statements, not the name or version
	debug: false,
	debug_all: false
};

//Site configuration. The first 4 parameters set up the option to activate the script.
//The last parameter (config_file) establishes the JSON page that contains the remaining configuration features
var Tag_settings = {
	portlet_location: 'p-cactions',
	portlet_text: 'A. Curto',
	portlet_id: 'ca-Tag',
	portlet_tooltip: 'A. Curto',
	config_file: 'MediaWiki:Gadget-Marcadorcurtos.json',
	help_page: 'Wikipedia:Marcador de artigos curtos'
};

//DO NOT CHANGE ANYTHING BELOW THIS LINE
var Tag_page = {};
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
var summary_post = ' - ' + Tag_config.name + ' (v. ' + Tag_config.version + ') ([[' + Tag_settings.help_page + ']])';

mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () { 
    	mw.util.addPortletLink ( Tag_settings.portlet_location, 'javascript:void(0)', Tag_settings.portlet_text, Tag_settings.portlet_id, Tag_settings.portlet_tooltip);
    	$('#' + Tag_settings.portlet_id).on('click', function() {
        	Tag();
    	} );
    } );
} );
function Tag(){
	var page = mw.config.get('wgPageName').replace(/_/g, ' ');
	set_up( page );
	var new_content = Tag_page.template + get_page( page );
	set_page( page, new_content, Tag_page.page_summary);
	addNewSectionTo( Tag_page.section_heading, Tag_page.user_talk_section, Tag_page.user_talk_summary, Tag_page.user_talk_page );
}
function set_up( page ){
	var config = get_JSON( Tag_settings.config_file );
	Tag_page.page = page;
	Tag_page.template = '{{' + config.page_template + '}}\n';
	Tag_page.page_summary = config.page_summary + summary_post;
	Tag_page.user_talk_page = config.user_talk_ns + ':' + get_creator ( page );
	Tag_page.user_talk_summary = config.user_talk_section_summary_part_1 + '[[' + page + ']]' + config.user_talk_section_summary_part_2 + summary_post;
	Tag_page.user_talk_section = config.user_talk_section_message_part_1 + '[[' + page + ']]' + config.user_talk_section_message_part_2;
	Tag_page.section_heading = config.user_talk_section_heading;
	if (Tag_page.section_heading == 'PAGE_NAME') Tag_page.section_heading = page;
	if (Tag_config.debug) console.log( Tag_page );
}
function get_JSON ( page ){
	var JSONed = JSON.parse( get_page( page ) );
	if (Tag_config.debug_all) console.log( JSONed );
	return JSONed;
}
function get_page( name ){
    var page_to_get = {
        action: 'query',
        titles: name,
        prop: 'revisions',
        rvprop: 'content',
        format: 'json',
        formatversion: 2
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: page_to_get,
		dataType: 'json',
		async: false,
		success: function(page) {
			if (Tag_config.debug_all) console.log( page );
	    	result = page.query.pages["0"].revisions["0"].content;
	    	if (Tag_config.debug_all) console.log( result );
		} 
	});
	return result;
}
function set_page ( page, new_content, pg_summary ){
	if (Tag_config.debug_all) console.log( page, new_content );
    var to_send = {
        action: 'edit',
        title: page,
        text: new_content,
        summary: pg_summary,
        token: mw.user.tokens.get( 'csrfToken' )
    };
    if (Tag_config.debug) console.log( to_send );
    $.when(
        $.post( scriptUrl, to_send, function( response ){ } )
    ).done( function() { location.reload(); } );
}
function addNewSectionTo( section_title, content, summary, target) {
    $.ajax({
        url: scriptUrl,
        data: {
            format: 'json',
            action: 'edit',
            title: target,
            section: 'new',
            sectiontitle: section_title,
            summary: summary,
            text: content,
            token: mw.user.tokens.get( 'csrfToken' )
        },
        dataType: 'json',
        type: 'POST',
        success: function( data ) {
            if		( data && data.edit && data.edit.result == 'Success' ) { if (Tag_config.debug) console.log ('Section added'); }
            else if ( data && data.error )	{ alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info ); }
            else	{ alert( 'Error: Unknown result from API.' ); }
        },
        error: function( xhr ) { alert( 'Error: Request failed.' ); }
    });
}
function get_creator ( page ){
	var get_req = {
        action: 'query',
        titles: page,
        prop: 'revisions',
        rvprop: 'user',
        rvdir: 'newer',
        rvlimit: 1,
        format: 'json',
        formatversion: 2
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: get_req,
		dataType: 'json',
		async: false,
		success: function(data) {
			if (Tag_config.debug_all) console.log ( data );
			result = data.query.pages[0].revisions[0].user;
			if (Tag_config.debug) console.log(result);
		} 
	});
	return result;
}
});
//</nowiki>