MediaWiki:Gadget-wikibugs.js

Z Wikisłownika – wolnego słownika wielojęzycznego

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
/**
 * @author: [[w:pl:User:Dodek]] (idea and text)
 * @author: [[w:pl:User:Adziura]] (idea and text)
 * @author: [[w:pl:User:Nux]] (implementation)
 * @author: [[w:pl:User:Saper]] (implementation)
 * @author: [[w:pl:User:Beau]] (implementation)
 * @author: [[w:pt:User:Helder.wiki]] (implementation)
 * @author: [[wikt:pl:User:Peter Bowman]] (rework)
 */

var config = mw.config.get( [
		'wgPageName',
		'wgRestrictionEdit',
		'wgUserGroups',
		'wgUserLanguage'
	] ),
	/**
	 * A name of a page where reports are saved.
	 */
	reportPage = 'Wikisłownik:Zgłoś błąd w haśle',
	/**
	 * Black list of pages which reports should not refer to.
	 */
	badPages = [ reportPage ],
	windowManager = null,
	api = null;

function configureMessages() {
	mw.messages.set( {
		'wikibugs-dialog-title': 'Zgłoś błąd',
		'wikibugs-open-form-button': 'Przejdź do formularza',
		'wikibugs-edit-page-button': 'Popraw samodzielnie',
		'wikibugs-submit-report-button': 'Wyślij',
		'wikibugs-ip-address-notice': '<p><strong>Uwaga:</strong> Twój adres IP zostanie zapisany w historii zgłoszeń.</p>',
		'wikibugs-page-name-label': 'Nazwa strony:',
		'wikibugs-report-content-label': 'Treść zgłoszenia:',
		'wikibugs-report-content-placeholder': 'Opisz błąd możliwie jak najdokładniej. Jeżeli masz taką możliwość, podaj źródło informacji.',
		'wikibugs-signature-label': 'Podpis:',
		'wikibugs-incomplete-information': 'Opisz dokładnie zgłaszany błąd. Wprowadzony opis jest za krótki.',
		'wikibugs-invalid-page': 'Podaj nazwę strony.',
		'wikibugs-bad-reports-text': '<div id="wb-bad-reports"><p>Formularz <strong>nie służy</strong> do zgłaszania próśb o rozwinięcie ' +
			'bądź stworzenie nowych haseł. Zobacz stronę:</p><ul>' +
			'<li><a href="' + mw.util.getUrl( 'Wikisłownik:Potrzebne hasła' ) + '">' +
			'Potrzebne hasła</a></li></ul><p>Wikisłownik nie udziela jakichkolwiek porad na ' +
			'życzenie. Jeżeli masz jakąś prośbę o poradę, <b>nie zgłaszaj jej tutaj</b>.</p>' +
			'<p>Informacje, jak skontaktować się z Wikisłownikiem, można znaleźć na stronie:</p>' +
			'<ul><li><a href="' + mw.util.getUrl( 'Wikisłownik:Kontakt' ) + '">Kontakt</a></li></ul></div>',
		'wikibugs-article-intro': '<p>Możesz zgłaszać znalezione błędy w ' +
			'hasłach w Wikisłowniku, jednak pamiętaj, że Wikisłownik to ' +
			'<a href="' + mw.util.getUrl( 'wiki' ) + '">wiki</a> i można, a nawet należy ' +
			'śmiało poprawiać znalezione błędy samemu. Jeśli to możliwe, dodaj do zgłoszenia ' +
			'informację o źródle, na podstawie którego stwierdzasz istnienie błędu. Zdajemy sobie ' +
			'jednak sprawę z faktu, że niektórzy czytelnicy Wikisłownika mogą z różnych przyczyn nie ' +
			'chcieć samodzielnie poprawiać artykułów.</p><p><strong>Jeżeli potrafisz poprawić ' +
			'błąd – nie zgłaszaj go tutaj.</strong></p>',
		'wikibugs-already-reported-notice': '<p><strong>Uwaga:</strong> Jeśli zamieściłeś/-aś raport o błędzie, ' +
			'a teraz go już nie ma, to najprawdopodobniej został on naprawiony. <ul>' +
			'<li><a href="' + mw.util.getUrl( reportPage ) + '">' +
			'Zobacz listę aktualnie zgłoszonych błędów</a> oraz</li>' +
			'<li><a href="' + mw.util.getUrl( reportPage, { action: 'history' } ) + '">' +
			'historię zmian wprowadzanych na tej stronie.</a></li></ul>',
		'wikibugs-new-report': 'Nowe zgłoszenie: /* $1 */'
	} );
}

