Benutzer:Hoo man/LiteraturstipendiumLinks.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
mw.loader.using( ['mediawiki.api', 'jquery.ui'], function() {
	var api = new mw.Api(),
		listPages = [];

	function getLinks( title, namespace, done, queryContinue, allDone ) {
		var params = {
				action: 'query',
				generator: 'links',
				titles: title,
				gpllimit: 500,
				gplnamespace: namespace,
				indexpageids: 1,
				rawcontinue: true
			};

		if ( queryContinue ) {
			params.gplcontinue = queryContinue.from;
		}

		api.get( params )
		.done( function( data ) {
			var pages = [];
			if ( data.query ) {
				for ( var i = 0; i < data.query.pageids.length; i++ ) {
					pages.push( data.query.pages[ data.query.pageids[ i ] ] );
				}
				if ( queryContinue ) {
					pages = pages.concat( queryContinue.pages );
				}
			}
			if ( data['query-continue'] ) {
				// There are more links to get
				if ( !queryContinue ) {
					queryContinue = {};
				}
				queryContinue.from = data['query-continue'].links.gplcontinue;
				queryContinue.pages = pages;
				getLinks( title, namespace, done, queryContinue, allDone );
			} else {
				done( pages, allDone );
			}
		} );
	}

	function printRow( str, method ) {
		if ( str === '' ) {
			str = '<br>';
			method = 'html';
		}
		method = method || 'text';
		$( '<p>' )
			[method]( str )
			.appendTo( '#linkCountContainer' );
	}

	function onFirstStepComplete( pages, allDone ) {
		var i,
			titles = [];
		for( i = 0; i < pages.length; i++ ) {
			if ( pages[i].missing === undefined && pages[i].title.indexOf( '/' ) !== -1 ) {
				titles.push( pages[ i ].title );
			}
		}
		
		if ( !titles.length ) {
			// Nothing to do
			printRow( '' );
			allDone();
			return;
		}

		printRow( titles.length + ' verlinkte Benutzerunterseiten gefunden' );
		printRow( '' );
		secondStep( titles, allDone );
	}

	function secondStep( titles, allDone ) {
		var title = titles.shift();
		getLinks( title, 0, function( pages ) {
			var i,
				articleLinks = [];
			for( i = 0; i < pages.length; i++ ) {
				if ( pages[i].missing === undefined ) {
					articleLinks.push( pages[ i ].title );
				}
			}
			printRow( title + '&nbsp;&nbsp;&nbsp;&nbsp;' + articleLinks.length, 'html' );
			if ( titles.length ) {
				// There are more titles to get infos about...
				secondStep( titles, allDone );
			} else {
				// All done
				printRow( '' );
				allDone();
			}
		} );
	}

	function run() {
		if ( listPages.length ) {
			var page = listPages.pop().title;
			printRow( 'Hole Links für: ' + page );
			getLinks( page, 2, onFirstStepComplete, null, run );
		} else {
			printRow( '' );
			printRow( 'Erledigt \o/' );
		}
	}

	// Init code:

	// Dialog to show the data in
	$( '<div style="overflow: scroll;" id="linkCountContainer"></div>' )
		.dialog( { width: 450, height: 600 } );

	api.get( {
		action: 'query',
		list: 'allpages',
		apprefix: 'Literaturstipendium/Vergebene Stipendien/',
		apnamespace: 4,
		aplimit: 500
	} ).done( function( data ) {
		listPages = data.query.allpages;
		run();
	} );
} );