Module:Work link

From Wikisource
Jump to navigation Jump to search

--[=[
Module description
]=]

local p = {} --p stands for package
local getArgs = require( 'Module:Arguments' ).getArgs

-- bibliographic data from WD
local Work = require( 'Module:Work' )
-- formatting of {{book links}}
local BookLink = require( 'Module:Book link' )

local function valueIn( t, v )
	for _, tv in pairs( t ) do
		if tv == v then
			return true
		end
	end
	return false
end

local function constructLink( l, v )
	if not l then
		return '[' .. '[' .. v .. ']]'
	end
	return '[' .. '[' .. l .. '|' .. v .. ']]'
end

local function getPersonList( l, thisPage )
	local ps = {}
	
	if l == nil or #l == 0 then
		return nil
	end
	
	for _, v in pairs( l ) do
		
		local a_link = getWsSiteLink( v )
		local a_text = v:getLabel()
		
		if a_link ~= thisPage then
			table.insert(ps, {
				link = a_link,
				text = a_text
			})
		end
	end
	
	return ps
end

local function formatPersonList( ps )
	
	local spans = {}
	for _, v in pairs( ps ) do
		local aspan = mw.html.create( 'span' )
				:addClass( 'wst-worklink-author' )
				:wikitext( constructLink( v.link, v.text ) )
		table.insert( spans, tostring( aspan ) )	
	end
	
	return table.concat( spans, ', ' )
end

--[=[
Function docs
]=]
function p.link(frame)
	local args = getArgs(frame)
	
	local qidOrTitle = args[1]
	
	local workObject = Work.newWork( qidOrTitle )
	
	if workObject == nil then
		local cat = "[" .. "[Category:Work links with missing Wikidata items]]"
		return "<span class='error'>Unable to load Wikidata item '" .. qidOrTitle .. "'.</span>" .. cat
	end
	
	-- use the given param, or fallback to the current page
	local thisPage = args['on page']
	if not thisPage then
		thisPage = mw.title.getCurrentTitle().prefixedText
	end
	
	if workObject.type == Work.TYPES.EDITION then
		-- edition -> FRBR edition
		
		-- Translate "pure" Work data into the arguments to a BookLink
		args = {
			-- if the page doesn't exist, assume it'll be the title
			title = workObject.wsPage or workObject.title,
			display = workObject.title,
			year = workObject.pubYear,
			authors = workObject.author,
			editors = workObject.editor,
			translators = workObject.translator,
			illustrators = workObject.illustrator,
			classes = { 'wst-worklink-frbr-edition' }
		}
		
		local res = BookLink.renderLink(args)
			:attr( 'data-wikidata-id', workObject.item.id )
		
		return tostring(res)
	elseif workObject.type == Work.TYPES.ARTICLE then
		return 'article'
		
	-- 	-- some kind of paper or article
	
	-- 	local link = getWsSiteLink( item )
	-- 	local title = getLocalStringProp( item, PROPS.title )
		
	-- 	local periodicalQid = getPropIds( item, PROPS.publishedIn )[ 1 ]
	-- 	local periodical = mw.wikibase.getEntity( periodicalQid )
	-- 	local p_link = getWsSiteLink( periodical )
	-- 	local p_disp = periodical:getLabel()
		
	-- 	if not p_link then
	-- 		p_link = p_disp	
	-- 	end
		
	-- 	local authors = getPropEntities( item, PROPS.author )
		
	-- 	local al_args = {
	-- 		periodical = p_link,
	-- 		display_periodical = p_disp,
	-- 		author = formatPersonList( getPersonList( authors, thisPage ) )
	-- 	}
		
	-- 	local volume = getStringProp( item, PROPS.volume )
	-- 	if volume then
	-- 		al_args.volume = volume
	-- 	end
	-- 	local issue = getStringProp( item, PROPS.issue )
	-- 	if issue then
	-- 		al_args.issue = issue
	-- 	end
		
	-- 	local pages = getStringProp( item, PROPS.pages )
	-- 	if pages then
	-- 		local parts = mw.text.split( pages, '-' )
	-- 		if #parts == 1 then
	-- 			al_args.p = pages
	-- 		else
	-- 			al_args.p = parts[1]
	-- 			al_args.pp = parts[2]
	-- 		end
	-- 	end
		
	-- 	if link then
	-- 		-- don't bother with all the guessing
	-- 		al_args.direct_link = link
	-- 	else
	-- 		al_args.article = title
	-- 	end
	-- 	-- mw.logObject( al_args )
		
	-- 	return frame:expandTemplate{ title = 'article link', args = al_args }
	else
		-- we just assume this is some kind of generic work
		-- edition -> FRBR work
		
		-- Translate "pure" Work data into the arguments to a BookLink
		args = {
			-- if the page doesn't exist, assume it'll be the title
			title = workObject.wsPage or workObject.title,
			display = workObject.title,
			year = workObject.pubYear,
			authors = workObject.author,
			editors = workObject.editor,
			translators = workObject.translator,
			illustrators = workObject.illustrator,
			classes = { 'wst-worklink-frbr-work' }
		}
		
		local res = BookLink.renderLink(args)
			:attr( 'data-wikidata-id', workObject.item.id )
			
		local cat = "[" .. "[Category:Work links with Wikidata items that look like FRBR works]]"
		
		return tostring(res) .. cat
	end

	return ''
end

return p