function buildWindowManager() {
	var wikibugsDialog, windowManager,
		canEdit = true,
		currentTitle = mw.Title.newFromText( config.wgPageName ),
		isBadPage = badPages.indexOf( currentTitle.toText() ) !== -1;
	
	if ( ( config.wgRestrictionEdit || [] ).length ) {
		canEdit = config.wgRestrictionEdit.some( function ( group ) {
			return config.wgUserGroups.indexOf( group ) !== -1;
		} );
	}
	
	function WikibugsDialog( config ) {
		WikibugsDialog.super.call( this, config );
	}
	
	OO.inheritClass( WikibugsDialog, OO.ui.ProcessDialog );
	
	WikibugsDialog.static.name = 'wikibugs';
	WikibugsDialog.static.title = mw.msg( 'wikibugs-dialog-title' );
	
	WikibugsDialog.static.actions = [
		{
			action: 'continue',
			modes: 'info',
			label: mw.msg( 'wikibugs-open-form-button' ),
			flags: [ 'primary', 'progressive' ]
		},
		{
			modes: 'info',
			flags: [ 'safe', 'close' ]
		},
		{
			action: 'submit',
			modes: 'report',
			label: mw.msg( 'wikibugs-submit-report-button' ),
			flags: [ 'primary', 'progressive' ]
		},
		{
			action: 'submit-captcha',
			modes: 'captcha',
			label: mw.msg( 'wikibugs-submit-report-button' ),
			flags: [ 'primary', 'progressive' ]
		},
		{
			action: 'back-to-info',
			modes: 'report',
			flags: [ 'safe', 'back' ]
		},
		{
			action: 'back-to-report',
			modes: 'captcha',
			flags: [ 'safe', 'back' ]
		}
	];
	
	if ( canEdit ) {
		WikibugsDialog.static.actions.push( {
			action: 'edit',
			modes: 'info',
			label: mw.msg( 'wikibugs-edit-page-button' ),
			icon: 'edit'
		} );
	}
	
	WikibugsDialog.prototype.initialize = function () {
		WikibugsDialog.super.prototype.initialize.apply( this, arguments );
		
		this.infoPanel = new OO.ui.PanelLayout( {
			padded: true,
			expanded: false,
			content: [
				new OO.ui.HtmlSnippet( mw.msg( 'wikibugs-bad-reports-text' ) ),
				new OO.ui.HtmlSnippet( mw.msg( 'wikibugs-article-intro' ) ),
				new OO.ui.HtmlSnippet( mw.msg( 'wikibugs-already-reported-notice' ) )
			]
		} );
		
		this.titleInput = new mw.widgets.TitleInputWidget( {
			required: true,
			showMissing: false,
			excludeDynamicNamespaces: true,
			data: { actions: this.getActions() },
			value: !isBadPage ? currentTitle.toText() : '',
			disabled: !isBadPage
		} );
		
		this.titleInput.setValidation( function () {
			var title = mw.Title.newFromText( this.getValue().replace( /[\[\]]/g, '' ) );
			
			if ( title && title.toString() !== '' && badPages.indexOf( title.toString() ) === -1 ) {
				this.getData().actions.get( { actions: 'submit' } )[ 0 ].setDisabled( false );
				return true;
			}
			
			return false;
		} );
		
		this.titleField = new OO.ui.FieldLayout( this.titleInput, {
			label: mw.msg( 'wikibugs-page-name-label' ),
			align: 'top'
		} );
		
		this.summaryInput = new OO.ui.MultilineTextInputWidget( {
			autocomplete: false,
			autofocus: true,
			data: { actions: this.getActions() },
			placeholder: mw.msg( 'wikibugs-report-content-placeholder' ),
			required: true,
			rows: 6
		} );
		
		this.summaryInput.setValidation( function () {
			if ( this.getValue().trim().length >= 20 ) {
				this.getData().actions.get( { actions: 'submit' } )[ 0 ].setDisabled( false );
				return true;
			}
			
			return false;
		} );
		
		this.summaryField = new OO.ui.FieldLayout( this.summaryInput, {
			label: mw.msg( 'wikibugs-report-content-label' ),
			align: 'top'
		} );
		
		this.reportFieldset = new OO.ui.FieldsetLayout( {
			items: [ this.titleField, this.summaryField ]
		} );
		
		this.reportPanel = new OO.ui.PanelLayout( {
			padded: true,
			expanded: false,
			content: [ this.reportFieldset ]
		} );
		
		if ( mw.user.isAnon() ) {
			this.reportAnonNotice = new OO.ui.HtmlSnippet( mw.msg( 'wikibugs-ip-address-notice' ) );
			this.reportPanel.$element.append( this.reportAnonNotice.toString() );
		}
		
		this.captchaPanel = new OO.ui.PanelLayout( {
			padded: true,
			expanded: false
		} );
		
		this.stackLayout = new OO.ui.StackLayout( {
			items: [ this.infoPanel, this.reportPanel, this.captchaPanel ]
		} );
		
		this.$body.append( this.stackLayout.$element );
	};
	
	WikibugsDialog.prototype.getSetupProcess = function ( data ) {
		return WikibugsDialog.super.prototype.getSetupProcess.call( this, data ).next( function () {
			this.stackLayout.setItem( this.infoPanel );
			this.getActions().setMode( 'info' );
		}, this );
	};
	
	WikibugsDialog.prototype.getActionProcess = function ( action ) {
		if ( action === 'continue' ) {
			this.stackLayout.setItem( this.reportPanel );
			this.getActions().setMode( 'report' );
			this.titleInput.getValue().trim() === '' ? this.titleInput.focus() : this.summaryInput.focus();
		} else if ( action === 'back-to-info' ) {
			this.stackLayout.setItem( this.infoPanel );
			this.getActions().setMode( 'info' );
		} else if ( action === 'back-to-report' ) {
			delete this.editParams;
			this.stackLayout.setItem( this.reportPanel );
			this.getActions().setMode( 'report' );
		} else if ( action === 'edit' ) {
			return new OO.ui.Process( function () {
				this.close();
				window.location = mw.util.getUrl( null, { action: 'edit' } );
			}, this );
		} else if ( action === 'submit' ) {
			return new OO.ui.Process( function () {
				return this.titleInput.getValidity().catch( function () {
					return $.Deferred().reject( new OO.ui.Error( mw.msg( 'wikibugs-invalid-page' ), {
						recoverable: false
					} ) ).promise();
				} );
			}, this ).next( function () {
				return this.summaryInput.getValidity().catch( function () {
					return $.Deferred().reject( new OO.ui.Error( mw.msg( 'wikibugs-incomplete-information' ), {
						recoverable: false
					} ) ).promise();
				} );
			}, this ).next( function () {
				var dialog = this,
					reportedPage = this.titleInput.getValue().trim(),
					content = this.summaryInput.getValue().trim() + ' ~~' + '~~';
				
				this.editParams = this.editParams || api.assertCurrentUser( {
					sectiontitle: mw.format( '[[:$1' + ']]', reportedPage ),
					summary: mw.msg( 'wikibugs-new-report', reportedPage )
				} );
				
				return api.newSection( reportPage, null, content, this.editParams ).then( function ( res ) {
					var deferred = $.Deferred();
					
					if ( res.edit.result === 'Failure' && 'captcha' in res.edit ) {
						dialog.captchaWidget = new mw.libs.confirmEdit.CaptchaInputWidget( res.edit.captcha );
						dialog.captchaPanel.$element.html( dialog.captchaWidget.$element );
						
						dialog.captchaWidget.on( 'load', function () {
							dialog.stackLayout.setItem( dialog.captchaPanel );
							dialog.getActions().setMode( 'captcha' );
							deferred.resolve();
						} );
					} else {
						location.href = mw.util.getUrl( reportPage ) + '#' + mw.util.escapeIdForLink( reportedPage );

						if ( currentTitle.toText() === reportPage ) {
							location.reload();
						} else {
							deferred.resolve();
						}
					}
					
					return deferred.promise();
				}, function ( code, data ) {
					var error = new OO.ui.Error( api.getErrorMessage( data ) );
					return $.Deferred().reject( error ).promise();
				} );
			}, this );
		} else if ( action === 'submit-captcha' ) {
			$.extend( this.editParams, {
				captchaid: this.captchaWidget.getCaptchaId(),
				captchaword: this.captchaWidget.getCaptchaWord()
			} );
			
			return new OO.ui.Process( function () {
				return this.executeAction( 'submit' );
			}, this );
		}
		
		return WikibugsDialog.super.prototype.getActionProcess.call( this, action );
	};
	
	wikibugsDialog = new WikibugsDialog( {
		size: 'large'
	} );
	
	windowManager = new OO.ui.WindowManager();
	windowManager.addWindows( { wikibugs: wikibugsDialog } );
	
	$( document.body ).append( windowManager.$element );
	return windowManager;
}

