唱片资料 维基

See Also


local yesno = require('Module:Yesno')
local checkType = require('libraryUtil').checkType

-- Trim function
function trim(s)
    return (s:gsub("^%s*(.-)%s*$", "%1"))
end

--------------------------------------------------------------------------------
-- Version class
--------------------------------------------------------------------------------

local Version = {}
Version.__index = Version

Version.fields = {
	t = true, -- album title 收录专辑
	y = true, -- year
	lb = true, -- label
	f = true, -- format
	v = true, -- version name
	n = true, -- longnote
	extra = true,
	r = true, -- remastered
	s = true, -- sound check 音质差异
}

Version.cellMethods = {
	t = 'makeTitleCell', -- 版本/专辑名称
	y = 'makeYearCell', -- 发行年份
	lb = 'makeLabelCell', -- 厂牌
	f = 'makeFormatCell', -- 介质
	v = 'makeVeridCell', -- ISRC ISBN etc.
	extra = 'makeExtraCell',
	n = 'makeLongNoteCell',
	s = 'makeSoundcheckCell',	
}

function Version.new(data, album)
	local self = setmetatable({}, Version)
	for field in pairs(Version.fields) do
		self[field] = data[field]
	end
	--self.number = assert(tonumber(self.number))
	--self.s = album
	
	return self
end


function Version:getLabelCredit()
	return self.lb
end

function Version:getVeridCredit()
	return self.v
end

function Version:getSoundcheckCredit()
	return self.s
end

function Version:getRemasteredCredit()
	return self.r
end

function Version:getExtraField()
	return self.extra
end

function Version:getLongNoteField()
	return self.n
end

function Version:getYearField()
	return self.y
end

-- Note: called with single dot syntax
function Version.makeSimpleCell(wikitext)
	wikitext = wikitext or ' '
	return '| style="text-align:center" | ' .. wikitext .. '\n'

end

function Version:makeFormatCell()
	return Version.makeSimpleCell(self.f)

end

function Version:makeTitleCell()
	if self.t and self.t ~= 'null' then
		self.t = self.t or ' '
		local title = '| style="padding-left:10px;" | ' .. self.t  .. '\n'
		return title
	else
		return ''
	end
end

function Version:makeYearCell()
	if self.y and self.y ~= 'null' then
		return Version.makeSimpleCell(self.y)
	else
		return ''
	end
end

function Version:makeSoundcheckCell()
	if self.s and self.s ~= 'null' then
		return Version.makeSimpleCell(self.s)
	else
		return ''
	end
end

function Version:makeLabelCell()
	--local ret = '[[' .. self.s .. ' (' .. self.a .. ')|' .. self.a .. ']]'
	return Version.makeSimpleCell(self.lb)

end

function Version:makeVeridCell()
	return Version.makeSimpleCell(self.v)
end

function Version:makeExtraCell()
	return Version.makeSimpleCell(self.extra)
end

function Version:makeLongNoteCell()
	self.n = self.n or ' '
	local longnoteCell = '| style="padding-left:10px;vertical-align:center" | ' 
	if (string.match(self.n, '%*') ) then
	else
		self.n = trim(self.n)
	end
	longnoteCell = longnoteCell .. self.n
	return trim(longnoteCell)
end

function Version:exportRow(options)
	options = options or {}
	local columns = options.columns or {}
	local row = '\n|-'
	options.color = options.color or 'var(--theme-page-background-color)'
	
	row = row .. ' style="background-color:' .. options.color ..';"\n'
	for i, column in ipairs(columns) do
		local method = Version.cellMethods[column]
		if method then
			--row = row .. method
			row = row .. tostring(self[method](self)) --row:node(self[method](self))
		else
			row = row .. 'invalidxxxxx'
		end
	end
	return row
end

function Version.setRow(data)
	local self = setmetatable({}, Version)
	for field in pairs(Version.fields) do
		self[field] = data[field]
	end

	-- albumtitle
	--local pname = tostring(mw.title.getCurrentTitle())
	--if (string.match(pname, '%(')) then
	--	local pattern = "^(.-)%s*%b()"
	--	self.s = string.match(pname, pattern)
	--else
	--	self.s = pname
	--end
	
	self.a = self.a or 'Various'
	-- Find columns to output
	
	local columns = {'t', 'y', 'lb', 'f', 'v', 's', 'n'}
	local color = 'var(--theme-page-background-color)'

	if self.r then
		color = '#e8fcd2; color:#000'
	end
	
	local vrow = ''
	vrow = self:exportRow({
			columns = columns,
			color = color
	})

	return vrow 
	
