Difference between revisions of "User:Genferei/moveClaim.js"

From Wikibase Personal data
Jump to navigation Jump to search
(Blanked the page)
Tag: Blanking
 
Line 1: Line 1:
( function ( mw, wb, $ ) {
 
  
if ( [ 0, 120, 146 ].indexOf( mw.config.get( 'wgNamespaceNumber' ) ) === -1 ||
 
!mw.config.exists( 'wbEntityId' ) ||
 
!mw.config.get( 'wbIsEditView' ) ||
 
!mw.config.get( 'wgIsProbablyEditable' )
 
) {
 
return;
 
}
 
 
switch ( mw.config.get( 'wgUserLanguage' ) ) {
 
default:
 
case 'en':
 
mw.messages.set( {
 
'close': 'Close',
 
'copy-claim': 'Copy claim',
 
'move-claim': 'Move claim',
 
'newentity': 'The id of the new entity:',
 
'new-magic-word': 'NEW',
 
'move-claim-intro': 'You can move a claim to another entity. Please give a valid entity id.',
 
'move-claim-intro-hint': 'If you write "$1", you will copy/move the claim to a new item.',
 
'successfully-copied': 'The claim $1 was successfully copied to $2.',
 
'successfully-moved': 'The claim $1 was successfully moved to $2.',
 
'error-sameid': 'The new entity\'s id has to be different to the current one\'s.',
 
'error-invalidid': 'The given id is not a valid entity identifier.',
 
'error-notexisting': 'The entity with the id $1 does not exist.',
 
'error-overwrite': 'The link for the given site in $1 is already set. Please check the entity\'s id or remove the link from the entity.',
 
'error-api': 'There was an error editing the entity: $1'
 
} );
 
break;
 
case 'el':
 
mw.messages.set( {
 
'close': 'Κλείσιμο',
 
'copy-claim': 'Αντιγραφή ισχυρισμού',
 
'move-claim': 'Μετακίνηση ισχυρισμού',
 
'newentity': 'Το αναγνωριστικό (id) της νέας οντότητας:',
 
'new-magic-word': 'ΝΕΟ',
 
'move-claim-intro': 'Μπορείτε να μετακινήσετε ένα ισχυρισμό σε μια άλλη οντότητα. Παρακαλώ δώστε έγκυρο αναγνωριστικό (id) οντότητας.',
 
'move-claim-intro-hint': 'Αν γράψετε "$1", θα αντιγράψει/μετακινήσει τον ισχυρισμό σε ένα νέο αντικείμενο.',
 
'successfully-copied': 'Ο ισχυρισμός $1 αντιγράφτηκε στο αντικείμενο $2.',
 
'successfully-moved': 'Ο ισχυρισμός $1 μετακινήθηκε στο αντικείμενο $2.',
 
'error-sameid': 'Το/τα αναγνωριστικό/ά (id) της νέας οντότητα/των νέων οντοτήτων πρέπει να είναι διαφορετικό/ά από το/τα τρέχον/τρέχοντα.',
 
'error-invalidid': 'Το δοθέν αναγνωριστικό (id) δεν είναι ένα έγκυρο αναγνωριστικό οντότητας.',
 
'error-notexisting': 'Η οντότητα με αναγνωριστικό (id) $1 δεν υπάρχει.',
 
'error-overwrite': 'Ο σύνδεσμος προς τη δοθείσα ιστοσελίδα στο αντικείμενο $1 υπάρχει ήδη. Παρακαλώ ελέγξτε το/τα αναγνωριστικό/ά (id) της οντότητας/των οντοτήτων ή μετακινείστε το σύνδεσμο από την οντότητα.',
 
'error-api': 'Παρουσιάστηκε ένα σφάλμα στην επεξεργασία της οντότητας: $1'
 
} );
 
break;
 
case 'fr':
 
mw.messages.set( {
 
'close': 'Fermer',
 
'copy-claim': 'Copier l’affirmation',
 
'move-claim': 'Déplacer l’affirmation',
 
'newentity': 'Identifiant du nouvel élément :',
 
'move-claim-intro': 'Vous pouvez déplacer cette affirmation vers un autre élément. Veuillez donner un identifiant d’élément valide.',
 
'new-magic-word': 'NOUVEAU',
 
'move-claim-intro-hint': 'Si vous rentrez "$1", vous copierez/déplacerez l’affirmation vers un nouvel élément.',
 
'successfully-copied': 'L’affirmation $1 a été copiée vers $2 avec succès.',
 
'successfully-moved': 'L’affirmation $1 a été déplacée vers $2 avec succès.',
 
'error-sameid': 'L’identifiant du nouvel élément doit être différent de celui de l’élément actuel.',
 
'error-invalidid': 'L’identifiant donné n’est pas un identifiant d’élément valide.',
 
'error-notexisting': 'L’élément avec l’identifiant $1 n’existe pas.',
 
'error-overwrite': 'Le lien pour le site présent dans $1 est déjà défini. Veuillez vérifier l’identifiant de l’élément ou supprimer ce lien de l’élément.',
 
'error-api': 'Une erreur est survenue lors de l’édition de l’élément : $1'
 
} );
 
break;
 
}
 
 
var api,
 
repoApi,
 
claimid,
 
oldentity,
 
oldtitle,
 
olddata = null,
 
newentity,
 
newtitle,
 
newdata,
 
last = true,
 
credit = 'using [[User:Matěj Suchánek/moveClaim.js|moveClaim.js]]';
 
 
function createSpinner() {
 
$( '#move-claim-result' ).html(
 
$.createSpinner( {
 
size: 'large',
 
type: 'block'
 
} )
 
);
 
}
 
 
function showError( error ) {
 
var parameters = Array.prototype.slice.call( arguments, 1 );
 
$( '#move-claim-result' ).html(
 
$( '<p>' )
 
.attr( 'class', 'error' )
 
.html( mw.message( 'error-' + error ).params( parameters ).parse() )
 
);
 
}
 
 
function onError( error, result ) {
 
showError( 'api', result && result.error && result.error.info || error );
 
}
 
 
function getFragmentedTitle( title, entity ) {
 
if ( entity.indexOf( '-' ) !== -1 ) {
 
title = title.replace( /:.+$/, ':' + entity.replace( '-', '#' ) );
 
}
 
return title;
 
}
 
 
function success( removed ) {
 
var $statement = $( '.wikibase-statement-' + $.escapeSelector( claimid ) ),
 
message = mw.message( removed ? 'successfully-moved' : 'successfully-copied',
 
$statement.closest( '.wikibase-statementgroupview' ).data( 'property-id' ),
 
mw.html.element( 'a', { href: mw.util.getUrl( newtitle ) }, newentity )
 
);
 
 
$( '#move-claim' ).dialog( 'close' );
 
$( '#move-newentity' ).val( newentity ); // update for new items
 
 
// Todo: Hide moved claim
 
if ( removed ) {
 
$statement.find( '.move-button-container' ).remove();
 
}
 
 
mw.notify( message, {
 
autoHide: false,
 
title: removed ? mw.msg( 'move-claim' ) : mw.msg( 'copy-claim' )
 
} );
 
}
 
 
function loadEntity() {
 
if ( olddata !== null ) {
 
return $.Deferred().resolve().promise();
 
}
 
return repoApi.getEntities( oldentity, [ 'info', 'claims' ] )
 
.then( function ( data ) {
 
olddata = data.entities[ oldentity ];
 
oldtitle = getFragmentedTitle( olddata.title, oldentity );
 
} );
 
}
 
 
function clone( data ) {
 
return JSON.parse( JSON.stringify( data ) );
 
}
 
 
function getHash( claim ) {
 
var hashes = [];
 
$.each( claim.qualifiers || {}, function ( p, qualifiers ) {
 
qualifiers.forEach( function ( qual ) {
 
hashes.push( qual.hash );
 
} );
 
} );
 
return claim.mainsnak.hash + hashes.sort().join( '|' );
 
}
 
 
function mergeClaim( claim, removing ) {
 
const property = claim.mainsnak.property;
 
var data = {},
 
hash = getHash( claim ),
 
same = false;
 
$.each( newdata.claims[ property ] || [], function ( i, _claim ) {
 
if ( hash === getHash( _claim ) ) {
 
same = _claim;
 
return false;
 
}
 
} );
 
if ( same !== false ) {
 
if ( !claim.references ) {
 
return $.Deferred().resolve().promise();
 
}
 
data = clone( same );
 
var hashes = ( same.references || [] ).map( function ( ref ) {
 
return ref.hash;
 
} );
 
claim.references.forEach( function ( ref ) {
 
if ( hashes.indexOf( ref.hash ) === -1 ) {
 
if ( data.references === undefined ) {
 
data.references = [];
 
}
 
data.references.push( ref );
 
}
 
} );
 
} else {
 
data = clone( claim );
 
delete data.id;
 
}
 
return api.postWithEditToken( {
 
formatversion: 2,
 
action: 'wbeditentity',
 
id: newentity,
 
data: JSON.stringify( { claims: [ data ] } ),
 
baserevid: newdata.lastrevid,
 
summary: removing
 
? 'Moving [[Property:' + property + ']] from [[' + oldtitle + ']], ' + credit
 
: 'Copying [[Property:' + property + ']] from [[' + oldtitle + ']], ' + credit
 
} );
 
}
 
 
function getAllClaims() {
 
var claims = [];
 
$.each( olddata.claims || {}, function ( prop, _claims ) {
 
Array.prototype.push.apply( claims, _claims );
 
} );
 
return claims;
 
}
 
 
function move( remove ) {
 
var claim;
 
$.each( getAllClaims(), function ( i, _claim ) {
 
if ( _claim.id === claimid ) {
 
claim = _claim;
 
return false;
 
}
 
} );
 
 
return mergeClaim( claim, remove )
 
.then( function () {
 
if ( !remove ) {
 
return;
 
}
 
return api.postWithEditToken( {
 
action: 'wbremoveclaims',
 
baserevid: olddata.lastrevid,
 
claim: claimid,
 
formatversion: 2,
 
summary: 'Moving claim to [[' + newtitle + ']], ' + credit,
 
} );
 
} );
 
}
 
 
function performMove( remove ) {
 
last = remove;
 
createSpinner();
 
oldentity = claimid.split( '$' )[0].toUpperCase();
 
newentity = $( '#move-newentity' ).val().toUpperCase();
 
if ( oldentity === newentity ) {
 
showError( 'sameid' );
 
return;
 
}
 
var promise,
 
error = false;
 
if ( [ 'NEW', mw.msg( 'new-magic-word' ) ].indexOf( newentity ) !== -1 ) {
 
promise = $.when(
 
repoApi.createEntity( 'item' ) // todo: lexemes (upstream)
 
.then( function ( data ) {
 
newdata = data.entity;
 
newentity = newdata.id;
 
newtitle = newentity; // fixme: getFragmentedTitle( newdata.title, newentity );
 
} ),
 
loadEntity()
 
);
 
} else {
 
if ( newentity.match( /^([PQ][1-9]\d*|L[1-9]\d*?(-F[1-9]\d*)?)$/g ) === null ) {
 
//if ( newentity.match( /^([PQ][1-9]\d*|L[1-9]\d*?(-[FS][1-9]\d*)?)$/g ) === null ) { T199896
 
if ( oldentity.indexOf( 'L' ) === 0 && newentity.match( /^F[1-9]\d*$/g ) ) {
 
//if ( oldentity.indexOf( 'L' ) === 0 && newentity.match( /^[FS][1-9]\d*$/g ) ) { T199896
 
newentity = oldentity + '-' + newentity;
 
} else {
 
showError( 'invalidid' );
 
return;
 
}
 
}
 
promise = repoApi.getEntities( [ oldentity, newentity ], [ 'info', 'claims' ] )
 
.then( function ( data ) {
 
const entity = data.entities[ newentity ];
 
if ( entity.hasOwnProperty( 'missing' ) ) {
 
showError( 'notexisting', mw.html.element( 'a', { href: mw.util.getUrl( newentity ) }, newentity ) );
 
error = true;
 
return;
 
}
 
 
if ( entity.hasOwnProperty( 'redirects' ) ) {
 
newentity = entity.redirects.to;
 
}
 
 
olddata = data.entities[ oldentity ];
 
newdata = entity;
 
 
oldtitle = getFragmentedTitle( olddata.title, oldentity );
 
newtitle = getFragmentedTitle( newdata.title, newentity );
 
} );
 
}
 
promise
 
.then( function () {
 
if ( !error ) {
 
return move( remove )
 
.then( function () {
 
success( remove );
 
} );
 
}
 
}, onError );
 
}
 
 
function openDialog() {
 
$( '#move-claim-result' ).empty();
 
$( '#move-claim' ).dialog( 'open' );
 
if ( $( '#move-newentity' ).val() !== '' ) {
 
if ( last ) {
 
$( '#move-claim-button-move' ).focus();
 
} else {
 
$( '#move-claim-button-copy' ).focus();
 
}
 
}
 
}
 
 
function addButton( $statement ) {
 
$statement.prepend(
 
$( '<div>' )
 
.attr( 'class', 'move-button-container' )
 
.css( {
 
'float': 'right',
 
'position': 'relative',
 
'z-index': 1,
 
} )
 
.append(
 
$( '<a>' )
 
.attr( {
 
'href': '#',
 
'class': 'move-button',
 
} )
 
.on( 'click', function ( event ) {
 
event.preventDefault();
 
claimid = $statement.attr( 'id' );
 
openDialog();
 
} )
 
)
 
);
 
}
 
 
function init() {
 
// Add click listener
 
$( '.wikibase-statementview' ).each( function () {
 
addButton( $( this ) );
 
} );
 
 
// Create dialog
 
$( '<div>' )
 
.attr( 'id', 'move-claim' )
 
.append(
 
$( '<form>' )
 
.submit( function ( event ) {
 
event.preventDefault();
 
performMove( last );
 
} )
 
.append(
 
$( '<fieldset>' )
 
.attr( 'id', 'claim-form' )
 
.append(
 
$( '<legend>' )
 
.text( mw.msg( 'move-claim' ) ),
 
// </legend>
 
$( '<p>' )
 
.attr( 'id', 'claim-intro' )
 
.text( mw.msg( 'move-claim-intro' ) ),
 
// </p>
 
$( '<p>' )
 
.attr( 'id', 'claim-intro-hint' )
 
.text(
 
mw.msg( 'move-claim-intro-hint' )
 
.replace( '$1', mw.msg( 'new-magic-word' ) )
 
),
 
// </p>
 
$( '<p>' )
 
.append(
 
$( '<label>' )
 
.attr( {
 
'for': 'move-newentity',
 
'class': 'move-label'
 
} )
 
.text( mw.msg( 'newentity' ) ),
 
' ',
 
// </label>
 
$( '<input>' )
 
.attr( {
 
'type': 'text',
 
'id': 'move-newentity',
 
'class': 'move-input'
 
} )
 
),
 
) // </p>
 
), // </fieldset>
 
// </form>
 
$( '<p>' )
 
.attr( 'id', 'move-claim-result' )
 
)
 
.dialog( {
 
dialogClass: 'move-dialog',
 
title: mw.message( 'move-claim' ).escaped(),
 
autoOpen: false,
 
modal: true,
 
width: 500,
 
buttons: [ {
 
id: 'move-claim-button-move',
 
text: mw.msg( 'move-claim' ),
 
click: function ( event ) {
 
event.preventDefault();
 
performMove( true );
 
}
 
}, {
 
id: 'move-claim-button-copy',
 
text: mw.msg( 'copy-claim' ),
 
click: function ( event ) {
 
event.preventDefault();
 
performMove( false );
 
}
 
}, {
 
id: 'move-claim-button-close',
 
text: mw.msg( 'close' ),
 
click: function ( event ) {
 
event.preventDefault();
 
$( '#move-claim' ).dialog( 'close' );
 
}
 
} ]
 
} );
 
}
 
 
if ( mw.loader.getState( 'ext.gadget.Move' ) !== 'ready' ) {
 
// don't load CSS twice
 
mw.loader.load( '//wiki.personaldata.io/w/index.php?title=MediaWiki:Gadget-Move.css&action=raw&ctype=text/css', 'text/css' );
 
}
 
 
$.when(
 
mw.loader.using( [
 
'jquery.spinner', 'jquery.ui.dialog', 'mediawiki.api',
 
'mediawiki.notify', 'mediawiki.util', 'wikibase.api.RepoApi',
 
] )
 
.then( function () {
 
api = new mw.Api();
 
repoApi = new wb.api.RepoApi( api );
 
} ),
 
$.ready
 
)
 
.then( init );
 
 
mw.hook( 'wikibase.statement.saved' ).add( function ( _, guid ) {
 
olddata = null;
 
//var $block = $( '#' + $.escapeSelector( guid ) );
 
var $block = $( '.wikibase-statement-' + $.escapeSelector( guid ) );
 
if ( $block.find( '.move-button-container' ).length === 0 ) {
 
addButton( $block );
 
}
 
} );
 
 
} ( mediaWiki, wikibase, jQuery ) );
 

Latest revision as of 07:40, 23 October 2019