Jump to content
Wikipedia The Free Encyclopedia

Module:Official website

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
Warning This Lua module is used on approximately 338,000 pages .
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.
This module uses the Wikidata property:
This module tracks the Wikidata property:
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.

This module implements {{Official website }}. Please see the template page for documentation.

Tracking categories

See also

The above documentation is transcluded from Module:Official website/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.

 localmakeUrl=require('Module:URL')._url

 localp={}

 -- Wrapper for pcall which returns nil on failure.
 localfunctionquickPcall(func)
 localsuccess,result=pcall(func)
 ifsuccessthen
 returnresult
 end
 end

 -- Gets the rank for a Wikidata property table. Returns 1, 0 or -1, in
 -- order of rank.
 localfunctiongetRank(prop)
 localrank=prop.rank
 ifrank=='preferred'then
 return1
 elseifrank=='normal'then
 return0
 elseifrank=='deprecated'then
 return-1
 else
 -- No rank or undefined rank is treated as "normal".
 return0
 end
 end

 -- Finds whether a Wikidata property is qualified as being in English.
 localfunctionisEnglish(prop)
 localret=quickPcall(function()
 fori,langinipairs(prop.qualifiers.P407)do
 iflang.datavalue.value['numeric-id']==1860then
 returntrue
 end
 end
 returnfalse
 end)
 returnret==true
 end

 -- Fetches the official website URL from Wikidata.
 localfetchWikidataUrl
 fetchWikidataUrl=function()
 -- Get objects for all official sites on Wikidata.
 localwebsites=quickPcall(function()
 returnmw.wikibase.getAllStatements(mw.wikibase.getEntityIdForCurrentPage(),'P856')
 end)

 -- Clone the objects in case other code needs them in their original order.
 websites=websitesandmw.clone(websites)or{}

 -- Add the table index to the objects in case it is needed in the sort.
 fori,websiteinipairs(websites)do
 website._index=i
 end

 -- Sort the websites, first by highest rank, and then by websites in the
 -- English language, then by the website's original position in the
 -- property list. When we are done, get the URL from the highest-sorted
 -- object.
 table.sort(websites,function(ws1,ws2)
 localr1=getRank(ws1)
 localr2=getRank(ws2)
 ifr1~=r2then
 returnr1>r2
 end
 locale1=isEnglish(ws1)
 locale2=isEnglish(ws2)
 ife1~=e2then
 returne1
 end
 returnws1._index<ws2._index
 end)
 localurl=quickPcall(function()
 returnwebsites[1].mainsnak.datavalue.value
 end)

 -- Cache the result so that we only do the heavy lifting once per #invoke.
 fetchWikidataUrl=function()
 returnurl
 end

 returnurl
 end

 -- Render the URL link, plus other visible output.
 localfunctionrenderUrl(options)
 ifnotoptions.urlandnotoptions.wikidataurlthen
 localqid=mw.wikibase.getEntityIdForCurrentPage()
 localresult='<strong class="error">'..
 'No URL found. Please specify a URL here or add one to Wikidata.'..
 '</strong>'
 ifqidthen
 result=result..' [[File:OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt=Edit this at Wikidata |link=https://www.wikidata.org/wiki/'..qid..'#P856|Edit this at Wikidata]]'
 end
 returnresult
 end
 localret={}
 ret[#ret+1]=string.format(
 '<span class="official-website">%s</span>',
 makeUrl(options.urloroptions.wikidataurl,options.display)
 )
 ifoptions.wikidataurlandnotoptions.urlthen
 localqid=mw.wikibase.getEntityIdForCurrentPage()
 ifqidthen
 ret[#ret+1]='[[File:OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt=Edit this at Wikidata |link=https://www.wikidata.org/wiki/'..qid..'#P856|Edit this at Wikidata]]'
 end
 end
 returntable.concat(ret,' ')
 end

 -- Render the tracking category.
 localfunctionrenderTrackingCategory(url,wikidataurl)
 ifmw.title.getCurrentTitle().namespace~=0then
 return''
 end
 localcategory
 ifnoturlandnotwikidataurlthen
 category='Official website missing URL'
 elseifnoturlandwikidataurlthen
 return''
 elseifurlandwikidataurlthen
 ifurl:gsub('/%s*$','')~=wikidataurl:gsub('/%s*$','')then
 category='Official website different in Wikidata and Wikipedia'
 end
 else
 category='Official website not in Wikidata'
 end
 returncategoryandstring.format('[[Category:%s]]',category)or''
 end

 functionp._main(args)
 localurl=args[1]orargs.URLorargs.url
 localwikidataurl=fetchWikidataUrl()
 localformattedUrl=renderUrl{
 url=url,
 wikidataurl=wikidataurl,
 display=args[2]orargs.nameor'Official website'
 }
 returnformattedUrl..renderTrackingCategory(url,wikidataurl)
 end

 functionp.main(frame)
 localargs=require('Module:Arguments').getArgs(frame,{
 wrappers='Template:Official website'
 })
 returnp._main(args)
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /