MediaWiki:Gadget-subjects.js

$(document).ready( function {	var $links = $( '#booklinks' ).find( 'a' ), titles, categories = [		'Freshly started books',		'Partly developed books',		'Half-finished books',		'Books nearing completion',		'Completed books',		'Featured books',		'Books with print version',		'Books with PDF version'	];

function showSubjectMetadataCB(obj) { $.each( obj.query && obj.query.pages || [], function( id, page ) {			var $link = [];

if ( !page.categories ) { return; }

$links.each( function {				var $this = $(this);				if ( $this.attr( 'title' ) === page.title ) {					$link = $this;					return false;				}			});

if ( !$link.length ) { return; }

$.each( page.categories, function( key, cat) {				var title = cat.title;

switch ( title ) { case 'Category:Freshly started books': $link.before( $( ' ' ).addClass( 'book-level-0' ) ); break; case 'Category:Partly developed books': $link.before( $( ' ' ).addClass( 'book-level-1' ) ); break; case 'Category:Half-finished books': $link.before( $( ' ' ).addClass( 'book-level-2' ) ); break; case 'Category:Books nearing completion': $link.before( $( ' ' ).addClass( 'book-level-3' ) ); break; case 'Category:Completed books': $link.before( $( ' ' ).addClass( 'book-level-4' ) ); break; case 'Category:Featured books': $link.before( $( ' ' ).addClass( 'book-level-5' ) ); break; case 'Category:Books with print version': $link.after( $( ' ' ).addClass( 'book-print' ) ); break; case 'Category:Books with PDF version': $link.after( $( ' ' ).addClass( 'book-pdf' ) ); break; }			});		});	}

if ( !$links.length ) { return; }

titles = $.map( $links, function( link ) { return $(link).attr( 'title' ); } ).join( '|' ); categories = 'Category:' + categories.join( '|Category:' );

$.getJSON( mw.util.wikiScript( 'api' ), {		'format':		'json',		'action':		'query',		'prop':			'categories',		'clshow':		'hidden',		'cllimit':		500,		'clcategories':		categories,		'titles':		titles	}, showSubjectMetadataCB); });