Jump to content

User:A930913/vada/plugin/ufd.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<nowiki>
if(typeof window!="undefined"){
	//inside Vada DOM
	(function(){
		var a930913_ufd={
			about:{
				name:"A930913's Unused File Director",
				description:"Given a file redirect, searches for all uses, replaces them, then tags the redirect for deletion.",
				namespace:"a930913_ufd",
				userscript:"User:A930913/vada/plugin/ufd.js",
			}
		}
		window.vada.loadPlugin(a930913_ufd);
	})();
} else {
	//inside Vada process
	vada.plugins.a930913_ufd={};
	vada.plugins.a930913_ufd.page=function(params){
		var query={};
		query.action="query";
		query.list="imageusage";
		query.iufilterredir="nonredirects";
		query.iutitle=params.page;
		var data=vada.api(query);
		if(data.query.imageusage.length===0){
			var bl=vada.api({"action":"query","list":"backlinks","bltitle":params.page}).query.backlinks,tag=true;
			for(var i in bl){if(bl[i].title.search("sandbox")==-1){tag=false;break;}}
			if(tag){
				var agepi=vada.api({"action":"query","prop":"revisions","titles":params.page,"rvprop":"timestamp","rvdir":"newer","rvlimit":"1"});
				for(var pageid in agepi.query.pages)break; 
				if(Date.now()-Date.parse(agepi.query.pages[pageid].revisions[0].timestamp)<2629744000){
					//tag for deletion
					var p=new vada.Page(params.page);
					vada.dom.summarybox.setText("Tagging retitled media for deletion (G6)");
					vada.dom.minorbox.uncheck();
				} else {
					//Older than a month, there may be external links pointing to the redirect
					vada.plugins.a930913_ufd.removenext(params.page);
				}
				try{
					if(p.text.substr(0,8)=="{{"+"db-g6|")throw("PageAlreadyTagged");
					vada.dom.load(p,"{{"+"db-g6|reason=G6 - Retitled media with no significant incoming links. (Please check for Commons referals)}}\n"+p.text);
				}catch(e){
					if(e=="PageDoesNotExist"||e=="PageAlreadyTagged"){
						//page already deleted
						vada.plugins.a930913_ufd.removenext(params.page);
					} else {
						throw(e);
					}
			}
			} else {
				vada.plugins.a930913_ufd.removenext(params.page);
			}
		} else {
			var nextms=null;
			for(var i in data.query.imageusage){
				if(data.query.imageusage[i].ns===0){
					nextms=data.query.imageusage[i].title;
					break;
				}
			}
			if(nextms){
				//replace redirects on this page
				var p=new vada.Page(nextms);
				var realpage=new vada.Page(params.page).text.match(/#REDIRECT \[\[(.+?)\]\]/)[1];
				vada.dom.summarybox.setText("Fixing redirect ("+params.page+" → [["+realpage+"]])");
				vada.dom.minorbox.check();
				//vada.dom.load(p,p.text.replace(new RegExp(params.page.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"),"g"),realpage));
				vada.dom.load(p,p.text.replace(new RegExp(params.page.split(":")[1].replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1").replace(/ /g,"[\ _]"),"g"),realpage.split(":")[1]));
			} else {
				vada.plugins.a930913_ufd.removenext(params.page);
			}
		}
	}
	vada.plugins.a930913_ufd.queueclick=function(params){
		vada.plugins.a930913_ufd.page({page:params.title});
		vada.dom.queue.splice(vada.dom.queue.indexOf(new vada.Page(params.title)),1);
		vada.dom.queue.redraw();
	}
	vada.plugins.a930913_ufd.saveclick=function(params){
		vada.dom.queue.next();
	}
	vada.plugins.a930913_ufd.removenext=function(page){
		vada.dom.queue.splice(vada.dom.queue.indexOf(new vada.Page(page)),1);
		vada.dom.queue.redraw();
		vada.dom.queue.next();
	}
	vada.plugins.a930913_ufd.start=function(){
		vada.events.trigger("plugins.stop",{});
		vada.events.addListener("plugins.stop",vada.plugins.a930913_ufd.stop);
		vada.dom.menu.new([vada.dom.menu.buttons.nextq,vada.dom.menu.buttons.save]);
		vada.dom.queue.builders.show();
		vada.events.addListener("pagename",vada.plugins.a930913_ufd.page);
		vada.events.addListener("queue.page.selected",vada.plugins.a930913_ufd.queueclick);
		vada.events.addListener("save.click",vada.plugins.a930913_ufd.saveclick);
		vada.plugins.a930913_ufd.running=true;
	}
	vada.plugins.a930913_ufd.stop=function(){
		vada.plugins.a930913_ufd.running=false;
		vada.dom.queue.builders.hide();
		vada.events.removeListener("pagename",vada.plugins.a930913_ufd.page);
		vada.events.removeListener("queue.page.selected",vada.plugins.a930913_ufd.queueclick);
		vada.events.removeListener("save.click",vada.plugins.a930913_ufd.saveclick);
	}
	vada.plugins.a930913_ufd.running=false;
	vada.plugins.a930913_ufd.button=(function(pl){
		var button=new vada.dom.menu.Button("a930913_ufd_start");
		button.text="A930913's Unused File Director";
		button.click=pl.start;
		vada.dom.menu.buttons.vada.children.push(button);
		vada.dom.menu.redraw();
		return button;
	})(vada.plugins.a930913_ufd);
}
//</nowiki>