User:So9q/duplicate item.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/*
This script is derived from User:DarwIn/duplicate_item.js
It has been improved with a cancel dialog as requested by Morbeus.
--
This script is derived from 'User:Magnus_Manske/duplicate_item.js' - it can duplicate the current item, minus sitelinks and descriptions (not allowed by Wikidata).
To use, add
	importScript( 'User:DarwIn/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 () {
		if(!window.confirm("Duplicate?")){
			return;
		}
		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 descriptions and aliases? You will need to fix them in all languages!") ) {
					data.descriptions = e.descriptions || {} ;
				}
				$.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 : ''
} ;

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() ;
} ) ;