function lazyOpen() {
	mw.loader.using( [
		'oojs-ui-core',
		'oojs-ui-widgets',
		'oojs-ui-windows',
		'oojs-ui.styles.icons-editing-core',
		'mediawiki.api',
		'mediawiki.Title',
		'mediawiki.user',
		'mediawiki.util',
		'mediawiki.widgets',
		'ext.confirmEdit.CaptchaInputWidget'
	] ).done( function () {
		if ( windowManager === null ) {
			mw.util.addCSS( '#wb-bad-reports { float: right; width: 220px; padding: 4px 10px 18px; margin: 2px 0px 10px 10px; font-size: 95%; border: 2px solid #900; }' );
			
			configureMessages();
			
			api = new mw.Api( { parameters: {
				errorformat: 'html',
				errorlang: config.wgUserLanguage,
				errorsuselocal: true
			} } );
			
			windowManager = buildWindowManager();
		}
		
		windowManager.openWindow( 'wikibugs' );
	} );
}

$( function () {
	$( '#n-bug_in_article' ).find( 'a' )
		.attr( 'href', '#' )
		.on( 'click', function ( evt ) {
			lazyOpen();
			evt.preventDefault();
		} );

	// Link on the report-an-error page itself
	$( '#report-bug-link a' ).on( 'click', function ( evt ) {
		lazyOpen();
		evt.preventDefault();
	} );
} );