モジュール:PRC admin

local z = {}
local function makeArticleLink( label, sitelink )
	if label ~= nil then
		if sitelink ~= nil then
			return  .. label .. 
		else
			return label
		end
	else
		if sitelink ~= nil then
			return ':' .. sitelink .. 
		else
			return  -- What's the best thing we can do here?
		end
	end
end
local function makeEntityLink( entity )
	local label, lang = entity:getLabelWithLang('ja')
	if lang ~= 'ja' then
		label = nil
		for _, statement in pairs( entity:getBestStatements( 'P1448' ) ) do
			if statement.mainsnak.datavalue.value.language == 'zh-cn' then
				label = statement.mainsnak.datavalue.value.text
				break
			end
		end
	end
	if not(label) then label = entity:getLabel('zh-cn') end
	
	local sitelink = entity:getSitelink('jawiki')
	local zh_sitelink = entity:getSitelink('zhwiki')
	if not(sitelink) and zh_sitelink then
		sitelink = 'zh:' .. zh_sitelink
	end
	
	return makeArticleLink( label, sitelink )
end
local premadeItemLinks = {
	Q986065 = '街道',
	Q735428 = '鎮',
	Q1500350 = '郷',
	Q2365159 = 'ソム',
	Q50231 = 'その他'
}
local function makeItemLink( item )
	if premadeItemLinksitem ~= nil then
		return premadeItemLinksitem
	end
	local label = mw.wikibase.label( item )
	local sitelink = mw.wikibase.sitelink( item )
	return makeArticleLink( label, sitelink )
end
local function makeFullNames( items )
	if type( items ) ~= 'table' then
		items = { items }
	end
	local fullNames = {}
	for _, item in pairs( items ) do
		local entity = mw.wikibase.getEntity( item )
		local entityLink = makeEntityLink( entity )
		local upperStatements = entity:getBestStatements( 'P131' )
		local upperItems = {}, upperFullNames
		for _, upperStatement in pairs( upperStatements ) do
			table.insert( upperItems, 'Q' .. upperStatement.mainsnak.datavalue.value'numeric-id' )
		end
		if #upperItems == 0 then
			-- Root item
			table.insert( fullNames, entityLink )
		else
			_, upperFullNames = makeFullNames( upperItems )
			for _, upperFullName in pairs( upperFullNames ) do
				table.insert( fullNames, upperFullName .. entityLink )
			end
		end
	end
	return mw.text.listToText( fullNames ), fullNames
end
local makeFullName = makeFullNames
z._makeFullNames = makeFullNames -- For debugging
-- items must be a table of entities
local function findUpperEntities( entities, levels )
	if levels  0 then
			if below ~=  then
				below = below .. 
			end
			below = below .. '参考：' .. mw.text.listToText( seeAlsoUpperLinks )
		end
	end
	if below ~=  then
		navboxArgs.belowstyle = 'text-align: left; font-size: 80%;';
		navboxArgs.below = below;
	end
	
	local border = args.border or frame:getParent().args.border or 
	if border == 'child' then
		navboxArgs.border = border
		navboxArgs.name = nil
		navboxArgs.title = nil
		navboxArgs.state = nil
		navboxArgs.above = nil
		navboxArgs.below = nil
	end
	
	return require('Module:Navbox')._navbox(navboxArgs)
end
-- Specify a division with item=Qxxx or defaults to current article
function z.children( frame, args )
	if args == nil then
		args = frame.args
	end
	local item = 
	if args.item ~= nil and args.item ~=  then
		item = args.item
	end
	local entity = mw.wikibase.getEntity( item )
	if entity == nil then
		return 
	end
	
	-- Children lists
	local list = {}
	local lowerStatements = entity:getBestStatements( 'P150' )
	for i, lowerStatement in pairs( lowerStatements ) do
		lowerStatement._entity = mw.wikibase.getEntity( 'Q' .. lowerStatement.mainsnak.datavalue.value'numeric-id' )
		lowerStatement._code = 'X'
		for _, codeStatement in pairs( lowerStatement._entity:getBestStatements( 'P442' ) ) do
			if codeStatement.mainsnak.datavalue.value < lowerStatement._code then
				lowerStatement._code = codeStatement.mainsnak.datavalue.value
			end
		end
	end
	table.sort( lowerStatements, function( a, b )
		return a._code < b._code;
	end )
	for _, lowerStatement in pairs( lowerStatements ) do
		local lowerEntity = lowerStatement._entity
		table.insert( list, makeEntityLink( lowerEntity ) )
	end
	
	return mw.text.listToText( list )
end
return z