end

--------------------------------------------------------------------------------
-- VersionListing class
--------------------------------------------------------------------------------

local VersionListing = {}
VersionListing.__index = VersionListing

VersionListing.fields = {
	album = true,
	collapsed = true,
	edition = true,
	year = true,
	soundcheck = true,
	headline = true,
	font_size = true,
	verid_header = true,
	extra_column = true,
	title_width = true,
	label_width = true,
	verid_width = true,
	extra_width = true,
	longnote_width = true,
	soundcheck_width = true,
	category = true,
	remastered = true,
}

function VersionListing.new(data)
	local self = setmetatable({}, VersionListing)

	-- Add properties
	for field in pairs(VersionListing.fields) do
		self[field] = data[field]
	end
	
	-- Evaluate boolean properties
	self.collapsed = yesno(self.collapsed, false)
	self.showCategories = yesno(self.category) ~= false
	self.category = nil

	-- albumtitle
	local pname = tostring(mw.title.getCurrentTitle())
	if (self.album) then
	elseif (string.match(pname, '%(')) then
		local pattern = "^(.-)%s*%b()"
		self.album = string.match(pname, pattern)
	else
		self.album = pname
	end
	
	-- Make version objects
	self.versions = {}
	for i, versionData in ipairs(data.versions or {}) do
		table.insert(self.versions, Version.new(versionData, self.album))
	end

	return self
end


function VersionListing:__tostring()
	-- Find columns to output
	local columns = {'number', 'y', 'v', 'a', 't', 'n'}
	
	self.verid_width = self.verid_width or '100'
	self.longnote_width = self.longnote_width or '300'
	self.title_width = self.title_width or '500'
	self.artist_width = self.artist_width or '80'
	
	-- Find intros
	local vheader = '版本名称'
	
	-- Root of the output
	local root = '{|' -- ''
	root = root .. 'class="wikitable" style="font-size:90%; table-layout: fixed;"\n'
	if (self.remastered) then 
		root = root .. '|+ Remastered version\n' 
		vheader = '对应版本'
	end
	root = root .. '|-\n'
	root = root .. '! 序号 !! scope="col" width="100" | 年份 !! ' -- n y
	root = root .. 'scope="col" width="' .. self.verid_width .. '"  | ' .. vheader .. ' !! ' -- v
	root = root .. 'scope="col" width="' .. self.artist_width .. '" | 演唱者 !! ' -- a
	root = root .. 'scope="col" width="' .. self.title_width .. '"  | 收录专辑 !! ' -- t
	root = root .. 'scope="col" width="' .. self.longnote_width .. '" | 备注 \n' -- n
	--root = root .. '|-\n'

	-- Versions
	local vrow = ''
	for i, version in ipairs(self.versions) do
		vrow = version:exportRow({
			columns = columns,
			color = i % 2 == 0 and 'var(--theme-page-background-color--secondary)' or 'var(--theme-page-background-color)'
		})
		root = root .. vrow
	end
	
	-- Warnings and versioning categories
	--root:wikitext(self:renderWarnings())
	--root:wikitext(self:renderVersioningCategories())

	return root .. '\n|}' 
end

