Module:Sandbox/Fred Gandt
Appearance
![]() | This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure. |
local p = {}
local function errorMessage( message )
return '<strong class="error">ERROR with invocation of [[Module:User:Fred Gandt]]: ' .. message .. '</strong>'
end
local function getWikidataDescription( name, lang )
local wikidata_id = mw.wikibase.getEntityIdForTitle( name )
if not wikidata_id then
return nil
end
local wikidata_description, wikidata_description_lang = mw.wikibase.getDescriptionWithLang( wikidata_id )
if not wikidata_description then
return nil
end
if lang.no or wikidata_description_lang == 'en' then
return wikidata_description
end
if not wikidata_description_lang then
return nil
end
local current_frame = mw.getCurrentFrame()
if not current_frame then
return errorMessage( 'could not getCurrentFrame' )
end
local non_english_wikidata_description = current_frame:expandTemplate {
title = 'lang',
args = {
wikidata_description_lang,
wikidata_description,
italic = lang.italic or '',
nocat = lang.nocat or '',
size = lang.size or '',
cat = lang.cat or '',
rtl = lang.rtl or ''
}
}
if not non_english_wikidata_description then
return errorMessage( 'could not expandTemplate lang' )
end
return non_english_wikidata_description
end
local function getImplicitDescription()
--[[
for m in string.gmatch( 'string', 'pattern' ) do
mw.log( m )
end
--]]
--[[
local current_title = mw.title.getCurrentTitle()
if not current_title then
return errorMessage( 'could not getCurrentTitle' )
end
--]]
-- let's assume that implicit descriptions will generally be set by infoboxes, but could be from anywhere
-- it would therefore makes sense to cycle through any infoboxes first
--[[ get the lead infoboxes (there might be none or many) first, since they're the most likely to place any implicit description ]]
--[[ since we will be preprocessing them, strip a bunch of stuff we definitely don't need or want ]]
--[[ being infoboxes; there SHOULD be no references, but since when did that stop anyone? -_- ]]
--local lead_infoboxes = getTransclusion( 'The Partisan#', { only = 'templates', categories = 0, references = 0, files = 0, templates = '[Ii]nfobox' } )
--mw.log( lead_infoboxes )
--[[ split the results, and loop through them ]]
--[[ preprocess and check for a description match ]]
--[[ if we get a hit; return the description ]]
-- if we didn't get a hit...
--[[ get any remaining infoboxes; multiple infoboxes is unusual, but this will unfortunately get the lead infobox again if there is one ]]
--[[ why not just do this first? because it's much more expensive and a lot less likely to be useful ]]
--[[ again; strip the stuff we don't need or want ]]
--local all_infoboxes = getTransclusion( 'The Partisan', { only = 'templates', categories = 0, references = 0, files = 0, templates = '[Ii]nfobox' } )
--mw.log( all_infoboxes )
--[[ split the results ]]
--[[ obviously, we don't want to preprocess the same infobox again, so ignore it ]]
--[[ if there are any more, loop through them ]]
--[[ preprocess and check for a description match ]]
--[[ if we get a hit; return the description ]]
-- if they weren't helpful, it's probably a good idea to stop doing this, but if you insist...
--[[ get all templates that aren't {{cite*}}s; it would be nice to not get infoboxes too, but that doesn't seem possible ]]
--[[ again; strip the stuff we don't need or want ]]
--local all_other_templates = getTransclusion( 'The Partisan', { only = 'templates', categories = 0, references = 0, files = 0, templates = '-[Cc]ite' } )
--mw.log( all_other_templates )
--[[ split the results ]]
--[[ obviously, we don't want to preprocess the infoboxes again, so ignore them ]]
--[[ if there are any more, loop through them ]]
--[[ preprocess and check for a description match ]]
--[[ if we get a hit; return the description ]]
-- if they didn't spit one out, we consider ourselves lucky to have survived
-- NOTE: if there are multiple implicit descriptions being set, it's theoretically possible we could find one that's redundant
-- I have zero intention to address this possibility
--[[
local current_title_lead_infobox = getTransclusion( tostring( current_title ), { only = 'templates' } )
if not current_title_lead_infobox then
return nil
end
current_title_lead_infobox = mw.text.trim( current_title_lead_infobox )
if not current_title_lead_infobox or current_title_lead_infobox == '' then
return nil
end
local current_frame = mw.getCurrentFrame()
if not current_frame then
return errorMessage( 'could not getCurrentFrame' )
end
local preprocessed_current_title_lead_infobox = current_frame:preprocess( current_title_lead_infobox )
if not preprocessed_current_title_lead_infobox then
return errorMessage( 'could not preprocess current_title_lead_infobox' )
end
-- <div class="shortdescription nomobile noexcerpt noprint searchaux" style="display:none">19th episode of the 7th season of Star Trek: Voyager</div>
-- right at the start too :)
return mw.text.nowiki( preprocessed_current_title_lead_infobox )
--]]
end
local function getExplicitDescription( name )
local page_content = mw.title.new( name ):getContent()
if not page_content then
return errorMessage( 'could not getContent' )
end
local short_description_template = string.match( page_content, '{{%s*[Ss]hort description%s*|.-}}' )
return short_description_template--[[
if not short_description_template then
return nil
end
local current_frame = mw.getCurrentFrame()
if not current_frame then
return errorMessage( 'could not getCurrentFrame' )
end
local preprocessed_short_description_template = current_frame:preprocess( short_description_template )
if not preprocessed_short_description_template then
return errorMessage( 'could not preprocess short_description_template' )
end
return string.match( preprocessed_short_description_template, '>%s*(.-)%s*<' )
--]]
end
local function getShortDescription( args )
local name = args.name
if not name then
return errorMessage( 'a page name (including namespace) MUST be provided' )
end
local result
local only = args.only
local prefer = args.prefer or 'explicit'
local lang = {}
lang.italic = args.lang_italic
lang.nocat = args.lang_nocat
lang.size = args.lang_size
lang.cat = args.lang_cat
lang.rtl = args.lang_rtl
lang.no = args.lang_no
if only == 'explicit' then
result = getExplicitDescription( name )
elseif only == 'wikidata' then
result = getWikidataDescription( name, lang )
elseif prefer == 'explicit' then
result = getExplicitDescription( name ) or getWikidataDescription( name, lang )
elseif prefer == 'wikidata' then
result = getWikidataDescription( name, lang ) or getExplicitDescription( name )
end
return result or args.fallback
end
function p.main( frame )
local args = frame.args
local result = getShortDescription( args ) or ''
mw.log( result )
return result
end
return p