User:MusikAnimal/MoreMenu-core-sandbox.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.
(function(){
	var userName;
	if(wgCanonicalSpecialPageName === 'Contributions') {
		userName = encodeURIComponent(wgRelevantUserName);
	} else {
		userName = encodeURIComponent(wgTitle.split('/')[0].replace(/ /g,'_'));
	}
	userName = userName.replace(/\'/g,"%27");
	var admin = wgUserGroups.indexOf('sysop') > -1;
	
	if(wgNamespaceNumber === 2 || wgNamespaceNumber === 3 || wgCanonicalSpecialPageName == 'Contributions') addUserMenu(userName);
	if(wgNamespaceNumber >= 0) addPageMenu();
	
	addListeners();
	
	function addUserMenu(userName) {
		menuList = {
			'User logs' : {
				'All logs' : {
					url : wgScript+'?title=Special:Log&action=view&user='+userName
				},
				'Block log' : {
					url : wgScript+'?title=Special:Log&action=view&type=block&user='+userName,
					rights : ['sysop']
				},
				'Deletion log' : {
					url : wgScript+'?title=Special:Log&action=view&type=delete&user='+userName,
					rights : ['sysop']
				},
				'Filter log' : {
					url : wgScript+'?title=Special:AbuseLog&wpSearchUser='+userName
				},
				'Move log' : {
					url : wgScript+'?title=Special:Log&action=view&type=move&user='+userName,
					rights : ['confirmed','autoconfirmed']
				},
				'Patrol log' : {
					url : wgScript+'?title=Special:Log&action=view&type=patrol&user='+userName,
					rights : ['confirmed','autoconfirmed']
				},
				'Protection log' : {
					url : wgScript+'?title=Special:Log&action=view&type=protect&user='+userName,
					rights : ['sysop']
				},
				'Review log' : {
					url : wgScript+'?title=Special:Log&action=view&type=review&user='+userName,
					rights : ['reviewer','sysop']
				},
				'Thanks log' : {
					url : wgScript+'?title=Special:Log&action=view&type=thanks&user='+userName,
					rights : ['user']
				},
				'Upload log' : {
					url : wgScript+'?title=Special:Log&action=view&type=upload&user='+userName,
					rights : ['confirmed','autoconfirmed']
				},
				'User creation log' : {
					url : wgScript+'?title=Special:Log&action=view&type=newusers&user='+userName,
					rights : ['accountcreator','sysop']
				},
				'User rights log' : {
					url : wgScript+'?title=Special:Log&action=view&type=rights&user='+userName,
					rights : ['epcoordinator','sysop']
				}
			},
			'RfXs' : {
				'RfAs' : {
					url : wgScript+'?title=Special:PrefixIndex/Wikipedia:Requests_for_adminship/'+userName+'&action=view',
					title : 'Requests for Adminship'
				},
				'RfBs' : {
					url : wgScript+'?title=Special:PrefixIndex/Wikipedia:Requests_for_bureaucratship/'+userName+'&action=view',
					title : 'Requests for Bureaucratship'
				},
				'RfAr' : {
					url : wgScript+'?title=Wikipedia:Requests_for_arbitration/'+userName+'&action=view',
					title : 'Requests for Arbitration'
				},
				'RfC' : {
					url : wgScript+'?title=Wikipedia:Requests_for_comment/'+userName+'&action=view',
					title : 'Requests for Comment'
				},
				'RfCU' : {
					url : wgScript+'?title=Wikipedia:Requests_for_checkuser/Case/'+userName+'&action=view',
					title : 'Request for Checkuser'
				},
				'SPI' : {
					url : wgScript+'?title=Wikipedia:Sockpuppet_investigations/'+userName+'&action=view',
					title : 'Sockpuppet investigations (as the sockmaster)'
				}
			},
			'Blocks' : {
				'Block user' : {
					url : wgScript+'?title=Special:Block/'+userName+'&action=view',
					userRights : ['sysop']
				},
				'Unblock user' : {
					url : wgScript+'?title=Special:Unblock/'+userName,
					userRights : ['sysop']
				},
				'View block' : {
					url : wgScript+'?title=Special:BlockList&action=view&ip='+userName
				},
				'View block log' : {
					url : wgScript+'?title=Special:Log&action=view&type=block&page=User:'+userName
				}
			},
			'Analysis' : {
				'Analysis – Cyberpower678' : {
					url : '//tools.wmflabs.org/supercount/index.php?project='+wgServerName+'&user='+userName,
					title : 'Cyberpower678s User Analysis Tool'
				},
				'Analysis – WikiChecker' : {
					url : 'http://en.wikichecker.com/user/?l=all&t='+userName
				},
				'Analysis – X!' : {
					url : '//tools.wmflabs.org/xtools/pcount/?user='+userName+'&project='+wgServerName
				},
				'Edit summary usage' : {
					url : '//tools.wmflabs.org/xtools/editsummary/index.php?lang=en&wiki=wikipedia&name='+userName
				},
				'Edit summary search' : {
					url : '//tools.wmflabs.org/sigma/summary.py?name='+userName
				},
				'Articles created' : {
					url : '//tools.wmflabs.org/sigma/created.py?name='+userName+'&server='+wgDBname+'&ns=,,&redirects=none',
					rights : ['user']
				},
				'Global contributions' : {
					url : '//tools.wmflabs.org/guc/?user='+userName+'&blocks=true'
				},
				'SUL' : {
					url : wgScript+'?title=Special:CentralAuth/'+userName
				}
			},
			'IP lookup' : {
				'WHOIS' : {
					url : 'http://whois.domaintools.com/'+userName,
					ipOnly : true
				},
				'rDNS' : {
					url : 'https://www.robtex.com/ip/'+userName+'.html',
					ipOnly : true
				},
				'Traceroute' : {
					url : 'http://www.domaintools.com/research/traceroute/?query='+userName,
					ipOnly : true
				},
				'Geolocate' : {
					url : 'http://whatismyipaddress.com/ip/'+userName,
					ipOnly : true
				},
				'Tor check' : {
					url : 'https://toolserver.org/~overlordq/scripts/checktor.fcgi?ip='+userName,
					ipOnly : true
				}
			},
			'Contributions' : {
				url : wgScript+'?title=Special:Contributions/'+userName
			},
			'Deleted contributions' : {
				url : wgScript+'?title=Special:DeletedContributions/'+userName,
				userRights : ['sysop']
			},
			'User subpages' : {
				url : wgScript+'?title=Special:PrefixIndex/User:'+userName,
				rights : ['user']
			},
			'Email user' : {
				url : wgScript+'?title=Special:EmailUser/'+userName,
				rights : ['user']
			},
			'User groups' : {
				url : wgScript+'?title=Special:ListUsers&limit=1&username='+userName,
				rights : ['user']
			},
			'User thanks received' : {
				url : wgScript+'?title=Special:Log&type=thanks&user=&page=User:'+userName,
				rights : ['user']
			},
			'User rights changes' : {
				url : wgScript+'?title=Special:Log&type=rights&user=&page=User:'+userName,
				rights : ['user']
			},
			'Change rights' : {
				url : wgScript+'?title=Special:UserRights&user=User:'+userName,
				rights : ['user'],
				userRights : ['sysop','epcoordinator']
			}
		};
	
		addTab('User',menuList);
		
		getUserInfo(menuList);
	}
	
	function addPageMenu() {
		var pageName = encodeURIComponent(mw.config.get('wgPageName')).replace(/[!'()*]/g, escape);
		
		menuList = {
			'Page logs' : {
				'All logs' : {
					url : wgScript+'?title=Special:Log&action=view&page='+pageName
				},
				'Deletion log' : {
					url : wgScript+'?title=Special:Log&action=view&type=delete&page='+pageName
				},
				'Move log' : {
					url : wgScript+'?title=Special:Log&action=view&type=move&page='+pageName
				},
				'Patrol log' : {
					url : wgScript+'?title=Special:Log&action=view&type=patrol&page='+pageName
				},
				'Protection log' : {
					url : wgScript+'?title=Special:Log&action=view&type=protect&page='+pageName
				}
			},
			'Anaylsis' : {
				'Basic statistics' : {
					url : 'https://en.wikipedia.org/w/index.php?title='+pageName+'&action=info',
					pageExists : true
				},
				'Contributor analysis' : {
					url : 'http://en.wikichecker.com/article/?a='+pageName,
					pageExists : true
				},
				'Search revision history' : {
					url : 'http://wikipedia.ramselehof.de/wikiblame.php?lang=en&article='+pageName,
					pageExists : true
				},
				'Search by contributor' : {
					url : 'https://tools.wmflabs.org/usersearch/index.html?page='+pageName,
					pageExists : true
				},
				'Traffic report' : {
					url : 'http://stats.grok.se/en/latest/'+pageName,
					pageExists : true
				}
			},
			'Tools' : {
				'Disambiguate links' : {
					url : 'http://dispenser.homenet.org/~dispenser/cgi-bin/dablinks.py?page='+pageName+'&lang='+wgPageContentLanguage,
					pageExists : true
				},
				'Check external links' : {
					url : 'http://dispenser.homenet.org/~dispenser/cgi-bin/webchecklinks.py?page='+pageName,
					pageExists : true
				},
				'Copyright vio detector' : {
					url : 'https://tools.wmflabs.org/copyvios?lang='+wgPageContentLanguage+'&project='+wgNoticeProject+'&title='+pageName+'&oldid=&action=search&use_engine=1&use_links=1',
					pageExists : true
				},
				'Peer reviewer' : {
					url : 'http://dispenser.homenet.org/~dispenser/view/Peer_reviewer#page:'+pageName,
					pageExists: true,
					databaseRestrict : ['enwiki'],
					namespaceRestrict : [0,2,118]
				},
				'Add titles to bare refs' : {
					url : 'http://dispenser.homenet.org/~dispenser/cgi-bin/reflinks.py?lang=en&page='+pageName+'&autoclick=wpDiff',
					pageExists: true,
					databaseRestrict : ['enwiki'],
					namespaceRestrict : [0,2,118]
				}
			},
			'Subpages' : {
				url: wgScript+'?title=Special:PrefixIndex/'+pageName+'&action=view',
			},
			'Move page' : {
				url : wgScript+'?title=Special:MovePage/'+pageName+'&action=view',
				pageExists : true
			},
			'Merge page' : {
				url : wgScript+'?title=Special:MergeHistory&target='+pageName,
				userRights : ['sysop']
			},
			'Protect page' : {
				url : wgScript+'?title='+pageName+'&action=protect',
				userRights : ['sysop']
			},
			'Delete page' : {
				url : wgScript+'?title='+pageName+'&action=delete',
				userRights : ['sysop'],
				pageExists : true
			},
			'Latest diff' : {
				url : wgScript+'?title='+pageName+'&action=view&diff='+wgCurRevisionId,
				pageExists : true
			},
			'Edit into' : {
				url : wgScript+'?title='+pageName+'&action=edit&section=0',
				namespaceRestrict : [0,1,2,3,4,5,118],
				pageExists : true
			},
			'Purge cache' : {
				url : wgScript+'?title='+pageName+'&action=purge',
				pageExists : true
			}
		};
	
		addTab('Page',menuList);
	}
	
	function sanitize(name) {
		return name.toLowerCase().replace(/ /g,'_');
	}
	
	function addListeners() {
		$(".c2-hover-menu").each(function() {
			$(this).hover(function() {
				$el = $(this).find(".submenu");
				$el.css({
					left : $(this).outerWidth(),
					top : '-1px',
					'border-top-width' : 1
				});
				$el.show();
			},function() {
				$(this).find(".submenu").hide();
			});
		});
	}
	
	function addTab(tabName,menuList) {
		var html = 	"<div id='p-"+tabName.toLowerCase()+"2' class='vectorMenu' style='z-index:500'>" +
					"<h3>" +
						"<span>"+tabName+"</span>" +
						"<a href='#'></a>" +
					"</h3>" +
					"<div class='menu'><ul>";
		html += generateMenuContent(menuList);
		html += "</ul></div></div>";
		
		if(skin === "vector") $(html).insertAfter($("#p-cactions"));
	}
	
	function generateMenuContent(menuList) {
		var html = "";
		$.each(menuList, function(name,action) {
			if(action) {
				var newHtml = "";
				if(Object.keys(action).indexOf('url') < 0) {
					newHtml += "<li style='position:relative' id='c2-" + name.toLowerCase().replace(/ /g,'_') +"' class='c2-hover-menu'>" +
								"<a style='font-weight:bold'>" + name + "&hellip;</a>" +
									"<div class='submenu menu' style='display:none; position:absolute'><ul>";
					$.each(action, function(k,v) {
						var namespaceConflict = (v.namespaceRestrict && v.namespaceRestrict.length && v.namespaceRestrict.indexOf(wgNamespaceNumber) < 0)
							|| (v.namespaceExclude && v.namespaceExclude.length && v.namespaceExclude.indexOf(wgNamespaceNumber) > -1);
						var existenceConditional = (v.pageExists && wgArticleId > 0) || (!v.pageExists);
						var isSupportedDatabase = v.databaseRestrict ? v.databaseRestrict.indexOf(wgDBname) !== -1 : true;
						if(!namespaceConflict && !rightsSkip(v.userRights,wgUserGroups) && existenceConditional && isSupportedDatabase) {
							newHtml += "<li id='c2-" + sanitize(k) +"'><a href='" + v.url + "' title='" + (v.title || '') + "'>" + k + "</a></li>";
						}
					});
					newHtml += "</ul></div></li>";
					if($(newHtml).last().find(".submenu li").length === 0) newHtml = "";
				} else {
					var namespaceConflict = (action.namespaceRestrict && action.namespaceRestrict.length && action.namespaceRestrict.indexOf(wgNamespaceNumber) < 0)
						|| (action.namespaceExclude && action.namespaceExclude.length && action.namespaceExclude.indexOf(wgNamespaceNumber) > -1);
					var existenceConditional = (action.pageExists && wgArticleId > 0) || (!action.pageExists);
					if(!namespaceConflict && !rightsSkip(action.userRights,wgUserGroups) && existenceConditional) {
						newHtml += "<li id='c2-" + sanitize(name) +"'><a href='" + action.url + "'>" + name + "</a></li>";
					}
				}
				html += newHtml;
			}
		});
		return html;
	}
	
	function rightsSkip(permittedRights,rights) {
		if(!rights.length) return false;
		var skip = (permittedRights && permittedRights.length);
		if(permittedRights && permittedRights.length) {
			for (var i = 0; i < permittedRights.length; i++) {
			    if (rights.indexOf(permittedRights[i]) > -1) {
			        skip = false;
			        break;
			    }
			}
		}
		return skip;
	}
	
	var api = new mw.Api();
	
	function apiGet(params) {
		debugger;
		return api.get(
			$.extend(params, {
				action: 'query',
				list: 'users'
			})
		);
	}
	
	function getUserInfo(menuList) {
		apiGet({
			list: 'users|blocks',
			ususers: userName,
			bkusers: userName,
			usprop: 'blockinfo|groups',
			bkprop: 'id'
		}).done(function (data) {
			if(data.query.blocks && data.query.blocks.length) {
				$("#c2-block_user").remove();
				$("#c2-view_block").find("a").css('color','#EE1111');
			} else {
				$("#c2-unblock_user").remove();
				$("#c2-view_block").remove();
			}
			
			if(data.query && data.query.users[0]) {
				var rights = data.query.users[0].groups;

				// if((rights && rights.indexOf("sysop") < 0) || !rights) {
					$.each(menuList, function(name,action) {
						if(action) {
							if(Object.keys(action).indexOf('url') < 0) {
								$.each(action, function(k,v) {
									if((rights && rightsSkip(v.rights,rights)) || (!rights && v.rights) || (rights && v.ipOnly)) {
										$("#c2-"+sanitize(k)).remove();
										$("#c2-"+sanitize(k)).remove(); // FIXME: second time's the charm?
									}
								});
								if($("#c2-"+sanitize(name)).find("li").length === 0) $("#c2-"+sanitize(name)).remove();
							} else {
								if((rights && rightsSkip(action.rights,rights)) || (!rights && action.rights) || (rights && action.ipOnly)) {
									$("#c2-"+sanitize(name)).remove();
								}
							}
						}
					});
				// }
			}
		});
		// $.get(wgScriptPath+'/api.php?format=json&action=query&list=users|blocks&ususers='+userName+'&bkusers='+userName+'&usprop=blockinfo|groups&bkprop=id', function(data) {
			
		// });
		
		if(!admin) $("#c2-blocks").hide();
		$.get(wgScriptPath+'/api.php?format=json&action=query&list=logevents&letype=block&letitle=User:'+userName+'&lelimit=1',function(data) {
			if(data.query.logevents.length === 0) {
				$("#c2-view_block_log").remove();
			} else {
				$("#c2-blocks").show();
			}
		});
		
		$("#c2-rfxs").hide();
		if(wgDBname === "enwiki") {
			$.get(wgScriptPath+'/api.php?format=json&action=query&list=allpages&apprefix=Requests_for_adminship%2F'+userName+'&apnamespace=4&aplimit=10', function(data) {
				if(data.query && data.query.allpages.length) {
					$("#c2-rfxs").show();
				} else {
					$("#c2-rfas").remove();
				}
			});
			$.get(wgScriptPath+'/api.php?format=json&action=query&list=allpages&apprefix=Requests_for_bureaucratship%2F'+userName+'&apnamespace=4&aplimit=10', function(data) {
				if(data.query && data.query.allpages.length) {
					$("#c2-rfxs").show();
				} else {
					$("#c2-rfbs").remove();
				}
			});
			$.get(wgScriptPath+'/api.php?format=json&action=query&titles=Wikipedia:Requests_for_adminship/'+userName+'|Wikipedia:Requests_for_bureaucratship/'+userName+'|Wikipedia:Requests_for_arbitration/'+userName+'|Wikipedia:Requests_for_comment/'+userName+'|Wikipedia:Requests_for_checkuser/Case/'+userName+'|Wikipedia:Sockpuppet_investigations/'+userName+'&letype=block&letitle=User:'+userName+'&prop=info',function(data) {
				for(var i in data.query.pages) switch(data.query.pages[i].title.split('/')[0]) {
					case 'Wikipedia:Requests for arbitration' : if(typeof data.query.pages[i].missing === "undefined"){$("#c2-rfxs").show();} else {$("#c2-rfar").remove();} break;
					case 'Wikipedia:Requests for comment' : if(typeof data.query.pages[i].missing === "undefined"){$("#c2-rfxs").show();} else {$("#c2-rfc").remove();} break;
					case 'Wikipedia:Requests for checkuser' : if(typeof data.query.pages[i].missing === "undefined"){$("#c2-rfxs").show();} else {$("#c2-rfcu").remove();} break;
					case 'Wikipedia:Sockpuppet investigations' : if(typeof data.query.pages[i].missing === "undefined"){$("#c2-rfxs").show();} else {$("#c2-spi").remove();} break;
				}
			});
		}
	}
})();