🇮🇷 Iran Proxy | https://www.wikipedia.org/wiki/Module:User:SD0001/Database_report
Jump to content

Module:User:SD0001/Database report

From Wikipedia, the free encyclopedia
--[[

Lua module for generating {{database report}} configurations.

This module provides a class-based interface to create valid database report 
wikitext configurations with proper parameter validation and formatting.

Usage from Lua:
local DatabaseReport = require('Module:User:SD0001/Database_report')
local report = DatabaseReport:new()
report:setSQL("SELECT page_title FROM page LIMIT 10")
report:setWikilinks(1)
report:setInterval(7)
local config = report:generate()
]]

local DatabaseReport = {}
DatabaseReport.__index = DatabaseReport

-- Constructor
function DatabaseReport:new()
    local obj = {
        sql = nil,
        wikilinks = {},
        widths = {},
        comments = {},
        remove_underscores = {},
        hide = {},
        excerpts = {},
        table_style = nil,
        table_class = nil,
        interval = nil,
        pagination = nil,
        max_pages = nil,
        row_template = nil,
        row_template_named_params = nil,
        skip_table = nil,
        header_template = nil,
        footer_template = nil,
        postprocess_js = nil,
        silent = nil
    }
    setmetatable(obj, self)
    return obj
end

-- Set SQL query
function DatabaseReport:setSQL(sql)
    if not sql or type(sql) ~= "string" or sql:match("^%s*$") then
        error("SQL must be a non-empty string")
    end
    self.sql = sql
    return self
end

-- Add wikilink for a column
function DatabaseReport:useWikilinks(column, namespace, show)
    if type(column) == "number" then
        table.insert(self.wikilinks, {
            column = column,
            namespace = namespace,
            show = show
        })
    elseif type(column) == "table" then
        -- Support for multiple wikilinks at once
        for _, link in ipairs(column) do
            table.insert(self.wikilinks, link)
        end
    end
    return self
end

-- Set column width
function DatabaseReport:setWidth(column, width)
    if type(column) == "number" and type(width) == "string" then
        table.insert(self.widths, {column = column, width = width})
    elseif type(column) == "table" then
        -- Support for multiple widths at once
        for _, w in ipairs(column) do
            table.insert(self.widths, w)
        end
    end
    return self
end

-- Add columns for comments
function DatabaseReport:setCommentColumns(...)
    for _, col in ipairs({...}) do
        if type(col) == "number" then
            table.insert(self.comments, col)
        end
    end
    return self
end

-- Add columns to remove underscores from
function DatabaseReport:removeUnderscores(...)
    for _, col in ipairs({...}) do
        if type(col) == "number" then
            table.insert(self.remove_underscores, col)
        end
    end
    return self
end

-- Add columns to hide
function DatabaseReport:hideColumns(...)
    for _, col in ipairs({...}) do
        if type(col) == "number" then
            table.insert(self.hide, col)
        end
    end
    return self
end

-- Add excerpt configuration
function DatabaseReport:useExcerpt(srcColumn, destColumn, namespace, charLimit, charHardLimit)
    table.insert(self.excerpts, {
        srcColumn = srcColumn,
        destColumn = destColumn,
        namespace = namespace,
        charLimit = charLimit,
        charHardLimit = charHardLimit
    })
    return self
end

-- Set table style
function DatabaseReport:setTableStyle(style)
    self.table_style = style
    return self
end

-- Set table class
function DatabaseReport:setTableClass(class)
    self.table_class = class
    return self
end

-- Set update interval
function DatabaseReport:setInterval(days)
    if type(days) == "number" and days >= 1 then
        self.interval = days
    else
        error("Interval must be a number >= 1")
    end
    return self
end

-- Set pagination
function DatabaseReport:setPagination(count)
    if type(count) == "number" and count > 0 then
        self.pagination = count
    else
        error("Pagination must be a positive number")
    end
    return self
end

-- Set max pages
function DatabaseReport:setMaxPages(count)
    if type(count) == "number" and count > 0 then
        self.max_pages = count
    else
        error("Max pages must be a positive number")
    end
    return self
end

-- Set row template
function DatabaseReport:setRowTemplate(template)
    self.row_template = template
    return self
end

-- Set row template named params
function DatabaseReport:useNamedParamsInRowTemplate(value)
    self.row_template_named_params = value
    return self
end

-- Set skip table
function DatabaseReport:skipTable(value)
    self.skip_table = value
    return self
end

