模块:FeaturedPage:修订间差异

来自「荏苒之境」
// via Wikitext Extension for VSCode
无编辑摘要
标签手工回退
(未显示同一用户的24个中间版本)
第1行: 第1行:
local translation = require("Module:Template translation")
local utils_str = require("Module:Utils/String")
local featured_page = {}
local featured_page = {}


featured_page.show = function(frame)
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, title_match)
     while true do
     while true do
         local title_text = mw.site.randomPages(1, 0)[1]
         local title_text = mw.site.randomPages(1, namespace, title_match)[1]
         if not string.match(title_text, ":") then
         if title_text == nil then
            local title = mw.title.new(
        return
                string.match(title_text, "([^/]+)"))
            return title.content
         end
         end
        title_text = title_match == nil
            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
local page = d[1]["__json"]["query"]["pages"][1]
return page["extract"], page["title"]
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 language = translation.getFrameLanguage(frame)
while true do
local title_match =
(language ~= nil and language ~= "zh-cn")
and "%/"..language or nil
local title
for i = 1, 10 do
title = get_random_title(0, title_match)
or get_random_title(0)
if string.find(title, "首页") ~= 1 then
break
end
end
if title == nil then
return frame.args.no_article_text
end
local extract, real_title = get_page_summary(title)
if extract ~= nil and extract ~= "" then
extract = utils_str.replace(extract, real_title, "[["..real_title.."]]", 1)
local pageimage = get_page_image(title)
if pageimage then
extract = "[[File:"..pageimage.."|200px|right]]"..extract
end
return extract.."[["..real_title.."|"..frame.args.link_text.."]]"
end
end
end
end


return featured_page
return featured_page

2025年8月20日 (三) 20:14的版本

此模块的文档可以在模块:FeaturedPage/doc创建

local translation = require("Module:Template translation")
local utils_str = require("Module:Utils/String")

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, title_match)
    while true do
        local title_text = mw.site.randomPages(1, namespace, title_match)[1]
        if title_text == nil then
        	return
        end
        title_text = title_match == nil
            and string.match(title_text, "([^/]+)") or title_text
        return title_text
    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
	local page = d[1]["__json"]["query"]["pages"][1]
	return page["extract"], page["title"]
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 language = translation.getFrameLanguage(frame)
	while true do
		local title_match =
			(language ~= nil and language ~= "zh-cn")
			and "%/"..language or nil
		local title
		
		for i = 1, 10 do
			title = get_random_title(0, title_match)
				or get_random_title(0)
			if string.find(title, "首页") ~= 1 then
				break
			end
		end
		
		if title == nil then
			return frame.args.no_article_text
		end
			
		local extract, real_title = get_page_summary(title)
		if extract ~= nil and extract ~= "" then
			extract = utils_str.replace(extract, real_title, "[["..real_title.."]]", 1)
			local pageimage = get_page_image(title)
			if pageimage then
				extract = "[[File:"..pageimage.."|200px|right]]"..extract
			end
			return extract.."[["..real_title.."|"..frame.args.link_text.."]]"
		end
	end
end

return featured_page