Jump to content

Module:GetShortDescription

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Fred Gandt (talk | contribs) at 14:42, 29 January 2023 (calming the error messages down a bit). 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 pipedLink( name ) return '[[:' .. name ..  '|' .. name .. ']]' end

local function isEmpty( value ) return value == nil or value == '' end

local function notEmpty( value ) return not isEmpty( value ) end

local function alarmingMessage( message )
	return '<span style="color:#d33">[[Module:GetShortDescription]] ' .. message .. '.</span>'
end

local function previewWarning( name, message )
	mw.addWarning( alarmingMessage( 'has detected that the {{tlx|short description}} on ' .. pipedLink( name ) .. ' ' .. message ) )
end

local function getWikidataDescription( name, lang )
	local wikidata_id = mw.wikibase.getEntityIdForTitle( name )
	if isEmpty( wikidata_id ) then return nil end
	local wikidata_description, wikidata_description_lang = mw.wikibase.getDescriptionWithLang( wikidata_id )
	if isEmpty( wikidata_description ) then return nil end
	if notEmpty( lang.no ) or wikidata_description_lang == 'en' then return wikidata_description end
	if isEmpty( wikidata_description_lang ) then return nil end
	return  mLang._lang {
		wikidata_description_lang,
		wikidata_description,
		italic = lang.italic,
		nocat = lang.nocat,
		size = lang.size,
		cat = lang.cat,
		rtl = lang.rtl
	}
end

local function getExplicitDescription( name )
	local page_content = mw.title.new( name ):getContent()
	if isEmpty( page_content ) then return alarmingMessage( 'could not getContent of ' .. pipedLink( name ) ) end
	local contents_of_all_short_description_templates = {}
	for template in mw.ustring.gmatch( page_content, '%{%b{}}' ) do
		local short_description_content = mw.ustring.match( template, '^{{%s*[Ss]hort description%s*|%s*(.-)%s*}}' )
		if notEmpty( short_description_content ) then
			contents_of_all_short_description_templates[ #contents_of_all_short_description_templates+1 ] = short_description_content
		end
	end
	if #contents_of_all_short_description_templates < 1 then return nil end
	if #contents_of_all_short_description_templates > 1 then
		previewWarning( name, 'has ' .. #contents_of_all_short_description_templates .. ' short description templates' )
	end
	local possible_short_descriptions = {}
	for tci, short_description_template_contents in ipairs( contents_of_all_short_description_templates ) do
		possible_short_descriptions[ tci ] = {}
		local short_description_template_params = mw.text.split( short_description_template_contents, '%s*|%s*' )
		if #short_description_template_params > 3 then
			previewWarning( name, 'is [[Template:Short description/doc|misconfigured]]' )
		end
		for i, param in ipairs( short_description_template_params ) do
			if not param:match( '=' ) or param:match( '^1' ) then
				if param == 'noreplace' then
					possible_short_descriptions[ tci ][ 'noreplace' ] = true
				else
					possible_short_descriptions[ tci ][ 'description' ] = param
				end
			end
		end
	end
	local probable_short_descriptions = {}
	for i, possible_short_description in ipairs( possible_short_descriptions ) do
		if possible_short_description[ 'description' ] then
			if possible_short_description[ 'noreplace' ] and #possible_short_descriptions > 1 then
				table.insert( probable_short_descriptions, #probable_short_descriptions, possible_short_description )
			else
				probable_short_descriptions[ #probable_short_descriptions+1 ] = possible_short_description
			end
		end
	end
	if #probable_short_descriptions < 1 then
		previewWarning( name, 'declares no short description' ) -- TODO: this needs to be issued later (but I've forgotten why)
		return nil
	elseif #probable_short_descriptions > 1 then
		previewWarning( name, 'declares ' .. #possible_short_descriptions .. ' short descriptions' )
	end
	local probable_short_description = probable_short_descriptions[ #probable_short_descriptions ][ 'description' ]
	if probable_short_description:match( '=' ) then
		probable_short_description = mw.ustring.gsub( probable_short_description, '^1%s*=%s*', '' )
	end
	if probable_short_description:match( '^[Nn]one$' ) then return nil end
	return probable_short_description
end

local function getShortDescription( args )
	local name = args.name
	if isEmpty( name ) then return alarmingMessage( 'requires a page name (including namespace)' ) end
	local result
	local only = args.only
	local prefer = args.prefer
	if isEmpty( prefer ) then prefer = 'explicit' end
	local lang = {
		italic = args.lang_italic,
		nocat = args.lang_nocat,
		size = args.lang_size,
		cat = args.lang_cat,
		rtl = args.lang_rtl,
		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