Module:Pageinfo

--hascat| |true|false| {{#invoke:pageinfo|hastemplate| |true|false| {{#invoke:pageinfo|getcats| --

local p = {} local u = require("Dev:Utility")

function p.hascat(frame) local args = u.getArgs(frame) if args then local categName = args[1] or args["cat"] local trueResult = args[2] or args["true"] local falseResult = args[3] or args["false"] local page = args["page"] or tostring(mw.title.getCurrentTitle) if hasCategory(page,categName) then return frame:preprocess(trueResult) end return frame:preprocess(falseResult) end end

function p.getcats(frame) local args = u.getArgs(frame) if args then local page = args[1] or args["page"] or tostring(mw.title.getCurrentTitle) local outputCats,_ = getPageCategories(page,categName) return outputCats end end

function p.hastemplate(frame) local args = u.getArgs(frame) if args then local templateName = args[1] or args["template"] local trueResult = args[2] or args["true"] local falseResult = args[3] or args["false"] local page = args["page"] or tostring(mw.title.getCurrentTitle)

if getTemplate(page,templateName) then return frame:preprocess(trueResult) end return frame:preprocess(falseResult) end end

function hasCategory(page,name) if page and name then local pageContent = mw.title.new(page):getContent if pageContent then local categories = pageContent:gmatch("%[%[[Cc]ategory:(.-)%]%]")           local sub = string.sub            for categoryName in categories do                if sub(categoryName,1,#name)==name then                    return true                end            end        end    end end

function getTemplate(page,templateName) if page and name then local pageContent = mw.title.new(page):getContent if pageContent then local templates = pageContent:gmatch("%{%{(.-)%}%}") local sub = string.sub for storedTemplate in templates do               if sub(storedTemplate,1,#templateName)==name or sub(storedTemplate,#templateName-1)==templateName then return true end end end end end

function getPageCategories(page) if not page then return end local pageContent = mw.title.new(page):getContent local categories = pageContent:gmatch("%[%[[Cc]ategory:(.-)%]%]")   local catTable =  {}    local outputCats =""    local gsub = string.gsub    if categories then        for categoryName in categories do            categoryName =gsub(categoryName,"%|.*","")            catTable[categoryName] =1            outputCats =   outputCats .. categoryName .. " ; "        end    end    return outputCats,catTable end

return p