模块:FeaturedPage:修订间差异
来自「荏苒之境」
// via Wikitext Extension for VSCode |
无编辑摘要 |
||
第1行: | 第1行: | ||
local translation = require("Module:Template translation") | |||
local featured_page = {} | local featured_page = {} | ||
local API_EXTRACT_TEMPALTE = "http://localhost/api.php?action=query&prop=extracts&exintro&redirects=1&format=json&titles=" | |||
local API_IMAGES_TEMPLATE = "http://localhost/api.php?action=query&prop=pageimages&pithumbsize=512&redirects=1&format=json&titles=" | |||
local function get_random_title(namespace, root_page_only) | local function get_random_title(namespace, root_page_only) | ||
第8行: | 第13行: | ||
title_text = root_page_only | title_text = root_page_only | ||
and string.match(title_text, "([^/]+)") or title_text | and string.match(title_text, "([^/]+)") or title_text | ||
return title_text | |||
end | end | ||
end | end | ||
end | end | ||
local function get_page_summary(title) | |||
local d, e = mw.ext.externaldata.getExternalData { | |||
url = API_EXTRACT_TEMPALTE..title, | |||
format = "json" | |||
} | |||
if e then | |||
error("发起请求失败") | |||
end | |||
return d[1]["__json"]["query"]["pages"][1]["extract"] | |||
end | |||
local function get_page_image(title) | |||
local d, e = mw.ext.externaldata.getExternalData { | |||
url = API_IMAGES_TEMPLATE..title, | |||
format = "json" | |||
} | |||
if e then | |||
error("发起请求失败") | |||
end | |||
return d[1]["__json"]["query"]["pages"][1]["pageimage"] | |||
end | |||
featured_page.get_random_title = get_random_title | featured_page.get_random_title = get_random_title | ||
featured_page.get_page_summary = get_page_summary | |||
featured_page.get_page_image = get_page_image | |||
featured_page.show = function(frame) | featured_page.show = function(frame) | ||
local langauge = translation.getLanguage() | |||
while true do | |||
local title = get_random_title(0) | |||
local title_translated = title.."/"..langauge | |||
title = mw.title.new(title_translated).exists | |||
and title_translated or title | |||
local extract = get_page_summary(title) | |||
if extract ~= "" then | |||
local pageimage = get_page_image(title) or "占位图片.jpg" | |||
return "[[File:"..pageimage.."|200px|right]]"..extract | |||
end | |||
end | |||
end | end | ||
return featured_page | return featured_page |
2025年8月20日 (三) 18:27的版本
此模块的文档可以在模块:FeaturedPage/doc创建
local translation = require("Module:Template translation")
local featured_page = {}
local API_EXTRACT_TEMPALTE = "http://localhost/api.php?action=query&prop=extracts&exintro&redirects=1&format=json&titles="
local API_IMAGES_TEMPLATE = "http://localhost/api.php?action=query&prop=pageimages&pithumbsize=512&redirects=1&format=json&titles="
local function get_random_title(namespace, root_page_only)
root_page_only = root_page_only or true
while true do
local title_text = mw.site.randomPages(1, namespace)[1]
if not string.match(title_text, ":") then
title_text = root_page_only
and string.match(title_text, "([^/]+)") or title_text
return title_text
end
end
end
local function get_page_summary(title)
local d, e = mw.ext.externaldata.getExternalData {
url = API_EXTRACT_TEMPALTE..title,
format = "json"
}
if e then
error("发起请求失败")
end
return d[1]["__json"]["query"]["pages"][1]["extract"]
end
local function get_page_image(title)
local d, e = mw.ext.externaldata.getExternalData {
url = API_IMAGES_TEMPLATE..title,
format = "json"
}
if e then
error("发起请求失败")
end
return d[1]["__json"]["query"]["pages"][1]["pageimage"]
end
featured_page.get_random_title = get_random_title
featured_page.get_page_summary = get_page_summary
featured_page.get_page_image = get_page_image
featured_page.show = function(frame)
local langauge = translation.getLanguage()
while true do
local title = get_random_title(0)
local title_translated = title.."/"..langauge
title = mw.title.new(title_translated).exists
and title_translated or title
local extract = get_page_summary(title)
if extract ~= "" then
local pageimage = get_page_image(title) or "占位图片.jpg"
return "[[File:"..pageimage.."|200px|right]]"..extract
end
end
end
return featured_page