-- Set header template
function DatabaseReport:setHeaderTemplate(template)
    self.header_template = template
    return self
end

-- Set footer template
function DatabaseReport:setFooterTemplate(template)
    self.footer_template = template
    return self
end

-- Set postprocess JS
function DatabaseReport:setPostprocessJS(js)
    self.postprocess_js = js
    return self
end

-- Set silent mode
function DatabaseReport:setSilent(value)
    self.silent = value
    return self
end

function DatabaseReport:validate()
    if not self.sql then
        error("SQL must be set before generating")
    end

    if not self.sql:match("^%s*SELECT") then
        error("SQL should start with SELECT")
    end
end

-- Generate the database report configuration
function DatabaseReport:generate()
    self:validate()
    
    -- Build the configuration
    local config = {}
    
    -- Add SQL (required)
    table.insert(config, "|sql = " .. self.sql)
    
    -- Add optional parameters
    local optionalParams = {
        wikilinks = formatWikilinks(self.wikilinks),
        comments = formatColumnList(self.comments),
        widths = formatWidths(self.widths),
        table_style = self.table_style,
        table_class = self.table_class,
        excerpts = formatExcerpts(self.excerpts),
        remove_underscores = formatColumnList(self.remove_underscores),
        interval = self.interval,
        pagination = self.pagination,
        max_pages = self.max_pages,
        hide = formatColumnList(self.hide),
        row_template = self.row_template,
        row_template_named_params = self.row_template_named_params,
        skip_table = self.skip_table,
        header_template = self.header_template,
        footer_template = self.footer_template,
        postprocess_js = self.postprocess_js,
        silent = self.silent
    }
    
    -- Add non-empty optional parameters
    for param, value in pairs(optionalParams) do
        if value and value ~= "" then
            table.insert(config, "|" .. param .. " = " .. tostring(value))
        end
    end
    
    -- Join configuration lines
    local configText = table.concat(config, "\n")
    
    -- Return the complete database report template
    return "{{Database report\n" .. configText .. "\n}}"
end

-- Helper function to format wikilinks
local function formatWikilinks(wikilinks)
    if not wikilinks or #wikilinks == 0 then return nil end
    
    local parts = {}
    for _, link in ipairs(wikilinks) do
        if type(link) == "table" and link.column then
            local part = tostring(link.column)
            if link.namespace then
                part = part .. ":" .. tostring(link.namespace)
            end
            if link.show then
                part = part .. ":show"
            end
            table.insert(parts, part)
        elseif type(link) == "string" then
            table.insert(parts, link)
        end
    end
    
    return #parts > 0 and table.concat(parts, ", ") or nil
end

-- Helper function to format widths
local function formatWidths(widths)
    if not widths or #widths == 0 then return nil end
    
    local parts = {}
    for _, width in ipairs(widths) do
        if type(width) == "table" and width.column and width.width then
            table.insert(parts, tostring(width.column) .. ":" .. width.width)
        elseif type(width) == "string" then
            table.insert(parts, width)
        end
    end
    
    return #parts > 0 and table.concat(parts, ", ") or nil
end

-- Helper function to format column lists
local function formatColumnList(columns)
    if not columns or #columns == 0 then return nil end
    
    local parts = {}
    for _, col in ipairs(columns) do
        if type(col) == "number" then
            table.insert(parts, tostring(col))
        elseif type(col) == "string" and col:match("^%d+$") then
            table.insert(parts, col)
        end
    end
    
    return #parts > 0 and table.concat(parts, ", ") or nil
end

-- Helper function to format excerpts
local function formatExcerpts(excerpts)
    if not excerpts or #excerpts == 0 then return nil end
    
    local parts = {}
    for _, excerpt in ipairs(excerpts) do
        if type(excerpt) == "table" and excerpt.srcColumn then
            local part = tostring(excerpt.srcColumn)
            if excerpt.destColumn then
                part = part .. ":" .. tostring(excerpt.destColumn)
            end
            if excerpt.namespace then
                part = part .. ":" .. tostring(excerpt.namespace)
            end
            if excerpt.charLimit then
                part = part .. ":" .. tostring(excerpt.charLimit)
            end
            if excerpt.charHardLimit then
                part = part .. ":" .. tostring(excerpt.charHardLimit)
            end
            table.insert(parts, part)
        elseif type(excerpt) == "string" then
            table.insert(parts, excerpt)
        end
    end
    
    return #parts > 0 and table.concat(parts, ", ") or nil
end


return DatabaseReport