Jump to content

MediaWiki:Gadget-libSensitiveIPs.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 03:50, 14 September 2016 (add empty arrays and objects for results with no matches; fix isSensitive data property name). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
( function ( $, mw, undefined ) {
	'use strict';
	var sensitiveIPs;
	sensitiveIPs = mw.libs.sensitiveips = {
		query: function ( params ) {
			if ( !( params instanceof Object ) ) {
				throw new TypeError( "type error in arg #1 to 'query' (object expected)" );
			}
			params.format = 'json';
			return mw.libs.lua.call( {
				format: 'json',
				module: 'Sensitive IP addresses/API',
				func: 'query',
				args: [ params ]
			} ).then( function ( data ) {
				// Add blank arrays and objects for queries that didn't return
				// a match. This can't be done in Lua, as Scribunto's
				// mw.text.jsonEncode can't distinguish between empty arrays
				// and empty objects due to the nature of Lua tables.
				data.sensitiveips.matches = data.sensitiveips.matches || [];
				data.sensitiveips['matched-ranges'] = data.sensitiveips['matched-ranges'] || {};
				data.sensitiveips.entities = data.sensitiveips.entities || {};
				data.sensitiveips['entity-ids'] = data.sensitiveips['entity-ids'] || [];
			} );
		},

		isSensitive: function ( s ) {
			return sensitiveIPs.query( { test: [ s ] } ).then( function ( data ) {
				return data.sensitiveips.matches.length > 0;
			} );
		}
	};
} )( jQuery, mediaWiki );