Module:User:Mr. Stradivarius/sandbox
Appearance
![]() | This is the module sandbox page for Module:User:Mr. Stradivarius. |
local p = {}
local getArgs = require( 'Module:Arguments' ).getArgs
local function errorMessage( message )
return '<strong class="error">ERROR with invocation of [[Module:GetShortDescription]]: ' .. 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 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*|.-}}' )
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 = getArgs( frame.args )
if not args then
return errorMessage( 'could not getArgs' )
end
return getShortDescription( args ) or ''
end
return p