Usuário:Max51/Testes/MediaWiki:Gadget-fastbuttons.js

Origem: Wikipédia, a enciclopédia livre.

Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
  • Opera: Pressione Ctrl-F5.
/**
 * FastButtons
 *
 * @author: [[es:User:Racso]] (versão original do script, na Wikipédia em espanhol)
 * @author: [[en:User:Macy]] (versão adaptada para a Wikipédia inglesa)
 * @author: Outros editores que aparecem no histórico
 * @source: [[en:User:Macy/FastButtons.js]] ([[en:Special:PermaLink/230473471]])
 * @source: [[es:Special:PrefixIndex/User:Racso/FB]]
 * @see: [[MediaWiki:Gadget-fastbuttons.js/ButtonList.js]]
 */
/*jslint browser: true, white: true, devel: true, continue: true, regexp: true, plusplus: true, forin:true */
/*global mediaWiki, jQuery */
/*testes por [[pt:Usuário:Max51]] (em junho de 2013)*/

( function( mw, $, window ) {
'use strict';

var fb = {
		version: /*{{subst:Autossubstituição/Estampa com data e hora|js|.*/ '2020-10-08 10:02:15 (UTC)' /*}}.*/,
		buttons: {},
		process: {},
		$menu: $( '<div id="fb-menu"></div>' ),
		$submenu: $( '<div id="fb-submenu"></div>' ),
		$pageInfo: $( '<div id="fb-pageInfo"></div>' ),
		log: function( errorText ) {
			throw new Error( 'FastButtons: ' + errorText );
		}
	},
	nsNum = mw.config.get( 'wgNamespaceNumber' ),
	title = mw.config.get( 'wgTitle' ),
	pageName = mw.config.get( 'wgPageName' ),
	action = mw.config.get( 'wgAction' ),
	api = new mw.Api();

/**
 * Create new button links
 *
 * @param {(jQuery|Array|Object)}
 *	$target A jQuery object representing the target or
 *	an object representing a button (see 'buttons' param below)
 * 	an Array of this kind of objects
 *	(Defaults to fb.$menu if the it is not an instance of jQuery)
 * @param {{action: (string|function), text: string, title: string, prompt: string, label: string, disable: boolean, after: string}=}
 *		 buttons Atributes and other information for the new button
 */
fb.addButton = function( $target, buttons ) {
	var i, comment, props, button,
		doNothing = function( event ) {
			event.preventDefault();
		},
		buttonClick = function( btn ) {
			return function( event ) {
				var question = ( typeof btn.action === 'string' && btn.action.substr( 0, 2 ) === 'ER' )
					? 'Se necessário, coloque uma observação.'
					: btn.prompt,
					promptCallback = function( comment ) {
						if ( $.isFunction( btn.action ) ) {
							btn.action();
						} else {
							fb.run( btn.action, comment, btn.sum );
						}
					};

				doNothing( event );

				if ( fb.$submenu.text() !== 'Carregando...'
					&& typeof btn.action !== 'string'
					&& ( fb.textButton === btn.text && fb.textButton !== 'ESR' )
				) {
					fb.textButton = '';

					return fb.$submenu.empty();
				}

				if ( typeof btn.action === 'string'
					&& btn.action.search( /(apagar|VDA|susp|redirect)/ ) !== -1
					&& fb.$submenu.html() !== ''
				) {
					fb.$submenu.empty();
				}

				if ( question ) {
					comment = fb.nicePrompt( question, btn.label, promptCallback );
				} else {
					promptCallback( comment );
				}

				fb.textButton = btn.text;
			};
		};

	if ( !$target.jquery ) {
		//if $target is not an instance of jQuery, use it as 'buttons'
		buttons = $target;
		$target = fb.$menu;
	}

	if ( !$.isArray( buttons ) ) {
		buttons = [ buttons ];
	}

	for ( i = 0; i < buttons.length; i++ ) {
		button = buttons[ i ];
		props = {
			title: button.title,
			text: button.text
		};

		if ( button.disable ) {
			props[ 'class' ] = 'fb-button fb-disabled-button';
			props.click = doNothing;
		} else {
			props[ 'class' ] = $.isFunction( button.action )
				? 'fb-button fb-menu-button'
				: 'fb-button fb-action-button';
			props.click = buttonClick( button );
		}

		if ( button.after && $( '.fb-button' ).length ) {
			$( '.fb-button' ).eq( button.after ).after( $( '<a></a>', props ) );
		} else {
			$( '<a></a>', props ).appendTo( $target ).after( ' ' );
		}
	}
};

/**
 * Add an input for stubs
 */
fb.addFieldForStubs = function() {
	var $stubInput, $okButton,
		ok = function( event ) {
			if ( event.which === 13 ) {
				event = 'ok';
			}

			if ( event === 'ok' ) {
				if ( $stubInput.val() === '' ) {
					return $stubInput.addClass( 'fb-fill-field' );
				}

				$stubInput.removeClass( 'fb-fill-field' );

				fb.run( 'esboço-' + $( '#fb-esb-input' ).val() );
			}
		};

	$stubInput = $( '<input type="text" id="fb-esb-input" size="14" />' )
		.keyup( function() {
			fb.callAPI( 'esb' );
		} )
		.keypress( function( event ) {
			ok( event );
		} );

	$okButton = $( '<input type="button" value="OK" />' )
		.mousedown( function() {
			ok( 'ok' );
		} );

	fb.$submenu.append( ' esboço-', $stubInput, ' ', $okButton );
};

/**
 * Add the search buttons
 */
fb.addSearchButtons = function() {
	var i,
		list = fb.buttons.search,
		$searchButtons = $( '<span class="plainlinks"></span>' ),
		search = encodeURIComponent(
			title.indexOf( ' ' ) !== -1
				? '"' + title + '"'
				: title
		);

	for ( i = 0; i < list.length; i++ ) {
		$searchButtons.append( $( '<a></a>', {
			'class': 'external text',
			'target': '_blank',
			'href': list[ i ].url + search,
			'text': list[ i ].text,
			'title': list[ i ].desc
		} ) );

		if ( list[ i ].after ) {
			$searchButtons.append( list[ i ].after );
		}
	}

	fb.$submenu.append( $searchButtons );
};

/**
 * Add the CatScan buttons
 */
fb.addCatButtons = function() {
	var i,
		list = fb.buttons.cat,
		catScanUrl = '//toolserver.org/~daniel/WikiSense/CategoryIntersect.php?'
			+ $.param( {
				wikilang:  mw.config.get( 'wgContentLanguage' ),
				wikifam: '.wikipedia.org',
				userlang: mw.config.get( 'wgUserLanguage' ),
				basecat: title,
				basedeep: '1',
				go: 'Examinar',
				format: 'html',
				mode: ''
			} );

	fb.$submenu.append( 'Procurar nesta categoria: ' );

	for ( i = 0; i < list.length; i++ ) {
		fb.$submenu.append( $('<a></a>', {
			'class': 'fb-button fb-link-button',
			'target': '_blank',
			'href': catScanUrl + list[ i ].url,
			'text': list[ i ].text,
			'title': list[ i ].desc
		} ) );
	}
};

/**
 * Creates a dialog
 */
fb.dialog = function( info ) {
	var $fbDialog = $( '<div id="fb-dialog" class="ui-widget"></div>' ).append( info.content );

	if ( $( '#fb-dialog' ).length ) {
		$( '#fb-dialog' ).dialog( 'close' );
	}

	if ( !info.modal ) {
		info.modal = true;
	}

	$.extend( info, {
		title: 'FasButtons - ' + info.title,
		open: function() {
			$( '.ui-dialog-titlebar-close' ).hide();
		},
		close: function() {
			$fbDialog
				.dialog( 'destroy' )
				.remove();
		}
	} );

	$fbDialog.dialog( info );
};

/**
 * Manipulate the text page
 *
 * @param {string} See "fb.run"
 * @param {string} See "fb.run"
 * @param {string} Summary of edit
 * @param {string} Text of page
 * @param {boolean} If is edit page
 */
fb.manipulateTextPage = function( code, extraText, sum, value, isEditPage ) {
	var fbSummary,
		isEliminationTag = code.indexOf( 'av-' ) === -1
			&& code.search( /ES?R/ ) !== -1
			&& code.search( /ER(\|(1\b|7|8|9|12|18|C1|D1|D2|U1|U2|R1|R2))/i ) === -1,
		refreshPage = function() {
			if ( action === 'view' ) {
				location.href = mw.util.getUrl()
					+ '?fastb=success'
					+ ( code === 'redirect' ? '&redirect=no' : '' );
			} else if ( action === 'edit' || action === 'submit' ) {
				$( '#editform' )
					.unbind( 'submit' )
					.trigger( 'submit' );
			}
		},
		showDialog = function() {
			var sendWarn = function( template, userPageCreator ) {
				$( '#fb-dialog' ).dialog( 'close' );

				mw.notify( 'Enviando a notificação para o criador...' );

				api.editPage( {
					watchlist: 'preferences',
					minor: true,
					title: 'User talk:' + userPageCreator,
					appendtext: '\n\n{' + '{subst:Aviso-' + template + '}} ~~' + '~~',
					summary:  'Aviso sobre '
						+ ( template === 'não remova'
							? 'remoção de marcações de eliminação das páginas '
							: 'a eliminação da página "[[' + pageName.replace( /_/g, ' ' ) + ']]" '
						) + '(usando [[WP:FB|FastButtons]])',
					done: function() {
						refreshPage();
					}
				} ).fail( function() {
					alert( 'Erro ao tentar enviar a notificação de eliminação.' );
				} );
			};

			fb.callAPI( 'pageRevisions' ).done( function( data ) {
				var revisions = data.query.pages[ mw.config.get( 'wgArticleId' ) ].revisions,
					userPageCreator = revisions[ 0 ].user;

				if ( userPageCreator === mw.config.get( 'wgUserName' )
					|| ( revisions.length === 1 && revisions[ 0 ].comment.search( /moveu \[\[.+?\]\] para \[\[.+?\]\]/ ) !== -1 )
					|| ( ( mw.util.isIPv4Address( userPageCreator ) || mw.util.isIPv6Address( userPageCreator ) )
						&& ( Date.parse( revisions[ 0 ].timestamp ) + 86400000 ) < new Date().getTime()
					)
				) {
					return refreshPage();
				}

				fb.dialog( {
					title: 'Selecione uma opção abaixo: ',
					width: 'auto',
					height: 'auto',
					content: '<select id="fb-send-message">'
							+ '<option value="1">Enviar um aviso de eliminação</option>'
							+ '<option value="2">Enviar um aviso de remoção de marcações de eliminação nas páginas</option>'
							+ '<option>Não enviar nada</option>'
						+ '</select><br /><br />',
					buttons: {
						'OK': function() {
							if ( $( '#fb-send-message' ).val() === '1' ) {
								sendWarn( 'E'
									+ ( code.indexOf( 'ESR' ) !== -1 ? 'S' : '' )
									+ 'R|' + pageName.replace( /_/g, ' ' ),
									userPageCreator
								);
							} else if ( $( '#fb-send-message' ).val() === '2' ) {
								sendWarn( 'não remova', userPageCreator );
							} else {
								refreshPage();
							}
						},
						'Cancelar': function() {
							$( this ).dialog( 'close' );
							refreshPage();
						}
					}
				} );
			} );
		};

	if ( code !== 'redirect'
		&& value.search( /\{\{(?:subst:)?(?:ER|ESR(?:2?|-banda|-bio|-empresa|-bsre|-organização|-matrad)|VDA|Usuário(?:\(a\))?\:Salebot\/Impróprio)[\|\}]/i ) !== -1
		&& nsNum !== 3
	) {
		alert( 'Já existe uma predefinição de eliminação nesta página.' );

		return false;
	}

	if ( code.indexOf( 'ER' ) === 0 ) {
		extraText = extraText ? '|3=' + extraText : '';
		extraText = '{' + '{' + code + '|2=~~' + '~~' + extraText + '}}\n';
		extraText = nsNum === 10 ? '<noinclude>' + extraText + '</noinclude>' : extraText;

		value = extraText + value;
		fbSummary = 'Página proposta para [[WP:ER|eliminação rápida]] (regra ' + code.substring( 3 ) + ')';
	} else if ( code === 'redirect' ) {
		extraText = extraText.split( '|2=' );

		if ( nsNum === 14 ) {
			value = '{' + '{redirecionamento de categoria|' + extraText[ 0 ] + '|2=' + extraText[ 1 ] + '}}';
			fbSummary = 'Redirecionando para a [[categoria:' + extraText[ 0 ] + ']] (' + extraText[ 1 ] + ')';
		} else {
			value = '#REDIRECIONAMENTO [[' + extraText[ 0 ] + ']]';
			fbSummary = 'Feito redirecionamento para [[' + extraText[ 0 ] + ']]'
				+ ( extraText[ 1 ] !== '' ? ' (' + extraText[ 1 ] + ')' : '' );
		}
	} else {
		extraText = extraText ? '|1=' + extraText : '';
		extraText = '{' + '{' + code + extraText + '}}';

		if ( code.indexOf( 'matrad' ) !== -1 ) {
			extraText = extraText.replace( '1=', 'língua=' );
		}

		if ( nsNum === 3 ) {
			value += '\n\n' + extraText + ' ~~' + '~~';
		} else if ( code.indexOf( 'esboço' ) === 0 ) {
			if ( value.match( /\n\n\[\[/ ) ) {
				value = value.substring( 0, value.search( /\n\n\[\[/ ) ) + '\n\n'
					+ extraText + value.substring( value.search( /\n\n\[\[/ ) );
			} else {
				value += '\n\n' + extraText;
			}

			fbSummary = 'Página marcada como [[WP:EBC|esboço]]';
		} else if ( code.indexOf( 'subst:VDA' ) !== -1 ) {
			value = nsNum === 10 ? '<noinclude>' + extraText + '</noinclude>' : extraText;
		} else {
			value = extraText + '\n' + value;
			value = code.search( /subst:(apagar|ESR)/ ) !== -1 && nsNum === 10
				? '<noinclude>' + value + '</noinclude>'
				: value;
		}

		if ( nsNum === 3 ) {
			fbSummary = 'Adicionando mensagem';
		} else {
			if ( code.indexOf( 'subst:ESR' ) === 0 ) {
				code = 'subst:ESR';
			}

			fbSummary = sum || 'Adicionando marcação';
		}

		if ( code.indexOf( 'ESR' ) !== -1 ) {
			fbSummary = 'Página proposta para [[WP:ESR|eliminação semirrápida]]';
		} else if ( code.search( /subst:(fu|f-de|f-com)/ ) !== -1 ) {
			fbSummary = 'Página marcada para [[WP:Fusão|fusão]]';
		}
	}

	if ( isEditPage ) {
		$( '#wpTextbox1' ).val( value );

		if ( isEliminationTag ) {
			$( '#editform' ).submit( function( e ) {
				e.preventDefault();
				showDialog();
			} );
		}

		return;
	}

	$( '.fb-button' ).each( function() {
		$( this )
			.addClass( 'fb-disabled-button' )
			.unbind( 'click' );
	} );

	mw.notify( 'Editando a página...' );

	api.editPage( {
		watchlist: 'preferences',
		minor: true,
		summary: fbSummary + ' (usando [[WP:FB|FastButtons]])',
		text: value,
		done: {
			success: function() {
				return isEliminationTag ? showDialog() : refreshPage();
			},
			apiError: function( error ) {
				mw.notify(
					$.parseHTML(
						'Não foi possível editar a página.<br />A API retornou o código de erro "'
						+ error.code + '": ' + error.info + '.<br />Se o problema persistir,'
						+ ' favor informar no <a href="' + mw.util.getUrl( 'WP:Café dos Programadores' ) + '">Café dos programadores</a>'
						+ ' ou em <a href="' + mw.util.getUrl( 'MediaWiki Discussão:Gadget-fastbuttons.js' ) + '">MediaWiki Discussão:Gadget-fastbuttons.js</a>'
					)
				);
			},
			unknownError: function() {
				mw.notify( 'Não foi possível editar a página devido a um erro desconhecido da API.' );
			}
		}
	} ).fail( function() {
		mw.notify( 'Não foi possível fazer uma requisição com o servidor. Verifique sua conexão com a internet.' );
	} );
};

/**
 * Edit function
 *
 * @param: {string} A template name, possibly preceded by "subst:" and optionally followed by "|" and some parameter(s)
 * 	@example: 'não remova' or 'subst:ER|5' or 'subst:ESR-matrad|1=~~' + '~~|língua='
 * @param {(string|null)=} (optional) Extra text for templates and redirects
 * @return: {boolean} Returns false if the user canceled or the page already has a template
 */
fb.run = function( code, extraText, sum ) {
	var rcid;

	if ( action === 'edit' || action === 'submit' ) {
		fb.manipulateTextPage( code, extraText, sum, $( '#wpTextbox1' ).val(), true );
	} else if ( action === 'view' ) {
		rcid = mw.util.getParamValue(
			'rcid',
			mw.util.$content.find( 'div.patrollink a' ).attr( 'href' ) || ''
		);

		if ( rcid ) {
			mw.notify( 'Patrulhando a página...' );
			fb.callAPI( 'patrolPage' );
		}

		mw.notify( 'Obtendo o conteúdo da página...' );

		api.getCurrentPageText().done( function( value ) {
			fb.manipulateTextPage( code, extraText, sum, value );
		} );
	}
};

/**
 * Changes the content of the submenu
 *
 * @param {string} items The new buttons for the new submenu
 * @param {number|array} (optional) If needs include only some buttons
 *	@example: 14 or [ 14, 16, 20 ]. The numbers are the index in the array of buttons ("items" param)
 */
fb.changeSubmenu = function( items, justEnable ) {
	var i;

	fb.$submenu.empty();

	if ( !$.isArray( items ) ) {
		items = [ items ];
	}

	if ( justEnable && !$.isArray( justEnable ) ) {
		justEnable = [ justEnable ];
	}

	for ( i = 0; i < items.length; i++ ) {
		if ( $.isFunction( items[ i ] ) ) {
			items[ i ]();
		} else if ( typeof items[ i ] === 'string' ) {
			fb.$submenu.append( items[ i ] );
		} else {
			if ( justEnable && $.inArray( i, justEnable ) === -1 ) {
				items[ i ].disable = true;
			}

			fb.addButton( fb.$submenu, items[ i ], true );

		}
	}

	fb.$submenu.find( 'a, span' ).tipsy();
};

/**
 * Callback function for API calls
 *
 * @param {string} Code an abbreviation such as "esb", "PV"
 * @param (string} (optional) Used only when the "code" param is "pageQuality'. Can be 'noCallback' or 'noBeforeRequest'
 */
fb.callAPI = function( code, extra ) {
	var esb, apiParams, callbacks;

	if ( $.inArray( code, [ 'PV', 'PN', 'MR' ] ) !== -1
		&& fb.$submenu.html() === 'Carregando...'
	) {
		return;
	}

	apiParams = {
		esb: {
			list: 'allpages',
			aplimit: '1',
			apnamespace: '10'
		},

		PV: {
			list: 'watchlist',
			wlprop: 'user|comment|title|sizes',
			wlexcludeuser: mw.config.get( 'wgUserName' )
		},

		PN: {
			list: 'recentchanges',
			rctype: 'new',
			rcnamespace: '0',
			rcshow: '!patrolled',
			rcprop: 'user|comment|title|sizes|ids'
		},

		MR: {
			list: 'recentchanges',
			rctype: 'edit',
			rcnamespace: '0',
			rcshow: 'anon',
			rcprop: 'user|comment|title|sizes'
		},

		usu: {
			list: 'allusers',
			aulimit: '1',
			auprop: 'editcount|registration',
			aufrom: title.split( '/' )[ 0 ]
		},

		anon: {
			list: 'usercontribs',
			uclimit: '500',
			ucprop: 'timestamp',
			ucuser: title.split( '/' )[ 0 ]
		},

		deletedContribs: {
			list: 'deletedrevs',
			titles: title,
			drlimit: '500'
		},

		backLinks: {
			list: 'backlinks',
			bllimit: '1',
			blfilterredir: 'nonredirects',
			blnamespace: '0',
			bltitle: pageName
		},

		pageQuality: {
			prop: 'categories',
			indexpageids: '1',
			titles: mw.config.get( 'wgFormattedNamespaces' )[ nsNum + 1 ] + ':' + title
		},

		patrolPage: {
			list: 'recentchanges',
			rctoken: 'patrol',
			rclimit: '1',
			rcid: mw.util.getParamValue(
				'rcid',
				mw.util.$content.find( 'div.patrollink a' ).attr( 'href' )
			),
			token: mw.user.tokens.get( 'patrolToken' )
		},

		pageRevisions: {
			prop: 'revisions',
			rvprop: 'user|comment|timestamp',
			rvlimit: '2',
			rvdir: 'newer',
			titles: pageName,
			format: 'json'
		}
	};

	callbacks = {
		esb: fb.process.stubs,
		PV: fb.process.recentEdits,
		PN: fb.process.recentEdits,
		MR: fb.process.recentEdits,
		usu: fb.process.userInfo,
		anon: fb.process.userInfo,
		deletedContribs: fb.process.deletedContribs,
		backLinks: fb.process.backLinks,
		pageQuality: fb.process.pageQuality
	};

	if ( code === 'esb' ) {
		esb = $( '#fb-esb-input' ).val();

		if ( esb === esb.substr( 0, ( esb.length - 1 ) ) ) {
			return;
		}

		apiParams.esb.apprefix = 'Esboço-' + esb;
	} else if ( $.inArray( code, [ 'MR', 'PN', 'PV' ] ) !== -1 ) {
		fb.changeSubmenu( 'Carregando...' );
	}

	if ( $.isPlainObject( callbacks[ code ] ) ) {
		if ( code === 'pageQuality' ) {
			if ( extra === 'noCallback' ) {
				return callbacks[ code ].beforeRequest();
			}
		} else {
			callbacks[ code ].beforeRequest();
		}

		callbacks[ code ] = callbacks[ code ].callback;
	}

	apiParams[ code ].format = 'json';
	apiParams[ code ].action = 'query';

	return $.getJSON(
		mw.util.wikiScript( 'api' ),
		apiParams[ code ],
		function( data ) {
			if ( !data ) {
				fb.log( 'Unable get the list "' + apiParams[ code ].list + '" for the API.' );

				return;
			}

			if ( callbacks[ code ] ) {
				callbacks[ code ]( data.query, code, data );
			}
		}
	);
};

/**
 * Callback function for the request of stub templates
 *
 * @param {Object} Query of data JSON content returned by API
 */
fb.process.stubs = function( query ) {
	var template, start, sel,
		esb = document.getElementById( 'fb-esb-input' );

	if ( query ) {
		template = query.allpages && query.allpages[ 0 ] && query.allpages[ 0 ].title;

		if ( !template ) {
			fb.log( 'The stub templates query returned incomplete data.' );

			return false;
		}

		template = template.replace( /^Predefinição:Esboço-/gi, '' );
	} else {
		fb.log( 'The stub templates query returned no data.' );

		return false;
	}

	if ( ( esb.setSelectionRange
		|| esb.createTextRange
		|| ( esb.selectionStart !== undefined
			&& esb.selectionEnd !== undefined
		)
	) && esb.value === template.substr( 0, esb.value.length ) ) {
		//Shows sugestions. Based in HotCat script
		start = esb.value.length;
		esb.value = template;

		if ( esb.setSelectionRange ) {
			esb.setSelectionRange( start, template.length );
		} else if ( esb.createTextRange ) {
			sel = esb.createTextRange();
			sel.move( 'character', start );
			sel.moveEnd( 'character', template.length - start );
			sel.select();
		} else {
			esb.selectionStart = start;
			esb.selectionEnd = template.length;
		}
	}
};

/**
 * Callback function for the request of recent edits (from watchlist or recent changes)
 *
 * @param {Object} Query of data JSON content returned by API
 * @param {string} Code passed through the "fb.callAPI"
 */
fb.process.recentEdits = function( query, code ) {
	var i, j, list, max, item, title, length, comment,
		pages = [],
		charnum = 0,
		listName = {
			PV: 'watchlist',
			MR: 'recentchanges',
			PN: 'recentchanges'
		},
		summaryChanges = {
			PV: [
				[ '[[Ajuda:SEA|?]]', '' ],
				[ '/*', '?' ],
				[ '*/', ':' ]
			],

			MR: [
				[ '/*', '?' ],
				[ '*/', ':' ]
			],

			PN: [
				[ '[[Ajuda:SEA|?]] ', '' ]
			]
		},
		urlParam = {
			PV: 'diff=last',
			MR: 'diff=last',
			PN: 'redirect=no&rcid='
		};

	list = query[ listName[ code ] ];
	max = ( list.length < 10 ) ? list.length : 10;

	for ( i = 0; i < max; i++ ) {
		item = list[ i ];

		if ( !item ) {
			continue;
		}

		title = item.title;
		charnum += title.length;

		if ( charnum > 180 ) {
			break;
		}

		length = item.newlen - item.oldlen;

		if ( length > 0 ) {
			length = '+' + length;
		}

		comment = item.comment || '';

		for ( j = 0; j < summaryChanges[ code ].length; j += 1 ) {
			comment = comment.replace(
				summaryChanges[ code ][ 0 ],
				summaryChanges[ code ][ 1 ]
			);
		}

		if ( comment ) {
			comment = '(' + comment + ')';
		}

		if ( code === 'PN' ) {
			urlParam[ code ] += item.rcid;
		}

		pages.push(
			'<a href="' + mw.util.getUrl( title ) + '?' +
			urlParam[ code ] + '" title="(' + length + ') ' + item.user +
			' ' + comment + '">' + title + '</a>'
		);
	}

	fb.changeSubmenu( '<div class="fb-horizontal">' + pages.join( ' ' ) + '</div>' );
};

/**
 * Works with the page info, and process the backlinks request
 */
fb.process.backLinks = {
	beforeRequest: function() {
		var key,
			$catLine = $( '#mw-normal-catlinks' ),
			info = {
				'ref': {
					condition: $( '.references' ).length,
					title: '$1 está referênciada'
				},

				'cat': {
					condition: $catLine.length
						&& $catLine.html().indexOf( '><a href="' + mw.config.get( 'wgArticlePath' ).replace( '$1', '' ) ) !== -1,
					title: '$1 está categorizada'
				},

				'iw': {
					condition: $( '#p-lang' ).length,
					title: '$1 possui interwikis'
				}
			};

		fb.$pageInfo.append( '(' );

		for ( key in info ) {
			fb.$pageInfo.append( $( '<span></span>', {
				'class': 'fb-' + ( info[ key ].condition ? 'ok' : 'missing' ),
				'title': 'A página ' + info[ key ].title.replace( '$1 ', ( info[ key ].condition ? '' : 'não ' ) ),
				'text': key
			} ), ' · ' );
		}
	},

	callback: function( query ) {
		var info = '',
			backlinks = query && query.backlinks;

		if ( !backlinks ) {
			return fb.log( 'The backlinks query returned no data.' );
		}

		if ( backlinks.length ) {
			info += '<a href="'
				+ mw.util.getUrl( 'Especial:Páginas afluentes/' + pageName )
				+ '" title="Afluentes da página">afl</a>)</span>';
		} else {
			info += '<span class="fb-missing" title="Esta página ainda não possui afluentes">afl</span>)';
		}

		fb.$pageInfo.append( info );

		fb.callAPI( 'pageQuality', 'noCallback' );
	}
};

/**
 * Works with page quality of article/"anexo" and process the request of categories of the discussion page
 */
fb.process.pageQuality = {
	beforeRequest: function() {
		var $featuredContent = $( '#destaques1' );

		if ( !$featuredContent.length ) {
			return fb.callAPI( 'pageQuality', 'noBeforeRequest' );
		}

		$featuredContent = $( '#destaques1' ).find( 'img' ).attr( 'alt' );

		fb.$pageInfo.append(
			'<span title="Qualidade do artigo" style="cursor:default;"> Q-<b>'
				+ $featuredContent.substring(
					nsNum === 102 ? 16 : 17,
					$featuredContent.indexOf( '.' )
				)
			+ '</b></span>'
		).find( 'span' ).tipsy();

		fb.verifyRequestDeletion();

		return false;
	},

	callback: function( query ) {
		var i, cats, pageids, cat, quality;

		if ( query ) {
			cats = query.pages;
			pageids = query.pageids;

			if ( cats && pageids && pageids.length ) {
				cats = cats[ pageids[ 0 ] ].categories;
			} else {
				fb.log( 'The query for page quality categories returned incomplete data.' );

				return false;
			}
		} else {
			fb.log( 'The query for page quality categories returned no data.' );

			return false;
		}

		if ( cats ) {
			for ( i = 0; i < cats.length; i++ ) {
				cat = cats[ i ].title;

				if ( cat && cat.indexOf( '!Artigos de qualidade' ) !== -1 ) {
					// Categoria:!Artigos de qualidade 1 sobre ...
					//				   ^
					quality = cat.substr( 32, 1 );

					if ( quality === 'd' ) {
						quality = '?';
					}

					break;
				}
			}
		}

		fb.$pageInfo.append(
			'<span title="Qualidade do artigo" style="cursor:default;"> Q-<b>'
				+ ( quality || '?' )
			+ '</b></span>'
		).find( 'span' ).tipsy();

		fb.verifyRequestDeletion();
	}
};

/**
 * Works with user info, and process the request of user editcount
 */
fb.process.userInfo = {
	beforeRequest: function() {
		var i,
			list = fb.buttons.userInfo;

		fb.changeSubmenu( '<span id="fb-editcount">Carregando...</span>' );

		for ( i = 0; i < list.length; i++ ) {
			fb.$submenu.append( $( '<a></a>', {
				'class': 'fb-button fb-link-button',
				'target': '_blank',
				'href': list[ i ].href,
				'title': list[ i ].title,
				'text': list[ i ].text
			} ) );
		}
	},

	callback: function( query, code, data ) {
		var contribs, regDate,
			user = {};

		if ( !query ) {
			fb.log( 'The user info query returned no data.' );

			return false;
		}

		if ( code === 'anon' ) {
			if ( !query.usercontribs ) {
				return;
			}

			contribs = query.usercontribs;
			user.name = contribs[ 0 ].user;
			user.editcount = contribs.length;
			user.registration = contribs[ contribs.length - 1 ].timestamp;

			if ( data[ 'query-continue' ] ) {
				user.editcount = 'mais de ' + user.editcount;
				user.registration = 'antes de ' + user.registration;
			}
		} else {
			user = query.allusers && query.allusers[ 0 ];
		}

		if ( user ) {
			// Ex.: YYYY-MM-DDThh:mm:ssZ
			regDate = user.registration;
			regDate = regDate.substr( 8, 2 ) + '/' + regDate.substr( 5, 2 ) +
				'/' + regDate.substr( 0, 4 );

			$( '#fb-editcount' ).html( user.editcount + ' edições desde ' + regDate + '.' );
		}
	}
};

/**
 * Callback function for the request of deleted contributions
 *
 * @param {Object} Query of data JSON content returned by API
 */
fb.process.deletedContribs = function( query ) {
	var numDeletedRevs,
		deletedRevs = query.deletedrevs[ 0 ];

	numDeletedRevs = !deletedRevs ? 0 : deletedRevs.revisions.length;
	numDeletedRevs = parseInt( numDeletedRevs, 10 ) || 'Nenhuma';

	fb.$pageInfo.append( ' · ', $( '<a></a>', {
		'href': mw.util.getUrl( 'Especial:Restaurar/' + title ),
		'title': 'Link para as edições eliminadas da página',
		'text': numDeletedRevs
			+ ( $.inArray( numDeletedRevs, [ 1, 'Nenhuma' ] ) !== -1
				? ' edição eliminada'
				: ' edições eliminadas'
			)
	} ) );
};

/**
 * Checks if the page has already been proposed for deletion
 */
fb.verifyRequestDeletion = function() {
	var tagType,
		info = ( $.inArray( nsNum, [ 0, 102 ] ) === -1 ? '' : ' · ' ) + '<';

	api.getCurrentPageText( 'Wikipédia:Páginas para eliminar/' + pageName ).done( function( text ) {
		tagType = ( text === '' ) ? 'span' : 'a';

		if ( text === '' ) {
			info += tagType + ' title="A página nunca foi proposta para eliminação" style="cursor:default;"';
		} else {
			info += tagType + ' title="Pedido de eliminação desta página" href="'
				+ mw.util.getUrl( 'Wikipédia:Páginas para eliminar/' + pageName );
		}

		fb.$pageInfo.append( info + '">PE</' + tagType + '>' ).find( 'a, span' ).tipsy();

		if ( $.inArray( 'sysop', mw.config.get( 'wgUserGroups' ) ) !== -1 ) {
			fb.callAPI( 'deletedContribs' );
		}
	} );
};

/**
 * The prompt of gadget
 *
 * @param {string} Title of prompt
 * @param {string} Labels of inputs
 *	@example: 'field1|2=field2|3=field3', results in 3 inputs with the respective labels
 * @param {function} Callback
 */
fb.nicePrompt = function( title, label, callback ) {
	if ( !label ) {
		label = 'Observação';
	}

	var i,
		dialogContent = '',
		multipleQuestions = label.indexOf( '|2=' );

	if ( multipleQuestions !== -1 ) {
		for ( i = 1, label = label.split( /\|\d+=/ ); i <= label.length; i++ ) {
			dialogContent += '<label for="fb-nprompt-input' + i + '">'
				+ label[ i - 1 ].replace( /\[.+\]/, '' )
				+ ': <input type="text" class="fb-field" id="fb-nprompt-input' + i + '" /></label>';
		}
	} else {
		dialogContent = '<label for="fb-nprompt-input">'
			+ label.replace( /\[.+\]/, '' )
			+ ': <input type="text" class="fb-field" id="fb-nprompt-input" /></label>';
	}

	fb.dialog( {
		title: title,
		content: dialogContent,
		width: 400,
		height: 'auto',
		buttons: {
			'OK': function() {
				var awnsers,
					$inputs = $( '#fb-dialog input' );

				if ( label !== 'Observação' && label.length !== 1 ) {
					$inputs.each( function( i ) {
						if ( $( this ).val() === '' && label[ i ].search( /\[true\]/ ) === -1 ) {
							$( this ).addClass( 'fb-fill-field' );
						} else {
							$( this ).removeClass( 'fb-fill-field' );
						}
					} );
				}

				if ( $( '.fb-field' ).hasClass( 'fb-fill-field' ) ) {
					return;
				}

				if ( multipleQuestions === -1 ) {
					callback( $( '#fb-nprompt-input' ).val() );
				} else {
					for ( i = 1; i <= $inputs.length; i++ ) {
						awnsers = ( i === 1 )
							? $( '#fb-nprompt-input1' ).val()
							: awnsers + '|' + i + '=' + $( '#fb-nprompt-input' + i ).val();
					}

					callback( awnsers );
				}

				$( this ).dialog( 'close' );
			},
			'Cancelar': function() {
				$( this ).dialog( 'close' );
			}
		},
		focus: function() {
			$( ':input', this ).keyup( function( event ) {
				if ( event.keyCode === 13 ) {
					$( '.ui-dialog-buttonpane button:first' ).click();
				}
			} );
		}
	} );
};

/**
 * ESR prompt
*/
fb.ESRPrompt = function() {
	var i,
		subjects = [
			'Arte',
			'Carnaval',
			'Ciência',
			'Ciências sociais',
			'Cinema',
			'Educação',
			'Elementos de ficção',
			'Empresas, produtos e serviços',
			'Entretenimento',
			'Esporte/Desporto',
			'Geografia',
			'Ginástica',
			'História e sociedade',
			'Medicina',
			'Música',
			'Países',
			'Pessoas',
			'Política',
			'Rádio e televisão'
		],
		defaultJustifications = {
			'pessoas': 'Biografia sem fontes fiáveis independentes que confirmem as afirmações do texto e atestem a '
				+ ' notoriedade do biografado. Ver princípio da verificabilidade e critérios de notoriedade. Páginas pessoais'
				+ ' , zines, blogues e redes sociais (como MySpace, Facebook, etc.) não são considerados fontes fiáveis.',

			'música': 'Artigo sobre banda/grupo musical sem [[WP:FF|fontes fiáveis]] independentes que confirmem as afirmações do'
				+ ' texto e atestem a notoriedade do grupo. Ver [[WP:V|princípio da verificabilidade]] e [[WP:CDN|critérios de notoriedade]]'
				+ ' (e o critério específico para músicos). Páginas pessoais, zines, blogues e redes sociais (como '
				+ ' MySpace, Facebook, etc.) não são fontes fiáveis.',

			'empresas, produtos e serviços': 'Artigo sobre organização sem [[WP:FF|fontes fiáveis]] independentes que confirmem as afirmações'
				+ ' do texto e atestem notoriedade. Ver [[WP:V|princípio da verificabilidade]] e [[WP:CDN|critérios '
				+ ' de notoriedade]]. Páginas oficiais, blogues, fóruns e redes sociais (como Facebook, etc.) não são'
				+ ' considerados fontes fiáveis.'
		};

	fb.dialog( {
		title: 'Eliminação semi-rápida',
		width: 'auto',
		height: 'auto',
		content: '<div id="fb-ESR">'
				+ '<a id="fb-ESR-matrad" class="fb-button fb-action-button">Má-tradução</a><br /><br />'
				+ '<div id="fb-ESR-justification">'
					+ '<label for="fb-ESR-justification-field">Justificativa:<br />'
						+ '<textarea style="height:170px;width:250px;" id="fb-ESR-justification-field" placeholder="Não é necessário assinar."></textarea>'
					+ '</label>'
				+ '</div>'
				+ '<div id="fb-ESR-subject">'
					+ '<label for="fb-ESR-subject">Assunto:<br />'
						+ '<select id="fb-ESR-subject-select">'
							+ '<option></option>'
						+ '</select>'
					+ '</label><br />'
					+ '<label for="fb-ESR-subject-other">'
						+ '<input type="checkbox" id="fb-ESR-subject-other" /> Outro'
					+ '</label>'
				+ '</div>'
			+ '</div>',
		buttons: {
			'OK': function() {
				if ( $( '#fb-ESR-justification-field' ).val() === '' ) {
					return $( '#fb-ESR-justification-field' )
						.addClass( 'fb-fill-field' );
				}

				if ( $( '#fb-ESR-subject-select' ).val() === ''
					&& !$( '#fb-ESR-subject-other' ).prop( 'checked' )
				) {
					return $( '#fb-ESR-subject-otherField' )
						.addClass( 'fb-fill-field' );
				}

				fb.run(
					'subst:ESR|' + $( '#fb-ESR-justification-field' ).val() + ' ~~' + '~~'
						+ ( ( !$( '#fb-ESR-subject-other' ).prop( 'checked' )
							&& $( '#fb-ESR-subject-select' ).val() !== '' )
							? '|assunto=' + $( '#fb-ESR-subject-select' ).val()
							: ''
						)
				);

				$( this ).dialog( 'close' );
			},
			'Cancelar': function() {
				$( this ).dialog( 'close' );
			}
		}
	} );

	for ( i = 0; i < subjects.length; i++ ) {
		$( '#fb-ESR-subject-select' ).append(
			'<option value="' + subjects[ i ].toLowerCase() + '">' + subjects[ i ] + '</option>'
		);
	}

	$( '#fb-ESR-subject-select' ).change( function() {
		if ( defaultJustifications[ $( this ).val() ] ) {
			$( '#fb-ESR-justification-field' ).val( defaultJustifications[ $( this ).val() ] );
		}
	} );

	$( '#fb-ESR-matrad' ).click( function() {
		$( '#fb-dialog' ).dialog( 'close' );

		fb.nicePrompt(
			'De qual língua o artigo foi traduzido?',
			'Língua',
			function( lang ) {
				fb.run( 'subst:ESR-matrad', lang.toLowerCase() );
			}
		);
	} );
};

/**
 * Initialize the gadget
 */
fb.init = function() {
	var basePageName;

	if ( mw.util.getParamValue( 'printable' ) === 'yes' ) {
		return;
	}

	if ( mw.util.getParamValue( 'fastb' ) === 'success' ) {
		mw.notify( 'A página foi editada com sucesso.' );
		window.history.pushState( {}, 'Fast Buttons', mw.util.getUrl() + ( location.href.indexOf( 'redirect=no' ) !== -1 ? '?redirect=no' : '' ) );
	}

	if ( mw.config.get( 'skin' ) === 'modern' ) {
		$( '#contentSub' ).before( fb.$menu ).before( fb.$submenu );
	} else {
		$( 'h1' ).first().after( fb.$submenu ).after( fb.$menu );
	}

	if (  $.inArray( nsNum, [ 0, 102 ] ) !== -1 ) {
		fb.$submenu.after( fb.$pageInfo );
	}

	if ( pageName.indexOf( 'Wikipédia:Página_principal' ) === -1
		&& $.inArray( action, [ 'view', 'edit', 'submit' ] ) !== -1
	) {
		/*** Menu Principal ***/
		if ( nsNum !== -1 ) {
			if ( $.inArray( nsNum, [ 2, 3 ] ) !== -1
				&& title.indexOf( mw.config.get( 'wgUserName' ) ) === 0
			) {
				fb.addButton( {
					action: 'ER|1',
					text: 'ER1',
					title: 'Marcar subpágina do próprio usuário para eliminação'
				} );
			} else {
				fb.addButton( {
					action: function() {
						fb.changeSubmenu( fb.buttons.ER );
					},
					text: 'ER',
					title: 'Exibir regras para a eliminação rápida'
				} );
			}
		}

		if ( $.inArray( nsNum, [ 0, 102 ] ) !== -1 ) {
			fb.addButton( [ {
					action: 'subst:VDA',
					prompt: 'Informe o url da página copiada',
					label: 'Url',
					text: 'VDA',
					title: 'Marcar como cópia ou violação de direito autoral',
					sum: 'Página marcada como [[WP:VDA|VDA]]'
				}, {
					action: 'subst:suspeito',
					text: 'Suspeito',
					title: 'Marcar como suspeito de violação de direitos autorais',
					sum: 'Página marcada como suspeita de [[WP:VDA|VDA]]'
				}, {
					action: 'redirect',
					text: '#R',
					title: 'Redirecionar para outro título',
					prompt: 'Informe a página e o motivo do redirecionamento',
					label: 'Página|2=Motivo[' + ( nsNum !== 14 ) + ']'
				}, {
					action: function() {
						fb.changeSubmenu( fb.buttons.maintenance );
					},
					text: 'Manutenção',
					title: 'Exibir predefinições para manutenção'
				}, {
					action: function() {
						fb.changeSubmenu( fb.buttons.esb );
					},
					text: 'Esboço',
					title: 'Exibir predefinições para esboços'
				}, {
					action: function() {
						fb.changeSubmenu( fb.addSearchButtons );
					},
					text: 'Busca',
					title: 'Exibir opções para a busca de fontes'
				}
			] );

			fb.callAPI( 'backLinks' );
		} else {
			if ( nsNum % 2 === 0 ) {
				fb.addButton( {
					action: 'redirect',
					text: '#R',
					title: 'Redirecionar para outro título',
					prompt: 'Informe a página e o motivo do redirecionamento',
					label: 'Página|2=Motivo[' + ( nsNum !== 14 ) + ']',
					disable: nsNum === 8 || nsNum === 828
				} );
			}

			if ( nsNum === 2 || nsNum === 3 ) {
				basePageName = title.split( '/' )[ 0 ];

				if ( mw.util.isIPv4Address( basePageName ) || mw.util.isIPv6Address( basePageName ) ) {
					fb.addButton( {
						action: function() {
							fb.callAPI( 'anon' );
						},
						text: 'Sobre o IP',
						title: 'Exibir informações sobre este IP'
					} );
				} else {
					fb.addButton( {
						action: function() {
							fb.callAPI( 'usu' );
						},
						text: 'Sobre a contatatata',
						title: 'Exibir informações sobre esta contatatata'
					} );
				}
			}

			if ( nsNum === 3 ) {
				fb.addButton( {
					action: function() {
						fb.changeSubmenu( fb.buttons.warn );
					},
					text: 'Aviso',
					title: 'Exibir lista de predefinições para avisos'
				} );
			} else if ( nsNum === 4 || nsNum === 12 ) {
				fb.addButton( {
					action: function() {
						fb.changeSubmenu( fb.buttons.maintenance, [ 12, 13, 14 ] );
					},
					text: 'Manutenção',
					title: 'Exibir predefinições para manutenção'
				} );
			} else if ( nsNum === 14 ) {
				fb.addButton( {
					action: function() {
						fb.changeSubmenu( fb.addCatButtons );
					},
					text: 'CatScan',
					title: 'Exibir opções do CatScan para procurar páginas nesta categoria'
				} );
			}
		}

		if ( $.inArray( nsNum, [ 0, 2, 6, 10, 102 ] ) !== -1 ) {
			fb.addButton( [ {
				action: function() {
					fb.ESRPrompt();
				},
				text: 'ESR',
				title: 'Exibe um prompt para propor a eliminação semi-rápida da página',
				disable: nsNum === 2,
				after: '0'
			}, {
				action: 'subst:apagar',
				text: 'PE',
				title: 'Marcar para eliminação por consenso',
				after: '1',
				sum: 'Página proposta para [[WP:Eliminação por consenso|eliminação por consenso]]'
			} ] );
		}
	}

	fb.addButton( [ {
			action: function() {
				fb.callAPI( 'PV' );
			},
			text: 'PV',
			title: 'Exibir páginas vigiadas que foram alteradas recentemente'
		}, {
			action: function() {
				fb.callAPI( 'PN' );
			},
			text: 'PN',
			title: 'Exibir páginas novas que ainda não foram patrulhadas'
		}, {
			action: function() {
				fb.callAPI( 'MR' );
			},
			text: 'MR',
			title: 'Exibir mudanças recentes feitas por IPs em páginas do domínio principal'
		}
	] );

	fb.$menu.find( 'a' ).tipsy();
};

window.fastButtons = fb;

//Executes the gadget when document is ready
$( fb.init );

}( mediaWiki, jQuery, window ) );