Module:GetShortDescription/sandbox: Difference between revisions

Content deleted Content added
correction for redirect title conversion; will properly test later; so tired
refactor in progress; unfinished and probably non-functional but real life requires my attention
Line 3:
local mLang = require( 'Module:Lang' )
 
local function pipedLink( nametitle ) return '[[:' .. nametitle .. '|' .. nametitle .. ']]' end
 
local function isNone( desc ) return type( desc ) == 'string' and desc:match( '^[Nn]one$' ) end
Line 24:
 
-- Grammatically reasonable concatenation of possible issues into one message per problematic link target.
local function previewWarning( nametitle, 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( nametitle ) .. ' ' .. 'has ' .. quantity_of_things.templates .. ' {{tlx|short description}}' .. message
mw.addWarning( alarmingMessage( message, true ) )
end
end
 
local function getWikidataDescription( nametitle, lang, not_explicit )
local wikidata_id = mw.wikibase.getEntityIdForTitle( nametitle )
if isEmpty( wikidata_id ) then return nil end
local wikidata_description, wikidata_description_lang = mw.wikibase.getDescriptionWithLang( wikidata_id )
if isEmpty( wikidata_description ) or isNone( wikidata_description ) then return nil end
returnlocal result = { wikidata = wikidata_description }
if isEmpty( lang.no ) and notEmpty( wikidata_description_lang ) and wikidata_description_lang ~= 'en' then
result.language = wikidata_description_lang
wikidata_descriptionresult.wikidata = mLang._lang {
wikidata_description_lang,
wikidata_description,
Line 50 ⟶ 52:
}
end
return result
if notEmpty( not_explicit ) then wikidata_description = { wikidata = wikidata_description } end
return wikidata_description
end
 
local function getExplicitDescriptiongetShortDescription( nametitle, new_titletitle_table )
local page_content = new_titletitle_table:getContent()
if isEmpty( page_content ) then
-- Try to avoid asking if the page exists; it can be expensive.
if report_redlinks and not new_titletitle_table.exists then return { redlink = true } end
return nil
end
Line 119 ⟶ 120:
end
-- Let previewWarning() work out if these numbers are bad.
previewWarning( nametitle, quantity_of_things )
if #short_descriptions >= 1 then return short_descriptions[ #short_descriptions ].description end
return nil
end
 
local function isSisterProjectLink( nametitle )
local sister_project_prefixes = {
'wiktionary', 'wikt',
Line 142 ⟶ 143:
'phabricator', 'phab'
}
local pre_colon = nametitle:match( '^(%a+):' )
if pre_colon then
for i, sister in ipairs( sister_project_prefixes ) do
Line 151 ⟶ 152:
end
 
-- Resolve redirects (including multiple) to their end target.
local function getShortDescription( args )
local function getTitleAndTable( orig_name )
local title_table = mw.title.new( orig_name )
new_titletitle_table = new_titletitle_table.redirectTarget or new_titletitle_table
local nametitle = new_titletitle_table.prefixedText
if title == orig_name then
return title, title_table
end
return getTitleAndTable( title )
end
 
local function getShortDescriptiongetDescription( 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 new_titletitle, title_table = mw.title.newgetTitleAndTable( args_name )
new_title = new_title.redirectTarget or new_title
local name = new_title.prefixedText
local fallback = args.fallback
if isSisterProjectLink( nametitle ) then return fallback end
local prefer = args.prefer or 'explicit'
local only = args.only
local lang = {
italic = args.lang_italic,
Line 173 ⟶ 189:
no = args.lang_no
}
local result
if only == 'explicit' then result = getExplicitDescription( args_name, new_title )
result = getShortDescription( args_name, title_table )
elseif only == 'wikidata' then result = getWikidataDescription( name, lang )
elseif only == 'wikidata' then
if isEmpty( result ) then result = getWikidataDescription( nametitle, lang, true )
elseif prefer == 'explicit' then
result = getExplicitDescriptiongetShortDescription( args_name, new_titletitle_table )
if isEmpty( result ) then result = getWikidataDescription( name, lang, true )
elseif if onlyisEmpty( ==result 'wikidata') then result = getWikidataDescription( nametitle, lang, true )
elseif isNone( result ) then
local wikidata_description = getWikidataDescription( nametitle, lang, true )
if notEmpty( wikidata_description ) then
wikidata_description.none = true
Line 187 ⟶ 212:
end
end
elseif prefer == 'wikidata' then result = getWikidataDescription( name, lang ) or getExplicitDescription( args_name, new_title ) end
elseif prefer == 'wikidata' then
elseif prefer == 'wikidata' then result = getWikidataDescription( nametitle, lang ) or getExplicitDescriptiongetShortDescription( args_name, new_titletitle_table ) end
if isNone( result ) then result = nil end
Line 199 ⟶ 226:
local args = getArgs( frame )
if isEmpty( args ) then return alarmingMessage( 'could not getArgs' ) end
returnlocal getShortDescriptiond = getDescription( args ) or ''
mw.logObject( d )
return d
end