模組:Transclude random subpage
此模块使用Lua语言: |
此模板從一組子頁面的其中一個提取內容。主要使用於主題頁。
Layout
{{Transclude random subpage |max = |subpage = |rootpage = }}
Usage
- rootpage: (Optional) The root of the transcluded page, e.g. Portal:Foo.
- subpage: The subpage of the transcluded page, e.g. Selected picture.
- max: The maximum number of sub-subpages. Must be a positive integer.
Formatting subpages
- Create a sub-page first. If your portal is
Portal:Foo
, then a sub-page could bePortal:Foo/Selected bar
. - On the
Portal:Foo/Selected bar
sub-page, place the template{{numbered subpages}}
on the page and save it. - Once the list of subject links have been created, use one of the
Selected <subject>
templates from below to format each of the subject sub-pages.
Why this template?
This template was created to enable {{Random portal component}}
to be replaced with standard portal box notation, including {{Box-header colour}}
and {{Box-footer}}
.
Example:
{{Random portal component|max=22|header=Selected article}}
could be replaced by this:
{{Box-header colour|Selected article}} {{Transclude random subpage |max=22|subpage=Selected article}} {{Box-footer}}
This also allows a box to be modified (edited) independently of its contents, and to match the other boxes on the page.
-- This module implements [[Template:Transclude random subpage]]. It is alpha software.
local p = {}
local mRandom = require('Module:Random')
local currentTitle = mw.title.getCurrentTitle()
-- tracking function added by BHG 29/04/2019
-- called as subPageTrackingCategories(pages, args.max)
local function subPageTrackingCategories(pages, max)
local retval = "";
local availableSubPageCount = 0;
local i;
local thispagetitle = mw.title.getCurrentTitle().text
-- don't track DYK etc, only selected/featured articles, biogs etc
if ((string.find(pages.subpage, "/[sS]elected") == -1) and (string.find(pages.subpage, "/[fF]eatured") == -1)) then
return retval
end
-- no tracking unless we are in Portal namespace
if (mw.title.getCurrentTitle().nsText ~= "Portal") then
return ""
end
-- no tracking if this is a subpage
if ((mw.ustring.match(thispagetitle, "/") ~= nil) and (thispagetitle ~= "AC/DC")) then
return retval
end
if (max == nil) then
return "[[Category:Random portal component with no value for max]]"
end
-- limit checking to prevent Lua overload
local myMaxCheck = 60
if tonumber(max) < myMaxCheck then
myMaxCheck = tonumber(max)
end
for i=1,myMaxCheck do
local aSubPage = mw.title.new(pages.subpage .. '/' .. i)
if (aSubPage.exists) then
availableSubPageCount = availableSubPageCount + 1;
end
end
if myMaxCheck >= tonumber(max) then
if (availableSubPageCount < tonumber(max)) then
retval = retval .. "[[Category:Random portal component with fewer available subpages than specified max]]"
elseif (availableSubPageCount > tonumber(max)) then
retval = retval .. "[[Category:Random portal component with more available subpages than specified max]]"
end
end
-- before categorising, check what type of subpage we are categorising, and if detected, categorise images separately
local subpageType = "子页面" -- generic type
local subpageName = pages.subpage
subpageName = mw.ustring.gsub(subpageName, "^[^/]*/", "")
subpageName = mw.ustring.lower(subpageName)
if ((mw.ustring.find(subpageName, "picture", 1, true) ~= nil) or
(mw.ustring.find(subpageName, "image", 1, true) ~= nil) or
(mw.ustring.find(subpageName, "panorama", 1, true) ~= nil) or
(mw.ustring.find(subpageName, "图", 1, true) ~= nil) or
(mw.ustring.find(subpageName, "圖", 1, true) ~= nil)) then
subpageType = "图片子页面"
end
if (availableSubPageCount < 2) then
retval = retval .. "[[Category:随机组件的" .. subpageType .. "少于2个的主题]]"
elseif (availableSubPageCount <= 5) then
retval = retval .. "[[Category:随机组件有2-5个" .. subpageType .. "的主题]]"
elseif (availableSubPageCount <= 10) then
retval = retval .. "[[Category:随机组件有6-10个" .. subpageType .. "的主题]]"
elseif (availableSubPageCount <= 15) then
retval = retval .. "[[Category:随机组件有11-15个" .. subpageType .. "的主题]]"
elseif (availableSubPageCount <= 20) then
retval = retval .. "[[Category:随机组件有16-20个" .. subpageType .. "的主题]]"
elseif (availableSubPageCount <= 25) then
retval = retval .. "[[Category:随机组件有21-25个" .. subpageType .. "的主题]]"
elseif (availableSubPageCount <= 30) then
retval = retval .. "[[Category:随机组件有26-30个" .. subpageType .. "的主题]]"
elseif (availableSubPageCount <= 40) then
retval = retval .. "[[Category:随机组件有31-40个" .. subpageType .. "的主题]]"
elseif (availableSubPageCount <= 50) then
retval = retval .. "[[Category:随机组件有41-50个" .. subpageType .. "的主题]]"
else
retval = retval .. "[[Category:随机组件的" .. subpageType .. "超过50个的主题]]"
end
return retval;
end
local function getRandomNumber(max)
-- gets a random integer between 1 and max; max defaults to 1
return mRandom.number{max or 1}
end
local function expandArg(args, key)
-- Emulate how unspecified template parameters appear in wikitext. If the
-- specified argument exists, its value is returned, and if not the argument
-- name is returned inside triple curly braces.
local val = args[key]
if val then
return val
else
return string.format('{{{%s}}}', key)
end
end
local function getPages(args)
local pages = {}
pages.root = args.rootpage or currentTitle.prefixedText
pages.subpage = pages.root .. '/' .. expandArg(args, 'subpage')
return pages
end
local function tryExpandTemplate(frame, title, args)
local success, result = pcall(frame.expandTemplate, frame, {title = title, args = args})
if success then
return result
else
local msg = string.format(
'<strong class="error">页面"[[%s]]"不存在</strong>',
title
)
if mw.title.getCurrentTitle().namespace == 100 then -- is in the portal namespace
msg = msg .. '[[Category:需要关注的主题]]'
end
return msg
end
end
local function getNumberedSubpageContent(frame, pages, num)
return tryExpandTemplate(
frame,
pages.subpage .. '/' .. num
)
end
function p._main(args, frame)
frame = frame or mw.getCurrentFrame()
local pages = getPages(args)
local prefix = args.prefix or ''
local max = tonumber(args.max or 1)
local several = tonumber(args.several or 1)
if several > max then
several = max
end
local ret = {}
local random_numbers = {}
for i = 1, several do
local num = getRandomNumber(max)
while random_numbers[num] do
num = getRandomNumber(max)
end
random_numbers[num] = true
ret[#ret + 1] = prefix .. getNumberedSubpageContent(frame, pages, num)
end
if args.more then
ret[#ret + 1] = string.format('<div class="noprint" style="float: right;"><b>[[%s|%s]]</b></div>', pages.subpage, args.more)
end
if args.leftfooter then
ret[#ret + 1] = string.format('<div class="noprint" style="float: left;">%s</div>', args.leftfooter)
end
if args.rightfooter then
ret[#ret + 1] = string.format('<div class="noprint" style="float: right;">%s</div>', args.rightfooter)
end
return table.concat(ret, '\n') .. subPageTrackingCategories(pages, max, args.header)
end
local function makeInvokeFunction(func)
return function (frame)
local args = require('Module:Arguments').getArgs(frame, {
trim = false,
removeBlanks = false,
wrappers = {
'Template:Transclude random subpage',
'Template:Transclude random subpage/BHG-test',
}
})
return func(args, frame)
end
end
p.main = makeInvokeFunction(p._main)
return p