跳转到内容

模組:WikidataLink

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875留言 | 贡献2020年2月8日 (六) 05:16 建立内容为“local p={} local lib_arg={}; local ilh = {} local yesno = {} function _ilh(arg) if type(ilh._ilh) ~= type(type) then ilh = require('Module:Ilh') en…”的新页面)编辑。这可能和当前版本存在着巨大的差异。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)

local p={}
local lib_arg={};
local ilh = {}
local yesno = {}
function _ilh(arg)
	if type(ilh._ilh) ~= type(type) then ilh = require('Module:Ilh') end
	if type(yesno) ~= type(type) then yesno = require('Module:Yesno') end
	local context={}
	context["isMainPage"]=ilh.isMainPage()
	context["localPage"]=arg[1]
	context["foreignPage"]=arg[2] or arg[1] --如果{{{2}}}不传入的话
	local _d=arg["d"]
	local _1=arg[1] or arg["1"]
	local _3=arg[3] or arg["3"]
	local dpN1=_3 or _d
	context["displayName"]=(dpN1 and {dpN1} or {_1})[1]
	context["langCode"]=arg["lang-code"]
	context["lang"]=arg["lang"]
	context["nocat"]=yesno( arg["nocat"] , false )

	context["isExist"]= (arg["$exist$"] and arg["$exist$"]==1) or ilh.isExist(context["localPage"])
	
	local curPage_obj=mw.title.getCurrentTitle()
	context["isNoCatWithNamespace"]=curPage_obj:inNamespaces(2,828) --User,Module
	--context["curPageNamespace"]=curPage_obj.namespace

	return (context["isMainPage"] and ilh.onlyLink(context)) or ilh.functionLink(context)
end

function p._getWikidataLink(wikidata_id, find_lang) 
	if mw.wikibase.entityExists( wikidata_id ) then
		local entity = mw.wikibase.getEntity( wikidata_id )
		if mw.text.trim(entity:getSitelink() or '') ~= '' then return '[[' .. entity:getSitelink() .. ']]' end
		local interwiki = 'd'
		local interwikiPage = wikidata_id
		for ____,value in ipairs(find_lang) do
			if mw.text.trim(entity:getSitelink( value ) or '') ~= '' then
				interwiki, interwikiPage = value, entity:getSitelink( value )
				break
			elseif mw.text.trim(entity:getSitelink( value .. 'wiki' ) or '') ~= '' then
				interwiki, interwikiPage = value, entity:getSitelink( value .. 'wiki' )
				break
			end
		end
		if interwiki ~= 'd' then
			local lang = mw.language.fetchLanguageName( interwiki, mw.getContentLanguage():getCode() )
			if (lang or '') ~= '' then
				return _ilh({
					[1] = entity:getLabel(),
					[2] = interwikiPage,
					['lang'] = lang,
					['lang-code'] = interwiki
				})
			end
		end
		local eLabel = entity:getLabel()
		if mw.text.trim(eLabel or '') == '' then eLabel = interwikiPage end
		local result = _ilh({
					[1] = eLabel,
					[2] = interwikiPage,
					['lang'] = '維基數據',
					['lang-code'] = interwiki
				})
		return mw.ustring.gsub(result,'(>維基數據)(<)','%1所列之%2')
	else return wikidata_id end
end

function p.linkwikidata(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {parentFirst=true})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_article = args[1] or args['1'] or args.id or ''
    local find_lang_str = args[2] or args['2'] or args.lang or args.langs or 'en,de,fr,ja'
    local find_lang = mw.text.split(find_lang_str, ',') or {}
    if #find_lang <= 1 then find_lang[1] = find_lang_str end
	local result = mw.ustring.gsub(input_article,'([Qq][%d]+)',function(wikidata_id) 
		return p._getWikidataLink(wikidata_id, find_lang)
	end)
	return result
end
return p;