Модуль:Основная статья/песочница
Перейти к навигации
Перейти к поиску
Документация
local p = {}
local get_args = require('Module:Arguments').getArgs
local mHatnote = require('Module:Hatnote')
local mWikidata
local category_ns_number = 14
local function get_property(args)
return mWikidata.formatProperty(mw.getCurrentFrame(), args)
end
local function get_link_target(wikitext)
local target = wikitext:match('%[%[([^%]%|]+)%]') or wikitext:match('%[%[([^%|]+)%|')
return {
target = target or wikitext,
contains_link = target ~= nil
}
end
local function get_catmain_from_wikidata(properties)
if not mWikidata then
mWikidata = require('Module:Песочница/grain of sand')
end
local values, res = {}, {}
for _, property in pairs(properties) do
local property_values = get_property{property = property}
local i = 1
while property_values[i] do
values[#values + 1] = property_values[i]
i = i + 1
end
-- values[#values + 1] = get_property{property = property, plain = true}
end
if #values > 0 then
for _, value in pairs(values) do
local link_target = get_link_target(value)
local title = mw.title.new(link_target.target)
res[#res + 1] = {
title = value,
exists = title and title.exists,
missed = not link_target.contains_link
}
end
end
return res
end
function p.main(frame)
local args = get_args(frame)
local current_title = mw.title.getCurrentTitle()
local categories = mHatnote.define_categories{
by_wikidata = 'Википедия:Категории с основной статьей из Викиданных',
by_pagename = 'Википедия:Категории c искусственно сконструированной основной статьёй',
missed = 'Википедия:Категории с существующей основной статьей, не указанной в Викиданных'
}
local hatnote_args = setmetatable({
prefix = 'Основная статья:',
prefix_plural = 'Основные статьи:',
bold_links = true
}, { __index = args })
local overridden_categories = {}
local nocat = args.nocat
if current_title.namespace == category_ns_number then
hatnote_args.id = 'catmore'
overridden_categories.red_link = 'Википедия:Категории без основных статей'
local first_title_exists
if not hatnote_args[1] then
local catmain = get_catmain_from_wikidata{'p301', 'p1753'}
if #catmain > 0 then
local missed = false
for i, v in pairs(catmain) do
hatnote_args[i] = v.title
missed = missed or v.missed
end
first_title_exists = not #catmain == 1 or catmain[1].exists or missed
categories:add('by_wikidata', nocat)
if missed then
categories:add('missed')
end
else
hatnote_args[1] = current_title.text
categories:add('by_pagename', nocat)
end
end
if first_title_exists == nil then
link = mHatnote.parse_link{hatnote_args[1]}
first_title_exists = not link or mw.title.new(link).exists
end
if not first_title_exists then
hatnote_args.prefix = 'У этой категории нет основной статьи —'
end
end
return mHatnote.main(hatnote_args, overridden_categories) .. categories
end
return p