Bikarhêner:Balyozxane/skrîpt/js/kat-ceke.js

Ji Wîkîpediya, ensîklopediya azad.

Zanibe: Piştî weşandinê, ji bo dîtina guhartinan dibe ku hewce be "cache"ya geroka xwe paqij bikî.

  • Firefox / Safari: Pê li Shift û Reload bike an jî Ctrl-F5 an Ctrl-R bike (ji bo Mac: ⌘-R)
  • Google Chrome: Pê li Ctrl-Shift-R (ji bo Mac: ⌘-Shift-R) bike
  • Internet Explorer / Edge: Pê li Ctrl û Refresh bike, an jî Ctrl-F5 bike
  • Opera: Pê li Ctrl-F5 bike.
//<nowiki>
// Kategoriyên Wîkîprojeyan li gorî tabloya statîstîkê çêdike. 
$(document).ready(function() {

	mw.loader.using("mediawiki.api").done(function() {
		// Define the generateCategoryName function
		function generateCategoryName(muhimi, sinif, mijar, proje) {
			let category = "";

			if (sinif !== '' && muhimi !== '') {
				if (sinif === "Nesinifandî") {
					category = "Gotarên" + category + " " + (mijar.length > 0 ? mijar + " yên nehatine sinifandin û muhîmiya " : "nehatine sinifandin û muhîmiya ") + muhimi;
				} else {
					category = "Gotarên" + category + " " + (mijar.length > 0 ? mijar + " " : "") + "bi sinifa " + sinif + " û muhîmiya " + muhimi;
				}
			} else if (muhimi !== '') {
				category = "Gotarên" + category + " " + (mijar.length > 0 ? mijar + " " : "") + "bi muhîmiya " + muhimi;
			} else if (sinif !== '') {
				if (sinif === "Nesinifandî") {
					category = "Gotarên" + category + " " + (mijar.length > 0 ? mijar + " yên nehatine sinifandin" : "nehatine sinifandin");
				} else {
					category = "Gotarên" + category + " " + (mijar.length > 0 ? mijar + " " : "") + "bi sinifa " + sinif;
				}
			} else if (proje.length > 0) {
				category = "Hemû rûpelên Wîkîproje " + proje;
			}

			return "Kategorî:" + category;
		}

		function createPage(page_title, content, proje) {

			const api = new mw.Api();
			const summary = `Ji bo [[Wîkîpediya:Wîkîproje ${proje}|Wîkîproje ${proje}]] kategorî tê çêkirin ([[Bikarhêner:Balyozxane/skrîpt/js/kat-ceke.js|kat-ceke.js]])`;

			api.postWithToken('csrf', {
				action: 'edit',
				title: page_title,
				text: content,
				summary: summary,
				createonly: true
			}).then(data => {
				mw.notify(`${page_title} hat çêkirin.`, {
					type: 'success'
				});
			}).catch(err => {
				mw.notify(`Xeletî di çêkirina rûpelê de çêbû: ${err}`, {
					type: 'error'
				});
				console.log(err);
			});
		}

		function determineTemplate(muhimi, sinif, mijar, proje) {
			let template;

			if (sinif !== '' && muhimi !== '') {
				template = "{{Wîkîproje otokat|proje=" + proje + "|mijar=" + mijar + "|muhîmî=" + muhimi + "|sinif=" + sinif + "}}";
			} else if (muhimi !== '') {
				template = "{{Kategorîkirina muhîmiyê|mijar=" + mijar + "|muhîmî=" + muhimi + "}}";
			} else if (sinif !== '') {
				template = "{{Kategorîkirina sinifê|proje=" + proje + "|mijar=" + mijar + "|sinif=" + sinif + "}}";
			} else if (sinif == '' && muhimi == '') {
				template = "{{Kategoriya çavdêriyê}}\n";
				template += "{{Cat more|Wîkîpediya:Wîkîproje " + proje + "}}\n";
				template += "{{CatAutoTOC}}\n\n";
				template += "[[Kategorî:Gotarên Wîkîproje " + proje + "|Hemû]]";
			}

			return template;
		}

		// Function to handle button click
		function createCategories() {
			// Find all div elements with class "Wikiproje-ceke"
			var wikiprojeCekeDivs = document.querySelectorAll('.Wikiproje-ceke');
			if (wikiprojeCekeDivs.length > 0) { // Check if any elements are found
				// Find the table
				var table = document.querySelector('.ratingstable.wikitable.plainlinks');

				// Find or create the button
				var button = table.querySelector('.create-categories-button');
				if (!button) {
					button = document.createElement('button');
					button.textContent = 'Create Categories';
					button.className = 'create-categories-button';

					// Set button style for positioning
					button.style.position = 'absolute';
					button.style.top = '0';
					button.style.left = '0';

					table.style.position = 'relative'; // Ensure table is positioned relative for absolute positioning of the button

					table.appendChild(button);

					// Add click event listener to the button
					button.addEventListener('click', function() {
						// Iterate over each found div
						wikiprojeCekeDivs.forEach(function(wikiprojeCekeDiv, index) { // Add index parameter for setTimeout
							// Find child div with class that holds the data
							var dataDiv = wikiprojeCekeDiv.querySelector('div[class^="proje-"]');

							// If data div exists, extract class and data
							if (dataDiv) {
								var classData = dataDiv.getAttribute('class');
								var classes = classData.split(' ');
								var muhimi = "",
									sinif = "",
									mijar = "",
									proje = "";
								classes.forEach(function(className) {
									if (className.startsWith('muhimi-')) {
										muhimi = className.substring(7).replace('_', ' ');
									} else if (className.startsWith('sinif-')) {
										sinif = className.substring(6);
									} else if (className.startsWith('mijar-')) {
										mijar = className.substring(6);
									} else if (className.startsWith('proje-')) {
										proje = className.substring(6);
									}
								});
								var categoryTitle = generateCategoryName(muhimi, sinif, mijar, proje);
								var template = determineTemplate(muhimi, sinif, mijar, proje);
								console.log(categoryTitle + "\n" + template);

								// Add delay for creating the next category
								setTimeout(function() {
									createPage(categoryTitle, template, proje);
								}, index * 2000); // Multiply index by 2000 milliseconds (2 second) for each iteration
							}
						});
					});
				}
			}
		}

		// Call the function to attach the button
		createCategories();

	});
});
//</nowiki>