Jump to content

Module:GetShortDescription/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 08:13, 26 January 2023 (create sandbox from Special:PermaLink/1135215889). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
local p = {}

local getArgs = require( 'Module:Arguments' ).getArgs

local getTransclusion = require( 'Module:Transcluder' ).get

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 short_description_template = getTransclusion( name .. '#', { only = 'templates', templates = '[Ss]hort description' } )
	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 mw.ustring.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