Module:Accessory: Difference between revisions

Simplify Compatible CRT Models table: show only CRT model and Type columns (removed Photo, Model, Size) (via update-page on MediaWiki MCP Server)
Update Documents section to query related documents via Has related accessory and prefill accessory in Add a document URL (via update-page on MediaWiki MCP Server)
Line 256: Line 256:
addSectionRow(container, 'Documents')
addSectionRow(container, 'Documents')
-- Build upload link
-- Build upload link with prefilled accessory
local timestamp = os.time()
local timestamp = os.time()
math.randomseed(timestamp + pageId)
math.randomseed(timestamp + pageId)
local random = math.random(1000, 9999)
local random = math.random(1000, 9999)
local target = "Document:" .. timestamp .. "-" .. random
local target = "Document:" .. timestamp .. "-" .. random
local uploadUrl = mw.uri.fullUrl(
local uploadUrl = mw.uri.fullUrl(
"Special:FormEdit",
"Special:FormEdit",
{
{
form = "Document",
form = "Document",
target = target
target = target,
["Document[accessories]"] = pageName
}
}
)
)
-- Query for document count
local documentsCountWikitext =
'{{#ask:\n' ..
' [[Category:Documents]]\n' ..
' [[Has related accessory::' .. fullPageName .. ']]\n' ..
' |format=count\n' ..
'}}'
local documentCount = frame:preprocess(documentsCountWikitext)
local hasDocuments = documentCount and tonumber(trim(documentCount)) and tonumber(trim(documentCount)) > 0
-- Create the content div
local docsDiv = container:tag('div')
local docsDiv = container:tag('div')
css(docsDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em; text-align:center;')
css(docsDiv, 'grid-column: 1 / -1; padding:0.2em 0.4em;')
docsDiv:wikitext('[' .. tostring(uploadUrl) .. ' <span class="infobox-button infobox-button-secondary">Add a document</span>]')
if hasDocuments then
-- Use #ask to get documents
local docsQueryWikitext =
'{{#ask:\n' ..
' [[Category:Documents]]\n' ..
' [[Has related accessory::' .. fullPageName .. ']]\n' ..
' |?Has document type\n' ..
' |?Has document URL\n' ..
' |?Has document file\n' ..
' |?Has document title\n' ..
' |format=broadtable\n' ..
' |headers=hide\n' ..
' |link=none\n' ..
'}}'
local docsResult = frame:preprocess(docsQueryWikitext)
-- Create UL element
local ul = mw.html.create('ul')
ul:css('margin', '0'):css('padding-left', '1.5em')
-- Parse the HTML table
if docsResult then
for rowContent in docsResult:gmatch('<tr[^>]*>(.-)</tr>') do
local cells = {}
for cellContent in rowContent:gmatch('<td[^>]*>(.-)</td>') do
local cleanContent = cellContent:gsub('<[^>]+>', '')
table.insert(cells, cleanContent)
end
-- cells[1] = page name, cells[2] = type, cells[3] = URL, cells[4] = file, cells[5] = title
if #cells >= 2 then
local docType = trim(cells[2])
local docTitle = trim(cells[5])
local docPageName = trim(cells[1])
if docPageName then
local displayText = 'Document'
if docTitle then
displayText = docTitle
elseif docType then
displayText = docType
end
local li = ul:tag('li')
li:wikitext('[[' .. docPageName .. '|' .. displayText .. ']]')
end
end
end
end
docsDiv:node(ul)
-- Add upload link as a button
local uploadDiv = mw.html.create('div')
uploadDiv:css('margin-top', '0.5em'):css('text-align', 'center')
uploadDiv:wikitext('[' .. tostring(uploadUrl) .. ' <span class="infobox-button infobox-button-secondary">Add a document</span>]')
docsDiv:node(uploadDiv)
else
-- Just show the upload link as a button
local uploadDiv = mw.html.create('div')
uploadDiv:css('text-align', 'center')
uploadDiv:wikitext('[' .. tostring(uploadUrl) .. ' <span class="infobox-button infobox-button-secondary">Add a document</span>]')
docsDiv:node(uploadDiv)
end


-- Archival Links section: Query for link subobjects and display by type
-- Archival Links section: Query for link subobjects and display by type
MediaWiki Appliance - Powered by TurnKey Linux