Module:Institution

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Institution/doc

Code

--
-- This module implements {{Institution}}
--
local getArgs = require('Module:Arguments').getArgs
local wikidata = require('Module:Wikidata')
local p = {}

-- This will need {{int}} magic at some point
local param2WikiDataMap = {
		parent = 'p749',
		['native name'] = 'p1448',
		location = {'p1334','p131'},
		latitude = 'p625',
		longitude = 'p625',
		OSM = {'402', 'p1282'},
		geopoly = '',
		established = 'p571',
		website = 'p856',
		image = 'p18',
		homecat = 'p910',
		authority = '', -- go out to template
		inventory = '',
		wikidata = '',
		stub = '';
	}
-- This will need {{int}} magic at some point

local nameToLabelMap = {
		image = 'Image',
		name = 'Name',
		parent = 'Parent institution',
		['native name'] = 'Native name',
		location = 'Location',
		coordinates = 'Coordinates',
		established = 'Established',
		website = 'Website',
		authority = 'Authority',
		inventory = 'Inventory',
	}

-- Layout function
local function _render()
-- Data model for one view
--[[
local datamodel = {
		name = '',
		parent = '',
		['native name'] = '',
		coordinates = '',
		established = '',
		website = '',
		image = '',
		homecat = '', // not needed in view
		authority = '',
		inventory = '', 
		wikidata = '', // not needed in view
		stub = '' // not needed in view
	}
	--]]
    return tostring(root)
end

local function getWikiData( args )
	local n = {}
	local Q = args['wikidata']
	local institution = mw.wikibase.getEntityObject(Q)

    if not institution then
    	return n
	end
	local ulang = 'en';
	n['name'] = wikidata._getLabel(Q) or args['name']
	n['native name'] = wikidata.formatStatements({item = Q , property = 'p1448', lang = ulang}) or args['native name']
	n['parent'] = wikidata.formatStatements({item = Q , property = 'p749', lang = ulang})  or args['parent']
	n['location'] = wikidata.formatStatements({item = Q , property = 'p1334', lang = ulang}) or wikidata.formatStatements({item = Q , property = 'p131', lang = ulang})  or args['location']
	--n['coordinates'] = wikidata.formatStatements({item = Q , property = 'p625', lang = ulang}) or args['name']
	-- ^^ wikidata formatter broken, fallback to args only
	if args['latitude'] and args['longitude'] then
		n['coordinates'] = args['latitude'] .. args['longitude']
		frame:preprocess('{{Institution/coordinates|lat='.. args['latitude'] ..
			'lon=' .. args['longitude'] .. '|attributes=type:institution|OSM=' .. 
			args['osm'] .. '|GeoPoly=' .. args['geopoly'] .. '|title= }}')
	end
	-- Add OSM and GeoPoly to n['coordinates'], but coordinates doesn't support 
	--[[ 
		{{#if:{{{latitude|{{{Latitude|}}}}}}|{{#if:{{{longitude|{{{Longitude|}}} }}}|
			{{Institution/coordinates|lat={{{latitude|{{{Latitude|}}}}}}|lon={{{longitude|{{{Longitude|}}} }}}|attributes=type:institution|OSM={{{OSM|{{{osm|}}} }}}|GeoPoly={{{GeoPoly|{{{geopoly|}}} }}}|title={{{homecat|{{{Homecat|}}} }}} }} }} }}
	--]]
	n['established'] = wikidata.formatStatements({item = Q , property = 'p571', lang = ulang})  or args['established']
	n['website'] = wikidata.formatStatements({item = Q , property = 'p856', lang = ulang})  or args['website']
	n['image'] = wikidata.formatStatements({item = Q , property = 'p18', lang = ulang})  or args['image']
	n['imagesize'] = wikidata.formatStatements({item = Q , property = 'p910', lang = ulang})  or args['imagesize']
	return n
end

local function _normalizedArgs( args )
	local n = {}
	n['name'] = args['name'] or args['Name']
	n['native name'] = args['native_name'] or args['native name'] or args['Native_name'] or args['Native name']
	n['inventory'] = args['inventory'] or args['Inventory']
	n['parent'] = args['parent'] or args['Parent']
	n['location'] = args['location'] or args['Location']
	n['latitude'] = args['latitude'] or args['Latitude']
	n['longitude'] = args['longitude'] or args['Longitude']
	n['established'] = args['established'] or args['Established']
	n['website'] = args['website'] or args['Website']
	n['image'] = args['image'] or args['Image']
	n['imagesize'] = args['imagesize'] or args['Imagesize']
	n['authority'] = args['authority'] or args['Authority']
	n['wikidata'] = args['wikidata'] or args['WikiData']
	n['osm'] = args['osm'] or args['OSM']
	-- n['geopl'] = args['osm'] or args['OSM']

	return n
end

function p._institution(args)
	local normalizedArgs = _normalizedArgs( args );
	
	local data = getWikiData( normalizedArgs )

    --return _render()
    return mw.dumpObject(data);
end

-- Input function
function p.institution(frame)
	local args = getArgs(frame, {
		wrappers = 'Template:Wrapper template'
	})

	return p._institution(args)
end

function p.testcase()
	return p._institution({ wikidata = 'Q160112', ['native name'] = 'TESTING THE ADDITION', website = "http://www.google.com" } )
end
 
return p