User:DannyS712/Tag.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:DannyS712/Tag. |
/* _____________________________________________________________________________
* | |
* | === 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>