MediaWiki:Gadget-wikibugs-core.js

Z Wikipedii, wolnej encyklopedii

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.
/**
 * Report bugs in wiki pages (mostly in articles).
 * 
 * @author: [[:pl:User:Dodek]] (idea and text)
 * @author: [[:pl:User:Adziura]] (idea and text)
 * @author: [[:pl:User:Nux]] (implementation)
 * @author: [[:pl:User:Saper]] (implementation)
 * @author: [[:pl:User:Beau]] (implementation)
 * @author: [[:pt:User:Helder.wiki]] (implementation)
 * @author: [[:pl:User:Peter Bowman]] (rework)
 * @author: [[:pl:User:Msz2001]] (UI rework)
 */
/* eslint-disable no-useless-escape */
/* global mw, OO, $ */
// <nowiki>

var config = mw.config.get( [
	'wgNamespaceIds',
	'wgNamespaceNumber',
	'wgPageName',
	'wgRestrictionEdit',
	'wgUserGroups',
	'wgUserLanguage'
] );
/**
 * A name of a page where reports are saved.
 */
var reportPage = 'Wikipedia:Zgłoś błąd w artykule';
/**
 * Black list of pages which reports should not refer to.
 */
var badPages = [
	reportPage,
	'Wikipedia:Sprzątanie Wikipedii',
	'Wikipedia:Zgłoszone grafiki',
	'Wikipedia:Kontakt z wikipedystami',
	'Dyskusja Wikipedii:Kontakt z wikipedystami',
	'Wikipedia:Prośby o przejrzenie edycji'
];
var windowManager = null;
var api = null;

