User:Technical 13/SandBox/Gadget-BugStatusUpdate.js/sandbox.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:Technical 13/SandBox/Gadget-BugStatusUpdate.js/sandbox. |
/*
* Bug Status Update Gadget
* Author: Donald Fortier (User:Technical_13)
* * Based on original code:
* ** Author: Rob Moen (robm)
* ** Source: [[mw:User:Robmoen/bugStatusUpdate.js]]
* Description:
* Finds and updates bug status templates on a page.
* Makes 1 JSONP request to Bugzilla JSON RPC api.
*/
(function($){
// // Create function to retrieve bug statuses
function bugStatus(){
$.ajax({
url: target,
dataType: 'jsonp',
data: getParams( ids ),
success: function ( data ) {
var color = {
"RESOLVED": "green",
"CRITICAL": "red"
},
statusProps = {
'font-weight': 'bold',
'font-size': '1.5em',
'text-transform': 'uppercase'
};
if ( data.result.bugs ) {
for( var b in data.result.bugs ) {
// //find the right bug to update
var $item = $( '.mw-trackedTemplate' ).find( 'a[title^="bugzilla:' + data.result.bugs[b].id + '"]' );
var title = $( '.trakbug-' + data.result.bugs[b].id );
if( title ) {
title.text( data.result.bugs[b].summary );
}
if( $item ) {
// // Find child, if it exists.
$status = $item
.parent()
.next( 'p' )
.children( 'span' );
// //create the status element if it does not exist
if( $status.length === 0 ){
$item
.parent()
.parent()
.append(
$( '<p />' ).append(
$( '<span />' ).css( statusProps )
.text( 'Status' )
)
);
}
// // Udpate the status element.
$item
.parent()
.next( 'p' )
.children( 'span' )
.css( 'color', color[data.result.bugs[b].status] || '#333333' )
.text( data.result.bugs[b].status );
$status = null;
}
}
}
error: function ( foo, data, error ) {
/* Using this string with 49741 being a ticket I can't see:
http://bugzilla.wikimedia.org/jsonrpc.cgi?method=Bug.get&id=158¶ms=[{%20%22ids%22:%20[35810,35909,300,48745,49741,54598],%22include_fields%22:[%22last_change_time%22,%22status%22,%20%22id%22,%20%22summary%22]}]
returns:
{"error":{"message":"You are not authorized to access bug #49741. To see this bug, you must first log in to an account with the appropriate permissions.","code":102},"id":"158","result":null}
*/
console.error( 'Foo: %o\nRaw data: %o\nError: %o', foo, data, error );
console.error( 'id array: %o', ids );
}
});
}
var ids = [], target = 'http://bugzilla.wikimedia.org/jsonrpc.cgi';
// // Ugly way to compose the request parameters. Though, bugzilla is happy with it.
var getParams = function ( ids ) {
return 'method=Bug.get&id=158¶ms=[{ "ids": [' + ids.join(',') + '],"include_fields":["last_change_time","status", "id", "summary"]}]';
};
// // Get the bug id numbers on the page
$( '.mw-trackedTemplate' ).each( function() {
var title = $( this ).find( 'a[title^="bugzilla:"]' ).attr( 'title' );
ids.push( title.split( ':' )[1] );
});
// // Do not query if no ids were found
if ( !ids.length ) {
return;
}
// Make jsonp request
bugStatus();
})(jQuery);