Lompat ke isi

Modul:Index data

Dari Wikisumber bahasa Indonesia, perpustakaan bebas

Dokumentasi untuk modul ini dapat dibuat di Modul:Index data/doc

local wikidataTypeToIndexType = {
	['Q3331189'] = 'book',
	['Q1238720'] = 'journal',
	['Q28869365'] = 'journal',
	['Q191067'] = 'journal',
	['Q23622'] = 'dictionary',
	['Q187685'] = 'phdthesis'
}
--You can remove unwanted fields by adding -- at the beginning of the respective live. For example, 
--    ['publishedin'] = 'P253129',
local indexToWikidata = {
    ['subtitle'] =  'P392',
    ['volume'] = 'P478',
    ['edition'] = 'P393',   
    ['author'] = 'P50',
    -- ['author'] = 'P2093',
    ['translator'] = 'P655',
    ['editor'] = 'P98',
    ['illustrator'] = 'P110',
    ['publisher'] = 'P123',
    ['printer'] = 'P872',
    ['address'] = 'P291',
    -- ['publishedin'] = 'P253129',
    ['year'] = 'P577',
    -- ['viaf'] = 'P752',
    ['parts'] = 'P747',
}

function indexDataWithWikidata(frame)
	local args = {}
	for k,v in pairs(frame.args) do
		if v ~= '' then
			args[k] = v
		end
	end
	
	local item = nil
	if args.wikidata_item then
		item = mw.wikibase.getEntity(args.wikidata_item)
		if item == nil then
			mw.addWarning('The Wikidata entity identifier [[d:' .. args.wikidata_item .. '|' .. args.wikidata_item .. ']] put in the "Wikidata entity" parameter of the Book page: does not seem valid.') 
		end
	end
	if not item then
		return {
			['args'] = args,
			['item'] = nil
		}
	end

	if not args.type then
		for _, statement in pairs(item:getBestStatements('P31')) do
			if statement.mainsnak.datavalue ~= nil then
				local typeId = statement.mainsnak.datavalue.value
				if wikidataTypeToIndexType[typeId] then
					args.type = wikidataTypeToIndexType[typeId] 
				end
			end
		end
	end
	
	if not args.image then
		for _, statement in pairs(item:getBestStatements('P18')) do
			if statement.mainsnak.datavalue.value ~= nil then
				args.image = statement.mainsnak.datavalue.value
			end
		end
	end
	
	if not args.title then
		local value = item:formatStatements('P1476')['value'] or ''
		if value == '' then
			value = item:getLabel() or ''
		end
		if value ~= '' then
			local siteLink =  item:getSitelink()
			if siteLink then
				value = '[[' .. siteLink .. '|' .. value .. ']]'
			end
--Please translate the text "View and edit data on Wikidata" into your language.
			args.title = value .. ' [[File:OOjs UI icon edit-ltr.svg|Lihat dan sunting data ini di Wikidata|10px|baseline|class=noviewer|link=d:' .. item.id .. '#P1476]]'
		end
	end

    if not args.year then
		for _, statement in pairs(item:getBestStatements('P577')) do
			if statement.mainsnak.datavalue ~= nil then
				local current_year = statement.mainsnak.datavalue.value.time
                args['year'] = mw.ustring.sub(current_year, 2, 5)
			end
		end
    end

	for arg, propertyId in pairs(indexToWikidata) do
		if not args[arg] then
			local value = item:formatStatements(propertyId)["value"]
			if value ~= '' then
				args[arg] = value 
			end
		end
	end
	

	return {
		['args'] = args,
		['item'] = item
	}
end

local p = {}
 
function p.indexDataWithWikidata(frame)
    return indexDataWithWikidata(frame)
end
 
return p