Utente:Lucarosty/lw-src.js

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca

Questa pagina definisce alcuni parametri di aspetto e comportamento generale di tutte le pagine. Per personalizzarli vedi Aiuto:Stile utente.


Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti (per le pagine globali è comunque necessario attendere qualche minuto). Per Mozilla / Firefox / Safari: fare clic su Ricarica tenendo premuto il tasto delle maiuscole, oppure premere Ctrl-F5 o Ctrl-R (Command-R su Mac); per Chrome: premere Ctrl-Shift-R (Command-Shift-R su un Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menù Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.

/**
 * Highlight revisions by their scores
 *
 * @author: Lucarosty
 * @license: CC BY-SA 3.0 <https://creativecommons.org/licenses/by-sa/3.0/>
 * based on the ores script written by Helder (https://github.com/he7d3r)
 */
(
	function ( mw, $ ) { 
		'use strict';
		var showScores = mw.util.getParamValue( 'showscores' ) !== '0',
			models = [ 'damaging' ],
			chosenModels = [ 'damaging', 'reverted' ],
			conf = mw.config.get( [
				'wgIsArticle',
				'wgCurRevisionId',
				'wgCanonicalSpecialPageName',
				'wgDBname',
				'wgAction',
				'ScoredRevisionsThresholds',
				'ScoredRevisionsServerUrl',
				'ScoredRevisionsEnableForPatrolledRevs' // Currently broken
			] ),
			serverUrl = conf.ScoredRevisionsServerUrl || '//api.wikimedia.org/service/lw/inference/v1/models/',
			enabledOnCurrentPage = showScores && (
					$.inArray( conf.wgCanonicalSpecialPageName, [
						'Watchlist',
						'Recentchanges',
						'Recentchangeslinked',
						'Contributions'
					] ) !== -1 ||
					conf.wgAction === 'history' ||
					( conf.wgIsArticle && conf.wgAction === 'view' )
				),
	        idsOnPage = [],
	        idsGet = [],
	        changes = {},
	        old_changes = {},
			thresholds = conf.ScoredRevisionsThresholds ||
				{
					low: 0.45,
					medium: 0.58,
					high: 0.80
				};
		function getRevIdsFromCurrentPage() {
			var dfd = $.Deferred(),
				idsFound = {},
				pageids = {},
				isChangesList = conf.wgCanonicalSpecialPageName === 'Watchlist' ||
					conf.wgCanonicalSpecialPageName === 'Recentchanges' ||
					conf.wgCanonicalSpecialPageName === 'Recentchangeslinked',
				/*jshint eqeqeq:false*/
				container = isChangesList ?
					'.mw-changeslist' :
					conf.wgCanonicalSpecialPageName === 'Contributions' ?
						'.mw-contributions-list' :
						'#pagehistory',
				// This "usenewrc" can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
				rowSelector = mw.user.options.get( 'usenewrc' ) == 1 && isChangesList ?
					'tr' :
					'li',
				linkSelector = conf.wgCanonicalSpecialPageName === 'Contributions' ||
					conf.wgAction === 'history' ?
					'a.mw-changeslist-date' :
					'a',
				filterPatrolled = $( '.unpatrolled' ).length
					&& !conf.ScoredRevisionsEnableForPatrolledRevs;
	
			if ( conf.wgIsArticle && conf.wgAction === 'view' ) {
				changes[ conf.wgCurRevisionId ] = $( '#ca-history a' );
				return dfd.resolve( [ conf.wgCurRevisionId ] ).promise();
			}
			$( container )
				.find( rowSelector )
				.filter( function () {
					var $row = $( this );
					if ( $row.hasClass( 'wikibase-edit' ) ) {
						// Skip external edits from Wikidata
						return false;
					}
					/* TODO: The following filter is not functional
					if ( filterPatrolled && !$row.has( '.unpatrolled' ).length ) {
						// skip patrolled edits
						return false;
					}
					*/
					return true;
				} )
				.each( function () {
					var $row = $( this ),
						id, pageid;
	
					$row.find( linkSelector )
						.each( function () {
							var href = $( this ).attr( 'href' );
							id = mw.util.getParamValue( 'diff', href );
							if ( id === 'prev' || conf.wgCanonicalSpecialPageName === 'Contributions' ||
								conf.wgAction === 'history' ) {
								id = mw.util.getParamValue( 'oldid', href );
							}
							if ( id && /^([1-9]\d*)$/.test( id ) ) {
								// Found a revid, stop
								return false;
							} else if ( !pageid ) {
								pageid = mw.util.getParamValue( 'curid', href );
							}
						} );
					// use id or pageid
					if ( id ) {
						changes[ id ] = $row;
						idsFound[ id ] = true;
					} else if ( pageid && pageid !== '0') {
						pageids[ pageid ] = $row;
					}
				} );
			if ( $.isEmptyObject( pageids ) ) {
				dfd.resolve( Object.keys( idsFound ) );
			} else {
				$.getJSON( mw.util.wikiScript( 'api' ), {
					format: 'json',
					action: 'query',
					prop: 'revisions',
					// FIXME: the API does not allow using this with multiple pageids
					// rvdir: 'newer',
					rvprop: 'ids',
					pageids: Object.keys( pageids ).join( '|' )
				} )
				.done( function ( data ) {
					if ( data && data.query && data.query.pages ) {
						$.each( data.query.pages, function ( pageid, page ) {
							var id = page.revisions[ 0 ].revid;
							if ( !changes[ id ] ) {
								changes[ id ] = pageids[ pageid ];
								idsFound[ id ] = true;
							}
						} );
					}
				} )
				.always( function () {
					dfd.resolve( Object.keys( idsFound ) );
				} );
			}
			return dfd.promise();
		}
		function processScores( data ) {
			var i, revid, m, score, scoreData, scoreTitles, classes,
				idsWithScores = Object.keys( data[conf.wgDBname].scores );
			if ( data.error ) {
				mw.log.error( data.error );
				return;
			}
			for (const [revid, scoreData] of Object.entries(data[conf.wgDBname].scores)) {
				classes = [];
				scoreTitles = [];
				for ( m = 0; m < models.length; m++ ) {
					if ( !scoreData || scoreData.error || !scoreData[ models[ m ] ] || scoreData[ models[ m ] ].error ) {
						continue;
					} else {
						score = scoreData[ models[ m ] ].score.probability[ 'true' ];
					}
					scoreTitles.push( ( 100 * score ).toFixed( 0 ) + '% ' + models[ m ] );
					// Allow users to customize the style (colors, icons, hide, etc) using classes
					// 'sr-reverted-high', 'sr-reverted-medium', 'sr-reverted-low' and 'sr-reverted-none'
					// 'sr-damaging-high', 'sr-damaging-medium', 'sr-damaging-low' and 'sr-damaging-none'
					classes.push(
						score >= thresholds.high ?
							'sr-' + models[ m ] + '-high' :
							score >= thresholds.medium ?
								'sr-' + models[ m ] + '-medium' :
								score >= thresholds.low ?
									'sr-' + models[ m ] + '-low' :
									'sr-' + models[ m ] + '-none'
					);
				}
				changes[ revid ]
					.addClass( classes.join( ' ' ) )
					.attr( 'title', 'Scores: ' + scoreTitles.join( '; ' ) );
			}
		}
		function load() {
			var i = 0,
				scoreBatch = function ( idsOnBatch, models ) {
					$.post( {
						url: serverUrl + conf.wgDBname + '-' + models + ":predict",
						data: JSON.stringify({
							rev_id: parseInt(idsOnBatch)
						}),
						dataType: 'json',
						contentType: 'text/plain'
					} )
					.done( function ( data ) {
						processScores( data );
					} )
					.fail( function () {
						mw.log.error( 'The request failed.', arguments );
					} );
				};
			mw.loader.load( '//meta.wikimedia.org/w/index.php?title=User:He7d3r/Tools/ScoredRevisions.css&action=raw&ctype=text/css', 'text/css' );
			getRevIdsFromCurrentPage()
			.done( function ( idsFromPage ) {
				idsOnPage = idsFromPage;
				if ( idsOnPage.length ) {
					let idsToGet = [];
					for (let j = 0; j < idsOnPage.length; j++) {
						if (!idsGet.includes(idsOnPage[ j ])) {
							idsToGet.push(idsOnPage[ j ]);
						}
						else {
							changes[ idsOnPage[ j ] ].attr('class', old_changes[ idsOnPage[ j ] ].attr('class'));
							changes[ idsOnPage[ j ] ].attr('title', old_changes[ idsOnPage[ j ] ].attr('title'));
						}
					}
					if (idsToGet.length) {
						for (i = 0; i < idsToGet.length; i++)
							scoreBatch( idsToGet[i], "damaging" );
					}
					idsGet = idsOnPage;
					old_changes = Object.assign({}, changes);
				}
			} );
		}
		
		if ( enabledOnCurrentPage ) {
			mw.hook( 'wikipage.content' ).add( load );
		}
	}( mediaWiki, jQuery ) );