PC Gaming Shelter
An archive dedicated to preserving PC Gaming history and more

Module:Resource: Difference between revisions

From PC Gaming Shelter
No edit summary
No edit summary
Line 2: Line 2:


local p = {}
local p = {}
local function isValidUrl(url)
return clean(url) and mw.ustring.match(url, '^https?://[^%s]+$') ~= nil
end


local function clean(value)
local function clean(value)
Line 12: Line 16:
end
end


local function isTruthy(value)
local function externalHost(url)
value = clean(value)
url = clean(url)
if not value then
if not url then
return false
return nil
end
end


value = mw.ustring.lower(value)
local host = mw.ustring.match(url, '^https?://([^/%?#]+)')
return value == '1' or value == 'true' or value == 'yes' or value == 'y' or value == 'on'
if not host then
end
return nil
 
local function splitValues(value)
value = clean(value)
if not value then
return {}
end
end


local values = {}
return mw.ustring.gsub(host, '^www%.', '')
for part in string.gmatch(value, '([^,]+)') do
local item = clean(part)
if item then
table.insert(values, item)
end
end
 
if #values == 0 then
table.insert(values, value)
end
 
return values
end
 
local function firstValue(value)
local values = splitValues(value)
return values[1]
end
end


Line 148: Line 130:


local function propertiesPlan()
local function propertiesPlan()
local scraped = scrapeVideoId(url)
local service = clean(scraped.service) or ''
local id = clean(scraped.id) or ''
-- ['Form field name'] = { 'Semantic property', 'delimiter' }
-- ['Form field name'] = { 'Semantic property', 'delimiter' }
local propMap = {
local propMap = {
Line 160: Line 147:
['Has spoilers']          = {'Has spoilers', },
['Has spoilers']          = {'Has spoilers', },
['Has subtitles']          = {'Has subtitles' },
['Has subtitles']          = {'Has subtitles' },
['Has subtitles language'] = {'Has subtitles language', ',' }
['Has subtitles language'] = {'Has subtitles language', ',' },
['Has video provider']    = { service },
['Has video id']          = { id }
}
}
return propMap
return propMap
end
end


local function isValidUrl(url)
local function infoboxSectionPlan()
return clean(url) and mw.ustring.match(url, '^https?://[^%s]+$') ~= nil
local sectionMap = {
end
['Cover image'] = {
 
heading = '',
local function externalHost(url)
template = 'Infobox/Cover',
url = clean(url)
order = '1',
if not url then
rows = {
return nil
'Image'
end
}
 
},
local host = mw.ustring.match(url, '^https?://([^/%?#]+)')
['Resource Meta'] = {
if not host then
heading = 'Resource Info',
return nil
template = 'Infobox/Resource Info',
end
order = '2',
 
rows = {
return mw.ustring.gsub(host, '^www%.', '')
'Has short description',
end
'Related game',
 
'Has resource type',
local function embed(scraped)
'Has content type',
if not scraped then
'Has spoilers',
return
}
end
},
 
['Resource Info'] = {
local service = clean(scraped.service)
heading = 'Link',
local id = clean(scraped.id)
template = 'Infobox/Resource Link',
if not service or not id then
order = '2',
return
rows = {
end
'Has URL',
 
'Has video provider',
mw.smw.set({
'Has video id',
['Has video provider'] = service,
'Has language',
['Has video id'] = id
'Has subtitles',
})
'Has subtitles language',
end
'Has date',
 
'Created by'
local function cssList(values, linked)
}
if not values or #values == 0 then
}
return nil
}
end
return sectionMap
 
local items = {}
for _, value in ipairs(values) do
if linked then
table.insert(items, string.format('[[%s]]', value))
else
table.insert(items, value)
end
end
 
return table.concat(items, ', ')
end
 
local function addProperty(props, name, value, delimiter)
value = clean(value)
if not value then
return
end
 
if delimiter then
props[name] = splitValues(value)
else
props[name] = value
end
end
 
local function setProperties(args)
local props = {}
 
addProperty(props, 'Has URL', args['Has URL'])
addProperty(props, 'Has short description', args['Has short description'])
addProperty(props, 'Related game', args['Related game'], ',')
addProperty(props, 'Has resource type', args['Has resource type'], ',')
addProperty(props, 'Has content type', args['Has content type'], ',')
addProperty(props, 'Has date', args['Has date'])
addProperty(props, 'Created by', args['Created by'])
addProperty(props, 'Has language', args['Has language'], ',')
addProperty(props, 'Has subtitles language', args['Has subtitles language'], ',')
 
if isTruthy(args['Has spoilers']) then
props['Has spoilers'] = 'Yes'
end
 
if isTruthy(args['Has subtitles']) then
props['Has subtitles'] = 'Yes'
end
 
if next(props) then
mw.smw.set(props)
end
end
 
local function makeBadge(parent, text, badgeClass)
if not clean(text) then
return
end
 
