MediaWiki:Gadget-verifiable.js

Fra Wikipedia, den frie encyklopedi

Merk: Etter publisering vil det kanskje være nødvendig å slette mellomlageret i nettleseren din for å se endringene.

  • Firefox / Safari: Hold Shift mens du klikker på Oppdater, eller trykk enten Ctrl+F5 eller Ctrl+R (⌘+R på Mac)
  • Google Chrome: Trykk Ctrl+Shift+R (⌘+Shift+R på Mac)
  • Internet Explorer / Edge: Hold Ctrl mens du trykker på Oppdater eller trykk Ctrl+F5
  • Opera: Ttrykk Ctrl+F5.
// 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";
	$content = $content.find( '.mw-parser-output' );
	if ( !$content.length ) return;
	var sentCountDenom = 15;
	var textSizeDenom = 1000;
	var htmlSizeDenom = 2000;
	var weight = 5;
	var failing = 0;
	var excludeCategories = { 'Pekere':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').length > 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.length;
	// mw.log('numSrcs: ' + numSrcs);
	// count references
	var numRefs = $content.find('.references').children().length;
	// 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.loadMessagesIfMissing( [ 'maintenance-verifiable-title', 'maintenance-verifiable-desc' ] )
	.then( function() {
		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' );
	});
});