Module:Category described in year: Difference between revisions

Content deleted Content added
Tweak addord
 
(16 intermediate revisions by 5 users not shown)
Line 1:
require('Module:No globalsstrict')
 
local conf = require( 'Module:Category described in year/conf' ) --configuration module
 
--[[==========================================================================]]
Line 10 ⟶ 8:
if tonumber(i) then
local s = tostring(i)
local tens, ones = string.match(s, '(1?)(%d)$')
local ones = string.match(s, '%d$')
if tens then return s..'th'
if tens then return s..'th'
elseif ones == '1' then return s..'st'
elseif ones == '2' then return s..'nd'
Line 32 ⟶ 31:
 
function p.autodetect( frame )
local conf = require( 'Module:Category described in year/config' ) --configuration module
local commonsLink = require('Module:Commons link')
local currentTitle = mw.title.getCurrentTitle()
local parentArg = frame:getParent().args[1] --accept 1 unnamed category parameter if not in category namespace; required for testing/doc/etc. purposes
Line 42 ⟶ 43:
local toc = nil
local categories = {}
local trackingCategoriestrackingCats = {
[1] = '', --placeholder for [[Category:Described in year unknown category]]
[2] = '', --placeholder for [[Category:Described in year error]]
[3] = '', --placeholder for [[Category:Described in year with manual category]]
}
local outString = nil
Line 58 ⟶ 60:
if parentArg then
currCat = mw.ustring.gsub(parentArg, 'Category:', '')
currQID = mw.wikibase.getEntityIdForTitle('Category:' .. currCat)
else --currQID & currCat both nil
if currentTitle.fullText ~= 'Template:Category described in year' then --ignore self...
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|P]]' --missing a category parameter outside category namespace
end
end
end
--find commons & wikispecies link(s); produce {{Commons and category}} and/or {{Wikispecies}} template(s)
if currQID then
if commonsLink._hasGallery(currQID) or commonsLink._hasCategory(currQID) then
local commonsLinks = {}
commons = frame:expandTemplate{ title = 'Commons and category', args = { qid=currQID }}
end
local currEntity = mw.wikibase.getEntity(currQID)
if currEntity then
--check Commons"Other categorysites" propertysitelinks (P373)for Wikispecies
local ccPropState = currEntity:getBestStatements('P373')[1]
if ccPropState then
local ccPropVal = ccPropState.mainsnak.datavalue.value
if ccPropVal then
commonsLinks[#commonsLinks + 1] = 'Category:' .. mw.ustring.gsub(ccPropVal, 'Category:', '')
end
end
--check Commons gallery property (P935)
local cgPropState = currEntity:getBestStatements('P935')[1]
if cgPropState then
local cgPropVal = cgPropState.mainsnak.datavalue.value
if cgPropVal then
commonsLinks[#commonsLinks + 1] = cgPropVal
end
end
--check "Other sites" sitelinks for Commons and/or Wikispecies
local currSiteLinks = currEntity.sitelinks
if currSiteLinks then
local currCommonsWiki = currEntity.sitelinks.commonswiki
if currCommonsWiki then
local currCommonsWikiTitle = currEntity.sitelinks.commonswiki.title
if currCommonsWikiTitle then
commonsLinks[#commonsLinks + 1] = currCommonsWikiTitle
end
end
local currSpeciesWiki = currEntity.sitelinks.specieswiki
if currSpeciesWiki then
Line 102 ⟶ 83:
if currSpeciesWikiTitle then
wikispecies = frame:expandTemplate{ title = 'Wikispecies', args = { currSpeciesWikiTitle } }
end end end end end
end
end
end
--produce {{Commons}} template(s) (ignore duplicates)
if commonsLinks[1] then --turn these into a loop if # of commons sources >= 4
commons = frame:expandTemplate{ title = 'Commons', args = { commonsLinks[1] } }
if commonsLinks[2] and
commonsLinks[2] ~= commonsLinks[1] then
commons = commons .. frame:expandTemplate{ title = 'Commons', args = { commonsLinks[2] } }
end
if commonsLinks[3] and
commonsLinks[3] ~= commonsLinks[1] and
commonsLinks[3] ~= commonsLinks[2] then
commons = commons .. frame:expandTemplate{ title = 'Commons', args = { commonsLinks[3] } }
end
end
end --if currQID then
--[[======================================================================]]
Line 136 ⟶ 99:
local currCent = mw.ustring.match(currCat, 'described in the (%d+)[snrt][tdh] century$')
local currFrml = mw.ustring.match(currCat, 'by year of (formal) description$')
local lastCent, nextCent = nil, nil --used with currYear & currCent
local parentCent = nil --used with currYear
local minYear = tonumber(conf[currGroup].minyear)
if minYear == nil or
(minYear and (minYear <= 1700 or minYear >= 2000))
then
minYear = 1758 --default to 1758 per ICZN Art. 5
end
Line 150 ⟶ 113:
parentCent = 1 + mw.ustring.match(currYear, '^%d%d')
end
lastCent = parentCent - 1
nextCent = parentCent + 1
elseif currDeca then
currYDCF = 'decade'
bConfError = true
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|D]]' --invalid decade-parent (deprecated)
elseif currCent then
currYDCF = 'century'
lastCent = currCent - 1
nextCent = currCent + 1
elseif currFrml then
currYDCF = 'formal'
else
bConfError = true
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|N]]' --invalid category name
end
Line 173 ⟶ 132:
if conf[currGroup] == nil then
bConfError = true
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|1]]' --group (Bacteria/Plants/etc.) key missing from conf
elseif conf[currGroup][currYDCF] == nil then
bConfError = true
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|2]]' --year/century/formal key missing
else
if conf[currGroup][currYDCF].description == nil then
bConfError = true
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|3]]' --description key missing
end
if conf[currGroup][currYDCF].parent1 == nil then
bConfError = true
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|4]]' --parent key missing
end
end
Line 204 ⟶ 163:
if currCent then description = mw.ustring.gsub(description, '%%century%%', addOrd(currCent)) --"21st"
else description = mw.ustring.gsub(description, '%%century%%', 'this century') end
end
--produce toc
if mw.site.stats.pagesInCategory(currCat, 'pages') >= conf['tocmin'] then --expensive
local args = { numerals = 'no' }
toc = frame:expandTemplate{ title = 'Category TOC', args = args }
end
--produce cats & navs
local iparent = 1
local parenti = 'parent' .. iparent
local sortkeyi = 'sortkey' .. iparent
while conf[currGroup][currYDCF][parenti] do
local parent = conf[currGroup][currYDCF][parenti]
Line 223 ⟶ 176:
if currYDCF == 'year' then
if nav == nil then
local args = { ['min'] = minYear, ['skip-gaps'] = 'yes' }
if parentArg and currentTitle.namespace ~= 14 then
args['testcase'] = parentArg
end
nav = frame:expandTemplate{ title = 'NavseasoncatsCategory series navigation', args = args }
end
if parent == 'century' then
if isNilOrEmpty(sortkey) then sortkey = currYear end --default to currYear
categories[iparent] = '[[Category:'..currGroup..' described in the '..addOrd(parentCent)..' century|'..sortkey..']]'
elseif parent == 'biology' then
if isNilOrEmpty(sortkey) then sortkey = '' --default to none
Line 240 ⟶ 194:
categories[iparent] = '[[Category:'..currYear..' in biology'..sortkey..']]' --if/when all biology cats exists, merge this elseif with 'paleontology'
end
elseif parent == 'paleontology' then
if isNilOrEmpty(sortkey) then sortkey = '' --default to none
else sortkey = '|'..sortkey end
categories[iparent] = '[[Category:'..currYear..' in '..parent..sortkey..']]'
elseif parent == 'environment' then
if isNilOrEmpty(sortkey) then sortkey = '' --default to none
else sortkey = '|'..sortkey end
categories[iparent] = '[[Category:'..currYear..' in the environment'..sortkey..']]'
elseif mw.ustring.match(parent, '^%u[%l ]+') then --e.g. Animals/Insects/Fossil taxa
if isNilOrEmpty(sortkey) then sortkey = '' --default to none
else sortkey = '|'..sortkey end
categories[iparent] = '[[Category:'..parent..' described in '..currYear..sortkey..']]'
else
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|Y]]' --invalid year-parent
end
Line 259 ⟶ 217:
elseif currYDCF == 'century' then
if nav == nil then
local args = {}
nav = frame:expandTemplate{ title = 'Container category' }
if parentArg and currentTitle.namespace ~= 14 then
local args = { currGroup .. ' described in the ' .. addOrd(lastCent) .. ' century',
args['testcase'] = parentArg
currGroup .. ' described in the ' .. addOrd(nextCent) .. ' century' }
end
nav = nav .. frame:expandTemplate{ title = 'Category pair', args = args }
nav = frame:expandTemplate{ title = 'Container category' } ..
frame:expandTemplate{ title = 'Category series navigation', args = args }
end
if parent == 'formal' then
if isNilOrEmpty(sortkey) then sortkey = addOrd(currCent) end --default to currCent
categories[iparent] = '[[Category:'..currGroup..' by year of formal description|'..sortkey..']]'
elseif parent == 'biology' then
if isNilOrEmpty(sortkey) then sortkey = '' --default to none
Line 275 ⟶ 236:
categories[iparent] = '[[Category:'..addOrd(currCent)..' century in biology'..sortkey..']]' --if/when all biology cats exists, merge this elseif with 'paleontology'
end
elseif parent == 'paleontology' then
if isNilOrEmpty(sortkey) then sortkey = '' --default to none
else sortkey = '|'..sortkey end
categories[iparent] = '[[Category:'..addOrd(currCent)..' century in '..parent..sortkey..']]'
elseif parent == 'environment' then
if isNilOrEmpty(sortkey) then sortkey = '' --default to none
else sortkey = '|'..sortkey end
categories[iparent] = '[[Category:'..addOrd(currCent)..' century in the environment'..sortkey..']]'
elseif mw.ustring.match(parent, '^%u[%l ]+') then --e.g. Animals/Insects/Fossil taxa
if isNilOrEmpty(sortkey) then sortkey = '' --default to none
else sortkey = '|'..sortkey end
categories[iparent] = '[[Category:'..parent..' described in the '..addOrd(currCent)..' century'..sortkey..']]'
else
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|C]]' --invalid century-parent
end
--[[======================== Formal ==========================]]
elseif currYDCF == 'formal' then
local formalParentsDefaultSortkey_Space = {
['Animals'] = true,
['Insects'] = true,
['Molluscs'] = true,
['Fungi'] = true,
}
local formalParentsDefaultSortkey_None = {
['Species'] = true,
['Taxa'] = true,
['Fossil taxa'] = true,
}
if nav == nil then
nav = frame:expandTemplate{ title = 'Container category' }
Line 299 ⟶ 275:
if isNilOrEmpty(sortkey) then sortkey = ' Year' end --default to " Year"
categories[iparent] = '[[Category:'..currGroup..'|'..sortkey..']]'
elseif parent == 'Animals' or parent == 'Insects' or parent == 'Molluscs' then
elseif parent == 'paleontology' then
if isNilOrEmpty(sortkey) then sortkey = ' ' end --default to " "
if isNilOrEmpty(sortkey) then sortkey = ' ' end --default to " "; special parent
categories[iparent] = '[[Category:Paleontology by year|'..sortkey..']]'
elseif parent then --allow freeform formal-parents, as long as they exist
if mw.title.new( parent, 'Category' ).exists then
if sortkey then
categories[iparent] = '[[Category:'..parent..'|'..sortkey..']]'
else
categories[iparent] = '[[Category:'..parent..']]'
end
else
trackingCats[2] = '[[Category:Described in year error|G]]' --invalid freeform formal-parent
end
elseif formalParentsDefaultSortkey_Space[parent] then
if isNilOrEmpty(sortkey) then sortkey = ' ' end --default to " "; normal parent
categories[iparent] = '[[Category:'..parent..' by year of formal description|'..sortkey..']]'
elseif parent == 'Species' or parent == 'Taxa' or parent == 'Fossil taxa' then
elseif formalParentsDefaultSortkey_None[parent] then
if isNilOrEmpty(sortkey) then sortkey = '' --default to none
if isNilOrEmpty(sortkey) then sortkey = '' --default to none; normal parent
else sortkey = '|'..sortkey end
categories[iparent] = '[[Category:'..parent..' by year of formal description'..sortkey..']]'
elseif parent == 'paleontology' then
if isNilOrEmpty(sortkey) then sortkey = ' ' end --default to " "
categories[iparent] = '[[Category:Paleontology by year|'..sortkey..']]'
else
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|F]]' --invalid formal-parent
end
--[[========================= Error ==========================]]
else
trackingCategoriestrackingCats[2] = '[[Category:Described in year error|U]]' --unknown configuration
end
iparent = iparent + 1
parenti = 'parent' .. iparent
sortkeyi = 'sortkey' .. iparent
end --while conf[currGroup][currYDCF][parenti] do
end --if bConfError == false then
Line 328 ⟶ 319:
local cat = mw.ustring.match(category, '%[%[Category:([%w%s]+)')
if mw.title.new(cat, 14).exists == false then
trackingCategoriestrackingCats[1] = '[[Category:Described in year unknown category]]'
break
end
end
--check for manual cats
if currentTitle.namespace == 14 then --category namespace
local currContent = mw.title.makeTitle( 'Category', currCat or '' ):getContent()
local mancat = mw.ustring.match(currContent or '', '%[%[%s*Category')
if mancat then trackingCats[3] = '[[Category:Described in year with manual category]]' end
end
end --if currCat then
--build header & rem surrounding whitespace
local br = '<br />'
local n = '\n'
if nav then header = nav end
if portal then header = header .. n..portal end
if commons then header = header .. n..commons end
if wikispecies then header = header .. n..wikispecies end
if description and description ~= '' then
header = header .. description
elseif portal or commons or wikispecies then
header = mw.ustring.gsub(header, '<br ?/?>', '')
end
if toc then header = header .. '<br />' .. toc end
--rem surrounding whitespace
header = mw.text.trim(header)
header = mw.ustring.gsub(header, '^<br />'..br, '')
header = mw.ustring.gsub(header, '<br />..'$', '')
--append header to outString
if outString then outString = outString .. header
else outString = header end
--append cats to outString
if currentTitle.namespace == 14 then --category namespace
if table.maxn(categories) > 0 then
outString = outString .. table.concat(categories)
end
outString = outString .. table.concat(trackingCategoriestrackingCats)
else
if table.maxn(categories) > 0 then --might be 0 if there's an error before setting cats
outString = outString .. '<br />' .. mw.ustring.gsub(table.concat(categories, '<br />'), '%[%[', '[[:')
end
outString = outString..br..mw.ustring.gsub(table.concat(trackingCats, br), '%[%[', '[[:')
--ws cleanup
while string.match(outString, br..br) do --rem dup brs produced by empty ('') first/consecutive tracking cat/s
outString = string.gsub(outString, br..br, br)
end
outString = outString .. '<br />' .. mw.ustring.gsub(table.concat(trackingCategories, '<br />'), '%[%[', '[[:')
outString = mw.ustring.gsub(outString, '<br /><br />', '<br />') --produced by empty ('') first/consecutive tracking cat/s
outString = mw.ustring.gsub(outString, '<br /><br />', '<br />') --jic (use while loop if #trackingCategories >= 3 or 4)
end