Utente:MauroBot/BotCancellazioni/main.js/sandbox.js

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca

Questa pagina definisce alcuni parametri di aspetto e comportamento generale di tutte le pagine. Per personalizzarli vedi Aiuto:Stile utente.


Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti (per le pagine globali è comunque necessario attendere qualche minuto). Per Mozilla / Firefox / Safari: fare clic su Ricarica tenendo premuto il tasto delle maiuscole, oppure premere Ctrl-F5 o Ctrl-R (Command-R su Mac); per Chrome: premere Ctrl-Shift-R (Command-Shift-R su un Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menù Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.

var DelBot = DelBot || {};

// Current app status
DelBot.currentAppStatus = undefined;

// Log messages
DelBot.messages = [];

/**
 * Add a log message
 * @param message string
 */
DelBot.addMessage = function ( message ) {
	var d = new Date();
	DelBot.messages.push( '[' + d.toISOString() + '] ' + message );
};

/**
 * Add an important log message (asd)
 * @param message string
 */
DelBot.addImportantMessage = function ( message ) {
	DelBot.addMessage('*** ' + message + ' ***');
};

/**
 * Start the bot execution
 */
DelBot.startDeletionBot = function () {
	var d = new Date();

	var actions = {
		gestioneCategorieGiornaliere: true,
		gestioneCategorieMensili:     false,
		gestioneCategorieAnnuali:     false,
		letturaPDC:                   true,
		leggiStatoCatInCorso:         false
	};

	// Il primo giorno di ogni mese attivo la gestione delle categorie mensili
	if( 1 === d.getDate() ) {
		actions.gestioneCategorieMensili = true;

		// Il primo gennaio attivo la gestione delle categorie annuali
		if( 0 === d.getMonth() ) {
			actions.gestioneCategorieAnnuali = true;
		}
	}

	// Alle 12 gestisco l'aggiornamento della categoria Procedure di cancellazione in corso
	// if( 12 === d.getHours() ) {
	// 	r.leggiStatoCatInCorso = true;
	// }

	var appStatusRouter = [];
	for( var action in actions ) {
		if( actions[ action ] ) {
			appStatusRouter.push( action );
		}
	}

	DelBot.mainDeletionBot( d, DelBot.config.days, appStatusRouter, 15 );
};

/**
 * Main bot method
 *
 * @param PDC_date Date
 * @param days int
 * @param appStatusRouter object
 * @param timeout int
 */
DelBot.mainDeletionBot = function ( PDC_date, days, appStatusRouter, timeout ) {
	var catStatus  = [];
	var PDC_Status = [];
	var PDC_Errors = [];
	var iw = 0, ic = 0;
	var writeData = [];
	var writeStatus;
	var statusReq, statusLastUpd;
	var daysCnt   = 0;
	var statusCnt = 0;
	var nextAppStatus;
	var token = mw.user.tokens.get( 'editToken' );
	var startDate = new Date();

	// What does it do?
	var botTimeout = setTimeout( function () {
		DelBot.currentAppStatus = 'timeout';
	}, timeout * 60 * 1000 );

	// Initial status
	DelBot.currentAppStatus = 'selectStatus';

	// Repeat every 100 ms until conclusion
	var interval = setInterval( function() {
		var d = new Date();
		switch( DelBot.currentAppStatus ) {
			case 'selectStatus':
				if( statusCnt === appStatusRouter.length ) {
					DelBot.currentAppStatus = 'finish';
				} else {
					DelBot.currentAppStatus = appStatusRouter[ statusCnt ];
					statusCnt++;
				}
				break;
			case 'gestioneCategorieGiornaliere':   
				// Avvio la gestione delle categorie giornaliere
				DelBot.addImportantMessage( 'Gestione delle categorie giornaliere' );
				catStatus.length = 0;
				for( var i = 0; i < DelBot.config.tipologie.length; i++ ) {
					catStatus.push( new DelBot.classes.CategoryStatus() );
					DelBot.creazioneCategorie(
						DelBot.config.tipologie[ i ],
						new Date( PDC_date.getTime() ),
						days,
						catStatus[ i ]
					);
				}

				DelBot.currentAppStatus = 'runningCategorie';
				// Stato da eseguire quando le categorie giornaliere saranno processate
				nextAppStatus = 'selectStatus'; 
				// ic è il numero delle sessioni in parallelo
				ic = DelBot.config.tipologie.length;
				break;
			case 'gestioneCategorieMensili':
				// Avviso la gestione delle categorie
				DelBot.addImportantMessage( 'Gestione delle categorie mensili' );

				catStatus.push( new DelBot.classes.CategoryStatus() );
				DelBot.creazioneCategorieMese( PDC_date.getFullYear(), catStatus[ 0 ] )

				DelBot.currentAppStatus = 'runningCategorie';
				nextAppStatus = 'selectStatus'; 
				ic = 1;
				break;
			case 'gestioneCategorieAnnuali':
				// Avviso la gestione delle categorie

				DelBot.addImportantMessage( 'Gestione delle categorie annuali' );

				catStatus.push( new DelBot.classes.CategoryStatus() );
				DelBot.creazioneCategorieAnno( PDC_date.getFullYear(), catStatus[ 0 ] )

				DelBot.currentAppStatus = 'runningCategorie';
				nextAppStatus = 'selectStatus'; 
				ic = 1;
				break;
			case 'runningCategorie':
				// Attendo la lettura delle categorie
				var n;
				for( n = 0; n < ic; n++ ) {
					if( ! catStatus[ n ].done ) {
						break;
					}
				}
				if( n < ic ) {
					break;
				}

				// Lettura completata
				writeData.length = 0;
				for( var n = 0; n < ic; n++) {
					if( catStatus[ n ].ok ) {
						writeData = writeData.concat( catStatus[ n ].writeData );
					} else {
						DelBot.messages.push( catStatus[ n ].error );
					}
				}

				// Stato successivo alla scrittura
				DelBot.currentAppStatus = 'scrittura';
				break;
			case 'scrittura':
				if( writeData.length > 0 ) {
					iw = 0;
					writeStatus = new DelBot.classes.WriteStatus(); 
					DelBot.writePage(
						writeData[ 0 ].title,
						writeData[ 0 ].text,
						writeData[ 0 ].summary,
						token,
						writeStatus
					);
					DelBot.currentAppStatus = 'runningScrittura';
				} else {
					DelBot.addMessage( 'Nessuna pagina da scrivere' );
					DelBot.currentAppStatus = nextAppStatus;
				}
				break;
		case 'runningScrittura':
			if( writeStatus.done ) {
				if( ! writeStatus.ok ) {
					DelBot.messages.push(writeStatus.error);
				} else {
					DelBot.addMessage( 'Pagina creata/modificata: ' + writeData[ iw ].title );
				}
				iw++;
				if( iw < writeData.length ) {
					writeStatus = new DelBot.classes.WriteStatus(); 
					DelBot.writePage(
						writeData[ iw ].title,
						writeData[ iw ].text,
						writeData[ iw ].summary,
						token,
						writeStatus
					);
				} else {
					DelBot.currentAppStatus = nextAppStatus;
				}
			}
			break;
		case 'letturaPDC':
			Running_PDC_date = new Date( PDC_date.getTime() );
			daysCnt = days;
			DelBot.addImportantMessage( 'Lettura delle procedure di cancellazione' );

			// TODO: is wanted that there is not any break here?

		case 'iniziaLetturaPDC':
			DelBot.addMessage( 'Lettura procedure del ' + DelBot.date2CategoryName( Running_PDC_date ) );
			DelBot.currentAppStatus = 'leggiGiorno';
			PDC_Status.length = 0;
			PDC_Errors.length = 0;
			for( var i=0; i < DelBot.config.tipologie.length; i++ ) {
				PDC_Status.push( new DelBot.classes.ReadStatus() );
				DelBot.startReadingPDCCategory(DelBot.config.tipologie[i], Running_PDC_date, PDC_Status[i]);
			}
			break;
		case 'leggiGiorno':
			finishCnt = 0;
			for( var i = 0; i < DelBot.config.tipologie.length; i++ ) {
				// Lettura completata?
				if( PDC_Status[ i ].done && ! PDC_Status[ i ].finish ) {
					if( PDC_Status[ i ].ok ) {
						// Gestione del query continue                     
						if( PDC_Status[ i ].qcontinue ) {
							DelBot.startReadingPDCCategory( DelBot.config.tipologie[ i ], Running_PDC_date, PDC_Status[ i ] );
						} else {
							PDC_Status[ i ].finish = true;
						}
					} else {
						// errore!!
						PDC_Status[ i ].finish = true; 
						PDC_Errors.push( PDC_Status[ i ].error );
					}
				}

				if( PDC_Status[ i ].finish ) {
					finishCnt++;
				}
			}

			if( finishCnt === DelBot.config.tipologie.length ) {
				daysCnt--;
				if( PDC_Errors.length > 0 ) {
					DelBot.messages = DelBot.messages.concat( PDC_Errors );
					// Gestione del giorno successivo
					if( daysCnt === 0) {
						DelBot.currentAppStatus = 'selectStatus';
					} else {
						Running_PDC_date.setDate( Running_PDC_date.getDate() - 1 );
						DelBot.currentAppStatus = 'iniziaLetturaPDC';
					}
				} else {
					var procedure = [];
					for( var i = 0; i < DelBot.config.tipologie.length; i++ ) {
						procedure[ DelBot.config.tipologie[i] ] = DelBot.processLetturaCategoriaPDC( DelBot.config.tipologie[ i ], PDC_Status[ i ].rawData );
					}

					DelBot.processPDCErrate( procedure );
					txt_conta = DelBot.generaTabellaConteggio( Running_PDC_date, procedure );

					txt_log = DelBot.generateDailyLog( Running_PDC_date, procedure );

					writeData.length = 0;
					writeData.push( {
						title: DelBot.config.pdcCountPrefix + DelBot.date2LogFormat( Running_PDC_date ),
						text: txt_conta,
						summary: 'Bot: aggiorno tabella di conteggio'
					} );
					writeData.push( {
						title: DelBot.config.pdcLogPrefix + DelBot.date2LogFormat( Running_PDC_date ),
						text: txt_log,
						summary: 'Bot: aggiorno log giornaliero'
					} );

					// Gestione del giorno successivo
					if( 0 === daysCnt ) {
						nextAppStatus = 'selectStatus'; 
						DelBot.currentAppStatus = 'scrittura';
					} else {
						Running_PDC_date.setDate( Running_PDC_date.getDate() - 1 );
						nextAppStatus = 'iniziaLetturaPDC';
						DelBot.currentAppStatus = 'scrittura';
					}
				}
			}
			break;

		case 'leggiStatoCatInCorso':
			statusReq =     { timestamp: false, done: false, ok: false, error: '' };
			statusLastUpd = { timestamp: false, done: false, ok: false, error: '' };
			DelBot.leggiStatoCatInCorso( statusReq, statusLastUpd );
			DelBot.currentAppStatus = 'runningLeggiStatoCatInCorso';
			break;

		case 'runningLeggiStatoCatInCorso':
			if( statusReq.done && statusLastUpd.done ) {
				var t1 = statusReq.timestamp.getTime();
				var t2 = statusLastUpd.timestamp.getTime();
				var t3 = d.getTime();

				// TODO: to be decrypted :)
				if( ( (t1 == t2) || ( (t1-t2) > 5*24*3600*1000 && (t3-t1) > 5*24*3600*1000 ) ) && ( (t3-t1) > 23*3600*1000 ) ) {
					writeData.length = 0;
					writeData.push( {
						title: DelBot.config.pageLastCategoryUpdate,
						text: '20180409141924',
						summary: 'Bot: aggiornamento'
					} );
					nextAppStatus = 'selectStatus'; 
					DelBot.currentAppStatus = 'scrittura';
				} else {
					DelBot.currentAppStatus = 'selectStatus';
				}
			}
			break;
		case 'timeout':
			DelBot.addImportantMessage( 'TIMEOUT: esecuzione interrotta per il superamento del tempo limite' );
			//break;
		case 'finish':
			var finishDate = new Date();
			runningTime = Math.floor( ( finishDate.getTime() - startDate.getTime() ) / 1000 );
			DelBot.addImportantMessage( 'ESECUZIONE COMPLETATA in ' + Math.floor( runningTime / 60 ) + ' minuti e ' + runningTime % 60 + ' secondi' );
			clearTimeout( botTimeout );
			clearInterval( interval );
			DelBot.currentAppStatus = 'finished';
			break;
		}
		// end switch
	}, 100 );
	// end interval
};