Module:Sandbox/extract database report

.mw-parser-output .documentation,.mw-parser-output .documentation-metadata{border:1px solid var(--border-color-base,#a2a9b1);background-color:#ecfcf4;color:inhe

Module:Sandbox/extract database report
--- Extract values from Database report footers.
---
--- Reads saved page wikitext and matches
--- "{{Database report/footer|...}}" directly.
--- Supports "count", "last_updated", and
--- "query_runtime".
---
--- Arg 1 is the page title. Arg 2 is the
--- optional footer index. Default is 1.
--- Negative values count from the end.

require("strict")

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

---@alias IndexInput integer|string|nil

---@param value unknown
---@return string
local function trim(value)
    if type(value) ~= "string" then
        return ""
    end
    return mw.text.trim(value)
end

---@param message string
---@return string
local function format_error(message)
    return string.format(
        '<strong class="error">Database report extract: %s</strong>',
        message
    )
end

---@param raw IndexInput
---@return integer
local function parse_index(raw)
    local parsed = tonumber(trim(raw))
    if parsed == nil then
        return 1
    end

    parsed = math.floor(parsed)

    if parsed == 0 then
        return 1
    end

    return parsed
end

---@param requested integer
---@param total integer
---@return integer|nil
local function resolve_index(requested, total)
    if total < 1 then
        return nil
    end

    if requested < 0 then
        requested = total + requested + 1
    end

    if requested < 1 or requested > total then
        return nil
    end

    return requested
end

---@param title_text string
---@return string|nil, string|nil
local function get_page_text(title_text)
    local title = mw.title.new(title_text)

    if title == nil then
        return nil, "invalid page title"
    end

    local content = title:getContent()
    if content == nil then
        return nil, "page does not exist or has no readable content"
    end

    return content, nil
end

---@param page_text string
---@return string[]
local function find_footers(page_text)
    local footers = {}

    for footer in page_text:gmatch(
        "{{Database report/footer|[^{}]-}}"
    ) do
        table.insert(footers, footer)
    end

    return footers
end

---@param footer string
---@param key string
---@return string|nil
local function extract_param(footer, key)
    local pattern = "|" .. key .. "=([^|}]-)[|}]"
    local value = footer:match(pattern)

    if value == nil or value == "" then
        return nil
    end

    return value
end

---@param key string
---@param args table
---@return string
local function get_value(key, args)
    local title_text = trim(args[1])
    if title_text == "" then
        return format_error("missing page title")
    end

    local requested_index = parse_index(args[2])
    local page_text, read_error = get_page_text(title_text)

    if page_text == nil then
        return format_error(read_error or "unable to read page")
    end

    local footers = find_footers(page_text)
    if #footers == 0 then
        return format_error("no Database report/footer found")
    end

    local resolved_index = resolve_index(requested_index, #footers)
    if resolved_index == nil then
        return format_error(string.format(
            "report index %d is out of range; found %d footer(s)",
            requested_index,
            #footers
        ))
    end

    local value = extract_param(footers[resolved_index], key)
    if value == nil then
        return format_error(string.format(
            "footer %d has no %s parameter",
            resolved_index,
            key
        ))
    end

    return value
end

---@param frame table
---@return string
function p.count(frame)
    local args = getArgs(frame)
    return get_value("count", args)
end

---@param frame table
---@return string
function p.last_updated(frame)
    local args = getArgs(frame)
    return get_value("last_updated", args)
end

---@param frame table
---@return string
function p.query_runtime(frame)
    local args = getArgs(frame)
    return get_value("query_runtime", args)
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.