Module:GetShortDescription/sandbox: Difference between revisions

Content deleted Content added
refactor in progress; unfinished and probably non-functional but real life requires my attention
continued efforts to refactor all the way up the chain; gotta start here though; not done yet
Line 1:
 
local getArgs = require( 'Module:Arguments' ).getArgs
local mLang = require( 'Module:Lang' )
 
local function pipedLink( title ) return '[[:' .. title .. '|' .. title .. ']]' end
 
local function isNone( desc ) return type( desc ) == 'string' and desc:match( '^[Nn]one$' ) end
 
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]]' end
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( titleargs_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( title )args_name .. ' '|' .. args_name .. ']] has ' .. quantity_of_things.templates .. ' {{tlx|short description}}' .. message
mw.addWarning( alarmingMessage( message, true ) )
end
Line 42 ⟶ 29:
if isEmpty( lang.no ) and notEmpty( wikidata_description_lang ) and wikidata_description_lang ~= 'en' then
result.language = wikidata_description_lang
result.wikidata = mLangrequire( 'Module:Lang' )._lang {
wikidata_description_lang,
wikidata_description,
Line 55 ⟶ 42:
end
 
local function getShortDescription( titletitle_table, title_tableargs_name )
local page_content = title_table:getContent()
if isEmpty( page_content ) then return nil end
-- Try to avoid asking if the page exists; it can be expensive.
if report_redlinks and not title_table.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.
Line 87 ⟶ 70:
end
possible_short_descriptions[ template_content_index ] = {}
-- Look through the params:
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.
Line 98 ⟶ 79:
if not has_equals or param:match( '^1' ) then
if has_equals then param = mw.ustring.gsub( param, '^1%s*=%s*', '' ) end
-- If we made it this far; grabGrab 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.
Line 109 ⟶ 90:
end
local short_descriptions = {}
-- Look through the short descriptions.
for i, possible_short_description in ipairs( possible_short_descriptions ) do
if possible_short_description.description then
Line 120 ⟶ 102:
end
-- Let previewWarning() work out if these numbers are bad.
previewWarning( titleargs_name, quantity_of_things )
if #short_descriptions >= 1 then return short_descriptions[ #short_descriptions ].description end
return nil
Line 164 ⟶ 146:
 
local function getDescription( args )
objectify_alarm = args.objectify_alarm
report_redlinks = args.report_redlinks
local args_name = args.name
if isEmpty( args_name ) then return alarmingMessage( 'requires a page name (including namespace)' ) end
local title, title_table = getTitleAndTable( args_name )
if isSisterProjectLink( title ) then return fallbacknil end
local fallback = args.fallback
if isSisterProjectLink( title ) then return fallback end
local prefer = args.prefer or 'explicit'
local only = args.only
local lang = {
italic = args.lang_italic,
Line 189 ⟶ 160:
no = args.lang_no
}
-- Pass args_name to getShortDescription() so previewMessages won't be confusing.
if only == 'explicit' then return getShortDescription( title_table, args_name )
result elseif only == 'wikidata' then return getWikidataDescription( title, lang )
elseif prefer == 'explicit' then return getShortDescription( title_table, args_name ) or getWikidataDescription( title, lang )
result elseif prefer == 'wikidata' then return getWikidataDescription( title, lang ) or getShortDescription( args_nametitle_table, title_tableargs_name ) end
return nil
end
 
local function pipedLinkisNone( titledesc ) return '[[:'type( ..desc title ..) == '|string' ..and title ..desc:match( '^[Nn]]one$' ) end
 
local function main( args )
local description = getDescription( args )
-- Handle requirements and formatting
local result
if only == 'explicit' then
result = getShortDescription( args_name, title_table )
elseif only == 'wikidata' then
result = getWikidataDescription( title, lang )
elseif prefer == 'explicit' then
result = getShortDescription( args_name, title_table )
if isEmpty( result ) then result = getWikidataDescription( title, lang, true )
elseif isNone( result ) then
local wikidata_description = getWikidataDescription( title, lang, true )
if notEmpty( wikidata_description ) then
wikidata_description.none = true
result = wikidata_description
end
end
elseif prefer == 'wikidata' then
result = getWikidataDescription( title, lang ) or getShortDescription( args_name, title_table ) end
mw.logObject( ddescription )
if isNone( result ) then result = nil end
return result or fallbackdescription
end
 
Line 224 ⟶ 183:
 
function p.main( frame )
local args = require( 'Module:Arguments' ).getArgs( frame )
if isEmpty( args ) then return alarmingMessage( 'could not getArgs' ) end
localreturn d = getDescriptionmain( args ) or ''
mw.logObject( d )
return d
end