Jump to content

Module:GetShortDescription

Permanently protected module
From Wikipedia, the free encyclopedia

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
close