Module:GetShortDescription/sandbox: Difference between revisions

Content deleted Content added
continued efforts to refactor all the way up the chain; gotta start here though; not done yet
getting there; taking a break; note the |none_is_valid= param...
Line 3:
 
local function notEmpty( value ) return not isEmpty( value ) end
 
local function isNone( descvalue ) return typevalue:lower( desc ) == 'stringnone' and desc:match( '^[Nn]one$' ) end
 
local function alarmingMessage( message, preview )
Line 21 ⟶ 23:
end
 
local function getWikidataDescription( title, langargs )
local wikidata_id = mw.wikibase.getEntityIdForTitle( title )
if isEmpty( wikidata_id ) then return nil end
Line 27 ⟶ 29:
if isEmpty( wikidata_description ) then return nil end
local result = { wikidata = wikidata_description }
if isEmpty( langargs.nolang_no ) and notEmpty( wikidata_description_lang ) and wikidata_description_lang ~= 'en' then
result.language = wikidata_description_lang
result.wikidata = require( 'Module:Lang' )._lang {
wikidata_description_lang,
wikidata_description,
italic = langargs.italiclang_italic,
nocat = langargs.nocatlang_nocat,
size = langargs.sizelang_size,
cat = langargs.catlang_cat,
rtl = langargs.rtllang_rtl
}
end
Line 78 ⟶ 80:
local has_equals = param:match( '=' )
if not has_equals or param:match( '^1' ) then
-- Grab the short description.
if has_equals then param = mw.ustring.gsub( param, '^1%s*=%s*', '' ) end
-- GrabIf the template has both a numbered and an unnumbered short description.;
-- whichever comes last (ltr) will be used forby thethat pagetemplate, so overwriting works out great.
-- If the template has both a numbered and an unnumbered short description;
possible_short_descriptions[ template_content_index ].description = param
-- whichever comes last (ltr) will be used for the page, so overwriting works out great.
-- ButAnd we want to know the total quantity of descriptions being declared.
possible_short_descriptions[ template_content_index ].description = param
quantity_of_things.descriptions = quantity_of_things.descriptions + 1
-- But we want to know the total quantity of descriptions being declared.
quantity_of_things.descriptions = quantity_of_things.descriptions + 1
end
end
Line 93 ⟶ 95:
for i, possible_short_description in ipairs( possible_short_descriptions ) do
if possible_short_description.description then
-- If a description is 'noreplace'-ing or 'none'; demote it.
if ( possible_short_description.noreplace or isNone( possible_short_description.description ) ) 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 )
Line 103 ⟶ 105:
-- Let previewWarning() work out if these numbers are bad.
previewWarning( args_name, quantity_of_things )
if #short_descriptions >= 1 then return { explicit = short_descriptions[ #short_descriptions ].description } end
return nil
end
Line 147 ⟶ 149:
local function getDescription( args )
local args_name = args.name
if isEmpty( args_name ) then return alarmingMessage({ alarm = 'requires a page name (including namespace)' )} end
local title, title_table = getTitleAndTable( args_name )
if isSisterProjectLink( title ) then return nil end
local prefer = args.prefer or 'explicit'
local only = args.only
local langprefer = {args.prefer or 'explicit'
-- Pass args_name to getShortDescription() so previewWarning()s won't be confusing for redirects.
italic = args.lang_italic,
if notEmpty( only ) then
nocat = args.lang_nocat,
if only == 'explicit' then return getShortDescription( title_table, args_name ) end
size = args.lang_size,
elseif if only == 'wikidata' then return getWikidataDescription( title, langargs ) end
cat = args.lang_cat,
return { alarm = 'only accepts either "explicit" or "wikidata" as the value of |only=' } end
rtl = args.lang_rtl,
if notEmpty( prefer ) then
no = args.lang_no
local if prefer = args.prefer or= 'explicit' then
}
-- local Passshort_description args_name to= getShortDescription() sotitle_table, previewMessagesargs_name won't be confusing.)
if short_description and ( not isNone( short_description.explicit ) or args.none_is_valid ) then
if only == 'explicit' then return getShortDescription( title_table, args_name )
return short_description
elseif only == 'wikidata' then return getWikidataDescription( title, lang )
end
elseif prefer == 'explicit' then return getShortDescription( title_table, args_name ) or getWikidataDescription( title, lang )
elseif return getWikidataDescription( title, args ) end
if prefer == 'wikidata' then return getWikidataDescription( title, langargs ) or getShortDescription( title_table, args_name ) end
return { alarm = 'only accepts either "explicit" or "wikidata" as the value of |prefer=' }
return nil
end
end
 
local function isNone( desc ) return type( desc ) == 'string' and desc:match( '^[Nn]one$' ) end
 
local function main( args )
local descriptionresult = getDescription( args )
mw.logObject( descriptionresult )
-- Handle requirements and formatting
if result then
mw.logObject( description )
if result.alarm then return alarmingMessage( result.alarm ) end
end
return descriptionresult
end