Module:GetShortDescription/sandbox: Difference between revisions

Content deleted Content added
add some logs and a new entry point
use latest version
Line 1:
local p = {}
 
local getArgsmLang = require( 'Module:ArgumentsLang' ).getArgs
 
local getTransclusion = require( 'Module:Transcluder' ).get
 
local function errorMessage( message )
Line 10 ⟶ 8:
 
local function getWikidataDescription( name, lang )
mw.log("Getting Wikidata description: " .. name)
local wikidata_id = mw.wikibase.getEntityIdForTitle( name )
if not wikidata_id then
Line 19 ⟶ 16:
return nil
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
Line 25 ⟶ 23:
return nil
end
return mLang._lang {
local current_frame = mw.getCurrentFrame()
wikidata_description_lang,
if not current_frame then
wikidata_description,
return errorMessage( 'could not getCurrentFrame' )
italic = lang.italic or '',
end
nocat = lang.nocat or '',
local non_english_wikidata_description = current_frame:expandTemplate {
titlesize = 'lang.size or '',
argscat = {lang.cat or '',
rtl = lang.rtl or ''
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 ''
}
}
if not non_english_wikidata_description then
return errorMessage( 'could not expandTemplate lang' )
end
return non_english_wikidata_description
end
 
local function getExplicitDescription( name )
local page_content = mw.title.new( name ):getContent()
mw.log("Getting explicit description: " .. name)
if not current_framepage_content then
local short_description_template = getTransclusion( name .. '#', { only = 'templates', templates = '[Ss]hort description' } )
return errorMessage( 'could not getCurrentFramegetContent of [[:' .. name .. '|' .. name .. ']]' )
if not short_description_template then
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
-- The following mess is an alternative to preprocessing, which is expensive and
mw.log(short_description_template)
-- causes empty strings to be returned if the invocation is substituted.
local current_frame = mw.getCurrentFrame()
-- Why does it look like this though?
if not current_frame then
-- Short descriptions defined by template {{short description}} may contain:
return errorMessage( 'could not getCurrentFrame' )
-- a numbered or unnumbered 1st param for the description
-- a numbered or unnumbered 2nd param for noreplace
-- the pagetype named param
-- an empty description
-- we might therefore need to understand the content of:
-- {{short description|2=noreplace|pagetype=Redirect|Redirect page}} or
-- {{short description|2=noreplace|1=Redirect page|pagetype=Redirect}} or
-- {{short description|pagetype=Redirect|Redirect page|noreplace}} etc.
-- all with a variety white space.
-- Split the param content into trimmed param expressions.
local short_description_params = mw.text.split( short_description_content, '%s*|%s*' )
local short_description
-- For each param expession:
for i, param in ipairs( short_description_params ) do
-- ignore 'pagetype=<anything>'
-- ignore 'noreplace' if unnumbered or numbered as '2='
if not mw.ustring.match( param, '^pagetype%s*=' )
and not mw.ustring.match( param, '^noreplace$' )
and not mw.ustring.match( param, '^2%s*=' ) then
-- Whatever remains is the short description expression.
short_description = param
break
end
end
-- The short descrition expression might be in the form
-- 'short description' or
-- '1=short description'
-- If the latter; trim the leading '1='.
if mw.ustring.match( short_description, '=' ) then
short_description = mw.ustring.gsub( short_description, '^1%s*=%s*', '' )
end
-- Now we know what this template's short description is,
local preprocessed_short_description_template = current_frame:preprocess( short_description_template )
-- check if it's in use and return nil if not.
mw.log(preprocessed_short_description_template)
if mw.ustring.match( short_description, '^[Nn]one$' ) then
if not preprocessed_short_description_template then
short_description = nil
return errorMessage( 'could not preprocess short_description_template' )
end
-- It could still be a completely useless string e.g.
return mw.ustring.match( preprocessed_short_description_template, '>%s*(.-)%s*<' )
-- {{short description| &nbsp; &nbsp; &nbsp; &nbsp; }}
-- creates an 8 character short description of entirely whitespace.
-- Trim the result before return.
return mw.ustring.match( preprocessed_short_description_templateshort_description, '>^%s*(.[^%s]-)%s*<$' )
end
 
local function p._maingetShortDescription( args )
local name = args.name
if not name then
Line 90 ⟶ 116:
result = getWikidataDescription( name, lang ) or getExplicitDescription( name )
end
return result or args.fallback or ''
end
 
function p.main( frame )
localreturn args = getArgsgetShortDescription( frame.args ) or ''
if not args then
return errorMessage( 'could not getArgs' )
end
return p._main( args )
end