Módulo:Portal
40x40px | Module rating is invalid or not specified. |
Este módulo tem duas funções, portal
e imagem
. O portal
produz uma caixa com ligações para portal ou para múltiplos portais, e é utilizado pela predefinição {{portal}}
. É frequentemente utilizado na secção "Ver também" de um artigo. A função imagem
produz o nome da imagem utilizada pelo portal informado.
Índice
Função portal
A função portal produz uma caixa com as ligações dos portais.
Descrição
Esta predefinição serve para indicar, normalmente em páginas do domínio principal, que existem determinados portais relacionados com o tema tratado na página.
Uso
Sintaxe
Sintaxe mínima obrigatória:
{{Portal|Portal=nome do portal}}
O parâmetro nome do portal
é o nome completo da página do portal, incluindo o domínio. Ou seja para indicar o Portal:Literatura e Portal:Biografias deve-se usar o seguinte código:
{{Portal |Portal=[[Portal:Literatura|Portal de literatura]] |Portal2=[[Portal:Biografias|Portal de biografias]] }}
que resulta em:
Portal | A Wirchelp tem os portais: |
Código completo:
{{Portal |Portal = |Portal2 = (opcional) |Portal3 = (opcional) |Portal4 = (opcional) |Portal5 = (opcional) |imagem = (opcional) }}
Localização
A predefinição Portal deve ser colocada normalmente na secção "ver também", antes das páginas nessa secção para que a caixa seja mostrada ao lado das páginas. Exemplo de código:
==Ver também== {{Portal |Portal=[[Portal:Biografias|Portal de biografias]] |Portal2=[[Portal:Literatura|Portal de literatura]] }} *[[Literatura de Portugal]] *[[Literatura do Brasil]]
Ver também
Função imagem
A função imagem produz o nome da imagem utilizada pelo portal informado.
Uso
{{#invoke:Portal|imagem|portal}}
Exemplo
{{#invoke:Portal|image|Art}}
→ Erro em Lua em package.lua na linha 80: module 'Módulo:Portal/imagens/a' not found.
Função imagem duplicadas
A função de imagens duplicadas devolve uma lista de todas as imagens que estão a ser utilizadas por mais que um portal (os alias não estão incluídos). Isto pode ser útil para identificar entradas de imagens que devem ser alteradas para usar alias.
Uso
{{#invoke:Portal|imageDupes}}
Função mostrar tudo
A função mostrar tudo mostra um caixa com todos os portais que tem imagens. Isto é usado para manutenção, e não deve ser mostrado em artigos, porque a) existem cerca de 1500 portais com imagens, e mostrar 1500 imagens em uma página pode consumir muitos recursos dos servidores, e b) o módulo não tem maneira de saber a capitalização correcta do nome do portal, por isso as ligações para os portais vão estar quebradas. Esta função pode ser vista em Predefinição:Portal/doc/tudo.
Uso
{{#invoke:Portal|mostrartudo}}
--[==[ This module is a Lua implementation of the old {{Portal}} template. -- 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 [[Módulo:Portal/imagens]], listed below: -- [[Módulo:Portal/imagens/a]] - para nomes de portais que começam com "A". -- [[Módulo:Portal/imagens/b]] - para nomes de portais que começam com "B". -- [[Módulo:Portal/imagens/c]] - para nomes de portais que começam com "C". -- [[Módulo:Portal/imagens/d]] - para nomes de portais que começam com "D". -- [[Módulo:Portal/imagens/e]] - para nomes de portais que começam com "E". -- [[Módulo:Portal/imagens/f]] - para nomes de portais que começam com "F". -- [[Módulo:Portal/imagens/g]] - para nomes de portais que começam com "G". -- [[Módulo:Portal/imagens/h]] - para nomes de portais que começam com "H". -- [[Módulo:Portal/imagens/i]] - para nomes de portais que começam com "I". -- [[Módulo:Portal/imagens/j]] - para nomes de portais que começam com "J". -- [[Módulo:Portal/imagens/k]] - para nomes de portais que começam com "K". -- [[Módulo:Portal/imagens/l]] - para nomes de portais que começam com "L". -- [[Módulo:Portal/imagens/m]] - para nomes de portais que começam com "M". -- [[Módulo:Portal/imagens/n]] - para nomes de portais que começam com "N". -- [[Módulo:Portal/imagens/o]] - para nomes de portais que começam com "O". -- [[Módulo:Portal/imagens/p]] - para nomes de portais que começam com "P". -- [[Módulo:Portal/imagens/q]] - para nomes de portais que começam com "Q". -- [[Módulo:Portal/imagens/r]] - para nomes de portais que começam com "R". -- [[Módulo:Portal/imagens/s]] - para nomes de portais que começam com "S". -- [[Módulo:Portal/imagens/t]] - para nomes de portais que começam com "T". -- [[Módulo:Portal/imagens/u]] - para nomes de portais que começam com "U". -- [[Módulo:Portal/imagens/v]] - para nomes de portais que começam com "V". -- [[Módulo:Portal/imagens/w]] - para nomes de portais que começam co "W". -- [[Módulo:Portal/imagens/x]] - para nomes de portais que começam com "X". -- [[Módulo:Portal/imagens/y]] - para nomes de portais que começam com "Y". -- [[Módulo:Portal/imagens/z]] - para nomes de portais que começam com "Z". -- [[Módulo:Portal/imagens/outros]] - para nomes de portais que começam com quaiquer outras letras. Isso inclui números, -- letters with diacritics, and letters in non-Latin alphabets. -- [[Módulo:Portal/imagens/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 [[Módulo:Portal/imagens]], 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. ]==] local p = {} local isUserPage = false local function checkIfUserPage() local thisPage = mw.title.getCurrentTitle() if (thisPage.namespace == 2) or (thisPage.namespace == 3) then isUserPage = true return true end isUserPage = false return false end local function matchImagePage(s) -- Finds the appropriate image subpage given a lower-case -- portal name plus the first letter of that portal name. if type(s) ~= 'string' or #s < 1 then return end local firstLetter = mw.ustring.sub(s, 1, 1) local imagePage if mw.ustring.find(firstLetter, '^[a-z]') then imagePage = 'Módulo:Portal/imagens/' .. firstLetter else imagePage = 'Módulo:Portal/imagens/outros' end return mw.loadData(imagePage)[s] end local function getAlias(s) -- Gets an alias from the image alias data page. local aliasData = mw.loadData('Módulo:Portal/imagens/aliases') for portal, aliases in pairs(aliasData) do for _, alias in ipairs(aliases) do if alias == s then return portal end end end end local function getImageName(s) -- Gets the image name for a given string. local default = 'Portal-puzzle.svg|link=|alt=' if type(s) ~= 'string' or #s < 1 then return default end s = mw.ustring.lower(s) return matchImagePage(s) or matchImagePage(getAlias(s)) or default end local function checkPortalExists(portal) return not (mw.title.makeTitle(100, portal).id == 0) end function p._portal(portals, args) -- This function builds the portal box used by the {{portal}} template. checkIfUserPage() local root = mw.html.create('div') :attr('role', 'navigation') :attr('aria-label', 'Portals') :addClass('noprint portal plainlist') :addClass((args.left or args.esquerda) and 'tleft' or 'tright') :css('margin', args.margin or args.margem or (args.left == 'yes' or args.left == 'sim' or args.esquerda == 'yes' or args.esquerda == 'sim' and '0.5em 1em 0.5em 0') or '0.5em 0 0.5em 1em') :css('border', 'solid #aaa 1px') :newline() -- If no portals have been specified, display an error and add the page to a tracking category. if not portals[1] then if (args.nominimum == 'yes') or (args.nominimum == 'sim') or (args.nominimum == 'y') or (args.nominimum == 's') or (args.nominimum == 'true') or (args.nominimum == 'verdadeiro') or (args.nominimum == 'verdadeira') or (args['semmínimo'] == 'yes') or (args['semmínimo'] == 'sim') or (args['semmínimo'] == 'y') or (args['semmínimo'] == 's') or (args['semmínimo'] == 'true') or (args['semmínimo'] == 'verdadeiro') or (args['semmínimo'] == 'verdadeira') then -- if nominimum as been set to yes (or similar), omit the warning else root:wikitext('<strong class="error">Nenhuns portais especificados: por favor especifique ao menos um portal</strong>') end if not (isUserPage) then root:wikitext('[[Categoria:!Predefinições de portal sem um parâmetro]]') end return tostring(root) end -- scan for nonexistent portals, if they exist remove them from the portals table. If redlinks=yes, then don't remove local portallen = #portals -- traverse the list backwards to ensure that no portals are missed (table.remove also moves down the portals in the list, so that the next portal isn't checked if going fowards. -- going backwards allows us to circumvent this issue for i=portallen,1,-1 do -- the use of pcall here catches any errors that may occour when attempting to locate pages when the page name is invalid -- if pcall returns true, then rerun the function to find if the page exists if not pcall(checkPortalExists, portals[i]) or not checkPortalExists(portals[i]) then -- Getting here means a redlinked portal has been found if (args.redlinks == 'yes') or (args.redlinks == 'sim') or (args.redlinks == 'y') or (args.redlinks == 's') or (args.redlinks == 'true') or (args.redlinks == 'verdadeiro') or (args.redlinks == 'verdadeira') or (args.redlinks == 'include') or (args.redlinks == 'incluir') or (args.linksvermelhos == 'yes') or (args.linksvermelhos == 'sim') or (args.linksvermelhos == 'y') or (args.linksvermelhos == 's') or (args.linksvermelhos == 'true') or (args.linksvermelhos == 'verdadeiro') or (args.linksvermelhos == 'verdadeira') or (args.linksvermelhos == 'include') or (args.linksvermelhos == 'incluir') or (args['ligaçõesvermelhas'] == 'yes') or (args['ligaçõesvermelhas'] == 'sim') or (args['ligaçõesvermelhas'] == 'y') or (args['ligaçõesvermelhas'] == 's') or (args['ligaçõesvermelhas'] == 'true') or (args['ligaçõesvermelhas'] == 'verdadeiro') or (args['ligaçõesvermelhas'] == 'verdadeira') or (args['ligaçõesvermelhas'] == 'include') or (args['ligaçõesvermelhas'] == 'incluir') then -- if redlinks as been set to yes (or similar), add the cleanup category and then break the loop before the portal is removed from the list if not (isUserPage) then root:wikitext('[[Categoria:!Predefinições de portal com portais com links vermelhos]]') end break end -- remove the portal (this does not happen if redlinks=yes) table.remove(portals,i) end end -- if the length of the table is different, then rows were removed from the table, so portals were removed. If this is the case add the cleanup category if not (portallen == #portals) then if #portals == 0 then if (isUserPage) then return "" else return '[[Categoria:!Predefinições de portal com todos os portais com links vermelhos]]' end end if not (isUserPage) then root:wikitext('[[Categoria:!Predefinições de portal com portais com links vermelhos]]') end end -- Start the list. This corresponds to the start of the wikitext table in the old [[Template:Portal]]. local listroot = root:tag('ul') :css('display', 'table') :css('box-sizing', 'border-box') :css('padding', '0.1em') :css('max-width', '175px') :css('width', type(args.boxsize or args.tamanhocaixa) == 'string' and ((args.boxsize or args.tamanhocaixa) .. 'px') or nil) :css('background', '#f9f9f9') :css('font-size', '85%') :css('line-height', '110%') :css('font-style', 'italic') :css('font-weight', 'bold') -- Display the portals specified in the positional arguments. for _, portal in ipairs(portals) do local image = getImageName(portal) -- Generate the html for the image and the portal name. listroot :newline() :tag('li') :css('display', 'table-row') :tag('span') :css('display', 'table-cell') :css('padding', '0.2em') :css('vertical-align', 'middle') :css('text-align', 'center') :wikitext(string.format('[[Ficheiro:%s|32x28px|class=noviewer]]', image)) :done() :tag('span') :css('display', 'table-cell') :css('padding', '0.2em 0.2em 0.2em 0.3em') :css('vertical-align', 'middle') :wikitext(string.format('[[Portal:%s|%s%sportal]]', portal, portal, args['break'] and '<br />' or ' ')) end return tostring(root) end function p._image(portals) -- Wrapper function to allow getImageName() to be accessed through #invoke. local name = getImageName(portals[1]) return name:match('^(.-)|') or name -- FIXME: use a more elegant way to separate borders etc. from the image name end local function getAllImageTables() -- Returns an array containing all image subpages (minus aliases) as loaded by mw.loadData. local images = {} for i, subpage in ipairs{'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', 'outros'} do images[i] = mw.loadData('Módulo:Portal/imagens/' .. subpage) end return images end function p._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. local lang = mw.language.getContentLanguage() local count = 1 for _, imageTable in ipairs(getAllImageTables()) do for portal in pairs(imageTable) do portals[count] = lang:ucfirst(portal) count = count + 1 end end return p._portal(portals, args) end function p._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. local exists, dupes = {}, {} for _, imageTable in ipairs(getAllImageTables()) do for portal, image in pairs(imageTable) do if not exists[image] then exists[image] = portal else table.insert(dupes, string.format('A imagem "[[:Ficheiro:%s|%s]]" é utilizada tanto para o portal "%s" quanto para o portal "%s".', image, image, exists[image], portal)) end end end if #dupes < 1 then return 'Nenhumas imagens duplicadas foram encontradas.' else return 'As seguintes imagens duplicadas foram encontradas:\n* ' .. table.concat(dupes, '\n* ') end end local function processPortalArgs(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' and args or {} local portals = {} local namedArgs = {} for k, v in pairs(args) do if type(k) == 'number' and type(v) == 'string' then -- Make sure we have no non-string portal names. table.insert(portals, k) elseif type(k) ~= 'number' then namedArgs[k] = v end end table.sort(portals) for i, v in ipairs(portals) do portals[i] = args[v] end return portals, namedArgs end local function makeWrapper(funcName) -- Processes external arguments and sends them to the other functions. return function (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. local origArgs if type(frame.getParent) == 'function' then origArgs = frame:getParent().args for k, v in pairs(frame.args) do origArgs = frame.args break end else origArgs = frame end -- Trim whitespace and remove blank arguments. local args = {} for k, v in pairs(origArgs) do if type(v) == 'string' then v = mw.text.trim(v) end if v ~= '' then args[k] = v end end return p[funcName](processPortalArgs(args)) -- passes two tables to func: an array of portal names, and a table of named arguments. end end for _, funcName in ipairs{'portal', 'image', 'imageDupes', 'displayAll'} do p[funcName] = makeWrapper('_' .. funcName) end return p