Content deleted Content added
fix whitespace-trimming pattern |
Fred Gandt (talk | contribs) sync with live |
||
Line 1:
local getArgs = require( 'Module:Arguments' ).getArgs
local mLang = require( 'Module:Lang' )
local function
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
local wikidata_description, wikidata_description_lang = mw.wikibase.getDescriptionWithLang( wikidata_id )
if
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
if isEmpty( page_content ) then
-- Try to avoid asking if the page exists; it can be expensive.
if report_redlinks and not new_title.exists then return { redlink = true } end
return nil
end
local contents_of_all_short_description_templates = {}
-- Because there could be any number of short description templates, and not all where there should be; get all the templates.
for template in page_content:gmatch( '{%b{}}' ) do
local short_description_content = mw.ustring.match( template, '^{{%s*[Ss]hort description%s*|%s*(.-)%s*}}' )
-- Collect the contents of short description templates.
end
-- An opportunity for efficiency gain exists - to break if another type of template is found e.g. citation templates,
end
if #contents_of_all_short_description_templates < 1 then return nil end
local quantity_of_things = {
templates = #contents_of_all_short_description_templates,
descriptions = 0,
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 = {}
for i, possible_short_description in ipairs( possible_short_descriptions ) do
if possible_short_description.description then
-- If a description is 'noreplace'-ing; demote it.
if possible_short_description.noreplace and #possible_short_descriptions > 1 then
-- But don't demote it if it's already at the bottom.
if i > 1 then table.insert( short_descriptions, #short_descriptions, possible_short_description )
else short_descriptions[ #short_descriptions+1 ] = possible_short_description end
else short_descriptions[ #short_descriptions+1 ] = possible_short_description end
end
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
local result
local only = args.only
local prefer = args.prefer
if isEmpty( prefer ) then prefer = 'explicit' end
local lang
italic = args.lang_italic, }
elseif only == 'wikidata' then result = getWikidataDescription( name, lang )
elseif prefer == '
end
return result or args.fallback
end
local p = {}
function p.main( frame )
if isEmpty( args ) then return alarmingMessage( 'could not getArgs' ) end
return getShortDescription( args ) or ''
end
|