Module:GetShortDescription
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This Lua module is used on approximately 12,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
![]() | This module is required by Module:Annotated link. |
![]() | This module may, by design, output alarming informational messages under certain circumstances; if these messages are displayed on any page invoking this module directly or via any other module or template using it, the page will be added to Category:Pages displaying alarming messages about Module:GetShortDescription. |
Usage
{{#invoke:GetShortDescription|main |name= |only= |prefer= |fallback= |lang_italic= |lang_nocat= |lang_size= |lang_cat= |lang_rtl= |lang_no= }}
name
By providing only the required page name (including namespace), the module will look for an explicit {{short description}}
in that page, but if not found, will try to fetch the wikidata description. If neither are found, the result will be an empty string.
If the result is a wikidata description, and it is declared (at the source) as being of a foreign language (i.e. not en), the result will be appropriately formatted by Module:Lang (the module powering {{lang}}
), in accordance with MOS:OTHERLANG; see #Foreign language wikidata descriptions (below).
- Markup:
{{#invoke:GetShortDescription|main |name=Wikipedia}}
- Result: Free online crowdsourced encyclopedia
This is equivalent to stating |prefer=explicit
; see #prefer (below).
only
Providing a value for |only=
will limit the search to being only for the stated description. If no description is found, the result will be an empty string, unless a fallback is provided; see #fallback (below).
explicit
- Markup:
{{#invoke:GetShortDescription|main |name=Wikipedia |only=explicit }}
- Result: Free online crowdsourced encyclopedia
wikidata
- Markup:
{{#invoke:GetShortDescription|main |name=Wikipedia |only=wikidata }}
- Result: free multilingual online encyclopedia
prefer
Providing a value for |prefer=
will initiate the search for the stated description, but try for the alternative if none is found. If no description is found, the result will be an empty string, unless a fallback is provided; see #fallback (below).
explicit or wikidata
- Markup:
{{#invoke:GetShortDescription|main |name=Wikipedia |prefer=explicit }}
- Result: Free online crowdsourced encyclopedia
wikidata or explicit
- Markup:
{{#invoke:GetShortDescription|main |name=Wikipedia |prefer=wikidata }}
- Result: free multilingual online encyclopedia
fallback
If a value for |fallback=
is provided, and no description is found by the expressed route, the result will be the stated fallback.
only or fallback
- Markup:
{{#invoke:GetShortDescription|main |name=Example |only=explicit |fallback=This is a fallback }}
- Result: This is a fallback
prefer or fallback
- Markup:
{{#invoke:GetShortDescription|main |name=Example |prefer=wikidata |fallback=This is a fallback }}
- Result: Wikimedia disambiguation page
Foreign language wikidata descriptions
Should a wikidata description be retrieved, which is declared (at the source) as being of a foreign language (i.e. not en), per MOS:OTHERLANG, the return will be formatted as appropriate by Module:Lang by default. This may be disabled with |lang_no=yes
or adjusted via the parameters for {{lang}}
: |lang_italic=
, |lang_nocat=
, |lang_size=
, |lang_cat=
and |lang_rtl=
; see lang's documentation for details.
Requiring this module
Instances when a table is returned
Providing a value for |objectify_alarm=
will cause alarming messages (red informational messages) to be returned as tables.
Providing a value for |report_redlinks=
will cause the return of a report instead of nothing in the event that the page named is nonexistent (i.e. a WP:REDLINK) and a search for a {{short description}}
template is processed.
A table will also be returned in the event that the module is instructed to |prefer=explicit
(its default), and returns a Wikidata description. If the reason for there being no explicit short description is because it was set to none; the table will include a value for table.none
See below for examples of these behaviors:
local getShortDescription = require( 'Module:GetShortDescription' ).main
local short_description = getShortDescription( {
-- required
name = 'page name',
-- optional
prefer = 'explicit' or 'wikidata',
only = 'explicit' or 'wikidata',
fallback = 'fallback',
objectify_alarm = true,
report_redlinks = true,
lang_no = 'yes',
-- {{lang}} options
lang_italic = <yes, no, unset, invert, default>,
lang_nocat = <yes, y, true, t, on, 1>,
lang_size = <CSS font-size e.g. '1.5em'>,
lang_cat = <no, n, false, f, off, 0>,
lang_rtl = <no (default), yes>
} )
-- If we've requested to report_redlinks or to objectify_alarm then
if type( short_description ) == 'table' then
if short_description.alarm then
-- An alarming message has been returned
local alarming_message = short_description.alarm
elseif short_description.redlink then
-- Do something about that
elseif short_description.wikidata then
-- A Wikidata description was returned without being explicitly requested
local wikidata_description = short_description.wikidata
if short_description.none then
-- Because the explicit short desc was 'none'
end
end
end
local p = {}
local mLang = require( 'Module:Lang' )
local function pipedLink( name ) return '[[:' .. name .. '|' .. name .. ']]' end
local function errorMessage( message )
return '<strong class="error">ERROR with invocation of [[Module:GetShortDescription]]: ' .. message .. '.</strong>'
end
local function previewWarning( name, message )
mw.addWarning( '<span style="color:#d33">[[Module:GetShortDescription]] has detected that the {{tlc|short description}} on ' ..
pipedLink( name ) .. ' ' .. message .. '.</span>' )
end
local function isEmpty( value ) return value == nil or value == '' end
local function notEmpty( value ) return not isEmpty( value ) end
local function getWikidataDescription( name, lang )
local wikidata_id = mw.wikibase.getEntityIdForTitle( name )
-- Return nil immediately if we don't know the wikidata_id
if isEmpty( wikidata_id ) then return nil end
local wikidata_description, wikidata_description_lang = mw.wikibase.getDescriptionWithLang( wikidata_id )
-- Return nil immediately if we don't get a description
if isEmpty( wikidata_description ) then return nil end
-- Return wikidata_description immediately if no more processing is required.
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 page_content = mw.title.new( name ):getContent()
if isEmpty( page_content ) then return errorMessage( 'could not getContent of ' .. pipedLink( name ) ) end
-- The following mess is required as an alternative to preprocessing,
-- which apart from being expensive:
-- causes empty strings to be returned if the invocation is substituted
-- cannot differentiate which of multiple short descriptions is the one being applied
-- More than one short description template?
-- Yup.
local contents_of_all_short_description_templates = {}
-- For all templates:
for template in mw.ustring.gmatch( page_content, '%{%b{}}' ) do
-- Test for short description content
local short_description_content = mw.ustring.match( template, '^{{%s*[Ss]hort description%s*|%s*(.-)%s*}}' )
-- and if found
if notEmpty( short_description_content ) then
-- add it to contents_of_all_short_description_templates.
contents_of_all_short_description_templates[ #contents_of_all_short_description_templates+1 ] = short_description_content
-- In order to break as soon as possible after searching for short descriptions;
-- test for templates almost certain to follow, like citation templates, and if found, break.
-- elseif mw.ustring.match( template, '^{{%s*[Cc]it[ea]' ) then break
end
end
-- Return nil immediately if no contents_of_all_short_description_templates are found
local number_of_short_description_templates = #contents_of_all_short_description_templates
if number_of_short_description_templates < 1 then return nil end
mw.logObject( contents_of_all_short_description_templates )
-- Short descriptions defined by template {{short description}} may properly contain:
-- a numbered or unnumbered 1st param for the description
-- a numbered or unnumbered 2nd param for noreplace
-- the pagetype named param
-- but may also contain:
-- an empty description or no piped params at all
-- two descriptions; one numbered and one not
-- unknown params
-- we might therefore need to understand the content of:
-- {{short description|2=noreplace|pagetype=Redirect|Short desc}} or
-- {{short description|2=noreplace|1=Short desc|pagetype=Redirect}} or
-- {{short description|pagetype=Redirect|Short desc|noreplace}} or
-- {{short description|unknown=param|Short desc}} or
-- {{short description|Short desc|1=Another short desc}} and so on
-- all with a variety white space.
if number_of_short_description_templates == 1 then
-- Extract short_description_template_contents from contents_of_all_short_description_templates.
local short_description_template_contents = contents_of_all_short_description_templates[ 1 ]
-- Split the param content into trimmed param expressions.
local short_description_template_params = mw.text.split( short_description_template_contents, '%s*|%s*' )
-- Prepare to possibly issue a preview_warning.
local preview_warning
if #short_description_template_params > 3 then
preview_warning = ' is [[Template:Short description/doc|misconfigured]]'
end
-- Create a table to hold possible_short_descriptions.
local possible_short_descriptions = {}
-- For each param expession:
for i, param in ipairs( short_description_template_params ) do
-- Ignore anything that isn't either '1=Desc' or just 'Desc'
if param ~= 'noreplace' and ( not param:match( '=' ) or param:match( '^1' ) ) then
-- and the remainder are possible_short_descriptions.
possible_short_descriptions[ #possible_short_descriptions+1 ] = param
end
end
-- Return nil immediately if there are no possible_short_descriptions.
if #possible_short_descriptions < 1 then
local no_short_description = 'declares no short description'
if preview_warning then
preview_warning = preview_warning .. ' and ' .. no_short_description
else
preview_warning = no_short_description
end
previewWarning( name, preview_warning )
return nil
end
if #possible_short_descriptions > 1 then
previewWarning( name, 'declares ' .. #possible_short_descriptions .. ' short descriptions' ) -- TODO: this when we know more
end
-- Pop the possible_short_description from possible_short_descriptions; only the last will be used.
local possible_short_description = possible_short_descriptions[ #possible_short_descriptions ]
-- The possible_short_description might be in the form 'short description' or '1=short description',
-- but it could also simply start with a '1'.
if possible_short_description:match( '=' ) then
possible_short_description = mw.ustring.gsub( possible_short_description, '^1%s*=%s*', '' )
end
-- Now we know what this template's short description is; check if it's in use and return nil if not.
if possible_short_description:match( '^[Nn]one$' ) then return nil end
mw.log( possible_short_description )
return possible_short_description
else -- TODO: processing for multiple templates requires checking the noreplace params
mw.log( number_of_short_description_templates .. ' short description templates' )
-- For all contents_of_all_short_description_templates:
for i, short_description_template_contents in ipairs( contents_of_all_short_description_templates ) do
-- process each using most of the code above in much the same way
end
end
end
local function getShortDescription( args )
local name = args.name
if isEmpty( name ) then return errorMessage( 'a page name (including namespace) MUST be provided' ) end
local result
local only = args.only
local prefer = args.prefer
if isEmpty( prefer ) then prefer = 'explicit' end
local lang = {
italic = args.lang_italic,
nocat = args.lang_nocat,
size = args.lang_size,
cat = args.lang_cat,
rtl = args.lang_rtl,
no = args.lang_no
}
if only == 'explicit' then result = getExplicitDescription( name )
elseif only == 'wikidata' then result = getWikidataDescription( name, lang )
elseif prefer == 'explicit' then result = getExplicitDescription( name ) or getWikidataDescription( name, lang )
elseif prefer == 'wikidata' then result = getWikidataDescription( name, lang ) or getExplicitDescription( name )
end
return result or args.fallback
end
function p.main( frame ) return getShortDescription( frame.args ) or '' end
return p