VOOZH about

URL: https://en.wikipedia.org/wiki/Module:GetShortDescription

⇱ Module:GetShortDescription - Wikipedia


Jump to content
From Wikipedia, the free encyclopedia
👁 Image
Module documentation
[view] [edit] [history] [purge]
👁 Image
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
👁 Page template-protected
This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
👁 Warning
This Lua module is used on approximately 17,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.
👁 Image
This module depends on the following other modules:
👁 Image
This module is required by Module:Annotated link.
👁 Image
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: table

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: table

wikidata

Markup: {{#invoke:GetShortDescription|main |name=Wikipedia |only=wikidata }}
Result: table

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: table

wikidata or explicit

Markup: {{#invoke:GetShortDescription|main |name=Wikipedia |prefer=wikidata }}
Result: table

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:

prefer or fallback

Markup: {{#invoke:GetShortDescription|main |name=Example |prefer=wikidata |fallback=This is a fallback }}
Result: table

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:

localgetShortDescription=require('Module:GetShortDescription').main

localshort_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=<CSSfont-sizee.g.'1.5em'>,
 lang_cat = <no, n, false, f, off, 0>,
 lang_rtl = <no (default), yes>
} )

-- If we'verequestedtoreport_redlinksortoobjectify_alarmthen
iftype(short_description)=='table'then
ifshort_description.alarmthen
-- An alarming message has been returned
localalarming_message=short_description.alarm
elseifshort_description.redlinkthen
-- Do something about that
elseifshort_description.wikidatathen
-- A Wikidata description was returned without being explicitly requested
localwikidata_description=short_description.wikidata
ifshort_description.nonethen
-- Because the explicit short desc was 'none'
end
end
end
localfunctionisEmpty(value)returnvalue==nilorvalue==''end
localfunctionnotEmpty(value)returnnotisEmpty(value)end
localfunctionisNone(value)returnvalue:lower()=='none'end
localfunctionalarmingMessage(message,preview)
message='<span style="color:#d33">[[Module:GetShortDescription]] '..message..'.</span>'
ifnotpreviewthen
message=message..'[[Category:Pages displaying alarming messages about Module:GetShortDescription]]'
end
returnmessage
end
-- Grammatically reasonable concatenation of possible issues into one message per problematic link target.
localfunctionpreviewWarning(args_name,quantity_of_things)
localmessage=''
ifquantity_of_things.params>3then
message=message..' with extraneous parameters'
end
ifquantity_of_things.descriptions>1then
message=message..', declaring '..quantity_of_things.descriptions..' short descriptions'
end
ifquantity_of_things.templates>1ornotEmpty(message)then
message='has detected that [[:'..args_name..'|'..args_name..']] has '..
quantity_of_things.templates..' {{tlx|short description}}'..message
mw.addWarning(alarmingMessage(message,true))
end
end
localfunctiongetWikidataDescription(title,args,fallback)
localwikidata_id=mw.wikibase.getEntityIdForTitle(title)
ifisEmpty(wikidata_id)then
returnnil
end
localwikidata_description,wikidata_description_lang=mw.wikibase.getDescriptionWithLang(wikidata_id)
ifisEmpty(wikidata_description)then
returnnil
end
localresult={wikidata=wikidata_description}
ifisEmpty(args.lang_no)andnotEmpty(wikidata_description_lang)andwikidata_description_lang~='en'then
-- According to the docs this is a possibility...
result.wikidata=require('Module:Lang')._lang{
wikidata_description_lang,
wikidata_description,
italic=args.lang_italic,
nocat=args.lang_nocat,
size=args.lang_size,
cat=args.lang_cat,
rtl=args.lang_rtl
}
end
result.fellback=fallback
returnresult
end
localfunctiongetShortDescriptionTemplates(title_table)
localpage_content=title_table:getContent()
-- Assume no content means a nonexistent title because it's cheaper than testing if it exists.
ifisEmpty(page_content)then
return{redlink=true}
end
localcontents_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.
fortemplateinpage_content:gmatch('{%b{}}')do
localshort_description_content=mw.ustring.match(template,'^{{%s*[Ss]hort description%s*|%s*(.-)%s*}}')
ifnotEmpty(short_description_content)then
-- Collect the contents of short description templates.
contents_of_all_short_description_templates[#contents_of_all_short_description_templates+1]=short_description_content
end
-- An opportunity for efficiency gain exists - to break if another type of template is found e.g. citation templates,
-- but on an appallingly formatted page, a short description template down by the categories would likely be missed.
end
returncontents_of_all_short_description_templates
end
localfunctiongetShortDescription(args_name,args_name_title_table,title,title_table,fallback)
localcontents_of_all_short_description_templates={}
localredirected
-- Check for short description templates on redirect pages.
iftitle~=args_namethen
contents_of_all_short_description_templates=getShortDescriptionTemplates(args_name_title_table)
ifcontents_of_all_short_description_templates.redlinkthen
returncontents_of_all_short_description_templates
end
redirected=false
end
if#contents_of_all_short_description_templates<1then
contents_of_all_short_description_templates=getShortDescriptionTemplates(title_table)
ifnotEmpty(redirected)then
redirected=true
end
end
ifcontents_of_all_short_description_templates.redlinkthen
returncontents_of_all_short_description_templates
end
if#contents_of_all_short_description_templates<1then
returnnil
end
localquantity_of_things={
templates=#contents_of_all_short_description_templates,
descriptions=0,
params=0
}
localpossible_short_descriptions={}
-- Look through the short description templates:
fortemplate_content_index,short_description_template_contentsinipairs(contents_of_all_short_description_templates)do
-- Split the contents at pipes and trim.
localshort_description_template_params=mw.text.split(short_description_template_contents,'%s*|%s*')
if#short_description_template_params>quantity_of_things.paramsthen
quantity_of_things.params=#short_description_template_params
end
possible_short_descriptions[template_content_index]={}
-- Look through the params:
fori,paraminipairs(short_description_template_params)do
ifparam=='noreplace'ormw.ustring.match(param,'^2%s*=%s*noreplace$')then
-- Take note of 'noreplace'-ing for establishment of hierarchy later.
possible_short_descriptions[template_content_index].noreplace=true
else
localhas_equals=param:match('=')
ifnothas_equalsorparam:match('^1')then
-- Grab the short description.
ifhas_equalsthen
param=mw.ustring.gsub(param,'^1%s*=%s*','')
end
-- If the template has both a numbered and an unnumbered short description;
-- whichever comes last (ltr) will be used by that template, so overwriting works out great.
possible_short_descriptions[template_content_index].description=param
-- And we want to know the total quantity of descriptions being declared.
quantity_of_things.descriptions=quantity_of_things.descriptions+1
end
end
end
end
localshort_descriptions={}
-- Look through the possible short descriptions for definite short descriptions,
-- and prepare for working out which of possibly multiple short descriptions is actually being applied for the page:
fori,possible_short_descriptioninipairs(possible_short_descriptions)do
ifpossible_short_description.descriptionthen
-- If a description is 'noreplace'-ing or 'none'; demote it.
if(possible_short_description.noreplaceorisNone(possible_short_description.description))and
#possible_short_descriptions>1then
-- But don't demote it if it's already at the bottom.
ifi>1then
table.insert(short_descriptions,#short_descriptions,possible_short_description)
else
short_descriptions[#short_descriptions+1]=possible_short_description
end
else
short_descriptions[#short_descriptions+1]=possible_short_description
end
end
end
-- Let previewWarning() work out if these numbers are bad.
previewWarning(args_name,quantity_of_things)
if#short_descriptions>=1then
-- Pop!
localshort_description=short_descriptions[#short_descriptions].description
ifnotEmpty(short_description)then
return{explicit=short_description,fellback=fallback,redirected=redirected}
end
end
returnnil
end
localfunctionisSisterProjectLink(title)
localsister_project_prefixes={
'wiktionary','wikt',
'wikinews','n',
'wikibooks','b',
'wikiquote','q',
'wikisource','s',
'wikispecies','species',
'wikiversity','v',
'wikivoyage','voy',
'commons','c',
'wikidata','d',
'mediawikiwiki','mw',
'wikimedia','foundation','wmf',
'meta','m',
'incubator',
'phabricator','phab'
}
localpre_colon=title:match('^(%a+):')
ifpre_colonthen
fori,sisterinipairs(sister_project_prefixes)do
ifpre_colon==sisterthen
returntrue
end
end
end
returnfalse
end
-- Literally testing if title_table.isRedirect can be expensive;
-- processing this way resolves (multiple) redirects without the possibly expensive check.
localfunctiongetTitleAndTable(orig_name)
localtitle_table=mw.title.new(orig_name)
title_table=title_table.redirectTargetortitle_table
localtitle=title_table.prefixedText
iftitle==orig_namethen
returntitle,title_table
end
returngetTitleAndTable(title)
end
localfunctiongetDescription(args)
localargs_name=args.name
ifisEmpty(args_name)then
return{alarm='requires a page name (including namespace)'}
end
-- Keep the orginal name, cleaned up, and its title_table for later.
localargs_name_title_table=mw.title.new(args_name)
args_name=args_name_title_table.prefixedText
ifisSisterProjectLink(args_name)then
returnnil
end
localtitle,title_table=getTitleAndTable(args_name)
iftitle~=args_namethen
ifisSisterProjectLink(title)then
returnnil
end
end
localonly=args.only
localprefer=args.preferor'explicit'
-- Pass args_name to getShortDescription() so previewWarning()s won't be confusing for redirects.
ifnotEmpty(only)then
ifonly=='explicit'then
returngetShortDescription(args_name,args_name_title_table,title,title_table)
end
ifonly=='wikidata'then
returngetWikidataDescription(title,args)
end
return{alarm='accepts either "explicit" or "wikidata" as the value of |only='}
end
ifnotEmpty(prefer)then
ifprefer=='explicit'then
localshort_description=getShortDescription(args_name,args_name_title_table,title,title_table)
ifnotEmpty(short_description)then
-- Assume a Wikidata search would be a bad idea for an assumed nonexistent title.
ifshort_description.redlinkor(notisNone(short_description.explicit)orargs.none_is_valid)then
returnshort_description
end
end
returngetWikidataDescription(title,args,true)
end
ifprefer=='wikidata'then
returngetWikidataDescription(title,args)orgetShortDescription(args_name,args_name_title_table,title,title_table,true)
end
return{alarm='accepts either "explicit" or "wikidata" as the value of |prefer='}
end
end
localfunctionmain(args)
localresult=getDescription(args)
ifnotEmpty(result)then
ifresult.alarmthen
result.alarm=alarmingMessage(result.alarm)
end
ifargs.stringifythen
ifresult.alarmthen
result=result.alarm
else
result=result.explicitorresult.wikidata
ifargs.none_is_nilandisNone(result)then
result=nil
end
end
elseifnotresult.alarmandargs.none_is_nilthen
localdescription=result.explicitorresult.wikidata
ifdescriptionandargs.none_is_nilandisNone(description)then
result=nil
end
end
end
returnresult
end
localp={}
functionp.main(frame)
localargs=require('Module:Arguments').getArgs(frame)
ifisEmpty(args)then
returnalarmingMessage('could not getArgs')-- This really would be alarming.
end
returnmain(args)
end
returnp