Mô đun:Thông tin phần mềm
Giao diện
Mô đun này cung cấp một số hàm hỗ trợ {{Thông tin phần mềm}}.
Cách sử dụng
[sửa mã nguồn]Xem thêm
[sửa mã nguồn]
local p = {}
local lang = mw.language.getContentLanguage()
---Cho biết có so sánh được phiên bản dùng hàm compVersions().
function p.canCompVersion(version)
for component in mw.text.gsplit(version, ".", true) do
if not mw.ustring.find(component, "^%d+[a-z]*$") then return false end
end
return true
end
---So sánh (nguợc chiều) hai chuỗi phiên bản theo định dang “12.3.4a”.
function p.compVersions(version1, version2)
version1 = version1.mainsnak.datavalue.value
version2 = version2.mainsnak.datavalue.value
do
local func1, static1, var1 = mw.text.gsplit(version1, ".", true)
local func2, static2, var2 = mw.text.gsplit(version2, ".", true)
while true do
local component1 = func1(static1, var1)
local component2 = func2(static2, var2)
if component1 == nil or component2 == nil then break end
if component1 ~= component2 then
local num1 = tonumber(mw.ustring.match(component1, "%d+"))
local num2 = tonumber(mw.ustring.match(component2, "%d+"))
if num1 ~= num2 then return num1 > num2 end
local alpha1 = mw.ustring.match(component1, "[a-z]*") or ""
local alpha2 = mw.ustring.match(component2, "[a-z]*") or ""
if alpha1 ~= alpha2 then return alpha1 > alpha2 end
end
end
end
return version1 > version2
end
---Cho ra danh sách các phiên bản ổn định của phần mềm được miêu tả trong bài.
-- (Kiểm tra các bảng dữ liệu tại [[d:Special:ApiSandbox]].)
function p.versions(frame)
local item = mw.wikibase.getEntityObject()
if not item then return nil end
local versions = item.claims.P348
if not versions then return nil end
local canSort = true
for i, version in ipairs(versions) do
if not p.canCompVersion(version.mainsnak.datavalue.value) then
canSort = false
break
end
end
if canSort then table.sort(versions, p.compVersions) end
local list = {}
for i, version in ipairs(versions) do
local versionStr = version.mainsnak.datavalue.value
local platforms = version.qualifiers and (version.qualifiers.P400 or version.qualifiers.P306)
local modifiers = {}
if platforms and platforms[1] then
local platformsList = {}
for i, platform in ipairs(platforms) do
local id = "Q" .. platform.datavalue.value["numeric-id"]
local title = mw.wikibase.sitelink(id)
local label = mw.wikibase.label(id)
if title or label then
local link = mw.ustring.format("[[%s|%s]]",
title or label, label or title)
table.insert(platformsList, link)
end
end
if #platformsList > 0 then
table.insert(modifiers, mw.text.listToText(platformsList))
end
end
local releaseDate = version.qualifiers and version.qualifiers.P577
local releaseStr = ""
if releaseDate and releaseDate[1] then
local releaseDateISO = releaseDate[1].datavalue.value.time
if releaseDateISO then
releaseDateISO = releaseDateISO:sub(9)
local precision = tonumber(releaseDate[1].datavalue.value.precision)
local dateFormat = "Y"
if precision > 9 then dateFormat = '"tháng" n, ' .. dateFormat end
if precision > 10 then dateFormat = "j " .. dateFormat end
local releaseDateStr = lang:formatDate(dateFormat, releaseDateISO, true)
table.insert(modifiers, releaseDateStr)
local ago = frame:expandTemplate{
title = "Thời gian trước",
args = {releaseDateISO},
}
if #ago > 0 then table.insert(modifiers, ago) end
end
end
if #modifiers > 0 then
versionStr = versionStr .. " (" .. table.concat(modifiers, "; ") .. ")"
end
table.insert(list, versionStr)
end
return table.concat(list, "<br />")
end
return p