跳转到内容

模組:WikidataLink

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

这是本页的一个历史版本,由A2569875留言 | 贡献2021年9月1日 (三) 10:36 (補上遺失的第三參數)编辑。这可能和当前版本存在着巨大的差异。

local p={}
local lib_arg={};
local ilh = {}
local yesno = {}
function p.title2entity(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_title = mw.text.trim(tostring(args['1'] or args[1] or ''))
    local entity_id = mw.wikibase.getEntityIdForTitle(input_title)
    if entity_id then return tostring(entity_id) end
    local title_obj = mw.title.new(input_title)
    if (title_obj or {}).exists == true then
    	title_obj = title_obj.redirectTarget
    	if title_obj then entity_id = mw.wikibase.getEntityIdForTitle(title_obj.prefixedText) end
    	return entity_id
    end
    local start,tail = mw.ustring.find(input_title,'[qQ]%d+')
    if start then 
    	local in_id = 'Q' .. mw.ustring.sub(input_title,start+1,tail)
    	if mw.wikibase.getLabel(in_id) ~= nil then return in_id 
    	else return '' end
    end
    local try_parse = mw.ustring.gsub(input_title,'[.,]','')
    start,tail = mw.ustring.find(try_parse,'%d+')
    if start then 
    	local parseNum = tonumber(mw.ustring.sub(try_parse,start,tail) or '0') or 0
		if mw.wikibase.getLabel('Q'..parseNum) ~= nil then return 'Q'..parseNum
    	else return '' end
    end
    return mw.wikibase.getEntityIdForCurrentPage() or ''
end
function _wikidata_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, display_text) 
	local display_code = display_text or ''
	local display_linkcode = (mw.text.trim(display_code) ~= '')and ('|'..display_code) or display_code
	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() .. display_linkcode .. ']]' 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 _wikidata_ilh({
					[1] = entity:getLabel(),
					[2] = interwikiPage,
					[3] = (mw.text.trim(display_code) ~= '')and display_code or nil,
					['lang'] = lang,
					['lang-code'] = interwiki
				})
			end
		end
		local eLabel = entity:getLabel()
		if mw.text.trim(eLabel or '') == '' then eLabel = interwikiPage end
		local result = _wikidata_ilh({
					[1] = eLabel,
					[2] = interwikiPage,
					[3] = (mw.text.trim(display_code) ~= '')and display_code or nil,
					['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

function p.linkwikidata_single(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 input_display = args[3] or args['3']
    local find_lang = mw.text.split(find_lang_str, ',') or {}
    if #find_lang <= 1 then find_lang[1] = find_lang_str end
	return p._getWikidataLink(wikidata_id, find_lang, input_display)
end
return p;