function VersionListing.setHeader(data)
	local self = setmetatable({}, VersionListing)

	-- Add properties
	for field in pairs(VersionListing.fields) do
		self[field] = data[field]
	end
	
	-- Evaluate boolean properties
	self.collapsed = yesno(self.collapsed, false)
	self.edition = yesno(self.edition, false)
	self.year = yesno(self.year, false)
	self.soundcheck = yesno(self.soundcheck, false)
	self.showCategories = yesno(self.category) ~= false
	self.category = nil

	-- Album Title
	local pname = tostring(mw.title.getCurrentTitle())
	if (self.album) then
	elseif (string.match(pname, '%(')) then
		local pattern = "^(.-)%s*%b()"
		self.album = string.match(pname, pattern)
	else
		self.album = pname
	end
	
	self.font_size = self.font_size or '9pt'
	self.verid_width = self.verid_width or '200'
	self.longnote_width = self.longnote_width or '300'
	self.title_width = self.title_width or '150'
	self.label_width = self.label_width or '150'
	self.soundcheck_width = self.soundcheck_width or '350'
	
	-- Find intros
	local vheader = self.verid_header or '[[唱片编号]]/[[ISRC]]/[[条形码|EAN/UPC]]等'
	
	-- Root of the output
	local newline ='\n' -- new line char
	local header = 'class="wikitable" style="width:100%; table-layout: fixed; font-size:' .. self.font_size .. '"' .. newline
	if (self.soundcheck) then 
		header = header .. '|+ 《' .. self.album .. '》版本一览与音质差异 [[Category:音质]]' 
	else
		header = header .. '|+ 《' .. self.album .. '》版本一览' 
	end
	header = header .. newline ..'|-' .. newline ..'!'
	if (self.edition) then 
		header = header .. 'scope="col" width="' .. self.title_width .. '"  | [[版本]]/专辑名称 !! ' -- t
	end
	if (self.year) then 
		header = header .. 'scope="col" width="' .. '100' ..'" | 发行时间 !! '
	end	

	header = header .. 'scope="col" width="' .. self.label_width .. '" | [[厂牌|发行厂牌]] !! ' -- a
	header = header .. 'scope="col" width="' ..      '80'     .. '" | 介质 !! ' -- f
	header = header .. 'scope="col" width="' .. self.verid_width .. '"  | ' .. vheader .. ' !! ' -- v

	if (self.soundcheck) then 
		header = header .. 'scope="col" width="' .. self.soundcheck_width .. '" | 音质差异 !! ' -- sound
	end
	
	header = header .. 'scope="col" width="' .. self.longnote_width .. '" | 备注 ' .. newline -- n

	return header
end


------------------------------------- TEST ---------------------------------
function VersionListing.setHeadertest(data)
	return 
end

--------------------------------------------------------------------------------
-- Exports for Template Main Table
--------------------------------------------------------------------------------

local p = {}; 
function p._main(args)
	-- Process numerical args so that we can iterate through them.
	local data, versions = {}, {}
	for k, v in pairs(args) do
		if type(k) == 'string' then
			local prefix, num = k:match('^(%D.-)(%d+)$')
			if prefix and Version.fields[prefix] and (num == '0' or num:sub(1, 1) ~= '0') then
				-- Allow numbers like 0, 1, 2 ..., but not 00, 01, 02...,
				-- 000, 001, 002... etc.
				num = tonumber(num)
				versions[num] = versions[num] or {}
				versions[num][prefix] = v
			else
				data[k] = v
			end
		end
	end
	data.versions = (function (t)
		-- Compress sparse array
		local ret = {}
		for num, versionData in pairs(t) do
			versionData.number = num
			table.insert(ret, versionData) 
		end
		table.sort(ret, function (t1, t2)
			return t1.number < t2.number
		end)
		return ret
	end)(versions)

	return tostring(VersionListing.new(data))
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:版本'
	})
	return p._main(args)
end

--------------------------------------------------------------------------------
-- Exports for Template /Header
--------------------------------------------------------------------------------

function p._header(args)
	-- Process numerical args so that we can iterate through them.
	local data = {}
	for k, v in pairs(args) do
		if type(k) == 'string' then
			data[k] = v
		end
	end

	return tostring(VersionListing.setHeader(data))
end

function p.header(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:版本/Header'
	})
	return p._header(args)
end

function p._headertest(args)
	-- Process numerical args so that we can iterate through them.
	local data = {}
	for k, v in pairs(args) do
		if type(k) == 'string' then
			data[k] = v
		end
	end

	return tostring(VersionListing.setHeadertest(data))
end

function p.headertest(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:版本/Header'
	})
	return p._headertest(args)
end
--------------------------------------------------------------------------------
-- Exports for Template /Header
--------------------------------------------------------------------------------

function p.vdata(frame)
	local data = {}
	data['r'] = yesno(frame.args.r, false) -- remastered yes/no
	data['t'] = trim(frame.args.t) -- 版本/专辑名称,optional
	data['y'] = trim(frame.args.y) -- 年份,optional
	data['lb'] = trim(frame.args[1]) -- 厂牌 label
	data['f'] = trim(frame.args[2]) -- 介质 format
	data['v'] = frame.args[3] -- verID:ISRC ISBN etc.
	
	if (frame.args[5] and frame.args[5] ~= '' and frame.args[5] ~= '{{{5}}}') then
		data['s'] = frame.args[4]
		data['n'] = frame.args[5]
	else
		data['n'] = frame.args[4] -- 备注
	end
	
	--local args = require('Module:Arguments').getArgs(frame, {
	--	wrappers = 'Template:版本/Data'
	--})
	return trim(tostring(Version.setRow(data)))
end

return p