Lompat ke isi

Modul:Portal

Ti Wikipédia Sunda, énsiklopédi bébas

This module has two functions,portalandimage.Theportalproduces a box with links to aportalor to multiple portals, and is used by the {{portal}} template. It is most often used in the "See also" section of an article. Theimagefunction produces the name of the image used by the specified portal.

Portal function

[édit sumber]

The portal function produces a box of portal links.

Dokuméntasi citakan ieuditransklusitinaModul:Portal/doc[edit]

Cara make

[édit sumber]

This template is used to link an article to its relatedportal.

Optional parameters
{{Portal|name|image|size|boxsize|break=yes}}

Lokasi

[édit sumber]

This template is meant to be placed at the bottom of the article in the "See also" section.

Image function

[édit sumber]

The image function produces the name of the image used by the specified portal.

{{#invoke:Portal|image|portal}}

Example

[édit sumber]
  • {{#invoke:Portal|image|Art}}→ Ballerina-icon.jpg

Image dupes function

[édit sumber]

The image dupes function returns a list of all images that are being used by more than one portal (aliases are not included). This can be helpful in identifying image entries that should be changed to use aliases.

{{#invoke:Portal|imageDupes}}

Display all function

[édit sumber]

The display all function returns a box containing all portals that have images. This is used for maintenance, and should not be displayed in articles, because a) there are around 1500 portals with images, and displaying 1500 images on one page takes up a lot of server resources, and b) the module has no way to know the correct capitalisation of a portal name, so some links to portals will be broken. This function can be seen atTemplate:Portal/doc/all.

{{#invoke:Portal|displayAll}}

--[==[ This module is a Lua implementation of the old {{Portal}} template. As of February 2019 it is used on nearly 7,900,000 articles.
-- Please take care when updating it! It outputs two functions: p.portal, which generates a list of portals, and p.image, which
-- produces the image name for an individual portal.


-- The portal image data is kept in submodules of [[Module:Portal/images]], listed below:
-- [[Module:Portal/images/a]] - for portal names beginning with "A".
-- [[Module:Portal/images/b]] - for portal names beginning with "B".
-- [[Module:Portal/images/c]] - for portal names beginning with "C".
-- [[Module:Portal/images/d]] - for portal names beginning with "D".
-- [[Module:Portal/images/e]] - for portal names beginning with "E".
-- [[Module:Portal/images/f]] - for portal names beginning with "F".
-- [[Module:Portal/images/g]] - for portal names beginning with "G".
-- [[Module:Portal/images/h]] - for portal names beginning with "H".
-- [[Module:Portal/images/i]] - for portal names beginning with "I".
-- [[Module:Portal/images/j]] - for portal names beginning with "J".
-- [[Module:Portal/images/k]] - for portal names beginning with "K".
-- [[Module:Portal/images/l]] - for portal names beginning with "L".
-- [[Module:Portal/images/m]] - for portal names beginning with "M".
-- [[Module:Portal/images/n]] - for portal names beginning with "N".
-- [[Module:Portal/images/o]] - for portal names beginning with "O".
-- [[Module:Portal/images/p]] - for portal names beginning with "P".
-- [[Module:Portal/images/q]] - for portal names beginning with "Q".
-- [[Module:Portal/images/r]] - for portal names beginning with "R".
-- [[Module:Portal/images/s]] - for portal names beginning with "S".
-- [[Module:Portal/images/t]] - for portal names beginning with "T".
-- [[Module:Portal/images/u]] - for portal names beginning with "U".
-- [[Module:Portal/images/v]] - for portal names beginning with "V".
-- [[Module:Portal/images/w]] - for portal names beginning with "W".
-- [[Module:Portal/images/x]] - for portal names beginning with "X".
-- [[Module:Portal/images/y]] - for portal names beginning with "Y".
-- [[Module:Portal/images/z]] - for portal names beginning with "Z".
-- [[Module:Portal/images/other]] - for portal names beginning with any other letters. This includes numbers,
-- letters with diacritics, and letters in non-Latin alphabets.
-- [[Module:Portal/images/aliases]] - for adding aliases for existing portal names. Use this page for variations
-- in spelling and diacritics, etc., no matter what letter the portal begins with.
--
-- The images data pages are separated by the first letter to reduce server load when images are added, changed, or removed.
-- Previously all the images were on one data page at [[Module:Portal/images]], but this had the disadvantage that all
-- 5,000,000 pages using this module needed to be refreshed every time an image was added or removed.
]==]

localp={}

-- determine whether we're being called from a sandbox
localisSandbox=mw.getCurrentFrame():getTitle():find('sandbox',1,true)
localsandbox=isSandboxand'/sandbox'or''

localfunctionsandboxVersion(s)
returnisSandboxands..'-sand'ors
end

localtemplatestyles='Module:Portal'..sandbox..'/styles.css'

localgetArgs=require('Module:Arguments').getArgs
localyesno=require('Module:Yesno')

-- List of non-talk namespaces which should not be tracked (Talk pages are never tracked)
localbadNamespaces={'user','template','draft','wikipedia'}

-- Check whether to do tracking in this namespace
-- Returns true unless the page is one of the banned namespaces
localfunctioncheckTracking(title)
localthisPage=titleormw.title.getCurrentTitle()
ifthisPage.isTalkPagethen
returnfalse
end
localns=thisPage.nsText:lower()
for_,vinipairs(badNamespaces)do
ifns==vthen
returnfalse
end
end
returntrue
end

localfunctionmatchImagePage(s)
-- Finds the appropriate image subpage given a lower-case
-- portal name plus the first letter of that portal name.
iftype(s)~='string'or#s<1thenreturnend
localfirstLetter=mw.ustring.sub(s,1,1)
localimagePage
ifmw.ustring.find(firstLetter,'^[a-z]')then
imagePage='Module:Portal/images/'..firstLetter..sandbox
else
imagePage='Module:Portal/images/other'..sandbox
end
returnmw.loadData(imagePage)[s]
end

localfunctiongetAlias(s)
-- Gets an alias from the image alias data page.
localaliasData=mw.loadData('Module:Portal/images/aliases'..sandbox)
forportal,aliasesinpairs(aliasData)do
for_,aliasinipairs(aliases)do
ifalias==sthen
returnportal
end
end
end
end

localfunctiongetImageName(s)
-- Gets the image name for a given string.
localdefault='Portal-puzzle.svg|link=|alt='
iftype(s)~='string'or#s<1then
returndefault
end
s=mw.ustring.lower(s)
returnmatchImagePage(s)ormatchImagePage(getAlias(s))ordefault
end

-- Function to check argument portals for errors, generate tracking categories if needed
-- Function first checks for too few/many portals provided
-- Then checks the portal list to purge any portals that don't exist
-- Arguments:
-- portals: raw list of portals
-- args.tracking: is tracking requested? (will not track on bad titles or namespaces)
-- args.redlinks: should redlinks be displayed?
-- args.minPortals: minimum number of portal arguments
-- args.maxPortals: maximum number of portal arguments
-- Returns:
-- portals = list of portals, with redlinks purged (if args.redlinks=false)
-- trackingCat = possible tracking category
-- errorMsg = error message
functionp._checkPortals(portals,args)
localtrackingCat=''
localerrMsg=nil

-- Tracking is on by default.
-- It is disabled if any of the following is true
-- 1/ the parameter "tracking" is set to 'no, 'n', or 'false'
-- 2/ the current page fails the namespace or pagename tests
localtrackingEnabled=args.trackingandcheckTracking()

args.minPortals=args.minPortalsor1
args.maxPortals=args.maxPortalsor-1
-- check for too few portals
if#portals<args.minPortalsthen
errMsg='please specify at least '..args.minPortals..' portal'..(args.minPortals>1and's'or'')
trackingCat=(trackingEnabledand'[[Category:Portal templates with too few portals]]'or'')
returnportals,trackingCat,errMsg
end
-- check for too many portals
ifargs.maxPortals>=0and#portals>args.maxPortalsthen
errMsg='too many portals (maximum = '..args.maxPortals..')'
trackingCat=(trackingEnabledand'[[Category:Portal templates with too many portals]]'or'')
returnportals,trackingCat,errMsg
end
ifnotargs.redlinksortrackingEnabledthen
-- make new list of portals that exist
localexistingPortals={}
for_,portalinipairs(portals)do
localportalTitle=mw.title.new(portal,"Portal")
-- if portal exists, put it into list
ifportalTitleandportalTitle.existsthen
table.insert(existingPortals,portal)
-- otherwise set tracking cat
elseiftrackingEnabledthen
trackingCat="[[Category:Templat Portal dengan pranala merah]]"
end
end
-- If redlinks is off, use portal list purged of redlinks
portals=args.redlinksandportalsorexistingPortals
-- if nothing left after purge, set tracking cat
if#portals==0andtrackingEnabledthen
trackingCat=trackingCat.."[[Category:Pages with empty portal template]]"
end
end
returnportals,trackingCat,errMsg
end

functionp._portal(portals,args)
-- This function builds the portal box used by the {{portal}} template.

-- Normalize all arguments
ifargs.redlinks=='include'thenargs.redlinks=trueend
args.addBreak=args['break']
forkey,defaultinpairs({left=false,tracking=true,nominimum=false,
redlinks=false,addBreak=false,border=true})do
ifargs[key]==nilthenargs[key]=defaultend
args[key]=yesno(args[key],default)
end

localroot=mw.html.create('div')
:attr('role','navigation')
:attr('aria-label','Portals')
:addClass('noprint plainlist')
:addClass(sandboxVersion('portalbox'))
:addClass(args.borderandsandboxVersion('portalborder')or'')
:addClass(args.leftand'tleft'or'tright')
:css('margin',args.marginornil)
:newline()

localtrackingCat=''
localerrMsg=nil
args.minPortals=args.nominimumand0or1
args.maxPortals=-1
portals,trackingCat,errMsg=p._checkPortals(portals,args)
root:wikitext(trackingCat)
-- if error message, put it in the box and return
iferrMsgthen
ifargs.borderthen-- suppress error message when border=no
localerrTag=root:tag('strong')
errTag:addClass('error')
errTag:css('padding','0.2em')
errTag:wikitext('Error: '..errMsg)
end
returntostring(root)
end
-- if no portals (and no error), just return tracking category
if#portals==0then
returntrackingCat
end

-- Start the list. This corresponds to the start of the wikitext table in the old [[Template:Portal]].
locallistroot=root:tag('ul')
:css('width',(tonumber(args.boxsize)or0)>0and(args.boxsize..'px')orargs.boxsize)

-- Display the portals specified in the positional arguments.
for_,portalinipairs(portals)do
localimage=getImageName(portal)

-- Generate the html for the image and the portal name.
listroot
:newline()
:tag('li')
:tag('span')
:wikitext(string.format('[[File:%s|32x28px|class=noviewer]]',image))
:done()
:tag('span')
:wikitext(string.format('[[Portal:%s|Portal %s%s]]',portal,portal,args.addBreakand'<br />'or' '))
end
returntostring(root)
end

functionp._image(portal,nostrip)
-- Wrapper function to allow getImageName() to be accessed through #invoke.
-- backward compatibility: if table passed, take first element
iftype(portal)=='table'then
portal=portal[1]
end
localname=getImageName(portal)
-- If nostrip is yes (or equivalent), then allow all metadata (like image borders) to be returned
localstrip=notyesno(nostrip)
returnstripandname:match('^(.-)|')orname-- FIXME: use a more elegant way to separate borders etc. from the image name
end

localfunctiongetAllImageTables()
-- Returns an array containing all image subpages (minus aliases) as loaded by mw.loadData.
localimages={}
fori,subpageinipairs{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','other'}do
images[i]=mw.loadData('Module:Portal/images/'..subpage..sandbox)
end
returnimages
end

functionp._displayAll(portals,args)
-- This function displays all portals that have portal images. This function is for maintenance purposes and should not be used in
-- articles, for two reasons: 1) there are over 1500 portals with portal images, and 2) the module doesn't record how the portal
-- names are capitalized, so the portal links may be broken.
locallang=mw.language.getContentLanguage()
localcount=1
for_,imageTableinipairs(getAllImageTables())do
forportalinpairs(imageTable)do
portals[count]=lang:ucfirst(portal)
count=count+1
end
end
returnp._portal(portals,args)
end

functionp._imageDupes()
-- This function searches the image subpages to find duplicate images. If duplicate images exist, it is not necessarily a bad thing,
-- as different portals might just happen to choose the same image. However, this function is helpful in identifying images that
-- should be moved to a portal alias for ease of maintenance.
localexists,dupes={},{}
for_,imageTableinipairs(getAllImageTables())do
forportal,imageinpairs(imageTable)do
ifnotexists[image]then
exists[image]=portal
else
table.insert(dupes,string.format('The image "[[:File:%s|%s]]" is used for both portals "%s" and "%s".',image,image,exists[image],portal))
end
end
end
if#dupes<1then
return'No duplicate images found.'
else
return'The following duplicate images were found:\n* '..table.concat(dupes,'\n* ')
end
end

localfunctionprocessPortalArgs(args)
-- This function processes a table of arguments and returns two tables: an array of portal names for processing by ipairs, and a table of
-- the named arguments that specify style options, etc. We need to use ipairs because we want to list all the portals in the order
-- they were passed to the template, but we also want to be able to deal with positional arguments passed explicitly, for example
-- {{portal|2=Politics}}. The behaviour of ipairs is undefined if nil values are present, so we need to make sure they are all removed.
args=type(args)=='table'andargsor{}
localportals={}
localnamedArgs={}
fork,vinpairs(args)do
iftype(k)=='number'andtype(v)=='string'then-- Make sure we have no non-string portal names.
table.insert(portals,k)
elseiftype(k)~='number'then
namedArgs[k]=v
end
end
table.sort(portals)
fori,vinipairs(portals)do
portals[i]=args[v]
end
returnportals,namedArgs
end

-- Entry point for sorting portals from other named arguments
functionp._processPortalArgs(args)
returnprocessPortalArgs(args)
end

functionp.image(frame)
localorigArgs=getArgs(frame)
localportals,args=processPortalArgs(origArgs)
returnp._image(portals[1],args.border)
end

localfunctionmakeWrapper(funcName)
-- Processes external arguments and sends them to the other functions.
returnfunction(frame)
-- If called via #invoke, use the args passed into the invoking
-- template, or the args passed to #invoke if any exist. Otherwise
-- assume args are being passed directly in from the debug console
-- or from another Lua module.
-- Also: trim whitespace and remove blank arguments
localorigArgs=getArgs(frame)
-- create two tables to pass to func: an array of portal names, and a table of named arguments.
localportals,args=processPortalArgs(origArgs)
localresults=''
iffuncName=='_portal'orfuncName=='_displayAll'then
results=frame:extensionTag{name='templatestyles',args={src=templatestyles}}
end
returnresults..p[funcName](portals,args)
end
end

for_,funcNameinipairs{'portal','imageDupes','displayAll'}do
p[funcName]=makeWrapper('_'..funcName)
end

returnp