Module:Link counter

From ComputerCraft
Jump to navigation Jump to search

Documentation for this module may be created at Module:Link counter/doc

local p = {}

function p.getLinkCount(frame)
	local page = frame.args[1]
    local content = mw.title.new(page):getContent()
    
    local linkCount = 0
    for match in content:gfind("%[%[[%s%w%./#|_]+%]%]") do
    	linkCount = linkCount + 1	
    end
    
    return linkCount
end

function p.getExistingLinkCount(frame)
	local page = frame.args[1]
    local content = mw.title.new(page):getContent()
    
    local linkCount = 0
    for match in content:gfind("(%[%[[%s%w%./#|_]+%]%])") do
    	local link = match:match("%[%[([%s%w%./#_]+)|[%s%w%./#_]+%]%]") or match:match("%[%[([%s%w%./#_]+)%]%]")
    	local linkedPage = mw.title.new(link)
    	linkCount = linkCount + (linkedPage ~= nil and linkedPage.id ~= 0 and 1 or 0)	
    end
    
    return linkCount
end

return p