לדלג לתוכן

יחידה:קישור

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

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

בדיקות


local linkUtilities = {}
local tableTools = require("module:TableTools")
local flags = mw.loadJsonData("module:קישור/דגלים.json")


function linkUtilities.checkIsDismabig( targetPage )
	local objectPage = mw.title.new(targetPage)

	if objectPage == nil or not objectPage.exists then
		return false 
	end
	local contentPage = objectPage:getContent()

	if mw.ustring.match(contentPage, "{{פירושונים|") then
		return true
	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

function linkUtilities._flagIfExists( targetPage )
	mw.log("flagIfExists" .. targetPage)
    if tableTools.inArray(flags, targetPage) then
        return "קיים"
    end
    local targetTitle = mw.title.new( "תבנית:דגל/" .. targetPage )
    if targetTitle ~=nil and targetTitle.exists then
        return "קיים"
    end
    return "לא קיים"
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

function linkUtilities.flagIfExists( frame )
    local targetPage = frame.args['1']
    return linkUtilities._flagIfExists(targetPage)
end

return linkUtilities