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 09:19, 26 January 2023 (use preprocessing with safesubst to avoid trying to parse wikitext). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local mLang = require( 'Module:Lang' )

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
	-- Do as little as possible; return wikidata_description immediately if no more processing is required.
	if lang.no or wikidata_description_lang == 'en' then
		return wikidata_description
	end
	if not wikidata_description_lang then
		return nil
	end
	return  mLang._lang {
		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 ''
	}
end

local function getExplicitDescription( name )
	local page_content = mw.title.new( name ):getContent()
	if not page_content then
		return errorMessage( 'could not getContent of [[:' .. name ..  '|' .. name .. ']]' )
	end
	-- Search for a short description template and grab the parameters from it if found.
	-- Do as little as possible; return nil immediately if that's what we found.
	local short_description_content = mw.ustring.match( page_content, '{{%s*[Ss]hort description%s*|%s*(.-)%s*}}' )
	if not short_description_content then
		return nil
	end
	-- Preprocess the short description template so that we don't have to parse
	-- the wikitext ourselves. We have to use safesubst here so that the
	-- template is processed correctly when both transcluding and when
	-- substituting.
	local preprocessed_short_description_template = mw.getCurrentFrame():preprocess(
		'{{safesubst:short description|' .. short_description_content .. '}}'
	)
	-- Get the short description from the preprocessed template.
	local short_description = mw.ustring.match(
		preprocessed_short_description_template,
		'>%s*(.-)%s*<'
	)
	if not short_description then
		return nil
	end
	-- Now we know what this template's short description is,
	-- check if it's in use and return nil if not.
	if mw.ustring.match( short_description, '^[Nn]one$' ) then
		return nil
	end
	-- It could still be a completely useless string e.g.
	-- {{short description| &nbsp; &nbsp; &nbsp; &nbsp; }}
	-- creates an 8 character short description of entirely whitespace.
	-- Trim the result before return.
	return mw.ustring.match( short_description, '^%s*([^%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 )
	return getShortDescription( frame.args ) or ''
end

return p