User:Qwerfjkl/scripts/unusedCategories.js

In today's world, User:Qwerfjkl/scripts/unusedCategories.js is a topic of great relevance and debate. With the advancement of technology and globalization, User:Qwerfjkl/scripts/unusedCategories.js has become a key element in our society, impacting different aspects of daily life. From economics to politics, User:Qwerfjkl/scripts/unusedCategories.js has generated discussions and reflections in all areas. This is why it is essential to thoroughly analyze this phenomenon, understand its implications and seek solutions to the challenges it presents. In this article, we will explore different perspectives on User:Qwerfjkl/scripts/unusedCategories.js, from its origin to its impact today, with the aim of offering a complete and enriching overview of this very relevant topic.
// Forked from ]
// <nowiki>
function tagCategory()
{
	if ($("#mw-content-text").length > 0)
	{
		var $nodeListC = $('a'),
		pdneC = ' (page does not exist)';
		if ($nodeListC !== null)
		{
			var i;
			for (i = 0; i < $nodeListC.length; i++)
			{
				var $link = $($nodeListC);
				var newLinkStringC = "|";
				var redlinkC = false;
				var newLinkHrefC = $link.title;
				if(newLinkHrefC.includes(pdneC))
				{
					redlinkC = true;
					newLinkHrefC = newLinkHrefC.replace(pdneC, "");
				}
				var newLinkIdC = "TCsubmit" + i;
				if (redlinkC === false)
				{
				newLinkStringC += '<a style="font-size:x-small; display:inline-block;" title="Tag page for deletion." noPopup=1 id="' + newLinkIdC + 'Delete">Delete</a>|';
				newLinkStringC += '<a style="font-size:x-small; display:inline-block;" title="Tag page as empty category." noPopup=1 id="' + newLinkIdC + 'Tag">Tag</a>';
				$($link).after(newLinkStringC);
				$("#" + newLinkIdC + "Delete").click({"page":newLinkHrefC}, deleteSubmitC);
				$("#" + newLinkIdC + "Tag").click({"page":newLinkHrefC}, tagSubmitC);
				}
			}
		}
	}
}


function deleteSubmitC(event) {
	var page = event.data.page;
	window.UnusedCategoriesPage = page; // Page will be converted to ID later
	var params = {
			action: 'query',
			format: 'json',
			titles: page,
			prop: 'categoryinfo'
		},
		api = new mw.Api();
	
	api.get( params ).done( function ( data ) {
		var pages = data.query.pages,
			page;
		for ( page in pages ) {
			window.numPagesInCategoryC = pages.categoryinfo.pages + pages.categoryinfo.subcats;
			if (window.numPagesInCategoryC < 1) { // pages in cat < 1, else skip
				checkIfTaggedForDeletion( window.UnusedCategoriesPage, page );
			}
			else {
				mw.notify("Too many pages in category ("+window.numPagesInCategoryC+"), aborted.");
			}	
			}
	} );
	
}
function checkIfTaggedForDeletion ( page, pageID ) {
	window.abortIfCatEmpty = false;
	var params = {
			action: 'query',
			format: 'json',
			prop: 'categories',
			titles: page
		},
		api = new mw.Api();
	
	api.get( params ).done( function ( data ) {
		var pages = data.query.pages,
			p;
			for ( p in pages ) {
				if (pages.categories != null) { // has cats
				//alert(pages.categories)
				pages.categories.forEach( function ( cat ) {
					if ('Category:Empty categories awaiting deletion' === cat.title || 'Category:All categories for discussion' === cat.title || 'Category:Candidates for speedy deletion' === cat.title) {
						// Abort, already tagged
						window.abortIfCatEmpty = true;
						mw.notify('Aborted, "'+page+'" is in a "category discussion" category.');
					}
					
					
					
				} );
			}
		}
		if (window.abortIfCatEmpty === false) {
			checkPageWikitext2( window.UnusedCategoriesPage, pageID );
		}
		//else { // Empty page - no categories
		//	checkPageWikitext2( window.UnusedCategoriesPage, pageID );
		//}
	} );
}

