Module:GetShortDescription/sandbox: Difference between revisions

Content deleted Content added
fix whitespace-trimming pattern
sync with live
Line 1:
local getArgs = require( 'Module:Arguments' ).getArgs
local p = {}
 
local mLang = require( 'Module:Lang' )
 
local function errorMessagepipedLink( messagename ) return '[[:' .. name .. '|' .. name .. ']]' end
 
return '<strong class="error">ERROR with invocation of [[Module:GetShortDescription]]: ' .. message .. '</strong>'
local function isEmpty( value ) return value == nil or value == '' end
 
local function notEmpty( value ) return not isEmpty( value ) end
 
local objectify_alarm
local report_redlinks
 
local function alarmingMessage( message, preview )
message = '<span style="color:#d33">[[Module:GetShortDescription]] ' .. message .. '.</span>'
if not preview then
message = message .. '[[Category:Pages displaying alarming messages about Module:GetShortDescription]]'
if objectify_alarm then return { alarm = message } end
end
return message
end
 
-- Grammatically reasonable concatenation of possible issues into one message per problematic link target.
local function previewWarning( name, quantity_of_things )
local message = ''
if quantity_of_things.params > 3 then message = message .. ' with extraneous parameters' end
if quantity_of_things.descriptions > 1 then message = message .. ', declaring ' .. quantity_of_things.descriptions .. ' short descriptions' end
if quantity_of_things.templates > 1 or notEmpty( message ) then
message = 'has detected that ' .. pipedLink( name ) .. ' ' .. 'has ' .. quantity_of_things.templates .. ' {{tlx|short description}}' .. message
mw.addWarning( alarmingMessage( message, true ) )
end
end
 
local function getWikidataDescription( name, lang )
local wikidata_id = mw.wikibase.getEntityIdForTitle( name )
if notisEmpty( wikidata_id ) then return nil end
return nil
end
local wikidata_description, wikidata_description_lang = mw.wikibase.getDescriptionWithLang( wikidata_id )
if notisEmpty( wikidata_description ) then return nil end
if notEmpty( lang.no ) or wikidata_description_lang == 'en' then return wikidata_description end
return nil
if isEmpty( wikidata_description_lang ) then return nil end
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_contentnew_title = mw.title.new( name ):getContent()
if notlocal page_content then= new_title:getContent()
if isEmpty( page_content ) then
return errorMessage( 'could not getContent of [[:' .. name .. '|' .. name .. ']]' )
-- Try to avoid asking if the page exists; it can be expensive.
end
if report_redlinks and not new_title.exists then return { redlink = true } 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
local contents_of_all_short_description_templates = {}
-- Preprocess the short description template so that we don't have to parse
-- Because there could be any number of short description templates, and not all where there should be; get all the templates.
-- the wikitext ourselves. We have to use safesubst here so that the
for template in page_content:gmatch( '{%b{}}' ) do
-- template is processed correctly when both transcluding and when
local short_description_content = mw.ustring.match( template, '^{{%s*[Ss]hort description%s*|%s*(.-)%s*}}' )
-- substituting.
mw.log if notEmpty( short_description_content ) then
-- Collect the contents of short description templates.
local preprocessed_short_description_template = mw.getCurrentFrame():preprocess(
'{{safesubst:short contents_of_all_short_description_templates[ description|'#contents_of_all_short_description_templates+1 ..] = short_description_content .. '}}'
end
)
-- An opportunity for efficiency gain exists - to break if another type of template is found e.g. citation templates,
mw.log(preprocessed_short_description_template)
-- Getbut theon an appallingly formatted page, a short description fromtemplate down by the preprocessedcategories templatewould likely be missed.
local short_description = mw.ustring.match(
preprocessed_short_description_template,
'>%s*(.-)%s*<'
)
mw.log(short_description)
if not short_description then
return nil
end
if #contents_of_all_short_description_templates < 1 then return nil end
-- Now we know what this template's short description is,
local quantity_of_things = {
-- check if it's in use and return nil if not.
templates = #contents_of_all_short_description_templates,
if mw.ustring.match( short_description, '^[Nn]one$' ) then
descriptions = 0,
return nil
params = 0
}
local possible_short_descriptions = {}
for template_content_index, short_description_template_contents in ipairs( contents_of_all_short_description_templates ) do
-- Split the contents at pipes and trim.
local short_description_template_params = mw.text.split( short_description_template_contents, '%s*|%s*' )
if #short_description_template_params > quantity_of_things.params then
quantity_of_things.params = #short_description_template_params
end
possible_short_descriptions[ template_content_index ] = {}
for i, param in ipairs( short_description_template_params ) do
-- Because regular expressions haven't been invented yet...
-- ignore everything that isn't a declaration of 'noreplace'
-- or a short description that isn't 'none'.
if param == 'noreplace' or mw.ustring.match( param, '^2%s*=%s*noreplace$' ) then
-- Take note of 'noreplace'-ing for establishment of hierarchy later.
possible_short_descriptions[ template_content_index ].noreplace = true
else
local has_equals = param:match( '=' )
if not has_equals or param:match( '^1' ) then
if has_equals then param = mw.ustring.gsub( param, '^1%s*=%s*', '' ) end
if not param:match( '^[Nn]one$' ) then
-- If we made it this far; grab the short description.
-- If the template has both a numbered and an unnumbered short description;
-- whichever comes last (ltr) will be used for the page, so overwriting works out great.
possible_short_descriptions[ template_content_index ].description = param
-- But we want to know the total quantity of descriptions being declared.
quantity_of_things.descriptions = quantity_of_things.descriptions + 1
end
end
end
end
end
local short_descriptions = {}
mw.log(short_description)
for i, possible_short_description in ipairs( possible_short_descriptions ) do
-- It could still be a completely useless string e.g.
if possible_short_description.description then
-- {{short description| &nbsp; &nbsp; &nbsp; &nbsp; }}
-- If a description is 'noreplace'-ing; demote it.
-- creates an 8 character short description of entirely whitespace.
if possible_short_description.noreplace and #possible_short_descriptions > 1 then
-- Trim the result before return.
-- But don't demote it if it's already at the bottom.
local trimmed_short_description = mw.ustring.match( short_description, '^%s*(.-)%s*$' )
if i > 1 then table.insert( short_descriptions, #short_descriptions, possible_short_description )
if trimmed_short_description ~= '' then
else short_descriptions[ #short_descriptions+1 ] = possible_short_description end
return trimmed_short_description
else short_descriptions[ #short_descriptions+1 ] = possible_short_description end
else
end
return nil
end
-- Let previewWarning() work out if these numbers are bad.
previewWarning( name, quantity_of_things )
if #short_descriptions >= 1 then return short_descriptions[ #short_descriptions ].description end
return nil
end
 
local function getShortDescription( args )
objectify_alarm = args.objectify_alarm
report_redlinks = args.report_redlinks
local name = args.name
if isEmpty( name ) then return alarmingMessage( 'requires a page name (including namespace)' ) end
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'
if isEmpty( prefer ) then prefer = 'explicit' end
local lang = {}
local 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
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 == 'explicitwikidata' then result = getWikidataDescription( name, lang ) or getExplicitDescription( name )
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
 
local p = {}
 
function p.main( frame )
returnlocal getShortDescriptionargs = getArgs( frame.args ) or ''
if isEmpty( args ) then return alarmingMessage( 'could not getArgs' ) end
return getShortDescription( args ) or ''
end