Nothing Special   »   [go: up one dir, main page]

לדלג לתוכן

יחידה:קישור

מתוך ויקיפדיה, האנציקלופדיה החופשית

יחידה זו מיועדת לפונקציות שימושיות לקישורים.

בדיקות


local linkUtilities = {}


function linkUtilities.checkIsDismabig( targetPage )
	local targetQid = mw.wikibase.getEntityIdForTitle(targetPage)
	local WIKIDATA_DISAMBIG_QID = 'Q4167410'
	
	if targetQid == nil then
		return false -- target page doesnt have wikidata entity -> hence cant query if disambig
	end
	
	local targetType = mw.wikibase.getBestStatements(targetQid, 'P31')
	for _, v in pairs(targetType) do
		if v.mainsnak and v.mainsnak.datavalue  and v.mainsnak.datavalue.value and v.mainsnak.datavalue.value.id == WIKIDATA_DISAMBIG_QID then
			return true
		end
	end
	return false
end

function linkUtilities._linkIfExists(targetPage, linkText, noDisambig, currentPage) 
	if mw.ustring.match(targetPage, '%[%[') then
		return targetPage
	end
	if linkText == '' or linkText == nil then
		linkText = targetPage
    	-- remove parentheses 
    	linkText= mw.ustring.gsub(linkText, " *[(].*[)]","")
	end

	if targetPage == currentPage then return linkText	end
	
	if noDisambig then
		if linkUtilities.checkIsDismabig(targetPage) then
			return linkText
		end
	end
	
	local targetTitle = mw.title.new( targetPage )
	-- target title may be nil if the targetPage is invalid title
	if targetTitle ~=nil and targetTitle.exists then
		return mw.ustring.format('[[%s|%s]]', targetPage, linkText)
	else
		return linkText
	end
end

-- check if target is disambig page
function linkUtilities.isDismabig( frame )
	local targetPage = frame.args['1']
	if targetPage == '' or targetPage == nil then
		targetPage = mw.title.getCurrentTitle().text
	end
	return linkUtilities.checkIsDismabig(targetPage)
end

function linkUtilities.linkIfExists( frame )
	local targetPage = frame.args['1']
	local linkText = frame.args['2']
	local noDisambig = frame.args['noDisambig']~=nil and frame.args['noDisambig']~=''
	local currentPage = mw.title.getCurrentTitle().text
	return linkUtilities._linkIfExists(targetPage, linkText, noDisambig ,currentPage)
end

return linkUtilities