parent
:tag('span')
:addClass('badge')
:addClass(badgeClass or 'badge-secondary')
:addClass('mr-1 mb-1')
:wikitext(text)
end
 
local function addRow(parent, label, value)
value = clean(value)
if not value then
return
end
 
local row = parent:tag('div'):addClass('infobox-section d-flex flex-column flex-sm-row border-0 rounded-0 px-3 py-1')
row:tag('div'):addClass('infobox-label'):wikitext(label)
row:tag('div'):addClass('infobox-value'):wikitext(value)
end
 
local function renderUrl(args)
local url = clean(args['Has URL'])
if not isValidUrl(url) then
return nil
end
 
local label = externalHost(url) or url
return string.format('[%s %s]', url, label)
end
end


local function renderResource(frame, args)
function p.infobox(frame)
local url = clean(args['Has URL'])
local category  = 'Resources'
local title = mw.title.getCurrentTitle().text
local properties = propertiesPlan()
local host = externalHost(url)
local sections  = infoboxSectionPlan()
local resourceType = firstValue(args['Has resource type'])
local infobox   = utils.createInfobox(frame, category, properties, sections)
 
return infobox
local root = mw.html.create()
local card = root
:tag('div')
:addClass('infobox d-flex flex-column card shadow-none rounded-0 bg-transparent mb-4 float-sm-right')
 
local header = card
:tag('div')
:addClass('infobox-block border-bottom pb-2 mb-2')
 
local titleLine = header
:tag('div')
:addClass('d-flex flex-column flex-sm-row justify-content-between align-items-sm-start px-3 pt-3')
 
local titleBox = titleLine:tag('div')
if host then
titleBox:tag('div'):addClass('text-muted small mb-2'):wikitext(host)
end
 
local badges = titleLine:tag('div'):addClass('text-sm-right mt-2 mt-sm-0')
makeBadge(badges, resourceType, 'badge-info')
makeBadge(badges, contentType, 'badge-secondary')
 
if isTruthy(args['Has spoilers']) then
makeBadge(badges, 'Spoilers', 'badge-warning')
end
 
if isTruthy(args['Has subtitles']) then
makeBadge(badges, 'Subtitles', 'badge-success')
end
 
if isValidUrl(url) then
header
:tag('div')
:addClass('px-3 pb-3')
:tag('span')
:addClass('btn btn-success font-weight-bold')
:wikitext(renderUrl(args))
else
header
:tag('div')
:addClass('alert alert-warning mx-3 mb-3')
:wikitext('No valid URL has been provided.')
end
 
local details = card:tag('div'):addClass('infobox-block')
 
addRow(details, 'Game', cssList(splitValues(args['Related game']), true))
addRow(details, 'Media type', cssList(splitValues(args['Has resource type'])))
addRow(details, 'Content type', cssList(splitValues(args['Has content type'])))
addRow(details, 'Date', clean(args['Has date']))
addRow(details, 'Created by', clean(args['Created by']))
addRow(details, 'Language', cssList(splitValues(args['Has language'])))
 
if isTruthy(args['Has subtitles']) then
addRow(details, 'Subtitles', cssList(splitValues(args['Has subtitles language'])) or 'Yes')
end
 
return root
end
 
function p.main(frame)
local parent = frame:getParent()
local args = parent and parent.args or frame.args
 
setProperties(args)
return tostring(renderResource(frame, args))
end
end


return p
return p

Revision as of 21:43, 2 July 2026

Documentation for this module may be created at Module:Resource/doc

local utils = require( 'Module:Utils' )

local p = {}

local function isValidUrl(url)
	return clean(url) and mw.ustring.match(url, '^https?://[^%s]+$') ~= nil
end

local function clean(value)
	if value == nil then
		return nil
	end

	value = mw.text.trim(tostring(value))
	return value ~= '' and value or nil
end

local function externalHost(url)
	url = clean(url)
	if not url then
		return nil
	end

	local host = mw.ustring.match(url, '^https?://([^/%?#]+)')
	if not host then
		return nil
	end

	return mw.ustring.gsub(host, '^www%.', '')
end

