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: 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').mainlocalshort_description=getShortDescription({-- requiredname='page name',-- optionalprefer='explicit'or'wikidata',only='explicit'or'wikidata',fallback='fallback',objectify_alarm=true,report_redlinks=true,lang_no='yes',-- {{lang}} optionslang_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've requested to report_redlinks or to objectify_alarm theniftype(short_description)=='table'thenifshort_description.alarmthen-- An alarming message has been returnedlocalalarming_message=short_description.alarmelseifshort_description.redlinkthen-- Do something about thatelseifshort_description.wikidatathen-- A Wikidata description was returned without being explicitly requestedlocalwikidata_description=short_description.wikidataifshort_description.nonethen-- Because the explicit short desc was 'none'endendend
localfunctionisEmpty(value)returnvalue==nilorvalue==''endlocalfunctionnotEmpty(value)returnnotisEmpty(value)endlocalfunctionisNone(value)returnvalue:lower()=='none'endlocalfunctionalarmingMessage(message,preview)message='<span style="color:#d33">[[Module:GetShortDescription]] '..message..'.</span>'ifnotpreviewthenmessage=message..'[[Category:Pages displaying alarming messages about Module:GetShortDescription]]'endreturnmessageend-- Grammatically reasonable concatenation of possible issues into one message per problematic link target.localfunctionpreviewWarning(args_name,quantity_of_things)localmessage=''ifquantity_of_things.params>3thenmessage=message..' with extraneous parameters'endifquantity_of_things.descriptions>1thenmessage=message..', declaring '..quantity_of_things.descriptions..' short descriptions'endifquantity_of_things.templates>1ornotEmpty(message)thenmessage='has detected that [[:'..args_name..'|'..args_name..']] has '..quantity_of_things.templates..' {{tlx|short description}}'..messagemw.addWarning(alarmingMessage(message,true))endendlocalfunctiongetWikidataDescription(title,args,fallback)localwikidata_id=mw.wikibase.getEntityIdForTitle(title)ifisEmpty(wikidata_id)thenreturnnilendlocalwikidata_description,wikidata_description_lang=mw.wikibase.getDescriptionWithLang(wikidata_id)ifisEmpty(wikidata_description)thenreturnnilendlocalresult={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}endresult.fellback=fallbackreturnresultendlocalfunctiongetShortDescriptionTemplates(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)thenreturn{redlink=true}endlocalcontents_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{}}')dolocalshort_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_contentend-- 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.endreturncontents_of_all_short_description_templatesendlocalfunctiongetShortDescription(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_namethencontents_of_all_short_description_templates=getShortDescriptionTemplates(args_name_title_table)ifcontents_of_all_short_description_templates.redlinkthenreturncontents_of_all_short_description_templatesendredirected=falseendif#contents_of_all_short_description_templates<1thencontents_of_all_short_description_templates=getShortDescriptionTemplates(title_table)ifnotEmpty(redirected)thenredirected=trueendendifcontents_of_all_short_description_templates.redlinkthenreturncontents_of_all_short_description_templatesendif#contents_of_all_short_description_templates<1thenreturnnilendlocalquantity_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.paramsthenquantity_of_things.params=#short_description_template_paramsendpossible_short_descriptions[template_content_index]={}-- Look through the params:fori,paraminipairs(short_description_template_params)doifparam=='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=trueelselocalhas_equals=param:match('=')ifnothas_equalsorparam:match('^1')then-- Grab the short description.ifhas_equalsthenparam=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+1endendendendlocalshort_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)doifpossible_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>1thentable.insert(short_descriptions,#short_descriptions,possible_short_description)elseshort_descriptions[#short_descriptions+1]=possible_short_descriptionendelseshort_descriptions[#short_descriptions+1]=possible_short_descriptionendendend-- 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].descriptionifnotEmpty(short_description)thenreturn{explicit=short_description,fellback=fallback,redirected=redirected}endendreturnnilendlocalfunctionisSisterProjectLink(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_colonthenfori,sisterinipairs(sister_project_prefixes)doifpre_colon==sisterthenreturntrueendendendreturnfalseend-- 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_tablelocaltitle=title_table.prefixedTextiftitle==orig_namethenreturntitle,title_tableendreturngetTitleAndTable(title)endlocalfunctiongetDescription(args)localargs_name=args.nameifisEmpty(args_name)thenreturn{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.prefixedTextifisSisterProjectLink(args_name)thenreturnnilendlocaltitle,title_table=getTitleAndTable(args_name)iftitle~=args_namethenifisSisterProjectLink(title)thenreturnnilendendlocalonly=args.onlylocalprefer=args.preferor'explicit'-- Pass args_name to getShortDescription() so previewWarning()s won't be confusing for redirects.ifnotEmpty(only)thenifonly=='explicit'thenreturngetShortDescription(args_name,args_name_title_table,title,title_table)endifonly=='wikidata'thenreturngetWikidataDescription(title,args)endreturn{alarm='accepts either "explicit" or "wikidata" as the value of |only='}endifnotEmpty(prefer)thenifprefer=='explicit'thenlocalshort_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)thenreturnshort_descriptionendendreturngetWikidataDescription(title,args,true)endifprefer=='wikidata'thenreturngetWikidataDescription(title,args)orgetShortDescription(args_name,args_name_title_table,title,title_table,true)endreturn{alarm='accepts either "explicit" or "wikidata" as the value of |prefer='}endendlocalfunctionmain(args)localresult=getDescription(args)ifnotEmpty(result)thenifresult.alarmthenresult.alarm=alarmingMessage(result.alarm)endifargs.stringifythenifresult.alarmthenresult=result.alarmelseresult=result.explicitorresult.wikidataifargs.none_is_nilandisNone(result)thenresult=nilendendelseifnotresult.alarmandargs.none_is_nilthenlocaldescription=result.explicitorresult.wikidataifdescriptionandargs.none_is_nilandisNone(description)thenresult=nilendendendreturnresultendlocalp={}functionp.main(frame)localargs=require('Module:Arguments').getArgs(frame)ifisEmpty(args)thenreturnalarmingMessage('could not getArgs')-- This really would be alarming.endreturnmain(args)endreturnp