Module:Pageinfo: Difference between revisions

From The Satanic Wiki
Jump to navigation Jump to search
m (1 revision imported)
No edit summary
 
Line 10: Line 10:


local p = {}
local p = {}
local getArgs = require("Dev:Arguments").getArgs
local getArgs = require("Module:Arguments").getArgs


function p.hascat(frame)
function p.hascat(frame)

Latest revision as of 06:10, 2 May 2021

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

--[[<pre> Gets information about a page, e.g. if it has a category or list of cats in page
Syntax: 
{{#invoke:pageinfo|hascat|<categoryname>|true|false|<page=pagename>}}
{{#invoke:pageinfo|hastemplate|<templatename>|true|false|<page=pagename>}}
{{#invoke:pageinfo|getcategories|<pagename>}}
Notes: 
* This is expensive ONLY if the {{{page}}} parameter refers to another page.
* If the {{{page}}} parameter is ommited it will assume current page.
]]--

local p = {}
local getArgs = require("Module:Arguments").getArgs

function p.hascat(frame)
    local args = 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  trueResult
        end
        return  falseResult
    end
end

function p.getcategories(frame)
    local args = 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 = 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,categoryName)
    if page and categoryName then
        local lang =mw.language.new('en')
        local pageContent = mw.title.new(page):getContent()
        categoryName = lang:ucfirst(categoryName)
        
        if pageContent then
            local categories = pageContent:gmatch("%[%[[Cc]ategory:(.-)%]%]")
            local sub = string.sub
            
            for storedCategoryName in categories do
                storedCategoryName = lang:ucfirst(storedCategoryName)
       
                if sub(storedCategoryName,1,#categoryName)==categoryName then
                    return true
                end
            end
        end
    end
end


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

function getPageCategories(page)
    if not page then 
         return 
    end
    page = mw.language.new('en'):ucfirst(page)
    local pageContent = mw.title.new(page):getContent()
    
    if pageContent then
        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
end

return p