User:Kllhwang/duplicate-item.js: Difference between revisions

From Semantic Lab
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
/*
This Userscript was copied from Magnus Manske's script on Wikidata.
This script can duplicate the current item, minus sitelinks and descriptions (not allowed by Wikidata).
To use, add
importScript( 'User:Magnus_Manske/duplicate_item.js' );
to your common.js user subpage. This will add a new link "Duplicate this item" to your toolbox sidebar.
Clicking will duplicate the item, and open it in a new tab/window, or alert you to an error.
In case a browser plug-in like NoScript prevents the opening of the new item, check your "Contributions"; the new item will be on top.
*/
var wd_duplicate_item = {
api : '/w/api.php' ,
init : function () {
var self = this ;
var portletLink = mw.util.addPortletLink( 'p-tb', '#', 'Duplicate this item','t-wd_duplicate_item');
$(portletLink).click ( function () {
self.run() ;
return false ;
} ) ;
} ,
run : function () {
var self = this ;
var q = mw.config.get('wgTitle') ;
$.get ( self.api , {
action:'wbgetentities',
ids:q,
format:'json'
} , function ( d ) {
var e = d.entities[q] ;
var data = {
// descriptions : e.descriptions || {} ,
// labels : e.labels || {} ,
// aliases : e.aliases || {} ,
claims : e.claims || {}
} ;
if ( window.confirm("Also duplicate all labels and aliases? You will need to fix them in all languages!") ) {
data.labels = e.labels || {} ;
data.aliases = e.aliases || {} ;
}
$.each ( data.claims , function ( p , v ) {
$.each ( v , function ( dummy , c ) {
delete c.id ;
} ) ;
} ) ;
self.createNewItem ( q , data ) ;
} ,'json' ) ;
} ,
getEditToken : function ( callback ) {
var self = this ;
$.post ( self.api , {
action:'query',
meta:'tokens',
format : 'json'
} , function ( d ) {
var token = d.query.tokens.csrftoken ;
if ( typeof token == 'undefined' ) {
alert ( "Problem getting edit token") ;
return ;
}
callback ( token ) ;
} ) ;
} ,
createNewItem : function ( q , data ) {
var self = this ;
self.getEditToken ( function ( token ) {
$.post ( self.api , {
action:'wbeditentity',
'new':'item',
data:JSON.stringify(data),
token:token,
summary:'Item duplicated from '+q,
format:'json'
} , function ( d ) {
if ( d.success == 1 ) {
var nq = d.entity.id
var url = "/wiki/" + nq ;
window.open(url , '_blank');
} else {
console.log ( d ) ;
alert ( "A problem occurred, check JavaScript console for errors" ) ;
}
} , 'json' ) ;
} ) ;
} ,
dummy : ''
} ;
mw.loader.using( 'mediawiki.util', function () {
mw.loader.using( 'mediawiki.util', function () {
// Wait for the page to be parsed
  jQuery(document).ready ( function() {
    $( document ).ready( function () {  
if ( mw.config.get('wgNamespaceNumber') != 0 ) return ;
        // See the "Portlets (menus and tabs)" subsection below
if ( mw.config.get('wgAction') != 'view' ) return ;
        var link = mw.util.addPortletLink( 'p-tb', '#', 'Duplicate this item','t-wd_duplicate_item');  
if ( mw.config.get('wbIsEditView') == false ) return ;
        $( link ).click( function ( event ) {
if ( mw.config.get('wgIsRedirect') == true ) return ;
            event.preventDefault();
            doQwikify();
  wd_duplicate_item.init() ;
        } );
} ) ;
    } );
} );

Latest revision as of 22:03, 14 November 2024

/*
This Userscript was copied from Magnus Manske's script on Wikidata.

This script can duplicate the current item, minus sitelinks and descriptions (not allowed by Wikidata).
To use, add
	importScript( 'User:Magnus_Manske/duplicate_item.js' );
to your common.js user subpage. This will add a new link "Duplicate this item" to your toolbox sidebar.
Clicking will duplicate the item, and open it in a new tab/window, or alert you to an error.
In case a browser plug-in like NoScript prevents the opening of the new item, check your "Contributions"; the new item will be on top.
*/

var wd_duplicate_item = {
	api : '/w/api.php' ,
	
	init : function () {
		var self = this ;
		var portletLink = mw.util.addPortletLink( 'p-tb', '#', 'Duplicate this item','t-wd_duplicate_item');
		$(portletLink).click ( function () {
			self.run() ;
			return false ;
		} ) ;
	} ,
	
	run : function () {
		var self = this ;
		var q = mw.config.get('wgTitle') ;
		$.get ( self.api , {
			action:'wbgetentities',
			ids:q,
			format:'json'
		} , function ( d ) {
				var e = d.entities[q] ;
				var data = {
//					descriptions : e.descriptions || {} ,
//					labels : e.labels || {} ,
//					aliases : e.aliases || {} ,
					claims : e.claims || {} 
				} ;
				if ( window.confirm("Also duplicate all labels and aliases? You will need to fix them in all languages!") ) {
					data.labels = e.labels || {} ;
					data.aliases = e.aliases || {} ;
				}
				$.each ( data.claims , function ( p , v ) {
					$.each ( v , function ( dummy , c ) {
						delete c.id ;
					} ) ;
				} ) ;
				self.createNewItem ( q , data ) ;
		} ,'json' ) ;
	} ,
	
	getEditToken : function ( callback ) {
		var self = this ;
		$.post ( self.api , {
			action:'query',
			meta:'tokens',
			format : 'json'
		} , function ( d ) {
			var token = d.query.tokens.csrftoken ;
			if ( typeof token == 'undefined' ) {
				alert ( "Problem getting edit token") ;
				return ;
			}
			callback ( token ) ;
		} ) ;
	} ,
	
	createNewItem : function ( q , data ) {
		var self = this ;
		self.getEditToken ( function ( token ) {
			$.post ( self.api , {
				action:'wbeditentity',
				'new':'item',
				data:JSON.stringify(data),
				token:token,
				summary:'Item duplicated from '+q,
				format:'json'
			} , function ( d ) {
				if ( d.success == 1 ) {
					var nq = d.entity.id
					var url = "/wiki/" + nq ;
					window.open(url , '_blank');
				} else {
					console.log ( d ) ;
					alert ( "A problem occurred, check JavaScript console for errors" ) ;
				}
			} , 'json' ) ;
		} ) ;
	} ,
	
	dummy : ''
} ;



mw.loader.using( 'mediawiki.util', function () {
  jQuery(document).ready ( function() {
	if ( mw.config.get('wgNamespaceNumber') != 0 ) return ;
	if ( mw.config.get('wgAction') != 'view' ) return ;
	if ( mw.config.get('wbIsEditView') == false ) return ;
	if ( mw.config.get('wgIsRedirect') == true ) return ;
	
  wd_duplicate_item.init() ;
} ) ;