User:Ricordisamoa/WikidataTrust.js: Difference between revisions

From Wikidata
Jump to navigation Jump to search
Content deleted Content added
Ricordisamoa (talk | contribs)
fix labels and descriptions, other changes
Ricordisamoa (talk | contribs)
fix sitelinks
Line 145: Line 145:


$.each( newSitelinks, function ( i, e ) {
$.each( newSitelinks, function ( i, e ) {
var $el = $( 'tr.wb-sitelinks-' + e.split( ':' )[0] ).children().last()
var $el = $( '.wikibase-sitelinkview.wikibase-sitelinkview-' + e.split( ':' )[0] )
.children().first();
.children( '.wikibase-sitelinkview-link' ).last();
addLink( $el, function ( arg ) {
addLink( $el, function ( arg ) {
return $el.append( arg );
return $el.append( arg );
} );
}, { float: 'right' } );
} );
} );



Revision as of 01:14, 22 February 2015

/* <nowiki>
 * WikidataTrust.js by [[User:Ricordisamoa]]
 * uses jQuery & Ajax
 *
 * inspired by the original WikiTrust (www.wikitrust.net) by the University of California, Santa Cruz
*/
/* global mw, $ */
$( document ).ready( function () {
	if( !mw.config.exists( 'wbEntityId' ) ) {
		return;
	}
	var revisions = [],
	api = new mw.Api(),
	params = {
		prop: 'revisions',
		titles: mw.config.get( 'wgPageName' ),
		rvprop: 'content|user|ids',
		rvlimit: 'max'
	},
	getRevisions = function ( callback, cont ) {
		if ( cont ) {
			$.extend( params, cont );
		}
		api.get( params )
		.done( function ( data ) {
			revisions = revisions.concat( data.query.pages[Object.keys( data.query.pages )[0]].revisions );
			if ( data['query-continue'] && data['query-continue'].revisions ) {
				getRevisions( callback, data['query-continue'].revisions );
			} else {
				callback( revisions );
			}
		} );
	};
	getRevisions( function ( pages ) {
		//console.log( 'WikidataTrust: ' + pages.length + ' revisions to check.' );
		var mapClaims = function ( claims ) {
			return $.map( claims, function ( claim ) {
				return claim.id;
			} );
		},
		mapStrings = function ( v, k ) {
			return k + ':' + v;
		};
		$.each( pages, function ( y, z ) {
			if ( z === undefined || z['*'] === undefined ) {
				return;
			}
			var rev = JSON.parse( z['*'] ),
			currClaims = $.map( rev.claims || [], mapClaims ),
			currSitelinks = $.map( rev.sitelinks || [], mapStrings ),
			currLabels = $.map( rev.labels || [], mapStrings ),
			currDescriptions = $.map( rev.descriptions || [], mapStrings );
			/*
			console.log( 'claims: ' + currClaims );
			console.log( 'sitelinks: ' + currSitelinks );
			console.log( 'labels: ' + currLabels );
			console.log( 'descriptions: ' + currDescriptions );
			*/
			var prevClaims = [],
			prevSitelinks = [],
			prevLabels = [],
			prevDescriptions = [];
			if ( y < pages.length - 1 && pages[y + 1]['*'] !== undefined ) {
				var prevRev = JSON.parse( pages[y + 1]['*'] );
				prevClaims = $.map( prevRev.claims || [], mapClaims );
				prevSitelinks = $.map( prevRev.sitelinks || [], mapStrings );
				prevLabels = $.map( prevRev.labels || [], mapStrings );
				prevDescriptions = $.map( prevRev.descriptions || [], mapStrings );
			}
			var newClaims = $( currClaims ).not( prevClaims ).get(),
			newSitelinks = $( currSitelinks ).not( prevSitelinks ).get(),
			newLabels = $( currLabels ).not( prevLabels ).get(),
			newDescriptions = $( currDescriptions ).not( prevDescriptions ).get();
			/*
			console.log( 'prevClaims: ' + prevClaims );
			console.log( 'newClaims by ' + pages[y].user + ': ' + newClaims );
			console.log( 'prevSitelinks: ' + prevSitelinks );			
			console.log( 'newSitelinks by ' + pages[y].user + ': ' + newSitelinks );
			console.log( 'prevLabels: ' + prevLabels );
			console.log( 'newLabels by ' + pages[y].user + ': ' + newLabels );
			console.log( 'prevDescriptions: ' + prevDescriptions );
			console.log( 'newDescriptions by ' + pages[y].user + ': ' + newDescriptions );
			*/
			var userLink = $( '<a>' )
			.text( pages[y].user )
			.attr( 'href', mw.util.getUrl(
				( mw.util.isIPv4Address( pages[y].user ) || mw.util.isIPv6Address( pages[y].user ) ? 'Special:Contributions/' : 'User:' ) +
				pages[y].user
			) ),

			addLink = function ( $el, func, css ) {
				if ( $el.find( '.wikidata_trust-author' ).length === 0 ) {
					func(
						$( '<small>' )
						.addClass( 'wikidata_trust-author' )
						.css( css || {'margin-left': '2em'} )
						.text( 'by ' )
						.append( userLink.clone( true ) )
					);
				} else {
					var $attrib = $el.find( '.wikidata_trust-author' ),
					$links = $attrib.find( 'a' ),
					$sameLinks = $links.filter( function () {
						return $( this ).text() === pages[y].user;
					} );
					if ( $sameLinks.length === 0 ) {
						$attrib
						.append( ' and ' )
						.append( userLink.clone( true ) );
					}
				}
			},

			addLinkLabel = function ( e ) {
				var lang = e.split( ':' )[0],
				$el = $( '.wikibase-entityview[lang="' + lang + '"] .wb-firstHeading,' +
					'.wikibase-fingerprintview-' + lang )
				.first()
				.find( '.wikibase-labelview-container' )
				.first();
				addLink( $el, function ( arg ) {
					return $el.append( '<br/>' ).append( arg );
				} );
			},
			addLinkDescription = function ( e ) {
				var lang = e.split( ':' )[0],
				$el = $( '.wikibase-entityview[lang="' + lang +
					'"] .wb-firstHeading+.wikibase-descriptionview,' +
					'.wikibase-fingerprintview-' + lang )
				.first()
				.find( '.wikibase-descriptionview-container' )
				.first();
				addLink( $el, function ( arg ) {
					return $el.append( arg );
				} );
			};

			$.each( newLabels, function ( i, e ) {
				addLinkLabel( e );
			} );

			$.each( newDescriptions, function ( i, e ) {
				addLinkDescription( e );
			} );

			$.each( newSitelinks, function ( i, e ) {
				var $el = $( '.wikibase-sitelinkview.wikibase-sitelinkview-' + e.split( ':' )[0] )
					.children( '.wikibase-sitelinkview-link' ).last();
				addLink( $el, function ( arg ) {
					return $el.append( arg );
				}, { float: 'right' } );
			} );

			$.each( newClaims, function ( i, id ) {
				var $el = $( '.wb-claim' )
				.filter( function () {
					return $( this ).hasClass( 'wb-claim-' + id );
				} )
				.parents( '.wb-claimlistview' ).first().find( '.wb-claim-name' ).first();
				if ( $el.find( '.wikidata_trust-author' ).length === 0 ) {
					addLink( $el, function ( arg ) {
						return $el.append( '<br/>' ).append( arg );
					}, {} );
				} else {
					addLink( $el );
				}
			} );

			//var removedLinks = $( nextLinks ).not( links ).get();
		} );
	} );
} );