https://wiki.gslin.org/index.php?title=%E6%A8%A1%E5%9D%97:Official_website&feed=atom&action=historyModule:Official website - 版本历史2024-03-29T06:01:36Z本wiki上该页面的版本历史MediaWiki 1.39.3https://wiki.gslin.org/index.php?title=Module:Official_website&diff=48&oldid=prevGslin:创建页面,内容为“local makeUrl = require('Module:URL')._url local p = {} -- Wrapper for pcall which returns nil on failure. local function quickPcall(func) local success, result =…”2018-02-04T22:10:45Z<p>创建页面,内容为“local makeUrl = require('Module:URL')._url local p = {} -- Wrapper for pcall which returns nil on failure. local function quickPcall(func) local success, result =…”</p>
<p><b>新頁面</b></p><div>local makeUrl = require('Module:URL')._url<br />
<br />
local p = {}<br />
<br />
-- Wrapper for pcall which returns nil on failure.<br />
local function quickPcall(func)<br />
local success, result = pcall(func)<br />
if success then<br />
return result<br />
end<br />
end<br />
<br />
-- Gets the rank for a Wikidata property table. Returns 1, 0 or -1, in<br />
-- order of rank.<br />
local function getRank(prop)<br />
local rank = prop.rank<br />
if rank == 'preferred' then<br />
return 1<br />
elseif rank == 'normal' then<br />
return 0<br />
elseif rank == 'deprecated' then<br />
return -1<br />
else<br />
-- No rank or undefined rank is treated as "normal".<br />
return 0<br />
end<br />
end<br />
<br />
-- Finds whether a Wikidata property is qualified as being in English.<br />
local function isEnglish(prop)<br />
local ret = quickPcall(function ()<br />
for i, lang in ipairs(prop.qualifiers.P2439) do<br />
if lang.datavalue.value['numeric-id'] == 1860 then<br />
return true<br />
end<br />
end<br />
return false<br />
end)<br />
return ret == true<br />
end<br />
<br />
-- Fetches the official website URL from Wikidata.<br />
local fetchWikidataUrl<br />
fetchWikidataUrl = function()<br />
-- Get objects for all official sites on Wikidata.<br />
local websites = quickPcall(function ()<br />
return mw.wikibase.getEntityObject().claims.P856<br />
end)<br />
<br />
-- Clone the objects in case other code needs them in their original order.<br />
websites = websites and mw.clone(websites) or {}<br />
<br />
-- Add the table index to the objects in case it is needed in the sort.<br />
for i, website in ipairs(websites) do<br />
website._index = i<br />
end<br />
<br />
-- Sort the websites, first by highest rank, and then by websites in the<br />
-- English language, then by the website's original position in the<br />
-- property list. When we are done, get the URL from the highest-sorted<br />
-- object.<br />
table.sort(websites, function(ws1, ws2)<br />
local r1 = getRank(ws1)<br />
local r2 = getRank(ws2)<br />
if r1 ~= r2 then<br />
return r1 > r2<br />
end<br />
local e1 = isEnglish(ws1)<br />
local e2 = isEnglish(ws2)<br />
if e1 ~= e2 then<br />
return e1<br />
end<br />
return ws1._index < ws2._index <br />
end)<br />
local url = quickPcall(function ()<br />
return websites[1].mainsnak.datavalue.value<br />
end)<br />
<br />
-- Cache the result so that we only do the heavy lifting once per #invoke.<br />
fetchWikidataUrl = function ()<br />
return url<br />
end<br />
<br />
return url<br />
end<br />
<br />
-- Render the URL link, plus other visible output.<br />
local function renderUrl(options)<br />
if not options.url then<br />
return '<strong class="error">' ..<br />
'找不到URL。请在此处指定URL或在维基数据上添加。' ..<br />
'</strong>'<br />
end<br />
local ret = {}<br />
ret[#ret + 1] = string.format(<br />
'<span class="official-website">%s</span>',<br />
makeUrl(options.url, options.display)<br />
)<br />
if options.format == 'flash' then<br />
ret[#ret + 1] = mw.getCurrentFrame():expandTemplate{<br />
title = 'Link note',<br />
args = {note = '需要[[Adobe Flash Player]]'}<br />
}<br />
end<br />
if options.mobile then<br />
ret[#ret + 1] = '(' .. makeUrl(options.mobile, 'Mobile') .. ')'<br />
end<br />
return table.concat(ret, ' ')<br />
end<br />
<br />
-- Render the tracking category.<br />
local function renderTrackingCategory(url)<br />
if mw.title.getCurrentTitle().namespace ~= 0 then<br />
return ''<br />
end<br />
local category<br />
if not url then<br />
category = '官方网站没有URL'<br />
elseif fetchWikidataUrl() then<br />
if url and url ~= fetchWikidataUrl() then<br />
category = '维基百科和维基数据上的官方网站不同'<br />
end<br />
else<br />
category = '维基数据上没有官方网站'<br />
end<br />
return category and string.format('[[Category:%s]]', category) or ''<br />
end<br />
<br />
function p._main(args)<br />
local url = args[1] or args.URL or fetchWikidataUrl()<br />
local formattedUrl = renderUrl{<br />
url = url,<br />
display = args[2] or args.name or '官方网站',<br />
mobile = args.mobile,<br />
format = args.format<br />
}<br />
return formattedUrl .. renderTrackingCategory(url)<br />
end<br />
<br />
function p.main(frame)<br />
local args = require('Module:Arguments').getArgs(frame, {<br />
wrappers = 'Template:Official website'<br />
})<br />
return p._main(args)<br />
end<br />
<br />
return p</div>Gslin