local function scrapeVideoId(url)
	url = clean(url)
	if not isValidUrl(url) then
		return nil
	end

	local host = mw.ustring.lower(externalHost(url) or '')
	local id

	if host == 'archive.org' then
		id = pathMatch(url, '^/details/([^/]+)') or pathMatch(url, '^/embed/([^/]+)')
		return id and { service = 'archiveorg', id = id } or nil
	end

	if host == 'player.bilibili.com' then
		id = queryParam(url, 'bvid') or queryParam(url, 'aid')
		return id and { service = 'bilibili', id = id } or nil
	end

	if host == 'bilibili.com' or host == 'm.bilibili.com' then
		id = pathMatch(url, '^/video/([^/]+)')
		return id and { service = 'bilibili', id = id } or nil
	end

	if host == 'play-tv.kakao.com' then
		id = pathMatch(url, '^/embed/player/cliplink/([^/]+)')
		return id and { service = 'kakaotv', id = id } or nil
	end

	if host == 'tv.kakao.com' then
		id = pathMatch(url, '^/channel/[^/]+/cliplink/([^/]+)') or pathMatch(url, '^/v/([^/]+)')
		return id and { service = 'kakaotv', id = id } or nil
	end

	if host == 'tv.naver.com' then
		id = pathMatch(url, '^/embed/([^/]+)') or pathMatch(url, '^/v/([^/]+)')
		return id and { service = 'navertv', id = id } or nil
	end

	if host == 'embed.nicovideo.jp' or host == 'nicovideo.jp' or host == 'www.nicovideo.jp' then
		id = pathMatch(url, '^/watch/([^/]+)')
		return id and { service = 'niconico', id = id } or nil
	end

	if host == 'soundcloud.com' then
		return { service = 'soundcloud', id = url }
	end

	if host == 'open.spotify.com' then
		local spotifyType = pathMatch(url, '^/(album)/[^/]+') or pathMatch(url, '^/(artist)/[^/]+') or pathMatch(url, '^/(track)/[^/]+')
		id = pathMatch(url, '^/[^/]+/([^/]+)')
		if spotifyType and id then
			return { service = 'spotify' .. spotifyType, id = id }
		end
	end

	if host == 'twitch.tv' or host == 'www.twitch.tv' then
		id = pathMatch(url, '^/videos/([^/]+)')
		if id then
			return { service = 'twitchvod', id = id }
		end

		id = pathMatch(url, '^/[^/]+/clip/([^/]+)')
		if id then
			return { service = 'twitchclip', id = id }
		end

		id = pathMatch(url, '^/([^/]+)')
		return id and { service = 'twitch', id = id } or nil
	end

	if host == 'clips.twitch.tv' then
		id = pathMatch(url, '^/([^/]+)')
		return id and { service = 'twitchclip', id = id } or nil
	end

	if host == 'vimeo.com' or host == 'www.vimeo.com' or host == 'player.vimeo.com' then
		id = pathMatch(url, '^/video/([^/]+)') or pathMatch(url, '^/([^/]+)')
		return id and { service = 'vimeo', id = id } or nil
	end

	if host == 'youtube.com' or host == 'www.youtube.com' or host == 'm.youtube.com' or host == 'music.youtube.com' then
		id = queryParam(url, 'v') or pathMatch(url, '^/embed/([^/]+)') or pathMatch(url, '^/shorts/([^/]+)') or pathMatch(url, '^/live/([^/]+)')
		if id then
			return { service = 'youtube', id = id }
		end

		id = queryParam(url, 'list')
		return id and { service = 'youtubeplaylist', id = id } or nil
	end

	if host == 'youtu.be' then
		id = pathMatch(url, '^/([^/]+)')
		return id and { service = 'youtube', id = id } or nil
	end

	return nil
end

local function propertiesPlan()
	local scraped = scrapeVideoId(url)
	
	local service = clean(scraped.service) or ''
	local id = clean(scraped.id) or ''
	
	-- ['Form field name'] = { 'Semantic property', 'delimiter' }
	local propMap = {
		['Has URL']                = {'Has URL' },
		['Has short description']  = {'Has short description' },
		['Related game']           = {'Related game', ',' },
		['Has resource type']      = {'Has resource type' },
		['Has content type']       = {'Has content type', ',' },
		['Has date']               = {'Has date' },
		['Created by']             = {'Created by' },
		['Has language']           = {'Has language', ',' },
		['Has spoilers']           = {'Has spoilers', },
		['Has subtitles']          = {'Has subtitles' },
		['Has subtitles language'] = {'Has subtitles language', ',' },
		['Has video provider']     = { service },
		['Has video id']           = { id }
	}
	return propMap
end

local function infoboxSectionPlan()
	local sectionMap = {
		['Cover image'] = {
			heading = '',
			template = 'Infobox/Cover',
			order = '1',
			rows = {
				'Image'
			}
		},
		['Resource Meta'] = {
			heading = 'Resource Info',
			template = 'Infobox/Resource Info',
			order = '2',
			rows = {
				'Has short description',
				'Related game',
				'Has resource type',
				'Has content type',
				'Has spoilers',	
			}
		},
		['Resource Info'] = {
			heading = 'Link',
			template = 'Infobox/Resource Link',
			order = '2',
			rows = {
				'Has URL',
				'Has video provider',
				'Has video id',
				'Has language',	
				'Has subtitles',
				'Has subtitles language',
				'Has date',
				'Created by'
			}
		}
	}
	return sectionMap
end

function p.infobox(frame)
	local category   = 'Resources'
	local properties = propertiesPlan()
	local sections   = infoboxSectionPlan()
	local infobox    = utils.createInfobox(frame, category, properties, sections)
	return infobox
end

return p