function configureMessages() {
	var useVE = config.wgNamespaceNumber !== config.wgNamespaceIds.template;

	var editAction = { 'action': 'edit' };
	if( useVE ) editAction = { 'veaction': 'edit' };

	mw.messages.set( {
		'wikibugs-dialog-title': 'Zgłaszanie błędu',
		'wikibugs-open-page-button': 'Otwórz artykuł w nowej karcie',
		'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-page-name-bottomline': 'Wpisz tytuł artykułu lub nazwę pliku, którego dotyczy zgłoszenie, dokładnie tak, jak wyświetla się u góry odpowiedniej strony, np. „Józef Piłsudski” lub „Plik:Jozef Pilsudski1.jpg”, bez żadnych dopisków i zmian.',
		'wikibugs-report-self-correct': '<p><strong>Jeśli potrafisz samodzielnie naprawić błąd – zrób to <a href="' + mw.util.getUrl( 'WP:ŚMIAŁO' ) + '" target="_blank">śmiało</a>.</strong> Kliknij [<a href="' + mw.util.getUrl( null, editAction ) + '">edytuj</a>] i do dzieła! Nie obawiaj się, że coś zepsujesz, bo to niemożliwe. Pamiętaj tylko, by zamieścić <strong><a href="' + mw.util.getUrl( 'Pomoc:Przypisy' ) + '" target="_blank">źródło</a> informacji</strong>, którą wprowadzasz.</p>',
		'wikibugs-report-content-label': '<strong>Jeśli nie dasz rady poprawić błędu, opisz go poniżej.</strong> O ile to możliwe, <strong>zachowaj sugerowany format zgłoszenia</strong>. Pomoże nam to szybciej zareagować.',
		'wikibugs-report-content-label-noedit': 'Opisz błąd poniżej. O ile to możliwe, <strong>zachowaj sugerowany format zgłoszenia</strong>. Pomoże nam to szybciej zareagować.',
		'wikibugs-report-content-placeholder': 'Opisz błąd możliwie jak najdokładniej. Jeżeli to możliwe, podaj źródło informacji.',
		'wikibugs-report-content-default': '* Jak jest: \n* Jak powinno być: \n* Uzasadnienie: ',
		'wikibugs-report-content-bottomline': 'Pamiętaj, aby podać <a href="' + mw.util.getUrl( 'WP:Ź' ) + '" target="_blank">źródło</a>, w którym będziemy mogli zweryfikować powyższe informacje. Twoje zgłoszenie będzie dostępne publicznie na stronie <a href="' + mw.util.getUrl( 'WP:ZB' ) + '" target="_blank">Wikipedia:Zgłoś błąd w artykule</a>.',
		'wikibugs-no-missing-info-warning': '<p><strong>Nie zgłaszaj tu potrzeby rozbudowy strony!</strong> Jeśli brakuje na niej czegoś ważnego, uzupełnij to samodzielnie, podając <a href="' + mw.util.getUrl( 'Pomoc:Przypisy' ) + '" target="_blank">przypis</a> do tekstu źródłowego!</p>',
		'wikibugs-no-missing-info-warning-noedit': '<p><strong>Nie zgłaszaj tu potrzeby rozbudowy strony!</strong> Jeśli brakuje na niej czegoś ważnego, napisz o tym na jej stronie dyskusji lub w <a href="' + mw.util.getUrl( 'Wikipedia:Kawiarenka/Artykuły' ) + '" target="_blank">Kawiarence</a>, podając źródła, potwierdzające Twoją propozycję.</p>',
		'wikibugs-incomplete-information': 'Opisz dokładnie zgłaszany błąd. Wprowadzony opis jest za krótki.',
		'wikibugs-invalid-page': 'Podaj nazwę strony.',
		'wikibugs-file-section-line': '<!-- Zgłoszenia błędów w plikach wstawiaj poniżej tej linii. Nowe na górze. -->',
		'wikibugs-page-section-line': '<!-- Zgłoszenia błędów w artykułach wstawiaj poniżej tej linii. Nowe na górze. -->',
		'wikibugs-copyright-link': 'Informacja o licencji',
		'wikibugs-copyright': '<p>Wysyłając zgłoszenie, wyrażasz nieodwołalną zgodę na udostępnianie Twojego wkładu ' +
			'na licencjach CC-BY-SA 3.0 i GFDL oraz na wykorzystanie go w dowolnej formie ' +
			'pod warunkiem podania przynajmniej hiperłącza lub adresu URL do strony, ' +
			'na której powstała treść. Treść ta musi być dostępna na tych zasadach, ' +
			'jeśli nie jest wynikiem Twojej samodzielnej pracy.</p>',
		'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: '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' ]
		},
		{
			modes: 'report',
			flags: [ 'safe', 'close' ]
		},
		{
			action: 'back-to-report',
			modes: 'captcha',
			flags: [ 'safe', 'back' ]
		}
	];

	if ( !isBadPage ){
		WikibugsDialog.static.actions.push( {
			action: 'open-new-tab',
			modes: 'report',
			label: mw.msg( 'wikibugs-open-page-button' ),
			icon: 'linkExternal'
		} );
	}
	
	WikibugsDialog.prototype.initialize = function () {
		WikibugsDialog.super.prototype.initialize.apply( this, arguments );
		
		// This field is normally hidden, but will display on [[WP:ZB]]
		this.titleInput = new mw.widgets.TitleInputWidget( {
			required: true,
			showMissing: false,
			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',
			help: mw.msg( 'wikibugs-page-name-bottomline' ),
			helpInline: true
		} );
		
		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,
			value: mw.msg( 'wikibugs-report-content-default' ),
		} );
		
		this.summaryInput.setValidation( function () {
			if ( this.getValue().trim().length < 20 ) {
				return false;
			}
			if ( this.getValue() === mw.msg( 'wikibugs-report-content-default' ) ) {
				return false;
			}

			this.getData().actions.get( { actions: 'submit' } )[ 0 ].setDisabled( false );
			return true;
		} );
		
		// Encourage to editing only if the user can edit the page
		var summaryLabel = mw.msg( 'wikibugs-report-content-label' );
		if(isBadPage || !canEdit) {
			summaryLabel = mw.msg( 'wikibugs-report-content-label-noedit' );
		}
		this.summaryField = new OO.ui.FieldLayout( this.summaryInput, {
			label: new OO.ui.HtmlSnippet(summaryLabel),
			help: new OO.ui.HtmlSnippet( mw.msg( 'wikibugs-report-content-bottomline' ) ),
			helpInline: true,
			align: 'top'
		} );
		
		// If this is a casual page, we don't need to ask for the page name
		// Only ask for it on report-a-bug page
		this.reportFieldset = new OO.ui.FieldsetLayout( {
			items: !isBadPage ? [ this.summaryField ] : [ this.titleField, this.summaryField ]
		} );
		
		var reportPanelContent = [ this.reportFieldset ];
		if(!isBadPage && canEdit) {
			reportPanelContent.unshift(new OO.ui.HtmlSnippet(mw.msg('wikibugs-report-self-correct')));
		}
		this.reportPanel = new OO.ui.PanelLayout( {
			padded: true,
			expanded: false,
			content: reportPanelContent
		} );
		
		if ( mw.user.isAnon() ) {
			this.reportAnonNotice = new OO.ui.HtmlSnippet( mw.msg( 'wikibugs-ip-address-notice' ) );
			this.reportPanel.$element.append( this.reportAnonNotice.toString() );
		}

		this.noMissingInfoWarning = new OO.ui.HtmlSnippet( mw.msg(
			canEdit ? 'wikibugs-no-missing-info-warning' : 'wikibugs-no-missing-info-warning-noedit'
		) );
		this.reportPanel.$element.append( this.noMissingInfoWarning.toString() );

		this.copyrightInfoButton = new OO.ui.ButtonWidget({
			label: mw.msg( 'wikibugs-copyright-link' ),
			flags: 'progressive',
			framed: false,
			indicator: 'down'
		});
		this.reportPanel.$element.append( this.copyrightInfoButton.$element );

		var $copyrightNotice = $( mw.msg( 'wikibugs-copyright' ) );
		$copyrightNotice.hide();
		this.reportPanel.$element.append( $copyrightNotice );

		var window = this;
		this.copyrightInfoButton.on('click', function () {
			var indicator = window.copyrightInfoButton.getIndicator();
			if ( indicator !== 'up' ) {
				window.copyrightInfoButton.setIndicator( 'up' );
				$copyrightNotice.show();
			} else {
				window.copyrightInfoButton.setIndicator( 'down' );
				$copyrightNotice.hide();
			}
			window.updateSize();
		});
		
		this.captchaPanel = new OO.ui.PanelLayout( {
			padded: true,
			expanded: false
		} );
		
		this.stackLayout = new OO.ui.StackLayout( {
			items: [ 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.reportPanel );
			this.getActions().setMode( 'report' );
		}, this );
	};
	
	WikibugsDialog.prototype.getActionProcess = function ( action ) {
		if ( action === 'back-to-report' ) {
			delete this.editParams;
			this.stackLayout.setItem( this.reportPanel );
			this.getActions().setMode( 'report' );
		} else if ( action === 'open-new-tab' ) {
			window.open( mw.util.getUrl( null ), '_blank' );
			return new OO.ui.Process( function () {});
		} 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(),
					reportContent = this.summaryInput.getValue().trim().replace(/\s*~~~~$/, '') + ' ~~~~',
					reportSection = mw.format( '== [[:$1' + ']] ==\n{{Status zgłoszenia|nowe}}\n\n$2', reportedPage, reportContent ),
					isFile = mw.Title.newFromText( reportedPage ).getNamespaceId() === config.wgNamespaceIds.file,
					sectionMarker = mw.msg( !isFile ? 'wikibugs-page-section-line' : 'wikibugs-file-section-line' );
				
				this.editParams = this.editParams || {
					summary: mw.msg( 'wikibugs-new-report', reportedPage )
				};
				
				return api.edit( reportPage, function ( revision ) {
					return $.extend( {
						text: revision.content.indexOf( sectionMarker ) !== -1
							? revision.content.replace( sectionMarker, sectionMarker + '\n\n' + reportSection )
							: revision.content + '\n\n' + reportSection // simply append
					}, dialog.editParams );
				} ).then( function ( res ) {
					var deferred = $.Deferred();
					
					if ( res.result === 'Failure' && 'captcha' in res ) {
						dialog.captchaWidget = new mw.libs.confirmEdit.CaptchaInputWidget( res.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 ) {
			configureMessages();
			
			api = new mw.Api( { parameters: {
				errorformat: 'html',
				errorlang: config.wgUserLanguage,
				errorsuselocal: true
			} } );
			
			windowManager = buildWindowManager();
		}
		
		windowManager.openWindow( 'wikibugs' );
	} );
}

mw.hook('userjs.wikibugs.loaded').fire(lazyOpen);
// </nowiki>