মডিউল:প্রবেশদ্বার: সংশোধিত সংস্করণের মধ্যে পার্থক্য

বিষয়বস্তু বিয়োগ হয়েছে বিষয়বস্তু যোগ হয়েছে
Bellayet (আলোচনা | অবদান)
Bellayet ব্যবহারকারী মডিউল:প্রবেশদ্বার পাতাটিকে মডিউল:Portal শিরোনামে কোনো পুনর্নির্দেশনা ছাড়াই স...
সম্পাদনা সারাংশ নেই
১ নং লাইন:
--[==[ This module is a Lua implementation of the old {{Portal}} template. As of August 2013 it is used on nearly 5,000,000 articles.
-- This module implements {{Portal}}
-- Please take care when updating it! It outputs two functions: p.portal, which generates a table 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:
local p = {}
-- [[Module:Portal/images/a]] - for portal names beginning with "A".
-- [[Module:Portal/images/b]] - for portal names beginning with "B".
local HtmlBuilder = require('Module:HtmlBuilder')
-- [[Module:Portal/images/c]] - for portal names beginning with "C".
local HtmlError = require('Module:Error')
-- [[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.
]==]
 
local htmlBuilder = require( 'Module:HtmlBuilder' )
-- This function generates the html code.
local function _portal(args)
 
-- Finds the appropriate image subpage given a lower-case
local root = HtmlBuilder.create('div')
-- portal name plus the first letter of that portal name.
local function matchImagePage( s )
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, '^[অ-য়]' ) then
imagePage = 'Module:প্রবেশদ্বার/চিত্র/' .. firstLetter
else
imagePage= 'Module:প্রবেশদ্বার/চিত্র/অন্যান্য'
end
local images = mw.loadData( imagePage )
local image = images[ s ]
if image then
return image
end
end
 
-- Gets the image name for a given string.
local function getImageName( s )
if type( s ) ~= 'string' or #s < 1 then
return 'Portal-puzzle.svg'
end
s = mw.ustring.lower( s )
local image = matchImagePage( s )
if image then
return image
else
local aliases = mw.loadData( 'Module:প্রবেশদ্বার/চিত্র/উপনাম' )
local alias = aliases[ s ]
image = matchImagePage( alias )
if image then
return image
else
return 'Portal-puzzle.svg'
end
end
end
 
-- This function builds the portal box used by the {{portal}} template.
local function _portal( portals, args )
local root = htmlBuilder.create( 'div' )
root
.addClass( 'noprint' )
.addClass(( args.left == 'yes' and 'tleft') or 'tright' )
.addClass( 'portal' )
.css( 'border', 'solid #aaa 1px' )
.css( 'margin', args.margin or ( args.left == 'yes' and '0.5em 1em 0.5em 0' ) or '0.5em 0 0.5em 1em')
.newline()
 
-- Start the table. This corresponds to the start of the wikitext table in the old [[Template:Portal]].
local tableroot = root.tag( 'table' )
-- in the old [[Template:প্রবেশদ্বার]].
local tableroot = root .tagcss( 'tablebackground', '#f9f9f9' )
.css( 'backgroundfont-size', '#f9f9f985%' )
.css( 'fontline-sizeheight', '85110%' )
.css( 'linemax-heightwidth', '110%175px' )
.css( 'max-width', type( args.boxsize ) == '175pxstring' and ( args.boxsize .. 'px' ) )
.css('width', args.boxsize and (args.boxsize .. 'px'))
-- If no portals have been specified, display an error and add the page to a tracking category.
if not argsportals[ 1 ] then
tableroot.wikitext( '<strong class="error">কোন প্রবেশদ্বার নির্দিষ্ট করা হয়নি: অনুগ্রহ করে অন্তত একটি প্রবেশদ্বার উল্লেখ করুন</strong>[[Category:প্যারামিটার ছাড়া প্রবেশদ্বার টেমপ্লেট]]' )
tableroot.wikitext(
tostring(HtmlError.error{'কোন প্রবেশদ্বার নির্দিষ্ট করা হয়নি: অনুগ্রহ করে অন্তত একটি প্রবেশদ্বার উল্লেখ করুন'})
.. '[[Category:প্যারামিটার ছাড়া প্রবেশদ্বার টেমপ্লেট]]'
)
end
 
-- Display the portals specified in the positional arguments.
for i,v portal in ipairs(args portals ) do
local image = getImageName( portal )
v = mw.ustring.match(v, '^%s*(.*%S)') or '' -- Trim whitespace.
-- Portal image names are stored in subtemplates of [[Template:প্রবেশদ্বার/চিত্র]].
-- The name of the subtemplate is the portal name in all lower case, but with
-- the first character in upper case.
-- Work out the image subtemplate location.
local lang = mw.getContentLanguage()
local imagetemplatename = 'প্রবেশদ্বার/চিত্র/' .. lang:ucfirst(lang:lc(v))
 
-- Check the image template name. We need three checks: 1) check with pcall to see if
-- we are over the expensive function call limit; 2) check if the proposed image template
-- name uses invalid characters (mw.title.new returns nil if this is the case); and 3)
-- check if the image subtemplate exists.
local goodtitlecall, imagetemplateobject = pcall(mw.title.new, imagetemplatename, 'Template')
if not (goodtitlecall and imagetemplateobject and imagetemplateobject.exists) then
imagetemplatename = 'প্রবেশদ্বার/চিত্র/ডিফল্ট'
end
 
