Wikipedysta:Gower/common.js

( function ( mw, $ ) {
	
var $wpTextbox1, $wpSummary;

function prettyref_run() {
	$( '#mw-editbutton-prettyref' ).attr( 'src', '//upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif' );
	
	$.ajax( {
		url: location.protocol + '//pbbot.toolforge.org/pretty-ref',
		type: 'POST',
		cache: false,
		data: {
			text: $wpTextbox1.val(),
			format: 'json'
		},
		success: prettyref_callback
	} );
}

function prettyref_callback( json ) {
	// http://bugs.jquery.com/ticket/10338
	if ( typeof json === 'string' ) {
		json = JSON.parse( json );
	}
	
	if ( json.status !== 200 ) {
		if ( json.error === 'no refs section present?' ) {
			alert( 'Nie odnaleziono sekcji z przypisami.' );
		} else {
			alert( 'Błąd (' + json.status + '): ' + json.error +
				'. Przypisy na tej stronie są nieprawidłowo sformatowane lub wykorzystują konstrukcje, które jeszcze nie są obsługiwane.' +
				'\n\n\nDodatkowe informacje (debug):\n' + json.backtrace );
		}
	} else {
		$wpTextbox1.textSelection( 'setContents', json.content );
		
		$wpSummary.val( $wpSummary.val() + ', [[Wikipedysta:Matma_Rex/prettyref.js|przeniesienie refów na koniec]]' );
		$wpSummary.val( $wpSummary.val().replace( /(^|\/\*.+?\*\/ ?), /, '$1' ) );
		
		alert( 'OK. Przed zapisaniem sprawdź wykonane zmiany!' );
	}
	
	$( '#mw-editbutton-prettyref' ).attr( 'src', '//upload.wikimedia.org/wikipedia/commons/2/2b/Button_ref_inscription.png' );
}

if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
	$.when(
		mw.loader.using( [ 'jquery.textSelection', 'ext.gadget.lib-toolbar' ] ),
		$.ready
	).done( function () {
		$wpTextbox1 = $( '#wpTextbox1' );
		$wpSummary = $( '#wpSummary' );
		
		toolbarGadget.addButton( {
			title: 'Przenieś refy na koniec',
			alt: '{{r',
			id: 'mw-editbutton-prettyref',
			icon: '//upload.wikimedia.org/wikipedia/commons/2/2b/Button_ref_inscription.png',
			onclick: function () {
				prettyref_run();
			}
		} );
	} );
}

}( mediaWiki, jQuery ) );

importScript("Wikipedysta:Msz2001/wstaw-interwiki.js");

importScript('Wikipedysta:Msz2001/missing-anchors.js');

(function () {
    if (!document.querySelector('.wikitable.dyk-verify')) return;

    mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'mediawiki.api']).then(async function () {
        var pageTitle = mw.config.get('wgPageName').replace(/_/g, ' ');
        var user = mw.config.get('wgUserName');
        if (!user) return;

        var api = new mw.Api();
        var rev = await api.get({
            action: 'query',
            prop: 'revisions',
            rvprop: ['content'],
            rvslots: 'main',
            titles: pageTitle,
            formatversion: 2
        });
        var page = rev.query.pages[0];
        if (!page || page.missing) return;

        var content = page.revisions[0].slots.main.content;
        var tplBounds = findTemplateBounds(content, 'CW/weryfikacja');
        if (!tplBounds) return;

        var tplText = content.slice(tplBounds.start, tplBounds.end);
        var alreadyRE = new RegExp('^\\|\\s*\\d+\\.\\s*sprawdzenie\\s*=\\s*.*\\b' + escapeReg(user) + '\\b', 'gmi');
        if (alreadyRE.test(tplText)) return;

        var button = new OO.ui.ButtonWidget({
            label: 'Sprawdzone',
            flags: ['progressive', 'primary'],
            icon: 'check',
            classes: ['cw-check-button']
        });

        $(document.querySelector('.wikitable.dyk-verify')).after(button.$element);

        button.on('click', function () {
            button.setDisabled(true);
            button.setLabel('Zapisywanie…');

            addCheckmark(pageTitle, user, content, tplBounds)
                .then(function () {
                    button.setLabel('✔ Dodano');
                    button.setFlags(['success']);
                    location.reload();
                })
                .catch(function (err) {
                    console.error(err);
                    button.setDisabled(false);
                    button.setLabel('Sprawdzone');
                    mw.notify('Błąd: ' + (err.message || err), { type: 'error' });
                });
        });
    });

    async function addCheckmark(title, username, content, tplBounds) {
        var api = new mw.Api();
        var before = content.slice(0, tplBounds.start);
        var tplText = content.slice(tplBounds.start, tplBounds.end);
        var after = content.slice(tplBounds.end);

        var lines = tplText.split('\n');
        var inserted = false;
        var maxIndex = 0;

        for (var i = 0; i < lines.length; i++) {
            var m = lines[i].match(/^\|\s*(\d+)\.\s*sprawdzenie\s*=\s*(.*)$/i);
            if (m) {
                var idx = parseInt(m[1], 10) || 0;
                if (idx > maxIndex) maxIndex = idx;
                if (!inserted && m[2].trim() === '') {
                    lines[i] = '| ' + idx + '. sprawdzenie = ' + username;
                    inserted = true;
                    break;
                }
            }
        }

        if (!inserted) {
            lines.splice(lines.length - 1, 0, '| ' + (maxIndex + 1) + '. sprawdzenie = ' + username);
        }

        var newContent = before + lines.join('\n') + after;

        return api.postWithToken('csrf', {
            action: 'edit',
            title: title,
            text: newContent,
            summary: 'CW: dodanie sprawdzenia przez [[User:' + username + '|' + username + ']] ' + '(za pomocą [[User:AramilFeraxa/DYKCheck.js|DYKCheck]])',
            minor: 1,
            watchlist: 'nochange',
            nocreate: 1
        });
    }

    function findTemplateBounds(text, templateName) {
        var re = new RegExp('\\{\\{\\s*' + escapeReg(templateName) + '\\b', 'i');
        var m = re.exec(text);
        if (!m) return null;
        var i = m.index, depth = 0;
        for (var pos = i; pos < text.length; pos++) {
            if (text.startsWith('{{', pos)) { depth++; pos++; }
            else if (text.startsWith('}}', pos)) { depth--; pos++; if (depth === 0) return { start: i, end: pos + 1 }; }
        }
        return null;
    }

    function escapeReg(s) {
        return String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    }
})();

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.
Kembali kehalaman sebelumnya