function checkPageWikitext2(page, pageID) {
	    var send_req = {
		    action: 'expandtemplates',
		    text: "{{:"+page+"}}",
		    titles: page,
		    prop: 'wikitext',
		};
		$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', send_req, function( response ) {
		    window.textToCheckForCategories = $( response ).find( '*' ).text();
		    if (window.textToCheckForCategories.match(/__EXPECTUNUSEDCATEGORY__/) === null) { // No match
    			getPageDataC(pageID);

			}
		    else {
		    	mw.notify("'"+page+"' contains __EXPECTUNUSEDCATEGORY__, aborted.");
    		}
	    
});
}
function getPageDataC( pageID ) {
	// Get page creator - taken from ]
	var PageCreator = {
	        getData: function (callback) {
            var that = this;

            this.api.get({
                action: "query",
                prop: "revisions",
                titles: window.UnusedCategoriesPage,
                rvprop: "ids|timestamp|user|userid",
                rvlimit: "1",
                rvdir: "newer",
                format: "json"
            }).done(function ($data) {
                if (!$data.error) {
                    callback(that, $data);
                }
            });
        },
        /**
         * @method handleData
         * @param {object} that
         * @param {json} $result
         * @returns {void}
         */
        handleData: function (that, $result) {
        	//var pageID = getID( page )
        	//mw.notify(pageID)
            var $data = $result.query.pages.revisions;
            submitDeleteEditsC( $data.user, pageID );
        },
        init: function () {
        	this.api = new mw.Api();
        	this.getData(this.handleData);
        }
        
	};
	PageCreator.init();
}
function submitDeleteEditsC (pageCreator, page) {
	// Check
	if ( !confirm( "Are you sure? (Category contains "+window.numPagesInCategoryC+" pages.)")){
		mw.notify("Aborted.");
		return;
	}
	// Tag for deletion
	var requestDataC =
	{
		minor: false,
		prependtext: "{{db-catempty|help=off}}\n",
		summary: "Requesting speedy deletion (]) (via ])"
	};
	new mw.Api().edit(window.UnusedCategoriesPage, function() {
		return requestDataC;
	}).done(function(data) {
		
			if (data && data.result && data.result === 'Success')
			{
				mw.notify("'"+window.UnusedCategoriesPage+"' tagged for deletion!");
			}
			else
			{
				alert('There was an error tagging "'+window.UnusedCategoriesPage+'" for deletion. Code: ' + data);
			}
	}).fail(function(data) {
			alert('There was an error using AJAX to tag "'+window.UnusedCategoriesPage+'" for deletion. Error: ' + data);
		
	});
	// Notify talk

	var requestDataC2 =
	{
		minor: false,
		appendtext: "\n{{subst:db-catempty-notice|"+window.UnusedCategoriesPage+"}} ~~~~",
		summary: "Notifying user (]) (via ])"
	};
	//mw.notify("Created by: "+pageCreator)
	//mw.notify("Tagging: "+window.UnusedCategoriesPage)
	//mw.notify("Page ID: "+page)
	if (pageCreator.match(/bot/i) === null) { // page creator isn't a bot - no match for 'bot' in username
       if (pageCreator != 'Starcheerspeaksnewslostwars') {
	new mw.Api().edit('User_talk:'+pageCreator, function() { 
		
		return requestDataC2;
	}).done(function(data) {
		
			if (data && data.result && data.result === 'Success')
			{
				mw.notify("'User:"+pageCreator+"' notified.");
			}
			else
			{
				alert('There was an error notifying "'+pageCreator+'". Code: ' + data);
			}
	}).fail(function(data) {
			alert('There was an error using AJAX to notify the user "'+pageCreator+'". Error: ' + data);
		
	});
	}
		else {
			mw.notify('User:"'+pageCreator+'" was not notified, per request.');
		}
	}
	else {
		mw.notify("'User:"+pageCreator+"' is a bot, skipping.");
	}

}
function getID( page ) {
    mw.notify(page);
	var params = {
		action: 'query',
		format: 'json',
		titles: window.UnusedCategoriesPage,
		prop: 'info',
	},
	api = new mw.Api();

	api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		p;
	for ( p in pages ) {
		mw.notify(pages.pageid);
		return pages.pageid;
	}
} );
}


function tagSubmitC(event){
	var page = event.data.page;
	checkPageWikitext(page);

}
function checkPageWikitext(page) {
	    var send_req = {
		    action: 'expandtemplates',
		    text: "{{:"+page+"}}",
		    titles: page,
		    prop: 'wikitext',
		};
		$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', send_req, function( response ) {
		    window.textToCheckForCategories = $( response ).find( '*' ).text();
    		tagSubmitC2(page);
	    
});
}
function tagSubmitC2(page) {
	if (window.textToCheckForCategories.match(/__EXPECTUNUSEDCATEGORY__/) != null) { // There's a match
    	mw.notify( page+" contains __EXPECTUNUSEDCATEGORY__, aborted.");
	    return;
	}
	if ( !confirm( "Are you sure?")){
		mw.notify("Aborted.");
		return;
	}
	var requestDataC =
	{
		minor: false,
		prependtext: "{{Possibly empty category}}\n",
		summary: "+{{]}} (via ])"
	};
	new mw.Api().edit(page, function() {
		return requestDataC;
	}).done(function(data) {
		
			if (data && data.result && data.result === 'Success')
			{
				mw.notify(page+" tagged with {{Possibly empty category}}!");
			}
			else
			{
				alert('There was an error tagging "'+page+'". Code: ' + data);
			}
	}).fail(function(data) {
			alert('There was an error using AJAX to tag "'+page+'". Error: ' + data);
	});
}
if (mw.config.get("wgPageName") === "Wikipedia:Database_reports/Empty_categories" || mw.config.get("wgPageName") === "Special:UnusedCategories")
{
	mw.loader.using("mediawiki.api", function() 
	{
		// Taken from ] to highlight categories tagged for deletion
mw.loader.getScript('//en.wikipedia.org/w/index.php?title=User:Anomie/linkclassifier.js&action=raw&ctype=text/javascript')
.then( function () { 
		// Linkback: ]
		importStylesheet('User:Anomie/linkclassifier.css'); // Linkback: ]
        // needs delay - wait until class applied (else does nothing. TODO)
		$('a.deletion').parent().remove(); // hide tagged for deletion
        })
		tagCategory();
	});
}
// </nowiki>