MediaWiki:Gadget-wikiproject-classify.js

De la Wikipedia, enciclopedia liberă

Notă: După salvare, trebuie să treceți peste cache-ul browser-ului pentru a vedea modificările.

  • Mozilla, Google Chrome, Safari, Konqueror: țineți apăsat Shift în timp ce apăsați butonul Reload (sau apăsați Ctrl-Shift-R).
  • Internet Explorer: apăsați Ctrl-F5.
  • Opera: apăsați F5.
/*
 * Acest script funcționează ca gadget; îl puteți activa de la [[Special:Preferințe]]
 * în tabul ''Gadgeturi'', la opțiunea "Adaugă o opțiune care facilitează evaluarea calității unui articol și includerea în wikiproiecte."
 */
( function( DOM_d ) {
	$( DOM_d ).ready( function() {
		var saveModel = function(model, talkData, dialogWindow) {
			var saveableProjs = [];
			for (var projIdx = 0; projIdx < model.projects.length; projIdx++) {
				if (model.projects[projIdx] && model.projects[projIdx].name && model.projects[projIdx].name.trim() !== '') {
					saveableProjs.push(model.projects[projIdx]);
				}
			}
			if (saveableProjs.length === 0) {
				Morebits.status.error('Error', 'Nu s-a specificat niciun proiect');
				return;
			}
			var newTemplateString = '{{';
			if (saveableProjs.length === 1) {
				theProj = saveableProjs[0].name;
				theImportance = saveableProjs[0].importance;
				
				newTemplateString = newTemplateString + 'Proiect ' + theProj.trim();
				if (model.livingPerson) {
					newTemplateString = newTemplateString + '\n| living = yes';
				}
				newTemplateString = newTemplateString + '\n| clasament = ' + model.classification;
				if (theImportance && theImportance.trim() !== '') {
					newTemplateString = newTemplateString + '\n| importanță = ' + theImportance.trim();
				}
			} else {
				newTemplateString = newTemplateString + 'Proiecte multiple';
				if (model.livingPerson) {
					newTemplateString = newTemplateString + '\n| living = yes';
				}
				newTemplateString = newTemplateString + '\n| clasament = ' + model.classification;
				for (projIdx = 0; projIdx < saveableProjs.length; projIdx++) {
					newTemplateString = newTemplateString + '\n| proiect' + (1 + projIdx) + ' = ' + saveableProjs[projIdx].name;
					if (saveableProjs[projIdx].importance && saveableProjs[projIdx].importance.trim() !== '') {
						newTemplateString = newTemplateString + '\n| importanță' + (1 + projIdx) + ' = ' + saveableProjs[projIdx].importance;
					}
				}
			}
			newTemplateString = newTemplateString + '\n}}\n';
			
			var pageNamespace = mw.config.get('wgNamespaceNumber');
			var actualPageTitle = mw.config.get('wgPageName').replace('_', ' ');
			var talkPageTitle = 'Discuție' + (pageNamespace === 0 ? ':' : ' ') + actualPageTitle;
			var queryToGetTalkPage = {
					action: 'parse',
					prop: 'wikitext',
					page: talkPageTitle
				};
			new mw.Api().get(queryToGetTalkPage).then(
				function(promisedContent) {
					return Promise.resolve(promisedContent.parse.wikitext['*']);
				}, function() {
					return Promise.resolve('');
				}).then(function(talkContent) {
					var newTalkContent = '';
					if (talkContent && talkContent.length > 0) {
						var moreProjectsFinderRE = new RegExp("\\{\\{\\s*[Pp]roiecte multiple\\s*(\\|([^\\}]*))*\\}\\}", "g") ;
						var oneProjectFinderRE = new RegExp("\\{\\{\\s*(?:Wiki)?[Pp]ro[ij]ect ([^\\|\\}]+)(\\|([^\\}]*))*\\}\\}", "g");
						var moreProjectsFinderMatcher = moreProjectsFinderRE.exec(talkContent);
						var replacingMatcher = moreProjectsFinderMatcher ? moreProjectsFinderMatcher : oneProjectFinderRE.exec(talkContent);
						if (replacingMatcher) {
							var replaceStartIdx = replacingMatcher.index;
							var replacedPrefix = replaceStartIdx === 0 ? '' : talkContent.substring(0, replaceStartIdx);
							var replacedPostfix = talkContent.substring(replaceStartIdx + replacingMatcher[0].length);
							newTalkContent = replacedPrefix + newTemplateString + replacedPostfix;
						} else {
							newTalkContent = newTemplateString + talkContent;
						}
						if (model.livingPerson) {
							newTalkContent = newTalkContent.replace(new RegExp("\\{\\{\\s*[Bb]pv\\s*\\}\\}", "g"), '');
						}
					} else {
						newTalkContent = newTemplateString;
					}
					var queryToPostNewTalkContent = {
						action: 'edit',
						title: talkPageTitle,
						text: newTalkContent,
						summary: '[[WP:EVAL|Evaluare]] și atribuire la [[Proiect:Răsfoire|wikiproiecte]] (asistată de [[MediaWiki:Gadget-wikiproject-classify.js|script]])',
						token: mw.user.tokens.get('csrfToken')
					};
					new mw.Api().post(queryToPostNewTalkContent).then(function() {
						dialogWindow.close();
						location.reload();
					});
				}
			);
		};
		
		
		var addProjToForm = function(form, proj, idx, model) {
			if (!proj) {
				return;
			}
			var newFields = [];
			newFields.push(form.append({
				type: 'input',
				name: 'project' + (1 + idx),
				value: proj.name,
				label: 'Proiect' + (1 + idx),
				event: function(evt) {
					model.projects[idx].name = evt.target.value;
				}
			}));
			newFields.push(form.append({
				type: 'input',
				name: 'importance' + (1 + idx),
				value: proj.importance,
				label: 'Importanță' + (1 + idx),
				event: function(evt) {
					model.projects[idx].importance = evt.target.value;
				}
			}));
			return newFields;
		};
		
		
		var pageNamespace = mw.config.get('wgNamespaceNumber');
		if (pageNamespace === 0 || pageNamespace === 10) {
			var mainLink = mw.util.addPortletLink('p-cactions', '#', 'Clasificare', 'p-classify-wikiproject', 'Clasifică articolul');
			$( mainLink ).on('click', function () {
				var mainDialog = new Morebits.simpleWindow(600, 400);
				mainDialog.setScriptName('Classify');
				mainDialog.setTitle('Clasificarea articolului');
				
				var actualPageTitle = mw.config.get('wgPageName').replace('_', ' ');
				var talkPageTitle = 'Discuție' + (pageNamespace === 0 ? ':' : ' ') + actualPageTitle;
				var queryToGetTalkPage = {
					action: 'parse',
					prop: 'wikitext',
					page: talkPageTitle
				};
				new mw.Api().get(queryToGetTalkPage).then(
					function(promisedContent) {
						return Promise.resolve(promisedContent.parse.wikitext['*']);
					},
					function() {
						return Promise.resolve('');
					}).then(function(talkContent) {
						var projectsModel = {
							livingPerson: false,
							classification: null,
							projects: []
						};
						if (talkContent && talkContent.length > 0) {
							var moreProjectsFinderRE = new RegExp("\\{\\{\\s*[Pp]roiecte multiple\\s*(\\|([^\\}]*))*\\}\\}", "g");
							var oneProjectFinderRE = new RegExp("\\{\\{\\s*(?:[Ww]iki)?[Pp]ro[ij]ect ([^\\|\\}]+)([\\s\\S]*)\\}\\}", "g");
							var argsRE = new RegExp("\\|\\s*([^=\\|\\}]+)\\s*=\\s*([^\\|\\}]+)\\s*", "g");
							
							var moreProjectsFinderMatcher = moreProjectsFinderRE.exec(talkContent);
							var oneProjectFinderMatcher;
							if (moreProjectsFinderMatcher) {
								var moreProjectsArgString = moreProjectsFinderMatcher[1];
								var argsMatcher;
								while(argsMatcher = argsRE.exec(moreProjectsArgString)) {
									var projectIdxRE = new RegExp("proiect(\\d+)", "g");
									var importanceIdxRE = new RegExp("importanță(\\d+)", "g");
									if (argsMatcher[1].trim() === 'clasament') {
										projectsModel.classification = argsMatcher[2].trim();
									}
									if (argsMatcher[1].trim() === 'living' || argsMatcher[1].trim() === 'în viață') {
										projectsModel.livingPerson = (argsMatcher[2] && (argsMatcher[2].trim() === 'yes' || argsMatcher[2].trim() === 'da'));
									}
									var projectIdxMatcher = projectIdxRE.exec(argsMatcher[1]);
									if (projectIdxMatcher) {
										var projectIdx = parseInt(projectIdxMatcher[1]);
										if (!projectsModel.projects[projectIdx - 1]) {
											projectsModel.projects[projectIdx - 1] = {};
										}
										projectsModel.projects[projectIdx - 1].name = argsMatcher[2].trim();
									}
									var importanceIdxMatcher = importanceIdxRE.exec(argsMatcher[1]);
									if (importanceIdxMatcher) {
										var importanceIdx = parseInt(importanceIdxMatcher[1]);
										if (!projectsModel.projects[importanceIdx - 1]) {
											projectsModel.projects[importanceIdx - 1] = {};
										}
										projectsModel.projects[importanceIdx - 1].importance = argsMatcher[2].trim();
									}
								}
							} else if (oneProjectFinderMatcher = oneProjectFinderRE.exec(talkContent)) {
								var oneProjectArgString = oneProjectFinderMatcher[2].trim();
								var argsMatcher;
								var proj = { name: oneProjectFinderMatcher[1].trim() };
								while(argsMatcher = argsRE.exec(oneProjectArgString)) {
									if (argsMatcher[1].trim() === 'clasament') {
										projectsModel.classification = argsMatcher[2].trim();
									}
									if (argsMatcher[1].trim() === 'living' || argsMatcher[1].trim() === 'în viață') {
										projectsModel.livingPerson = (argsMatcher[2].trim() === 'yes' || argsMatcher[2].trim() === 'da');
									}
									if (argsMatcher[1].trim() == 'importanță') {
										proj.importance = argsMatcher[2].trim();
									}
								}
								projectsModel.projects.push(proj);
							}
						}
						
						var form = new Morebits.quickForm( function(evt) {
							saveModel(projectsModel, talkContent, mainDialog)
						} );
						form.append({
							type: 'checkbox',
							list: [
								{
									label: 'Persoană în viață',
									value: 'living',
									name: 'living',
									checked: projectsModel.livingPerson
								}
							],
							event: function(evt) {
								projectsModel.livingPerson = evt.target.checked;
							}
						});
						var classificationSelect = form.append({
							type: 'select',
							name: 'classification',
							label: 'Clasament',
							event: function (evt) {
								projectsModel.classification = evt.target.value;
							}
						});
						classificationSelect.append({
							type: 'option',
							value: 'AC',
							label: 'AC',
							selected: projectsModel.classification && (projectsModel.classification.toLowerCase() === 'ac' || projectsModel.classification.toLowerCase() === 'fa')
						});
						classificationSelect.append({
							type: 'option',
							value: 'A',
							label: 'A',
							selected: projectsModel.classification && projectsModel.classification.toLowerCase() === 'a'
						});
						classificationSelect.append({
							type: 'option',
							value: 'AB',
							label: 'AB',
							selected: projectsModel.classification && (projectsModel.classification.toLowerCase() === 'ga' || projectsModel.classification.toLowerCase() === 'ab')
						});
						classificationSelect.append({
							type: 'option',
							value: 'B',
							label: 'B',
							selected: projectsModel.classification && projectsModel.classification.toLowerCase() === 'b'
						});
						classificationSelect.append({
							type: 'option',
							value: 'C',
							label: 'C',
							selected: projectsModel.classification && projectsModel.classification.toLowerCase() === 'c'
						});
						classificationSelect.append({
							type: 'option',
							value: 'start',
							label: 'Început',
							selected: projectsModel.classification && (projectsModel.classification.toLowerCase() === 'start' || projectsModel.classification.toLowerCase() === 'început')
						});
						classificationSelect.append({
							type: 'option',
							value: 'ciot',
							label: 'Ciot',
							selected: projectsModel.classification && (projectsModel.classification.toLowerCase() === 'ciot' || projectsModel.classification.toLowerCase() === 'stub')
						});
						classificationSelect.append({
							type: 'option',
							value: 'listă',
							label: 'Listă',
							selected: projectsModel.classification && (projectsModel.classification.toLowerCase() === 'listă' || projectsModel.classification.toLowerCase() === 'list')
						});
						classificationSelect.append({
							type: 'option',
							value: 'lc',
							label: 'Listă de calitate',
							selected: projectsModel.classification && (projectsModel.classification.toLowerCase() === 'lc' || projectsModel.classification.toLowerCase() === 'fl')
						});
						classificationSelect.append({
							type: 'option',
							value: 'format',
							label: 'Format',
							selected: projectsModel.classification && (projectsModel.classification.toLowerCase() === 'format' || pageNamespace === 10)
						});
						classificationSelect.append({
							type: 'option',
							value: 'viitor',
							label: 'Viitor',
							selected: projectsModel.classification && (projectsModel.classification.toLowerCase() === 'viitor' || projectsModel.classification.toLowerCase() === 'future')
						});

						form.append({
							type: 'button',
							label: 'Adaugă proiect',
							event: function(evt) {
								var newFields = addProjToForm(form, {}, projectsModel.projects.length, projectsModel);
								projectsModel.projects.push({});
								newFields.forEach(function(eachField) {
									mainDialog.buttons = [];
									mainDialog.addContent(eachField.render());
								});
							}
						});
						
						projectsModel.projects.forEach(function(eachProj, eachIdx) {
							addProjToForm(form, eachProj, eachIdx, projectsModel);
						});
						form.append({
							type: 'submit',
							label: 'Salvează clasificarea'
						});
						mainDialog.setContent(form.render());
						mainDialog.display();
					});
			});
		}
	} );
} ( document ) );