MediaWiki-diskusjon:Gadget-verifiable.js

Sideinnholdet støttes ikke på andre språk.
Fra Wikipedia, den frie encyklopedi

Forslag til oppdatering av koden[rediger kilde]

Nedenfor er det et forslag til oppdatering av koden. Denne gjør det mulig å slå av notisen for bestemte kategorier («Pekere» og «Verifisert»). Det er også lagt inn en fiks slik at notisen ikke vises flere ganger for samme side, hvis ikke brukeren redigerer siden. — Jeblad 5. des. 2015 kl. 23:44 (CET)[svar]

Oppskrift
Forslag til oppdatert code, denne legges på siden MediaWiki:Gadget-verifiable.js (subjektsiden)
// Code to make a notice about verfiabillity
// © John Erling Blad, Creative Commons by Attribution 3.0

// the following var will be implicit wrapped in an outer function
// this happen as part of the Gadget production system
var callIndex = 0;

mw.hook( 'wikipage.content' ).add(function( $content ) {
	"use strict";
	var sentCountDenom = 15;
	var textSizeDenom = 1000;
	var htmlSizeDenom = 2000;
	var weight = 5;
	var failing = 0;
	var excludeCategories = { 'Pekere':true, 'Verifisert':true };
	var prefix = 'nowiki-verify-';
	var api = new mw.Api();
	var conf = mw.config.get( [
		'wgUserLanguage',
		'wgAction',
		'wgNamespaceNumber',
		'wgContentNamespaces',
		'wgArticleId',
		'wgCategories'
	] );
	// should be in a content namespace
	if ( conf.wgContentNamespaces.indexOf( conf.wgNamespaceNumber ) < 0 ) {
		mw.log( 'not in a content namespace, terminates' );
		return;
	}
	// create an article id for our own use
	var aid = prefix + conf.wgArticleId;
	// purge the articles view count in the session store
	if ( conf.wgAction === "edit" || mw.util.getParamValue( 'veaction' )) {
		if ( ++callIndex === 1 ) {
			sessionStorage.removeItem( aid );
			mw.log( 'editing article, terminates' );
			return;
		}
	}
	// should only be shown during ordinary view
	if ( conf.wgAction !== "view" ) {
		mw.log( 'not a view action, terminates' );
		return;
	}
	// should not be shown if the article id is logged in the session store
	if ( sessionStorage.getItem( aid ) !== null ) {
		mw.log( 'already seen, terminates' );
		return;
	}
	// should not be shown during diff actions
	if ( mw.util.getParamValue( 'type' )
		|| mw.util.getParamValue( 'diff' )
		|| mw.util.getParamValue( 'redirect' )
	) {
		mw.log( 'seems to be something fishy, terminates' );
		return;
	}
	// check if there any missing source/reference templates
	if ($content.find('.ambox-Unsourced, .ambox-Unreferenced').size() > 0) {
		mw.log( 'already has a unsourced/unreferenced template, terminates' );
		return;
	}
	// check if the category should be excluded
	if ( conf.wgCategories !== undefined ) {
		mw.log('found categories');
		for (var i=0,l=conf.wgCategories.length;i<l;i++) {
			if (conf.wgCategories[i] in excludeCategories) {
				mw.log( 'found among excluded categories, terminates' );
				return;
			}
		}
	}
	// count sources
	var $headers = $content.find('#Litteratur, #Kilder, #Kilde').parent();
	var $items = $headers.nextUntil('h2').filter('ul').children('li');
	var numSrcs = $items.size();
	mw.log('numSrcs: '+numSrcs);
	// count references
	var numRefs = $content.find('.references').children().size();
	mw.log('numRefs: '+numRefs);
	// text size
	var text = $content.children('p').text();
	if (text === null) {
		mw.log( 'no text found, terminates' );
		return;
	}
	var textSize = text.replace(/\s+/g, '').length; // eg. "anslag"
	mw.log('textSize: '+textSize);
	mw.log('textSize norm: '+textSize/textSizeDenom);
	// count sentences
	var sentCount = text.length - text.replace(/[.,:;!?]/g, '').length;
	mw.log('sentCount: '+sentCount);
	mw.log('sentCount norm: '+sentCount/sentCountDenom);
	// text size
	var html = $content.html();
	if (html === null) {
		mw.log( 'no html found, terminates' );
		return;
	}
	var htmlSize = html.replace(/<[^>]+>/g, '').replace(/\s+/g, ' ').length;
	mw.log('htmlSize: '+htmlSize);
	mw.log('htmlSize norm: '+htmlSize/htmlSizeDenom);
	var limit = Math.sqrt(Math.pow(numRefs, 2) + Math.pow(weight*numSrcs, 2));
	mw.log('limit: '+limit);
	if (limit < textSize/textSizeDenom) {
		failing++;
	}
	if (limit <sentCount/sentCountDenom) {
		failing++;
	}
	if (limit < htmlSize/htmlSizeDenom) {
		failing++;
	}
	// lets see if the tests are failing consistemtly
	mw.log('failing: '+failing);
	if (failing <= 1) {
		mw.log( 'failing consistently, that is good article, terminates' );
		return;
	}
	// still around, get additional messages
	api.ajax({
		action: 'query',
		meta: 'allmessages',
		amlang: mw.config.get( 'wgUserLanguage' ),
		ammessages: [
			'maintenance-verifiable-title',
			'maintenance-verifiable-desc'
		]
	})
	.then(function(data){
		// procede and create the warning
		mw.log( 'build the warning: '+conf.wgTitle );
		$.each( data.query.allmessages, function ( index, message ) {
			if ( message.missing !== '' ) {
				mw.messages.set( message.name, message['*'] );
			}
		});
		mw.notify(
			mw.message( 'maintenance-verifiable-desc' ).escaped(),
			{
				title:mw.message( 'maintenance-verifiable-title' ).escaped(),
				tag: 'verifiable'
			}
		);
		mw.log( 'sets the aid as shown for: '+aid );
		sessionStorage.setItem( aid, 'shown' );
	});
});
Kode lagt inn. Cocu (d) 5. des. 2015 kl. 23:56 (CET)[svar]
Denne dukker opp under redigering av omdirigeringer, så har lagt inn et lite tillegg. — Jeblad 12. des. 2015 kl. 18:24 (CET)[svar]
Kode oppdatert. Cocu (d) 12. des. 2015 kl. 21:07 (CET)[svar]