User:Ricordisamoa/Archive.js

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* <nowiki>
 * Archive.js
 * @author [[User:Ricordisamoa]]
 * Allows to 'archive' entities' content as user subpages
*/
$( document ).ready( function () {
	if ( mw.config.get( 'wgAction' ) !== 'view' || !mw.config.exists( 'wbEntityId' ) ) {
		return;// an entity, in view mode
	}
	var messages = {
		'en': {
			'p-label': 'Archive',
			'p-title': 'Archive this entity\'s content in a subpage of yours for further retrieval',
			'success': 'The item has been archived successfully!'
		},
		'it': {
			'p-label': 'Archivia',
			'p-title': 'Archivia il contenuto di questa entità in una tua sottopagina per recuperarlo in un secondo momento',
			'success': 'L\'elemento è stato archiviato con successo!'
		}
	};
	messages = messages[mw.config.get( 'wgUserLanguage' )] || messages.en;
	$( mw.util.addPortletLink( 'p-cactions', '#', messages['p-label'], 'entity-archive', messages['p-title'] ) )
	.click( function ( event ) {
		event.preventDefault();
		mw.hook( 'wikibase.entityPage.entityLoaded' ).add( function ( entity ) {
			new mw.Api().postWithToken( 'edit', {
				action: 'edit',
				text: JSON.stringify( entity ),
				title: mw.config.get( 'wgFormattedNamespaces' )[2] + ':' +
					mw.config.get( 'wgUserName' ) + '/archive/' +
					mw.config.get( 'wgPageName' ),
				summary: '[[User:Ricordisamoa/Archive.js|archiving]] an entity: [[' +
					mw.config.get( 'wgPageName' ) + ']]'
			} )
			.done( function ( data ) {
				mw.notify( messages.success );
			} )
			.fail( function ( error ) {
				mw.notify( error );
			} );
		} );
	} );
} );