User:Ricordisamoa/XMLNSense.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
function getData(aifrom){
	if(typeof(aifrom)=="undefined") var aifrom="";
	$.getJSON(
		mw.util.wikiScript("api"),
		{
			action:"query",
			format:"json",
			rawcontinue:"",
			list:"allimages",
			aiprop:"mime|url",// request MIME type + url
			aifrom:aifrom,
			ailimit:"max"
		},
		function(data){
			var d=$.grep(data.query.allimages,function(e){
				return e.mime==="image/svg+xml";
			});
			//console.log($.map(d,function(e){return e.name;}));
			if(data["query-continue"]&&data["query-continue"].allimages&&data["query-continue"].allimages.aicontinue){
				//console.log(data["query-continue"].allimages.aicontinue);
				getData(data["query-continue"].allimages.aicontinue);
			}
			$.each(d,function(i,e){
				$.get(e.url,
				function(code){
					var svg=$(code).find("svg").first();// the root element
					if(svg.length===0) svg=$(code).find("svg:svg").first();
					if(
					(!svg.attr("xmlns")&&!svg.attr("xmlns:svg"))
					||(typeof(svg.attr("xmlns"))=="undefined"&&typeof(svg.attr("xmlns:svg"))=="undefined")
					||(svg.attr("xmlns").length===0&&svg.attr("xmlns:svg").length===0)
					||(svg.attr("xmlns")===""&&svg.attr("xmlns:svg")==="")
					){	// something not OK
						console.warn("File:"+e.name+" does not have a proper XMLNS declaration!");
						$.ajax({
							url:mw.util.wikiScript(),
							data:{
								title:"User:SamoaBot/NoXMLNS/log",
								action:"raw"
							},
							cache:false,
							dataType:"text"
						})
						.done(function(log){
							if(log.indexOf("File:"+e.name)!=-1){
								console.log("File:"+e.name+" already logged, not going to do.");
								return;
							}
							else $.post(
								mw.util.wikiScript("api"),
								{
									action:"edit",
									appendtext:"\n*[[:File:"+e.name+"|"+e.name+"]]",
									title:"User:SamoaBot/NoXMLNS/log",
									summary:"added [[File:"+e.name+"|"+e.name+"]]",
									minor:true,
									bot:true,
									token:mw.user.tokens.get("csrfToken")
								},
								function(data){
									if(data&&data.error&&data.error.info) console.warn("Error while logging!");
									else console.log("File:"+e.name+" successfully logged!");
								}
							);
						});
					}
					else console.log("   --> File:"+e.name+"    |    "+svg.attr("xmlns"));
				},
				"xml"
				);
			});
		}
	);
}