Module:Sandbox/Awesome Aasim/Categories

From English Wikipedia @ Freddythechick

local p = {}
p._cachedData = {}
local function getCategoriesFromWikitext(wikitext)
    local categories = {}
    for category in mw.ustring.gmatch(wikitext, "%[%[Category:([%w%s%-_]+)%]%]") do
        table.insert(categories, category)
    end
    return categories
end

function p.getCategories(title, skipProcessing)
	title = tostring(title)
	-- if cached then return that
	if p._cachedData[title] ~= nil then
		mw.log("Cached")
		return p._cachedData[title]
	else
		mw.incrementExpensiveFunctionCount()
	    local wikitext = skipProcessing and mw.title.new(title):getContent() or mw.getCurrentFrame():preprocess(mw.title.new(title):getContent())
	    local categories = getCategoriesFromWikitext(wikitext)
		mw.log("Caching...")
	    p._cachedData[title] = categories
	    return categories
    end
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	return table.concat(p.getCategories(args[1], require("Module:Yesno")(args[2])), '|')
end

return p