MediaWiki:Gadget-popolaTestiAutore.js

Da Wikisource.

Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
  • Opera: premi Ctrl-F5.
var PopolaTestiAutore = {
	run: function() {
		mw.loader.using("mediawiki.api", function() {
			var api = new mw.Api();
			api.get({
				action: 'query',
				format: 'json',
				list: 'categorymembers',
				cmnamespace: '0',
				cmtype: 'page',
				cmlimit: 1000,
				cmtitle: 'Categoria:Testi di ' + mw.config.get("wgTitle")
			}).done(function (data) {
				newText = '';
				testi = [];
				traduzioni = [];
				curatele = [];
				
				if (data.query) {
					titles = data.query.categorymembers;
					if (titles && titles.length > 0) {
						newText = '== Opere ==';
						for (i = 0; i < titles.length; i++) {
							newText += '\n* {{Testo|' + titles[i].title + '}}';
							testi.push(titles[i].title);
						}
					}
				}
				
				api.get({
					action: 'query',
					format: 'json',
					list: 'categorymembers',
					cmnamespace: '0',
					cmtype: 'page',
					cmlimit: 1000,
					cmtitle: 'Categoria:Traduzioni di ' + mw.config.get("wgTitle")
				}).done(function (data) {
					if (data.query) {
						titles = data.query.categorymembers;
						if (titles && titles.length > 0) {
							if (newText === '') {
								newText = '== Traduzioni ==';
							}
							else {
								newText += '\n\n=== Traduzioni ===';
							}
							
							for (i = 0; i < titles.length; i++) {
								newText += '\n* {{Testo|' + titles[i].title + '}}';
								traduzioni.push(titles[i].title);
							}
						}
					}
					
					api.get({
						action: 'query',
						format: 'json',
						list: 'categorymembers',
						cmnamespace: '0',
						cmtype: 'page',
						cmlimit: 1000,
						cmtitle: 'Categoria:Testi curati da ' + mw.config.get("wgTitle")
					}).done(function (data) {
					
						if (data.query) {
							titles = data.query.categorymembers;
							if (titles && titles.length > 0) {
								if (newText === '') {
									newText = '== Curatele ==';
								}
								else {
									newText += '\n\n=== Curatele ===';
								}
								
								for (i = 0; i < titles.length; i++) {
									newText += '\n* {{Testo|' + titles[i].title + '}}';
									curatele.push(titles[i].title);
								}
							}
						}
					
						sezioneNote = '{{Sezione note}}';
						currentText = $('#wpTextbox1').val();
						isEmpty = currentText.replace(sezioneNote, '').trim() === '';
						isSezioneNote = currentText.indexOf(sezioneNote) != -1;
						
						if (newText !== '') {
							if (isSezioneNote) {
								newText += '\n\n' + sezioneNote;
							}
							
							$('#editform').prepend($('<a id="popolaTestiAutoreLink" href="#" class="btn blue">Rigenera l\'elenco dei testi</a>').click(function(e) {
								e.preventDefault();
								$('#wpTextbox1').val(newText);
								$('#wpSummary').val('Gadget PopolaTestiAutore: ricreato l\'elenco dei testi');
							}));
							
							if (isEmpty) {
								//disabilito la compilazione automatica
								//$('#wpTextbox1').val(newText);
								//$('#wpSummary').val('Gadget PopolaTestiAutore: inserito l\'elenco dei testi');
							}
							else {
								if (traduzioni.length) {
									traduzioniMancanti = [];
									for (i = 0; i < traduzioni.length; i++) {
										//cerca nel testo attuale
										re = new RegExp('\\{\\{\\s*[Tt]esto\\s*\\|\\s*' + traduzioni[i].replace(/[-[\]{}()*+?.,\\/^$|#\s]/g, "\\$&") + '\\s*[\\||\\}\\}]');
										if (!re.test(currentText)) {
											console.log('Non trovato: ' + traduzioni[i]);
											traduzioniMancanti.push(traduzioni[i]);
										}
									}
									if (traduzioniMancanti.length) {
										$('#popolaTestiAutoreLink').after($('<a id="aggiungiTraduzioniLink" href="#" class="btn blue">Aggiungi traduzioni mancanti (' + traduzioniMancanti.length + ')</a>').click(function(e) {
											e.preventDefault();
											
											//leggo di nuovo il currentText che può essere cambiato nel frattempo (concorrenza con editFormAutore)
											currentText = $('#wpTextbox1').val();
											newText = '', traduzioniMancantiText = '';
											for (i = 0; i < traduzioniMancanti.length; i++) {
												traduzioniMancantiText += '* {{Testo|' + traduzioniMancanti[i] + '}}\n';
											}
											
											sezioneNotePos = currentText.search(/\{\{Sezione note\}\}/);
											if (sezioneNotePos != -1) {
												newText = currentText.substr(0, sezioneNotePos) + '\n' + traduzioniMancantiText + '\n' + currentText.substr(sezioneNotePos);
											} else {
												newText = currentText + '\n' + traduzioniMancantiText;
											}
											$('#wpTextbox1').val(newText);
											$('#wpSummary').val('Gadget PopolaTestiAutore: aggiunti testi mancanti');
										}));
										$('#popolaTestiAutoreLink').after(' ');
									}
								}
								if (testi.length) {
									testiMancanti = [];
									for (i = 0; i < testi.length; i++) {
										//cerca nel testo attuale
										re = new RegExp('\\{\\{\\s*[Tt]esto\\s*\\|\\s*' + testi[i].replace(/[-[\]{}()*+?.,\\/^$|#\s]/g, "\\$&") + '\\s*[\\||\\}\\}]');
										if (!re.test(currentText)) {
											console.log('Non trovato: ' + testi[i]);
											testiMancanti.push(testi[i]);
										}
									}
									if (testiMancanti.length) {
										$('#popolaTestiAutoreLink').after($('<a id="aggiungiTestiLink" href="#" class="btn blue">Aggiungi testi mancanti (' + testiMancanti.length + ')</a>').click(function(e) {
											e.preventDefault();
											
											//leggo di nuovo il currentText che può essere cambiato nel frattempo (concorrenza con editFormAutore)
											currentText = $('#wpTextbox1').val();
											newText = '', testiMancantiText = '';
											for (i = 0; i < testiMancanti.length; i++) {
												testiMancantiText += '* {{Testo|' + testiMancanti[i] + '}}\n';
											}
											
											traduzioniPos = currentText.search(/\s*=+\s*Traduzioni\s*=+/);
											if (traduzioniPos != -1) {
												newText = currentText.substr(0, traduzioniPos) + '\n' + testiMancantiText + '\n' + currentText.substr(traduzioniPos);
											} else {
												sezioneNotePos = currentText.search(/\{\{Sezione note\}\}/);
												if (sezioneNotePos != -1) {
													newText = currentText.substr(0, sezioneNotePos) + '\n' + testiMancantiText + '\n' + currentText.substr(sezioneNotePos);
												} else {
													newText = currentText + '\n' + testiMancantiText;
												}
											}
											$('#wpTextbox1').val(newText);
											$('#wpSummary').val('Gadget PopolaTestiAutore: aggiunti testi mancanti');
										}));
										$('#popolaTestiAutoreLink').after(' ');
									}
								}
							}
						}
					});
				});
			});
		});
	}
};
module.exports = PopolaTestiAutore;