Modul:Hlavní článek

Tato stránka je zamčena
Z Wikipedie, otevřené encyklopedie
require 'strict'

local p = {}

local lib = require 'Modul:Wikidata/lib'
local Functions = require 'Modul:Functions'

local function in_array(value, array)
	local TableTools = require 'Modul:TableTools'
	return TableTools.inArray(array, value)
end

local function getSitelinks(entityid, property)
	local sitelinks = {}
	if entityid then
		local Wikidata = require 'Modul:Wikidata'
		local ids = Wikidata.getRawValues{ id = entityid, property = property, rank = 'best' }
		for _, id in ipairs(ids) do
			local sitelink = mw.wikibase.sitelink(id)
			if sitelink then
				table.insert(sitelinks, sitelink)
			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, redirectExists)
	local title = mw.title.new(page)
	if title.exists then
		if title.isRedirect then
			return redirectExists
		else
			return true
		end
	else
		return false
	end
end

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

	if Functions.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 Functions.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 namespace == 0 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 id = mw.wikibase.getEntityIdForCurrentPage()
	local Sitelinks = getSitelinks(id, 'P301')
	if #Local_links > 0 then
		local i = 0
		local second_category = ''
		for _, local_link in ipairs(Local_links) do
			local_link = Functions.firstToUpper(local_link)
			if in_array(local_link, Sitelinks) then
				i = i + 1
			end
			if second_category == '' and not pageExists(local_link, true) then
				second_category = '[[Kategorie:Údržba:Hlavní článek neexistuje|' .. pagename .. ']]'
			end
		end
		if i == 0 then
			local SitelinksP971 = getSitelinks(id, 'P971')
			for _, local_link in ipairs(Local_links) do
				if in_array(Functions.firstToUpper(local_link), SitelinksP971) then
					category = ''
					break
				end
				if pageExists(local_link, false) 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 .. second_category
	elseif #Sitelinks > 0 then
		return "'''" .. lib.addWdClass(intr_text(#Sitelinks) .. concatLinks(Sitelinks)) .. "'''" .. category
	else
		Sitelinks = getSitelinks(id, 'P971')
		if #Sitelinks > 0 then
			local Wikidata = require 'Modul:Wikidata'
			local Formatters = require 'Modul:Wikidata/Formatters'

			local items = Wikidata.getRawValues{ id = id, property = 'P971', rank = 'best' }
			if #items > 1 then
				local meta, not_meta = {}, {}
				for _, item in ipairs(items) do
					if #Wikidata.getStatements{ id = item, property = 'P31', withtarget = 'Q24571886' } > 0 then
						table.insert(meta, item)
					elseif #Wikidata.getStatements{ id = item, property = 'P31', withtarget = 'Q19361238' } > 0 then
						table.insert(meta, item)
					else
						table.insert(not_meta, item)
					end
				end
				items = {}
				for _, tbl in ipairs{ not_meta, meta } do
					for _, item in ipairs(tbl) do
						table.insert(items, item)
					end
				end
			end

			local list = {}
			for _, item in ipairs(items) do
				local value = Formatters.formatRawValue(item, 'wikibase-entityid', {})
				table.insert(list, "''" .. value .. "''")
			end

			local text = mw.text.listToText(list)
			if #list > 1 then
				return lib.addWdClass("'''Tato kategorie obsahuje průnik témat " .. text .. ".'''")
			elseif #list == 1 then
				category = '[[Kategorie:Monitoring:Pouze jedno téma kategorie|' .. pagename .. ']]'
				return lib.addWdClass("'''Tato kategorie obsahuje články týkající se tématu " .. text .. ".'''" .. category)
			end
		end
		if not pageExists(pagename, true) then
			category = '[[Kategorie:Údržba:Hlavní článek neexistuje|' .. pagename .. ']]'
		end
		return "'''" .. intr_text(1) .. makeLink(pagename) .. "'''" .. category
	end
end

return p