User:Xover/change-status.js
Appearance
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
- Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
For details and instructions about other browsers, see Wikipedia:Bypass your cache.
Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the central discussion page, Scriptorium. The code will be executed when previewing this page under some skins, including Monobook. You can in the interim if you wish to refresh the content sooner under another skin. |
Documentation for this user script can be added at User:Xover/change-status. |
// change-status.js
//
// Lightly modified version of [[User:DannyS712/Change status.js]]
//
$(function (){
var CS_config = {
name: '[[User:DannyS712/Change status|Change status.js]]',
version: 2.0,
debug: true
};
var CS_summary = 'Change status with ' + CS_config.name + ' (version ' + CS_config.version + ')';
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
mw.loader.using('mediawiki.util', function () {
$(document).ready(function () {
if (mw.config.get('wgNamespaceNumber') === 104) {
var statusPortlet = mw.util.addPortletLink(
'p-cactions', '#', 'Change status',
'ca-change-status', 'Change status'
);
$(statusPortlet).on('click', function(e) {
e.preventDefault();
change_status();
});
var $validateLink = $('<a id="wsg-change-status" href="#">[validate]</span>')
.css("float", "right");
$validateLink.on("click", function (e) {
e.preventDefault();
change_status();
});
$(".prp-page-qualityheader.quality3").append($validateLink);
var $proofreadLink = $('<a id="wsg-change-status" href="#">[proofread]</span>')
.css("float", "right");
$proofreadLink.on("click", function (e) {
e.preventDefault();
change_status();
});
$(".prp-page-qualityheader.quality1, .prp-page-qualityheader.quality2")
.append($proofreadLink);
}
} );
} );
function change_status(){
var name = mw.config.get( 'wgPageName' );
var page = get_page ( name );
var this_user = mw.config.get( 'wgUserName' );
if (CS_config.debug) console.log ( page );
var status = page.replace(/(?:.|\n)*(<pagequality.*? \/>)(?:.|\n)*/, '$1');
var status_user = status.replace(/.*?user="(.*?)" \/>/, '$1');
var status_level = status.replace(/.*?level="(\d+)" user=".*\/>/, '$1');
console.log( status, status_user, status_level, this_user );
var new_status, new_page;
if (status_level === "1" || status_level === "2"){
console.log('Status is 1 or 2');
new_status = '<pagequality level="3" user="' + this_user + '" />';
new_page = page.replace(/<pagequality.*? \/>/, new_status);
CS_summary = 'Mark as proofread with ' + CS_config.name + ' (version ' + CS_config.version + ')';
} else if (status_level === "3" && status_user != this_user){
console.log('Status is 3, can be validated');
new_status = '<pagequality level="4" user="' + this_user + '" />';
new_page = page.replace(/<pagequality.*? \/>/, new_status);
CS_summary = 'Mark as validated with ' + CS_config.name + ' (version ' + CS_config.version + ')';
}
if (CS_config.debug) console.log ( new_status, new_page );
if (page === new_page) return;
set_new ( name, new_page );
}
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 (CS_config.debug) console.log( page );
result = page.query.pages["0"].revisions["0"].content;
if (CS_config.debug) console.log( result );
}
});
return result;
}
function set_new ( page, new_content ){
console.log( page, new_content );
var to_send = {
action: 'edit',
title: page,
text: new_content,
summary: CS_summary,
token: mw.user.tokens.get( 'csrfToken' )
};
console.log( to_send );
$.when(
$.post( scriptUrl, to_send, function( response ){ } )
).done( function() {
$(".prp-page-qualityheader.quality3")
.html("This page has been validated.")
.addClass("quality4")
.removeClass("quality3");
$(".prp-page-qualityheader.quality1, .prp-page-qualityheader.quality2")
.html("This page has been proofread, but needs to be validated.")
.addClass("quality3")
.removeClass("quality1 quality2");
} );
}
});