Module:Resource
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Resource/doc
-- ================================================================================
-- Module dependencies
-- ================================================================================
local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo -- for cargo queries if needed
local cf = require("Module:Custom_functions")
local tf = require("Module:Table_functions")
local ibf = require("Module:Infobox_functions")
local html = mw.html.create()
-- ================================================================================
-- Main table
-- ================================================================================
local p = {}
-- ================================================================================
-- Infobox
-- ================================================================================
function p.infobox(frame)
local args = getArgs(frame)
local title = mw.title.getCurrentTitle().text
-- this is important to escape single and double quotes in titles
title = title:gsub("'", "''"):gsub('"', '""')
-- ========================================
-- Image file control
-- ========================================
-- First assume image is provided by the Cargo template call.
local image = args.File_name
-- Function to check if a file exists and is not deleted
function file_exists(filename)
local file_title = mw.title.new('File:' .. filename)
if not file_title then
return false
end
-- More reliable check for file existence
local image_info = mw.ext.data and mw.ext.data.getImageInfo(file_title.fullText)
return image_info and image_info.exists
end
-- Check if image is nil or empty
if not image or image == "" then
-- Assign standard filename pattern
image = title .. ".png"
-- Use the fixed file check
if not file_exists(image) then
image = "Resource icon.png"
end
end
-- ========================================
-- Begin creating html infobox code
-- ========================================
local root = ""
local html_theme_class = '-resource'
root = ibf.create_infobox(root, title, html_theme_class)
root = ibf.add_image(root, image, html_theme_class)
if long_title then
long_title = "<i>" .. long_title .. "</i>"
end
root = ibf.add_row(root, 'Alt title:', long_title, html_theme_class)
-- Don't add links to Author if resource category is Media
if args.Category == "Media" then
root = ibf.add_row(root, 'Author:', args.Author, html_theme_class, "string")
else
root = ibf.add_row(root, 'Author:', args.Author, html_theme_class, "page")
end
root = ibf.add_row(root, 'Language:', args.Language, html_theme_class)
root = ibf.add_row(root, 'Wiki file name:', args.Wiki_file_name, html_theme_class)
root = ibf.add_row(root, 'Published:', args.Year_published, html_theme_class)
root = ibf.add_row(root, 'Source link:', args.Resource_link, html_theme_class, "ext_link", "View")
root = ibf.add_row(root, 'Category:', args.Category, html_theme_class)
root = ibf.add_row(root, 'Keywords:', args.Keywords, html_theme_class)
-- ========================================
-- Format species
-- ========================================
-- check for the special case of a resource pertaining to many or all species
local species_display
if args.Species_purview_control == "all species" then
species_display = "All species (see resource for more details)"
root = ibf.add_header(root, "Species purview", html_theme_class, 2)
root = ibf.add_spanning_row(root, species_display, html_theme_class, "string")
elseif args.Species_purview_control == "many species" then
species_display = "Many species (see resource for more details)"
root = ibf.add_header(root, "Species purview", html_theme_class, 2)
root = ibf.add_spanning_row(root, species_display, html_theme_class, "string")
elseif args.Species_purview then
root = ibf.add_header(root, "Species purview", html_theme_class, 2)
root = ibf.add_spanning_row(root, args.Species_purview, html_theme_class, "page")
end
-- ========================================
-- Format geography
-- ========================================
if args.Geographic_purview then
root = ibf.add_header(root, "Geographic purview", html_theme_class, 2)
root = ibf.add_spanning_row(root, args.Geographic_purview, html_theme_class, "page")
end
return root
end
-- ================================================================================
-- Get resource author attribution and resource descriptions
-- ================================================================================
-- Despite the name, this function creates an author attribution and resource description section
function p.get_description(frame)
local args = getArgs(frame)
local title = mw.title.getCurrentTitle().text
-- Escape single quotes by replacing them with two single quotes.
-- Double quotes might not need escaping depending on the database's handling of string literals.
title = title:gsub("'", "''")
-- Run cargo query on resources so it can be accessed from functions below
local tables = "Resource"
local fields = "Name, Author, Resource_description, Resource_link, Subresource, File_name"
local where_clause = string.format("Name = '%s'", title)
local cargo_args = {
where = where_clause
}
local cargo_results = cargo.query(tables, fields, cargo_args)
local output = ""
if cargo_results and #cargo_results > 0 then
local description = cargo_results[1].Resource_description
local author = cf.format_delimited_string(cargo_results[1].Author)
if description ~= nil and description ~= "" then
local header_author = "== Source author(s) ==\n"
local header_desc = "== Resource description ==\n"
output = header_author .. author .. "\n" .. header_desc .. description .. "\n"
else
output = "== Resource description ==\nNo description found.\n"
end
else
output = "== Resource description ==\nError: No results returned for the given title.\n"
end
return output
end
-- ================================================================================
-- Get subresource links for a parent resource
-- ================================================================================
function p.get_links(frame)
local args = getArgs(frame)
local title = mw.title.getCurrentTitle().text
-- Escape single quotes by replacing them with two single quotes
title = title:gsub("'", "''")
local output = ""
-- Run cargo query on resources
local tables = "Resource"
local fields = "Name, Resource_description, Resource_link, Subresource, File_name"
local cargo_args = {
where = string.format("Name = '%s'", title)
}
local cargo_results = cargo.query(tables, fields, cargo_args)
-- Check if cargo_results are valid and has at least one result
if not cargo_results or #cargo_results == 0 then
return "== Resource links ==\nError: No results returned for the given title.\n"
end
local link = cargo_results[1].Resource_link
-- Run cargo query on Subresource
tables = "Subresource"
fields = "Name, Parent_resource, Resource_link, File_name"
cargo_args = {
where = string.format("Parent_resource = '%s'", title)
}
local cargo_subresource = cargo.query(tables, fields, cargo_args)
-- Loop to display results based on subresource availability
if cargo_subresource and #cargo_subresource > 0 then
local subresources = {}
for i, row in ipairs(cargo_subresource) do
local sub_link = row.Resource_link
local title = row.Name
local number = tonumber(title:match("%d+$")) or 0
table.insert(subresources, { link = sub_link, title = title, number = number })
end
table.sort(subresources, function(a, b) return a.title < b.title end)
local bulleted_list = ""
for i, subresource in ipairs(subresources) do
bulleted_list = bulleted_list .. "*[" .. subresource.link .. " " .. subresource.title .. "]\n"
end
local header = "== Resource links ==\n"
return header .. bulleted_list
elseif link then
local link_display = "[" .. link .. " Link to resource]"
local header = "== Resource links ==\n"
return header .. link_display .. "\n"
else
return "== Resource links ==\nNo links found.\n"
end
end
return p