-- Expand the image subtemplate to get the image name
local imagename = mw.getCurrentFrame():expandTemplate{ title = imagetemplatename }
-- Generate the html for the image and the portal name.
tableroot
.newline()
.tag( 'tr' )
.attr( 'valign', 'middle' )
.tag( 'td' )
.css( 'text-align', 'center' )
.wikitext( mw.ustring.format( '[[File:' .. imagename .. '%s|32x28px|alt=প্রবেশদ্বার আইকন]]', image ) )
.done()
.tag( 'td' )
.css( 'padding', '0 0.2em' )
.css( 'vertical-align', 'middle' )
.css( 'font-style', 'italic' )
.css( 'font-weight', 'bold' )
.wikitext(mw.ustring.format( '[[প্রবেশদ্বারPortal:' .. v .. '%s|%s%sপ্রবেশদ্বার]]', ..portal, vportal, .. ((args[ 'break' ] == 'yes' and '<br />') or ' ' ) .. 'প্রবেশদ্বার]]')
end
return tostring( root )
end
 
-- Wrapper function to allow getImageName() to be accessed through #invoke.
return tostring(root)
local function _image( portals )
return getImageName( portals[ 1 ] )
end
 
-- Returns an array containing all image subpages (minus aliases) as loaded by mw.loadData.
-- This function gets the arguments passed to the module and passes them
local function getAllImageTables()
-- to the _portal() function above.
local images = {}
function p.portal(frame)
local subpages = { 'অ-ঔ', 'ক-ধ', 'ন-য়', 'অন্যান্য' }
local orig_args
for i, subpage in ipairs( subpages ) do
if frame == mw.getCurrentFrame() then
table.insert( images, mw.loadData( 'Module:প্রবেশদ্বার/চিত্র/' .. subpage ) )
-- We're being called via #invoke. If the invoking template passed any arguments,
end
-- use them. Otherwise, use the arguments that were passed into the template.
return images
orig_args = frame:getParent().args
end
for k, v in pairs(frame.args) do
 
orig_args = frame.args
-- This function displays all portals that have portal images. This function is for maintenance purposes and should not be used in
break
-- 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 function _displayAll( portals, args )
local lang = mw.language.getContentLanguage()
local images = getAllImageTables()
local count = 1
for i, imageTable in ipairs( images ) do
for portal in pairs( imageTable ) do
portals[ count ] = lang:ucfirst( portal )
count = count + 1
end
else
-- We're being called from another module or from the debug console, so assume
-- the arguments are passed in directly.
orig_args = frame
end
return _portal( portals, args )
end
-- We want to list all the portals in the order they were passed to the template.
 
-- We also want to be able to deal with positional arguments passed explicitly,
-- This function searches the image subpages to find duplicate images. If duplicate images exist, it is not necessarily a bad thing,
-- for example {{portal|2=Politics}}. However, pairs() doesn't guarantee the correct
-- as different portals might just happen to choose the same image. However, this function is helpful in identifying images that
-- order, and ipairs() will stop after the first nil value. To get around this, we
-- should be moved to a portal alias for ease of maintenance.
-- create a new table of arguments where nil values have been removed, so that we
local function _findImageDupes()
-- can traverse the numerical arguments using ipairs(). We also remove values which
local exists, dupes = {}, {}
-- only consist of whitespace. ParserFunctions considers these to be false, and by
local images = getAllImageTables()
-- removing them Lua will consider them false too.
for i, imageTable in ipairs( images ) do
for portal, image in pairs( imageTable ) do
local args = {} -- Arguments table.
if not exists[ image ] then
local name_args = {} -- Temporary table for named arguments.
exists[ image ] = portal
for k, v in pairs(orig_args) do
if mw.ustring.match(v, '%S') then -- Remove values that are only whitespace.
if type(k) == 'number' then
table.insert(args, k) -- Put positional argument keys into the arguments table so we can sort them.
else
table.insert( dupes, mw.ustring.format( 'The image "[[:File:%s|%s]]" is used for both portals "%s" and "%s".', image, image, exists[ image ], portal ) )
-- Put named argument values in their own table while we sort the positional arguments,
-- so that we don't have to cycle through all the original arguments again.
name_args[k] = v
end
end
end
if #dupes < 1 then
table.sort(args) -- Sort the positional argument keys into numerical order.
return 'No duplicate images found.'
for i,v in ipairs(args) do
else
args[i] = orig_args[v] -- Replace positional argument keys with their corresponding values.
return 'The following duplicate images were found:\n* ' .. table.concat( dupes, '\n* ' )
end
end
for k,v in pairs(name_args) do
args[k] = v -- Add named arguments to the args table
-- 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.
local function processPortalArgs( args )
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
 
-- Processes external arguments and sends them to the other functions.
return _portal(args)
local function makeWrapper( func )
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 frame == mw.getCurrentFrame() 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 func( processPortalArgs( args ) ) -- passes two tables to func: an array of portal names, and a table of named arguments.
end
end
 
return p{
portal = makeWrapper( _portal ),
image = makeWrapper( _image ),
imageDupes = makeWrapper( _findImageDupes ),
displayAll = makeWrapper( _displayAll )
}