Jump to content

User:Technical 13/MoreMenu-sandbox.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Technical 13 (talk | contribs) at 16:04, 3 January 2015 (ce). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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>
// Script:         MoreMenu.js
// Version:        2.2.5
// Author:         MusikAnimal
// Documentation:  [[User:MusikAnimal/MoreMenu]]
//
 
( function( ) {
	var userName, api = new mw.Api(), admin = false, userGroups = mw.config.get( 'wgUserGroups' ),
		namespaceNumber = mw.config.get( 'wgNamespaceNumber' ), canonicalSpecialPageName = mw.config.get( 'wgCanonicalSpecialPageName' ),
		serverName = mw.config.get( 'wgServerName' ), siteName = mw.config.get( 'wgSiteName' ),
		contentLanguage = mw.config.get( 'wgContentLanguage' ), noticeProject = mw.config.get( 'wgNoticeProject' ),
		articleId = mw.config.get( 'wgArticleId' ), mwDBname = mw.config.get( 'wgDBname' );
	if ( canonicalSpecialPageName === 'Contributions' ) {
		userName = encodeURIComponent( mw.config.get( 'wgRelevantUserName' ) );
	} else {
		userName = encodeURIComponent( mw.config.get( 'wgTitle' ).split( '/' )[ 0 ].replace( / /g, '_' ));
	}
	userName = userName.replace( /\'/g, '%27' );
	var sysopList = [ 'steward', 'sysop', 'global-sysop', 'Global_sysops' ];
 
	for ( var i=0; i < userGroups.length; i++ ) {
		if ( sysopList.indexOf( userGroups[ i ] ) !== -1 ) {
			admin = true;
			break;
		}
	}
 
	if ( namespaceNumber === 2 || namespaceNumber === 3 || canonicalSpecialPageName == 'Contributions' ) addUserMenu( userName );
	if ( namespaceNumber >= 0 ) addPageMenu();
 
	addListeners();
 
	function addUserMenu( userName ) {
		menuList = {
			'User logs' : {
				'All logs' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName } )
				},
				'Block log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'block' } ),
					rights : sysopList
				},
				'Deletion log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'delete' } ),
					rights : sysopList
				},
				'Filter log' : {
					url : mw.util.getUrl( 'Special:AbuseLog', { wpSearchUser: userName } );
				},
				'Mass message log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'massmessage' } ),
					rights : sysopList.concat( [ 'massmessage-sender' ] )
				},
				'Move log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'move' } ),
					rights : [ 'confirmed', 'autoconfirmed' ]
				},
				'Patrol log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'patrol' } ),
					rights : [ 'confirmed', 'autoconfirmed' ]
				},
				'Protection log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'protect' } ),
					rights : sysopList
				},
				'Review log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'review' } ),
					rights : sysopList.concat( [ 'reviewer' ] )
				},
				'Thanks log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'thanks' } ),
					rights : [ 'user' ]
				},
				'Upload log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'upload' } ),
					rights : [ 'confirmed', 'autoconfirmed' ]
				},
				'User creation log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'newusers' } ),
					rights : sysopList.concat( [ 'accountcreator' ] )
				},
				'User rights log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', user: userName, type: 'rights' } ),
					rights : sysopList.concat( [ 'epcoordinator' ] )
				}
			},
			'RfXs' : {
				'RfAs' : {
					url : mw.util.getUrl( 'Special:PrefixIndex/Wikipedia:Requests_for_adminship/' + userName, { action: 'view' } ),
					title : 'Requests for Adminship'
				},
				'RfBs' : {
					url : mw.util.getUrl( 'Special:PrefixIndex/Wikipedia:Requests_for_bureaucratship/' + userName, { action: 'view' } ),
					title : 'Requests for Bureaucratship'
				},
				'RfAr' : {
					url : mw.util.getUrl( 'Wikipedia:Requests_for_arbitration/' + userName, { action: 'view' } ),
					title : 'Requests for Arbitration'
				},
				'RfC' : {
					url : mw.util.getUrl( 'Wikipedia:Requests_for_comment/' + userName, { action: 'view' } ),
					title : 'Requests for Comment'
				},
				'RfCU' : {
					url : mw.util.getUrl( 'Wikipedia:Requests_for_checkuser/Case/' + userName, { action: 'view' } ),
					title : 'Request for Checkuser'
				},
				'SPI' : {
					url : mw.util.getUrl( 'Wikipedia:Sockpuppet_investigations/' + userName, { action: 'view' } ),
					title : 'Sockpuppet investigations (as the sockmaster)'
				}
			},
			'Blocks' : {
				'Block user' : {
					url : mw.util.getUrl( 'Special:Block/' + userName, { action: 'view' } ),
					userRights : sysopList
				},
				'Change block' : {
					url : mw.util.getUrl( 'Special:Block/' + userName, { action: 'view' } ),
					userRights : sysopList
				},
				'Unblock user' : {
					url : mw.util.getUrl( 'Special:Unblock/' + userName ),
					userRights : sysopList
				},
				'View block' : {
					url : mw.util.getUrl( 'Special:BlockList&action=view&ip=' + userName )
				},
				'View block log' : {
					url : mw.util.getUrl( 'Special:Log&action=view&type=block&page=User:' + userName )
				}
			},
			'Analysis' : {
				'Analysis – Supercount' : {
					url : '//tools.wmflabs.org/supercount/index.php?project=' + serverName + '&user=' + userName,
					title : 'Cyberpower678s User Analysis Tool'
				},
				'Analysis – WikiChecker' : {
					url : 'http://' + contentLanguage + '.wikichecker.com/user/?l=all&t=' + userName,
					databaseRestrict : [ 'enwiki', 'jawiki', 'frwiki', 'ruwiki' ],
				},
				'Analysis – XTools' : {
					url : '//tools.wmflabs.org/xtools/ec/?user=' + userName + '&project=' + serverName
				},
				'Autoblocks' : {
					url : '//tools.wmflabs.org/xtools/autoblock/?user=' + userName + '&project=' + serverName
				},
				'Edit summary usage' : {
					url : '//tools.wmflabs.org/xtools/editsummary/index.php?lang=en&wiki=' + siteName + '&name=' + userName
				},
				'Edit summary search' : {
					url : '//tools.wmflabs.org/sigma/summary.py?name=' + userName
				},
				'Articles created' : {
					url : '//tools.wmflabs.org/xtools/pages/?user=' + userName + '&project='+ serverName + '&namespace=0&redirects=none',
					rights : ['user']
				},
				'Global contributions' : {
					url : '//tools.wmflabs.org/guc/?user=' + userName + '&blocks=true'
				},
				'SUL' : {
					url : mw.util.getUrl( '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
				}
			},
			'Contributions' : {
				url : mw.util.getUrl( 'Special:Contributions/' + userName )
			},
			'Deleted contributions' : {
				url : mw.util.getUrl( 'Special:DeletedContributions/' + userName ),
				userRights : sysopList
			},
			'User subpages' : {
				url : mw.util.getUrl( 'Special:PrefixIndex/User:' + userName ),
				rights : [ 'user' ]
			},
			'Email user' : {
				url : mw.util.getUrl( 'Special:EmailUser/' + userName ),
				rights : [ 'user' ]
			},
			'User groups' : {
				url : mw.util.getUrl( 'Special:ListUsers', { limit: 1, username: userName ),
				rights : [ 'user' ]
			},
			'User thanks received' : {
				url : mw.util.getUrl( 'Special:Log', { user: '' , page: 'User:' + userName, type: 'thanks' } ),
				rights : [ 'user' ]
			},
			'User rights changes' : {
				url : mw.util.getUrl( 'Special:Log', { user: '' , page: 'User:' + userName, type: 'rights' } ),
				rights : [ 'user' ]
			},
			'Change rights' : {
				url : mw.util.getUrl( 'Special:UserRights', { user: 'User:' + userName } ),
				rights : [ 'user' ],
				userRights : sysopList.concat( [ 'epcoordinator' ] )
			}
		};
 
		addTab( 'User', menuList );
 
		getUserInfo( menuList );
	}
 
	function addPageMenu() {
		var pageName = encodeURIComponent( mw.config.get( 'wgPageName' ) ).replace( /[!'()*]/g, escape );
 
		menuList = {
			'Page logs' : {
				'All logs' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', page: pageName )
				},
				'Deletion log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', page: pageName, type: 'delete' } )
				},
				'Move log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', page: pageName, type: 'move' } )
				},
				'Patrol log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', page: pageName, type: 'patrol' } )
				},
				'Protection log' : {
					url : mw.util.getUrl( 'Special:Log', { action: 'view', page: pageName, type: 'protect' } )
				}
			},
			'Anaylsis' : {
				'Analysis – WikiChecker' : {
					url : 'http://' + contentLanguage + '.wikichecker.com/article/?a=' + pageName,
					databaseRestrict : [ 'enwiki', 'jawiki', 'frwiki', 'ruwiki' ],
					pageExists : true
				},
				'Analysis – XTools' : {
					url : 'http://tools.wmflabs.org/xtools/articleinfo/?article=' + pageName + '&project=' + serverName,
					pageExists : true
				},
				'Basic statistics' : {
					url : mw.util.getUrl( pageName, { action: 'info' } ),
					pageExists : true
				},
				'Search by contributor' : {
					url : 'https://tools.wmflabs.org/usersearch/index.html?page=' + pageName,
					pageExists : true
				},
				'Search revision history' : {
					url : 'http://wikipedia.ramselehof.de/wikiblame.php?lang=' + contentLanguage + '&project=' + noticeProject + '&article=' + pageName,
					pageExists : true
				},
				'Traffic report' : {
					url : 'http://stats.grok.se/' + contentLanguage + '/latest/' + decodeURIComponent( pageName ),// don't ask about the decode!
					pageExists : true,
					noticeProjectRestrict : [ 'wikipedia' ]
				}
			},
			'Tools' : {
				'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 ]
				},
				'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='+  contentLanguage + '&project=' + noticeProject + '&title=' + pageName + '&oldid=&action=search&use_engine=1&use_links=1',
					pageExists : true
				},
				'Disambiguate links' : {
					url : 'http://dispenser.homenet.org/~dispenser/cgi-bin/dablinks.py?page=' + pageName + '&lang=' + contentLanguage,
					pageExists : true
				},
				'Peer reviewer' : {
					url : 'http://dispenser.homenet.org/~dispenser/view/Peer_reviewer#page:' + pageName,
					pageExists: true,
					databaseRestrict : [ 'enwiki' ],
					namespaceRestrict : [ 0, 2, 118 ]
				}
			},
			'Subpages' : {
				url: mw.util.getUrl( 'Special:PrefixIndex/' + pageName, { action: 'view' } ),
			},
			'Move page' : {
				url : mw.util.getUrl( 'Special:MovePage/' + pageName, { action: 'view' } ),
				pageExists : true
			},
			'Merge page' : {
				url : mw.util.getUrl( 'Special:MergeHistory', { target: pageName } ),
				userRights : sysopList
			},
			'Protect page' : {
				url : mw.util.getUrl( pageName, { action: 'protect' } ),
				userRights : sysopList
			},
			'Delete page' : {
				url : mw.util.getUrl( pageName, { action: 'delete' } ),
				userRights : sysopList,
				pageExists : true
			},
			'Latest diff' : {
				url : mw.util.getUrl( pageName, { action: 'view', diff: mw.config.get( 'wgCurRevisionId' ) } ),
				pageExists : true
			},
			'Edit into' : {
				url : mw.util.getUrl( pageName, { action: 'edit', section: 0 } ),
				namespaceRestrict : [ 0, 1, 2, 3, 4, 5, 118 ],
				pageExists : true
			},
			'Purge cache' : {
				url : mw.util.getUrl( pageName, { action: 'purge', forcelinkupdate: true } ),
				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( namespaceNumber ) < 0 )
							|| ( v.namespaceExclude && v.namespaceExclude.length && v.namespaceExclude.indexOf(namespaceNumber) > -1 );
						var existenceConditional = ( v.pageExists && articleId > 0 ) || ( !v.pageExists );
						var isSupportedDatabase = v.databaseRestrict ? v.databaseRestrict.indexOf( mwDBname ) !== -1 : true;
						var isSupportedNoticeProject = v.noticeProjectRestrict ? v.noticeProjectRestrict.indexOf( noticeProject ) !== -1 : true;
						if ( !namespaceConflict && !rightsSkip( v.userRights, userGroups ) && existenceConditional && isSupportedDatabase && isSupportedNoticeProject ) {
							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( namespaceNumber ) < 0 )
						|| ( action.namespaceExclude && action.namespaceExclude.length && action.namespaceExclude.indexOf( namespaceNumber ) > -1 );
					var existenceConditional = ( action.pageExists && articleId > 0 ) || ( !action.pageExists );
					if ( !namespaceConflict && !rightsSkip( action.userRights, userGroups ) && 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;
	}
 
	function apiGet( params ) {
		return api.get(
			$.extend( params, {
				action: 'query'
			} )
		);
	}
 
	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-change_block' ).remove();
				$( '#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?  Goggle Chrome?  Known issue. ;)
									}
								} );
								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();
								}
							}
						}
					} );
				// }
			}
		} );
 
		if ( !admin ) $( '#c2-blocks' ).hide();
		apiGet( {
			list: 'logevents',
			letype: 'block',
			letitle: 'User:' + userName,
			lelimit: 1
		} ).done( function( data ) {
			if ( data.query.logevents.length === 0) {
				$( '#c2-view_block_log' ).remove();
			} else {
				$( '#c2-blocks' ).show();
			}
		} );
 
		$( '#c2-rfxs' ).hide();
		if ( mwDBname === 'enwiki' ) {
			apiGet( {
				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'
			} ).done( function( data ) {
				for( var i in data.query.pages ) switch( data.query.pages[ i ].title.split( '/' )[0] ) {
					case 'Wikipedia:Requests for adminship' :
						if ( data.query.pages[ i ].missing === undefined ){
							$( '#c2-rfxs' ).show();
						} else {
							$( '#c2-rfas' ).remove();
						}
						break;
					case 'Wikipedia:Requests for bureaucratship' :
						if ( data.query.pages[ i ].missing === undefined ){
							$( '#c2-rfxs' ).show();
						} else {
							$( '#c2-rfbs' ).remove();
						}
						break;
					case 'Wikipedia:Requests for arbitration' :
						if ( data.query.pages[ i ].missing === undefined ){
							$( '#c2-rfxs' ).show();
						} else {
							$( '#c2-rfar' ).remove();
						}
						break;
					case 'Wikipedia:Requests for comment' :
						if ( data.query.pages[ i ].missing === undefined ){
							$( '#c2-rfxs' ).show();
						} else {
							$( '#c2-rfc' ).remove();
						}
						break;
					case 'Wikipedia:Requests for checkuser' :
						if ( data.query.pages[ i ].missing === undefined ){
							$( '#c2-rfxs' ).show();
						} else {
							$( '#c2-rfcu' ).remove();
						}
						break;
					case 'Wikipedia:Sockpuppet investigations' :
						if ( data.query.pages[ i ].missing === undefined ){
							$( '#c2-rfxs' ).show();
						} else {
							$( '#c2-spi' ).remove();
						}
						break;
				}
			} );
		}
	}
} )( );
//</nowiki>