Module:Govinfo

local p = {}

local function trim(s)
	if s == nil then return nil end
	return mw.text.trim(tostring(s))
end

local function is_nonempty(s)
	return s ~= nil and s ~= ''
end

-- Fetches P3837 ("US public law number") from Wikidata for a QID.
-- Returns the raw string value (e.g., "117-1") or nil if missing/unreadable.
local function get_p3837(qid)
	if not is_nonempty(qid) then return nil end
	if not mw.wikibase or not mw.wikibase.entityExists(qid) then return nil end

	local stmts = mw.wikibase.getBestStatements(qid, 'P3837')
	if not stmts or not stmts[1] then return nil end

	local snak = stmts[1].mainsnak
	if not snak or snak.snaktype ~= 'value' then return nil end

	local dv = snak.datavalue
	if not dv or dv.type ~= 'string' then return nil end

	return trim(dv.value)
end

-- Parses a public-law number string in the form "<congress>-<law>" (allowing whitespace).
-- Returns congress, law as strings (digits only), or nil on failure.
local function parse_plaw_number(plaw)
	if not is_nonempty(plaw) then return nil end
	local a, b = tostring(plaw):match('^%s*(%d+)%s*%-%s*(%d+)%s*$')
	if not a or not b then return nil end
	return a, b
end

-- Builds a govinfo "link-docs" URL. For our context we treat plaw/statute as sharing the same path shape,
-- differing only by the collection segment, per user requirement.
local function build_url(collection, congress, law, linktype)
	collection = trim(collection) or 'plaw'
	linktype = trim(linktype) or 'uslm'

	local base = 'https://www.govinfo.gov/link/' .. collection .. '/' .. congress .. '/public/' .. law

	-- Govinfo defaults to PDF if link-type is omitted, but this template defaults to USLM.
	-- If caller explicitly blanks |type=, we omit link-type= entirely (and the result becomes govinfo's default PDF).
	local url
	if is_nonempty(linktype) then
		url = base .. '?link-type=' .. mw.uri.encode(linktype, 'PATH')
	else
		url = base
	end

	-- MediaWiki filetype hinting: for PDFs, append "&.pdf" (govinfo ignores it, MW uses it).
	-- Only do this when link-type is exactly "pdf".
	if linktype == 'pdf' then
		if url:find('?', 1, true) then
			url = url .. '&.pdf'
		else
			url = url .. '?.pdf'
		end
	end

	return url
end

local function default_label(congress, law)
	-- Keep label identical regardless of collection.
	-- Use an en dash in the rendered label.
	return 'Pub. L. ' .. congress .. '–' .. law
end

function p.main(frame)
	local args = frame.args or {}
	local collection = trim(args.collection) or 'plaw'
	local qid = trim(args.qid)
	local id_override = trim(args.id)
	local linktype = args.type
	if linktype ~= nil then linktype = trim(linktype) end -- allow blanking via |type=

	-- Default to the page's Wikidata item when no QID is supplied.
	if not is_nonempty(qid)
		and mw.wikibase
		and mw.wikibase.getEntityIdForCurrentPage
	then
		qid = mw.wikibase.getEntityIdForCurrentPage()
	end

	local plaw_raw = nil
	if is_nonempty(id_override) then
		plaw_raw = id_override
	else
		plaw_raw = get_p3837(qid)
	end

	local congress, law = parse_plaw_number(plaw_raw)
	if not congress or not law then
		-- Fail closed: emit a small, explicit error instead of malformed output.
		-- No extra whitespace/newlines.
		return '<span class="error">Govinfo: missing/invalid P3837 (expected “&lt;congress&gt;-&lt;law&gt;”).</span>'
	end

	local url = build_url(collection, congress, law, linktype)

	local label = trim(args.label)
	if not is_nonempty(label) then
		label = default_label(congress, law)
	end

	return '[' .. url .. ' ' .. label .. ']'
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.