Modul:Hlavní článek

Z Enviwiki
Verze z 3. 9. 2017, 07:57, kterou vytvořil Admin (diskuse | příspěvky) (naimportována 1 revize)
(rozdíl) ← Starší verze | zobrazit aktuální verzi (rozdíl) | Novější verze → (rozdíl)
Skočit na navigaci Skočit na vyhledávání

Dokumentaci tohoto modulu lze vytvořit na stránce Modul:Hlavní článek/Dokumentace

require 'Modul:No globals'

local p = {}

local lib = require 'Modul:Wikidata/lib'
local firstToUpper = lib.common.firstToUpper

local function in_array(value, array)
	for _, val in ipairs(array) do
		if val == value then
			return true
		end
	end
	return false
end

local function getSitelinks(entity, property)
	local sitelinks = {}
	if entity and entity.claims and entity.claims[property] then
		local Statements = entity:getBestStatements(property) or {}
		for _, statement in ipairs(Statements) do
			if lib.IsSnakValue(statement.mainsnak) then
				local id = lib.getEntityIdFromValue(statement.mainsnak.datavalue.value)
				local sitelink = mw.wikibase.sitelink(id)
				if sitelink then
					table.insert(sitelinks, sitelink)
				end
			end
		end
	end
	return sitelinks
end

local function makeLink(value)
	return '[[' .. value .. ']]'
end

local function concatLinks(Strings)
	local Links = {}
	for key, value in pairs(Strings) do
		if type(key) == 'number' and value ~= '' then
			table.insert(Links, makeLink(value))
		end
	end
	return mw.text.listToText(Links)
end

local function intr_text(count)
	if count > 1 then
		return 'Hlavní články: '
	else
		return 'Hlavní článek: '
	end
end

local function pageExists(page)
	local title = mw.title.new(page)
	return title.exists and not title.isRedirect
end

function p.mainArticle(frame)
	local params = frame and frame.args
	local parent = frame:getParent()

	if lib.common.isTableEmpty(params) then
		params = parent and parent.args or {}
	end

	local title = mw.title.getCurrentTitle()
	local pagename = title.text
	local namespace = title.namespace

	local Local_links = {}
	for i, link in pairs(params) do
		local link = mw.text.trim(link)
		if tonumber(i) and link ~= '' then
			table.insert(Local_links, link)
		end
	end

	if namespace ~= 14 then
		if lib.common.isTableEmpty(Local_links) then
			local Error = require 'Modul:Error'
			local errorData = {
				template = 'Hlavní článek',
				text = 'Pro články vně jmenného prostoru „Kategorie“ je vyžadován alespoň jeden odkaz!'
			}
			return frame:preprocess(Error.getText(errorData))
		else
			local category = ''
			if parent:getTitle() ~= 'Šablona:Hlavní článek' then
				category = '[[Kategorie:Údržba:Šablona Hlavní článek mimo kategorie|' .. pagename .. ']]'
			end
			return ": ''" .. intr_text(#Local_links) .. concatLinks(Local_links) .. category .. "''"
		end
	end

	local category = ''
	local entity = mw.wikibase.getEntityObject()
	local Sitelinks = getSitelinks(entity, 'P301')
	if #Local_links > 0 then
		local i = 0
		for _, local_link in ipairs(Local_links) do
			local_link = firstToUpper(local_link)
			if in_array(local_link, Sitelinks) then
				i = i + 1
			end
		end
		if i == 0 then
			local SitelinksP971 = getSitelinks(entity, 'P971')
			for _, local_link in ipairs(Local_links) do
				if in_array(firstToUpper(local_link), SitelinksP971) then
					category = ''
					break
				end
				if pageExists(local_link) then
					category = lib.category('not', 'Hlavní článek')
				end
			end
		elseif i == #Local_links then
			-- [[Diskuse ke kategorii:Údržba:Hlavní článek odpovídá Wikidatům#K čemu?]]
			-- category = lib.category('same', 'Hlavní článek')
		else
			category = lib.category('diff', 'Hlavní článek')
		end
		return "'''" .. intr_text(#Local_links) .. concatLinks(Local_links) .. "'''" .. category
	elseif #Sitelinks > 0 then
		return "'''" .. lib.addWdClass(intr_text(#Sitelinks) .. concatLinks(Sitelinks)) .. "'''" .. category
	else
		return "'''" .. intr_text(1) .. makeLink(pagename) .. "'''" .. category
	end
end

return p