Changes

Jump to: navigation, search

Module:Set page header

3,272 bytes added, 8 months ago
Place all data in a `releaseData` object. Set SMW data.
local UTIL = require( 'Module:Util' )
local LANGUAGE_ENGLISH mwTitle = DATAmw.getLanguage( 'English' )title
-- Object containing data on this release of the set-- (Object is called `releaseData` rather than `setData`, which is ambiguous in programming terms.)---- @param pagename string The name of the current page (the list/gallery)-- @param set string The page name of the set-- @param name string The English name of the set-- @param localName string The name of the set in this particular print and language-- @param language table The language of this particular print e.g. "English", "Korean", etc.-- (See [[Module:Data/static/language/data]] for the table structure)-- @param region table The distribution region of this print e.g. "European English", "Korean", etc.-- (See [[Module:Data/static/region/data]] for the table structure)-- @param medium table The medium (OCG, TCG, etc.)-- (See [[Module:Data/static/medium/data]] for the table structure)-- @param edition table The edition of this particular print-- (See [[Module:Data/static/edition/data]] for the table structure)-- @param releaseDate string The date this print was releasedlocal mwTitle releaseData = { pagename = nil, set = mw.titlenil, name = nil, localName = nil, language = {}, region = {}, medium = {}, edition = nil, releaseDate = nil}
local function getRegion( pagename )
return table.concat( parts, '(', 1, length == 1 and length or ( length - 1 ) )
end
 
local function makeHeader( setPagename, region, edition )
local language = DATA.getLanguage( region.index )
 
local medium = DATA.getMedium( region.index )
 
local englishName = DATA.getName(
setPagename,
LANGUAGE_ENGLISH
)
 
local localizedName = DATA.getName(
setPagename,
language
)
local header = mw.html.create( 'div' ):addClass( 'page-header' )
header:tag( 'div' )
:addClass( 'page-header__link' )
:wikitext( UTIL.link( setPagenamereleaseData.set, englishName releaseData.name ) )
-- Localized set name:
if releaseData.language.index ~= LANGUAGE_ENGLISH'en' and releaseData.index and localizedName localName then
header:tag( 'div' )
:addClass( 'page-header__localized' )
:attr( 'lang', releaseData.language.index ) :wikitext( localizedName releaseData.localName )
end
header:tag( 'div' )
:addClass( 'page-header__caption' )
:wikitext( releaseData.region.full ) :wikitext( releaseData.edition and ( ' - %s' ):format( releaseData.edition.full ) )
return tostring( header )
end
local function makeCategories( ns, region, edition )
return table.concat{
( '[[Category:%s %s]]' ):format( releaseData.region.full, ns ), releaseData.edition and ( '[[Category:%s %s]]' ):format( releaseData.edition.full, ns ),
}
end
 
local function setSmwData( ns, setPagename, region, edition )
mw.smw.set({
['Set page'] = releaseData.set,
['Language'] = releaseData.language.full,
['Region'] = releaseData.region and releaseData.region.full,
['Edition'] = releaseData.edition and releaseData.edition.full,
['Release date'] = releaseData.releaseDate
})
end
 
-- Fetch data from the set page
-- @return table name, localName, releaseDate
local function querySetData()
return mw.smw.ask{
'[[' .. releaseData.set .. ']]',
'?English name = name' ,
'?' .. releaseData.language.full .. ' name = localName',
'?' .. releaseData.region.full .. ' release date = releaseDate',
mainlabel = '-'
}
end
 
-- Set data for the `releaseData` table based on the page name and parameters
local function setReleaseData( currentPageName, args )
-- Set the set page equal to the supplied set
-- or defaul to to the one that can be inferred from the page name
releaseData.set = args.set or getSetPagename( currentPageName )
 
-- Set the region equal to the supplied region
-- or default to the one that can be inferred from the page name
releaseData.region = args.region
and DATA.getRegion( args.region )
or getRegion( currentPageName )
 
-- Set the language, based on the region
releaseData.language = DATA.getLanguage( releaseData.region.index )
 
-- Set the medium based on the region
releaseData.medium = DATA.getMedium( releaseData.region.index )
 
-- Set the edition based on the page name
releaseData.edition = getEdition( currentPageName )
 
-- Query the set page to find more information
local queriedData = querySetData()
 
-- Update the `releaseData` object based on the results of the query
-- except where overwritten by the `args`.
releaseData.name = queriedData.name
releaseData.localName = args.name or queriedData.localName
releaseData.releaseDate = args.releaseDate or queriedData.releaseDate
end
function main( args )
-- Get the name of the current page (distinct from the name of the set page)
local currentTitle = args.pagename
and mwTitle.new( args.pagename )
local ns = currentTitle.subjectNsText
-- Skip everything if this is in the main namespace
if not UTIL.trim( ns ) then
return
end
local pagename = currentTitle.text-- Update the `releaseData` object based on information from the page name local region = args.region -- and DATAthe supplied arguments.getRegion( args.region ) or getRegion( pagename )  local edition = getEditionsetReleaseData( pagename currentTitle.text, args )
-- Get the header wikitext local setPagename header = args.set or getSetPagenamemakeHeader( pagename )
-- Get the categories wikitext local header categories = makeHeadermakeCategories( setPagename, region, edition ns )
local categories = makeCategories-- Set SMW properties setSmwData( ns, region, edition )
-- Return the wikitext to render
return table.concat{
'__NOTOC__',

Navigation menu