יחידה:אלבום

מתוך ויקיפדיה, האנציקלופדיה החופשית

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:אלבום/תיעוד

local Arguments = require( "Module:Arguments" )
local PropertyLink = require( "Module:PropertyLink" )
local Date = require('Module:תאריך')

function isParsableDuration(value)
	return value and type(value) == "string" and string.find(value, "שנייה")
end

-- fetch album duration from wikidata and return it in "mm:ss" format
function duration(frame)
	local property = frame.args["property"] or "P2047"
	local id = frame.args["id"]
	
	local value = PropertyLink.getProperty(property, false, false, id)
	if not isParsableDuration(value) then return nil end
	
	local totalSeconds = tonumber(value:gsub(" שנייה", ""):gsub(",", ""), 10)
	if not totalSeconds then return nil end
	local minutes = math.floor(totalSeconds/60)
	local seconds = math.floor(totalSeconds%60)
	
	local formattedDuration = string.format("%02d", minutes)..":"..string.format("%02d", seconds)
	local entity = mw.wikibase.getEntityObject(id)
	if not entity then return formattedDuration end
	local link = mw.title.makeTitle( 0, entity.id, property, 'wikidata' ):fullUrl('uselang=he')
	return formattedDuration .. mw.ustring.format(' [[File:Blue pencil RTL.svg|15px|link=%s|עריכת הנתון בוויקינתונים]]', link)
end

--Get year of album based on the album name
local function getYear(album) 
	if album == nil then return end
	album = mw.ustring.gsub(album, '%[%[(.+)%|.+%]%]', '%1')
	album = mw.ustring.gsub(album, '%[%[(.+)%]%]', '%1')
	local albumQid = mw.wikibase.getEntityIdForTitle( album )
	if albumQid == nil then return end
	local albumYearStatements = mw.wikibase.getBestStatements( albumQid, 'P577' )
	if not albumYearStatements or #albumYearStatements==0 then return end
	albumYearStatements = albumYearStatements[1]
	albumYearStatements = albumYearStatements.mainsnak and albumYearStatements.mainsnak.datavalue and albumYearStatements.mainsnak.datavalue.value
	if not albumYearStatements then return end
	local albumYear = Date.newFromWikidataValue( albumYearStatements ).year
	return albumYear
end

--wrap the year with template
local function wrapMusicYear(frame, year)
	if year==nil or #tostring(year)==0 then return '' end
	return mw.ustring.format('<br>(%s)', frame:expandTemplate{ title = 'שנה במוזיקה', args = { year } })
end

function chronology(frame)
	local prevAlbum = frame.args['אלבום לפני']
	if prevAlbum and #prevAlbum ==0 then prevAlbum = nil end
	local nextAlbum = frame.args['אלבום אחרי']
	if nextAlbum and #nextAlbum ==0 then nextAlbum = nil end
	local prevAlbumYear = frame.args['תאריך אלבום לפני']
	if prevAlbumYear and #prevAlbumYear ==0 then prevAlbumYear = nil end
	local albumYear = frame.args['תאריך אלבום']
	if albumYear and #albumYear ==0 then albumYear = nil end
	local nextAlbumYear = frame.args['תאריך אלבום אחרי']
	if nextAlbumYear and #nextAlbumYear ==0 then nextAlbumYear = nil end
	local albumName = frame.args['שם']
	if albumName and #albumName == 0 then albumName = nil end
	
	if prevAlbum==nil and  nextAlbum==nil then return end -- early terminate in case of no prev/next albums
	
	-- fetch wikidata if data is missing
	if albumYear==nil then albumYear = getYear(albumName) end
	
	local colWidth = '33.33%'
	local root = mw.html.create('table')
	root:cssText('background-color: transparent;font-size:small;width:100%')
	if prevAlbum then
		if prevAlbumYear==nil then prevAlbumYear = getYear(prevAlbum) end
		root:tag('td')
			:cssText('text-align:center;width:'..colWidth)
			:wikitext(prevAlbum .. wrapMusicYear(frame, prevAlbumYear))
	else
		root:tag('td')
			:cssText('width:'..colWidth)
			:wikitext('&nbsp;')
	end
	
	-- mark it with bold
	albumName = tostring(mw.html.create('b'):wikitext(albumName))
	if albumYear then
		albumName = albumName.. wrapMusicYear(frame, albumYear)
	end
	root:tag('td')
				:cssText('text-align:center;width:'..colWidth)
				:wikitext(albumName)
	if nextAlbum then
		if nextAlbumYear==nil then nextAlbumYear = getYear(nextAlbum) end
		root:tag('td')
			:cssText('text-align:center;width:'..colWidth)
			:wikitext(nextAlbum .. wrapMusicYear(frame, nextAlbumYear))
	else
		root:tag('td')
			:cssText('width:'..colWidth)
			:wikitext('&nbsp;')	
	end
	root:done()
	return root
end

return {
	['כרונולוגיה'] = chronology,
	['אורך'] = duration
}