MediaWiki:Gadget-DraftReview.js: differenze tra le versioni

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
Contenuto cancellato Contenuto aggiunto
m fix minore
m fix
Riga 12: Riga 12:
( function ( mw, $ ) {
( function ( mw, $ ) {
'use strict';
'use strict';
var api = new mw.Api();

//Controlli sulle categorie e sull'utente
//Controlli sulle categorie e sull'utente
var cats;
if (mw.config.get('wgCategories').includes("Bozze da revisionare") && mw.config.get('wgUserGroups').includes("autoconfirmed") ){
//Da mobile wgCategories è vuoto perché non appare il box categorie
//Controlla quindi solo namespace
if (mw.config.get('wgCategories') == null )
cats=(mw.config.get('wgNamespaceNumber')==118)
else
cats=mw.config.get('wgCategories').includes("Bozze da revisionare");
if (cats && mw.config.get('wgUserGroups').includes("autoconfirmed") ){


//Variabili globali
//Variabili globali
var avvisa, input, dropdown, header;
var avvisa, input, dropdown, header;
var api = new mw.Api();
var autore=null;
var autore=null;
var currentTimestamp, currentRevid;
var currentTimestamp, currentRevid;
Riga 266: Riga 276:
var button = new OO.ui.ButtonWidget( {
var button = new OO.ui.ButtonWidget( {
classes: 'pulsante-richiesta-revisione',
label: 'Effettua revisione',
label: 'Effettua revisione',
flags: [
flags: [
Riga 273: Riga 282:
]
]
} );
} );
button.$element.css("float", "right").css("font-size", "14px").css("margin", ".5em 2em");
button.on('click', onClick);
button.on('click', onClick);
$($boxbozza).append( button.$element );
$($boxbozza).append( button.$element );

Versione delle 12:25, 16 lug 2021

/*
----
Aggiungere quanto segue a [[Speciale:MiaPagina/common.js]] per utilizzare

mw.loader.load("https://it.wikipedia.org/wiki/User:ValeJappo/Script/DraftReview.js?action=raw&ctype=text/javascript");
----

Questo script facilizza il processo di revisione delle bozze.

*/
//<pre>
( function ( mw, $ ) {
	'use strict';
	var api = new mw.Api();

	
	//Controlli sulle categorie e sull'utente
	var cats;
	
	//Da mobile wgCategories è vuoto perché non appare il box categorie
	//Controlla quindi solo namespace
	if (mw.config.get('wgCategories') == null )
		cats=(mw.config.get('wgNamespaceNumber')==118)
	else 
		cats=mw.config.get('wgCategories').includes("Bozze da revisionare");
		
	if (cats && mw.config.get('wgUserGroups').includes("autoconfirmed") ){

		//Variabili globali	
		var avvisa, input, dropdown, header;
		var autore=null;
		var currentTimestamp, currentRevid;
		
		//API
		api.get( {
			"action": "query",
			"format": "json",
			"prop": "revisions",
			"titles": mw.config.get('wgPageName'),
			"formatversion": "2",
			"rvprop": "user",
			"rvslots": "main",
			"rvlimit": "1",
			"rvdir": "newer"
		} ).done( function ( data ) {
			autore=JSON.stringify(data.query.pages[0].revisions[0].user).replaceAll('"', '');
		} );
	
		// Carica risorse
		mw.loader.using( 'oojs-ui-core' ),
		mw.loader.using( 'oojs-ui-windows' ).done( function () { 
			$( function () {
				
				
				// Ottieni elemento del template
				var $boxbozza = $( ".pulsante-pubblicazione-bozza" ).parent();
				
				// Genera popup
				var MenuDialog = function MenuDialog( config ) {
					MenuDialog.super.call( this, config );
				};
				OO.inheritClass( MenuDialog, OO.ui.ProcessDialog );
				MenuDialog.static.name = 'menuDialog';
				MenuDialog.static.title = 'Revisione bozza';
				MenuDialog.static.actions = [
					{ action: 'save', label: 'Conferma', flags: [ 'primary', 'progressive' ] },
					{ action: 'cancel', label: 'Annulla', flags: [ 'safe', 'close' ] }
				];
				MenuDialog.prototype.getBodyHeight = function () {
					return 350;
				};
				MenuDialog.prototype.initialize = function () {
					// Contenuto del popup
					MenuDialog.super.prototype.initialize.apply( this, arguments );
				    this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
				    
				    // Dropdown segli esito
				    dropdown = new OO.ui.DropdownWidget( {
				    	id: 'draftreview-esito',
						label: 'Seleziona esito',
						menu: {
							items: [
								new OO.ui.MenuOptionWidget( {
									data: 'R',
									label: 'Respinta'
								} ),
								new OO.ui.MenuOptionWidget( {
									data: 'A',
									label: 'Approvata'
								} )
							]
						}
					} );
					
					// Text input commento
					input = new OO.ui.TextInputWidget( {
						id: 'draftreview-commento',
						value: ''
					} );
					
					//Layout opzioni avviso
					avvisa = new OO.ui.HorizontalLayout( {
					  items: [
					  	new OO.ui.LabelWidget( { label: 'Avvisa autore' } ),
					    new OO.ui.CheckboxInputWidget( {
					        value: 'avvisa',
					        selected: true,
					        id: 'draftreview-doWarn',
					    } ),
					    new OO.ui.TextInputWidget( { value: autore, id: 'draftreview-autore' } )
					  ]
					} );
					
					//Aggiungi elementi
				    this.content.$element.append( '<p>Esito</p>', dropdown.$element, '<div id="draftreview-top"></div><br/><br/><p>Commento</p>' ,input.$element, '<br/>', avvisa.$element);
				    this.$body.append( this.content.$element );
				};
				
				// Gestisci azioni
				MenuDialog.prototype.getActionProcess = function ( action ) {
					if ( action == 'cancel' ) { // Chiudi
						return new OO.ui.Process( function () {
							this.close( { action: action } );
						}, this );
					} else if (action == 'save') { // Continua
						windowManager.closeWindow(menuDialog); // Chiudi popup
						
						// Gestisci esito
						if(dropdown.getLabel()=='Respinta'){ //Respinta
							
							//Ottieni contenuto
							api.get({
								"action": "parse",
								"format": "json",
								"page": mw.config.get('wgPageName'),
								"prop": "wikitext",
								"formatversion": "2"
							}).done(function (data1) {
								
								//Modifica contenuto
								
								var params = {
									"action": "edit",
									"format": "json",
									"title": mw.config.get('wgPageName'),
									"text": data1.parse.wikitext.replace("|esito=|utente=", "|esito=Respinta. "+input.value+"|utente="+mw.config.get('wgUserName')),
									"summary": "Esito revisione bozza",
									"minor": 1,
									"nocreate": 1,
									"starttimestamp": currentTimestamp,
									"baserevid": currentRevid
								};
								
								//Avvisa utente e fornisci esito
								api.postWithToken( 'csrf', params).done( function ( data2 ) {avvisof(mw.config.get('wgTitle'), 'R')}).fail(function (error){errorHandler(error)});
									
	
							});
							
						}else if(dropdown.getLabel()=='Approvata'){ //Approvata
							//Popup per inserire titolo
							OO.ui.prompt( 'Inserisci il nuovo titolo', { textInput: { value: mw.config.get('wgTitle') } } ).done( function ( result ) {
							    if ( result != null ) {
							    		//Sposta pagina
								        api.postWithToken( 'csrf', 
										{
									        action: "move",
									        from: mw.config.get('wgPageName'),
									        to: result,
									        reason: "Pubblicazzione bozza. "+input.value,
									        movetalk: "1",
									        noredirect: "1",
									        format: "json"
										}).done( function ( data ) { avvisof(result, 'A') }).fail(function (error){errorHandler(error)}); //Avvisa e mostra risultato
							    }
						    });
						}
					}
					return MenuDialog.super.prototype.getActionProcess.call( this, action );
				};
				
				//Preparazione per visualizzare popup
				var menuDialog = new MenuDialog( {
				    size: 'medium'
				} );
				// Genera window manager
				var windowManager = new OO.ui.WindowManager();
				$( document.body ).append( windowManager.$element );
				windowManager.addWindows( [ menuDialog ] );
				
				// -- Inizio funzioni --
				
				// Invia avviso
				var avvisof = function (titolo, esito) {
					if(avvisa.items[1].selected){
						api.postWithToken( 'csrf', 
						{
							"action": "edit",
							"format": "json",
							"title": "User talk:"+avvisa.items[2].value,
							"appendtext": "\n==Esito revisione bozza "+titolo+"==\n{{Erb|"+esito+"|"+titolo+"}}--~~~~",
							"summary": "Avviso esito revisione bozza",
							"minor": 1
						});
					}
					
					if(esito=="R")
						titolo="Bozza:"+titolo; // Se respinta fai tornare alla bozza
					
					success(titolo); // Mostra esito
					
				};
				
				// Messaggio di errore
				var errorHandler = function(error){
					var lbel;
					if (error=='editconflict')
						lbel=new OO.ui.HtmlSnippet('<b>Azione fallita</b><br/><p>Si è verificato un conflitto di edizione: ricarica la pagina e riprova</p>');
					else
						lbel= new OO.ui.HtmlSnippet("<b>Azione fallita</b><br/><p>Codice dell'errore: "+error+"</p>");
					header = new OO.ui.MessageWidget( {
						type: 'error',
						label: lbel
					} );
					$("#bodyContent").prepend(header.$element);
				};
				
				// Messaggio di successo
				var success = function(titolo){
					header = new OO.ui.MessageWidget( {
						type: 'success',
						label: new OO.ui.HtmlSnippet('<b>Azione eseguita con successo</b><br/><ul><li>Vai a <a href="/wiki/Wikipedia:Bozze da revisionare">Wikipedia:Bozze da revisionare</a></li><li>Vai a <a href="/wiki/'+titolo+'">'+titolo+'</a></li><li>Vai alla <a href="/wiki/User talk:'+avvisa.items[2].value+'">pagina di discussione di '+avvisa.items[2].value+'</a></li></ul>')
					} );
					$("#bodyContent").prepend(header.$element);
				};
				
				// Convertitore in timestamp ISO
				function ISODateString(d){
					function pad(n){return n<10 ? '0'+n : n}
					return d.getUTCFullYear()+'-'
					    + pad(d.getUTCMonth()+1)+'-'
					    + pad(d.getUTCDate())+'T'
					    + pad(d.getUTCHours())+':'
					    + pad(d.getUTCMinutes())+':'
					    + pad(d.getUTCSeconds())+'Z';
							
				}
				
				// Ottieni timestamp e ultimo revid
				currentTimestamp = ISODateString(new Date());
				api.get({
					"action": "query",
					"format": "json",
					"prop": "revisions",
					"titles": mw.config.get('wgPageName'),
					"formatversion": "2",
					"rvprop": "ids",
					"rvslots": "main",
					"rvlimit": "1",
					"rvdir": "older"
				}).done(function (datatimestamp) {
					currentRevid=JSON.stringify(datatimestamp.query.pages[0].revisions[0].revid).replaceAll('"', '');
				});
	
				    
				// Funzione al click del pulsante
				var onClick = function(){
					//Ripristina autore
					avvisa.items[2].setValue(autore);
					
					// Apri popup
					windowManager.openWindow( menuDialog );
				};
				
				// Genera pulsante
			
						var button = new OO.ui.ButtonWidget( {
							label: 'Effettua revisione',
							flags: [
								'primary',
								'progressive'
							]
						} );
						button.on('click', onClick);
						$($boxbozza).append( button.$element );
						
						
			});
		});
	}	
} )( mediaWiki, jQuery );
//</pre>