<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FWas_a_bee%2Ftree</id>
	<title>Module:Sandbox/Was a bee/tree - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FWas_a_bee%2Ftree"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Was_a_bee/tree&amp;action=history"/>
	<updated>2026-04-21T20:41:29Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://stockhub.co/index.php?title=Module:Sandbox/Was_a_bee/tree&amp;diff=146387&amp;oldid=prev</id>
		<title>imported&gt;Was a bee at 19:36, 23 October 2017</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Was_a_bee/tree&amp;diff=146387&amp;oldid=prev"/>
		<updated>2017-10-23T19:36:41Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local p = {}&lt;br /&gt;
&lt;br /&gt;
datas = {}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- These setdata() and getdata() functions are used to access (read and write) to &amp;quot;datas&amp;quot; table&lt;br /&gt;
function p.setdata(n, key, val)&lt;br /&gt;
	if datas[n] == nil then&lt;br /&gt;
		datas[n] = {}&lt;br /&gt;
	end&lt;br /&gt;
	datas[n][key] = val&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.getdata(n, key, defval)&lt;br /&gt;
	if  datas[n] == nil or datas[n][key] == nil then&lt;br /&gt;
		return defval&lt;br /&gt;
	else&lt;br /&gt;
		return datas[n][key] &lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.children(query, itemId, property, direction, maxdepth, sources, refStyle, pencil, lang1, lang2 )&lt;br /&gt;
 --------------------------------------------------------------------------------&lt;br /&gt;
 -- children function which listing all childrens that certain item has.&lt;br /&gt;
 -- Not listing grand childrens, or grand-grand childrens. Listing only childrens.&lt;br /&gt;
 -- Final returning value &amp;quot;childrens&amp;quot; is like {Q123123, Q3984198237, Q1874138746} &lt;br /&gt;
 --------------------------------------------------------------------------------&lt;br /&gt;
	local childrens = {}&lt;br /&gt;
	local entity = mw.wikibase.getEntity( itemId )&lt;br /&gt;
	local claims = nil&lt;br /&gt;
	local x = 1&lt;br /&gt;
	local tmpValue = {}&lt;br /&gt;
	&lt;br /&gt;
	---------------------------------------------------------------&lt;br /&gt;
	-- Start collecting data from the retrieved entity, and save that into the datas[] table.&lt;br /&gt;
	-- Because getEntity() function is EXPENSIVE, so here, collecting all data at once.&lt;br /&gt;
	-- https://en.wikipedia.org/wiki/WP:EXPENSIVE&lt;br /&gt;
	-- https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua#mw.wikibase.getEntity&lt;br /&gt;
	----------------------------------------------------------------&lt;br /&gt;
	local globalSiteId_1 = lang1 .. &amp;#039;wiki&amp;#039;&lt;br /&gt;
	local globalSiteId_2&lt;br /&gt;
	&lt;br /&gt;
	if lang2 and lang2 ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		globalSiteId_2 = lang2 .. &amp;#039;wiki&amp;#039;&lt;br /&gt;
	else&lt;br /&gt;
		globalSiteId_2 = nil&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	p.setdata(itemId, &amp;quot;sitelink_1&amp;quot;, entity:getSitelink( globalSiteId_1 ))&lt;br /&gt;
	p.setdata(itemId, &amp;quot;label_1&amp;quot;, entity:getLabel( lang1 ))&lt;br /&gt;
	if lang2 and lang2 ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		p.setdata(itemId, &amp;quot;sitelink_2&amp;quot;, entity:getSitelink( globalSiteId_2 ))&lt;br /&gt;
		p.setdata(itemId, &amp;quot;label_2&amp;quot;, entity:getLabel( lang2 ))&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	-- ★★★★ Start source specific code ★★★★ --&lt;br /&gt;
	------------TA98------------&lt;br /&gt;
	claims = entity[&amp;#039;claims&amp;#039;][&amp;#039;P1323&amp;#039;] --TA98 ID&lt;br /&gt;
	if claims then&lt;br /&gt;
		for  _,claim in pairs( claims ) do&lt;br /&gt;
			tmpValue[x] = claim.mainsnak.datavalue.value&lt;br /&gt;
			x = x + 1&lt;br /&gt;
		end&lt;br /&gt;
		p.setdata(itemId, &amp;quot;TA98&amp;quot;, tmpValue)&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	tmpValue = {} --initialize&lt;br /&gt;
	x = 1 --initialize&lt;br /&gt;
	claims = nil --initialize&lt;br /&gt;
	&lt;br /&gt;
	------------MeSH------------&lt;br /&gt;
	--claims = entity[&amp;#039;claims&amp;#039;][&amp;#039;P672&amp;#039;] --MeSH Code&lt;br /&gt;
	--if claims then&lt;br /&gt;
	--	for  _,claim in pairs( claims ) do&lt;br /&gt;
	--		tmpValue[x] = claim.mainsnak.datavalue.value&lt;br /&gt;
	--		x = x + 1&lt;br /&gt;
	--	end&lt;br /&gt;
	--	p.setdata(itemId, &amp;quot;MeSH&amp;quot;, tmpValue)&lt;br /&gt;
	--end&lt;br /&gt;
	--&lt;br /&gt;
	--tmpValue = {} --initialize&lt;br /&gt;
	--x = 1 --initialize&lt;br /&gt;
	--claims = nil --initialize&lt;br /&gt;
	-- ★★★★ End source specific code ★★★★ --&lt;br /&gt;
	&lt;br /&gt;
	---------------------------------------------------------------		&lt;br /&gt;
	-- End collecting data&lt;br /&gt;
	---------------------------------------------------------------&lt;br /&gt;
	&lt;br /&gt;
	--mw.logObject(datas[itemId])&lt;br /&gt;
	---------------------------------------------------------------		&lt;br /&gt;
	-- Start collecting childrens&lt;br /&gt;
	---------------------------------------------------------------&lt;br /&gt;
	claims = entity[&amp;#039;claims&amp;#039;][property]&lt;br /&gt;
	&lt;br /&gt;
	--local claims = wikidata.getClaims(query) &lt;br /&gt;
	if not claims then&lt;br /&gt;
		return {}&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local has_source&lt;br /&gt;
	&lt;br /&gt;
	if sources then -- If input setting about &amp;quot;sources&amp;quot; exists&lt;br /&gt;
		has_source = false -- default&lt;br /&gt;
	else&lt;br /&gt;
		has_source = true -- when setting doesn&amp;#039;t requires sources, assuming as if all cliams are sourced&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	for  _,claim in pairs( claims ) do&lt;br /&gt;
		if sources then -- If input setting about &amp;quot;sources&amp;quot; exists&lt;br /&gt;
			has_source = false -- reset&lt;br /&gt;
			&lt;br /&gt;
			-- ★★★★ Start source specific code ★★★★ --&lt;br /&gt;
			if claim[&amp;#039;references&amp;#039;] and claim[&amp;#039;references&amp;#039;][1][&amp;#039;snaks&amp;#039;][&amp;#039;P248&amp;#039;] then -- If P248 (&amp;quot;stated in&amp;quot;) claim exists&lt;br /&gt;
				if claim[&amp;#039;references&amp;#039;][1][&amp;#039;snaks&amp;#039;][&amp;#039;P248&amp;#039;][1][&amp;#039;datavalue&amp;#039;][&amp;#039;value&amp;#039;][&amp;#039;id&amp;#039;] == &amp;#039;Q286567&amp;#039; then -- If it is TA98 (&amp;#039;Q286567&amp;#039;)&lt;br /&gt;
					has_source = true --If the claim is sourced by Terminologia Anatomica, then &amp;quot;has_source&amp;quot; is true&lt;br /&gt;
					p.setdata(&amp;#039;Q&amp;#039; .. claim.mainsnak.datavalue.value[&amp;#039;numeric-id&amp;#039;], &amp;#039;sourcedBy&amp;#039;, &amp;#039;TA98&amp;#039;)-- set datas table, like datas[Q1234][&amp;#039;sourceType&amp;#039;] = &amp;#039;TA98&amp;#039;&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
			-- ★★★★ End source specific code ★★★★ --&lt;br /&gt;
		end&lt;br /&gt;
		&lt;br /&gt;
		--Adding item only when the claim is sourced by specific literature (e.g. Terminologia Anatomica)&lt;br /&gt;
		--or adding all item if input setting doesn&amp;#039;t need source&lt;br /&gt;
		if has_source then &lt;br /&gt;
			local value = claim.mainsnak.datavalue.value&lt;br /&gt;
			local nextitem = &amp;#039;Q&amp;#039; .. value[&amp;#039;numeric-id&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
			childrens[x] = nextitem&lt;br /&gt;
			x = x + 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	--mw.logObject(datas[itemId])&lt;br /&gt;
	--mw.log(&amp;quot;childrens are : &amp;quot;)&lt;br /&gt;
	--mw.logObject(childrens)&lt;br /&gt;
	return childrens&lt;br /&gt;
	---------------------------------------------------------------		&lt;br /&gt;
	-- End collecting childrens&lt;br /&gt;
	---------------------------------------------------------------&lt;br /&gt;
end&lt;br /&gt;
		&lt;br /&gt;
-- generator, see http://lua-users.org/wiki/LuaCoroutinesVersusPythonGenerators for lua doc&lt;br /&gt;
-- definition a function that when called several times will generate a sequence of strings&lt;br /&gt;
 &lt;br /&gt;
-- gensymbols = create_gensymbols(&amp;quot;ABC&amp;quot;)&lt;br /&gt;
-- gensymbols() == &amp;quot;A&amp;quot; &lt;br /&gt;
-- gensymbols() == &amp;quot;B&amp;quot; g&lt;br /&gt;
-- gensymbols() == &amp;quot;C&amp;quot; &lt;br /&gt;
-- gensymbols() == &amp;quot;AA&amp;quot; &amp;#039;&lt;br /&gt;
-- gensymbols() == &amp;quot;BABBA&amp;quot;&lt;br /&gt;
-- ...&lt;br /&gt;
function p.gensymbols(chars)&lt;br /&gt;
	local i = 0&lt;br /&gt;
	local charset = chars&lt;br /&gt;
 &lt;br /&gt;
	local generator = function ()&lt;br /&gt;
		local symbol = &amp;quot;&amp;quot;&lt;br /&gt;
		local rest&lt;br /&gt;
 &lt;br /&gt;
		rest = i&lt;br /&gt;
 &lt;br /&gt;
		repeat&lt;br /&gt;
			local j &lt;br /&gt;
			j = rest % string.len(charset)&lt;br /&gt;
			rest = math.floor(rest / string.len(charset))&lt;br /&gt;
			symbol = symbol .. string.sub(charset, j+1, j+1)&lt;br /&gt;
		until rest &amp;lt;= 0&lt;br /&gt;
		i = i + 1&lt;br /&gt;
		return symbol&lt;br /&gt;
	end&lt;br /&gt;
	return generator&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------------------------------------------	&lt;br /&gt;
-- Start definition of itemOutput function which returns text like &amp;#039;[[&amp;#039; .. link .. &amp;#039;|&amp;#039; .. label .. &amp;#039;]]&amp;#039;&lt;br /&gt;
-------------------------------------------------------------------------------------------	&lt;br /&gt;
function p.itemOutput(item, lang1, lang2, datas, setdata, getdata, refStyle, pencil)&lt;br /&gt;
		&lt;br /&gt;
	local globalSiteId_1 = lang1 .. &amp;#039;wiki&amp;#039;&lt;br /&gt;
	local globalSiteId_2 = lang2 .. &amp;#039;wiki&amp;#039;&lt;br /&gt;
	--local lang_2 = &amp;#039;en&amp;#039;&lt;br /&gt;
	--local langWiki = &amp;#039;//&amp;#039;..lang..&amp;#039;.wikipedia.org&amp;#039; -- if lang is &amp;#039;fr&amp;#039;, then this is &amp;#039;//fr.wikipedia.org&amp;#039; used to compare with mw.site.server&lt;br /&gt;
	&lt;br /&gt;
	--local currentEntity = mw.wikibase.getEntityObject( item )&lt;br /&gt;
	local currentLabel&lt;br /&gt;
	local currentLabel_temp&lt;br /&gt;
	&lt;br /&gt;
	local content&lt;br /&gt;
	local refTable = {}&lt;br /&gt;
	local referenceText = &amp;#039;&amp;#039;&lt;br /&gt;
	local pencilText&lt;br /&gt;
	if pencil == &amp;#039;none&amp;#039; then &lt;br /&gt;
		pencilText = &amp;#039;&amp;#039;&lt;br /&gt;
	else --(P527 &amp;quot;has part&amp;quot; or P361 &amp;quot;is part of&amp;quot;)&lt;br /&gt;
		pencilText = &amp;#039;[[File:Blue pencil.svg|frameless|text-top|10px|link=https://www.wikidata.org/wiki/&amp;#039; .. item .. &amp;#039;#P527]]&amp;#039;&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	refStyle = &amp;#039;asdasdsa&amp;#039;&lt;br /&gt;
	--mw.log(&amp;#039;refStyle is: &amp;#039; .. refStyle)&lt;br /&gt;
	&lt;br /&gt;
	if refStyle and refStyle ~= &amp;#039;none&amp;#039; then&lt;br /&gt;
	-- ★★★★ Start source specific code ★★★★ --&lt;br /&gt;
		if p.getdata(item, &amp;quot;sourcedBy&amp;quot;, nil ) and p.getdata(item, &amp;quot;sourcedBy&amp;quot;, nil )  == &amp;#039;TA98&amp;#039; then&lt;br /&gt;
			--mw.log(&amp;#039;datas[] is: &amp;#039;)&lt;br /&gt;
			--mw.logObject(datas)&lt;br /&gt;
			mw.log(&amp;#039;&amp;lt;br/&amp;gt;&amp;#039;)&lt;br /&gt;
			mw.log(&amp;#039;&amp;lt;br/&amp;gt;&amp;#039;)&lt;br /&gt;
			mw.log(&amp;#039;item is: &amp;#039;)&lt;br /&gt;
			mw.logObject(item)&lt;br /&gt;
			mw.log(&amp;#039;datas[item] is: &amp;#039;)&lt;br /&gt;
			mw.logObject(datas[item])&lt;br /&gt;
			mw.log(&amp;#039;datas[item][&amp;quot;TA98&amp;quot;] is: &amp;#039;)&lt;br /&gt;
			mw.logObject(datas[item][&amp;quot;TA98&amp;quot;])&lt;br /&gt;
				refTable = p.getdata(item, &amp;quot;TA98&amp;quot;, nil )&lt;br /&gt;
			mw.log(&amp;#039;refTable is: &amp;#039;)&lt;br /&gt;
			mw.logObject(refTable)&lt;br /&gt;
			mw.log(&amp;#039;&amp;lt;br/&amp;gt;&amp;#039;)&lt;br /&gt;
			mw.log(&amp;#039;&amp;lt;br/&amp;gt;&amp;#039;)&lt;br /&gt;
			if refTable and refTable ~=  &amp;#039;&amp;#039; then&lt;br /&gt;
				for  i, TA98ID in pairs( refTable ) do&lt;br /&gt;
					refTable[i] = string.sub(refTable[i], 2) --Remove first character &amp;quot;A&amp;quot; from ID&lt;br /&gt;
					refTable[i] = &amp;#039;[http://www.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/&amp;#039; .. refTable[i] .. &amp;#039;%20Entity%20TA98%20EN.htm&amp;#039; .. &amp;#039; &amp;#039; .. &amp;#039;A&amp;#039; .. refTable[i] ..&amp;#039;]&amp;#039; -- Creating wikitext which links to TA98 official site&lt;br /&gt;
					--mw.log(&amp;#039;refTable is :&amp;#039; .. tostring(table.concat(refTable)))&lt;br /&gt;
				end&lt;br /&gt;
				-- Converting multipul IDs into one line string, separated by comma, space &amp;quot;, &amp;quot;&lt;br /&gt;
				referenceText = tostring( table.concat( refTable, &amp;#039;, &amp;#039; ) )&lt;br /&gt;
				referenceText = referenceText .. &amp;#039; [[Terminologia Anatomica|TA98]], 1998&amp;#039;&lt;br /&gt;
				if refStyle == &amp;#039;normal&amp;#039; then&lt;br /&gt;
					referenceText = tostring( mw.getCurrentFrame():extensionTag( &amp;#039;ref&amp;#039;, referenceText, {} ) ) &lt;br /&gt;
				else&lt;br /&gt;
					referenceText = mw.getCurrentFrame():expandTemplate{ title = &amp;#039;efn&amp;#039;, args = {referenceText} }&lt;br /&gt;
				end&lt;br /&gt;
			else&lt;br /&gt;
				referenceText = &amp;#039;&amp;#039;&lt;br /&gt;
			end&lt;br /&gt;
		else&lt;br /&gt;
			referenceText = &amp;#039;&amp;#039;&lt;br /&gt;
		end&lt;br /&gt;
	-- ★★★★ End source specific code ★★★★ --&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	--mw.log(&amp;#039;referenceText is :&amp;#039; .. referenceText)&lt;br /&gt;
	&lt;br /&gt;
	--Link creation priority. Followings is an example.&lt;br /&gt;
	--&amp;#039;ja&amp;#039;(Japanese) represents your own language, and &amp;#039;en&amp;#039;(English) represents other language which is familiar with your readers.&lt;br /&gt;
	--This section would be better to customize for your own readers.&lt;br /&gt;
	&lt;br /&gt;
	--1. [[:ja:Sitelink(ja)|Sitelink(ja)]]           --If local sitelink data exists, use that.&lt;br /&gt;
	--2. [[:ja:Label(ja)|Label(ja)]](en link)        --If local sitelink data doesn&amp;#039;t exist, but local label data exists, use that (with en link).&lt;br /&gt;
	--3. Sitelink(en) (en link)  --If both local language data doesn&amp;#039;t exist, use sitelink to English Wikipedia&lt;br /&gt;
	--4. Label(en)(en link)        --If such data doesn&amp;#039;t exist, use English label&lt;br /&gt;
	--5. Q123456                     --If all of such kind data doesn&amp;#039;t exist, use bare item ID&lt;br /&gt;
	&lt;br /&gt;
	currentLabel = p.getdata(item, &amp;quot;sitelink_1&amp;quot;, nil )&lt;br /&gt;
	-- wd._references( {item, &amp;#039;P361&amp;#039;} ) &lt;br /&gt;
	if currentLabel ~= nil then&lt;br /&gt;
		content = &amp;#039;[[:&amp;#039; .. lang1 .. &amp;#039;:&amp;#039; .. currentLabel .. &amp;#039;|&amp;#039; .. currentLabel .. &amp;#039;]]&amp;#039;&lt;br /&gt;
		content = content .. &amp;#039; &amp;#039; .. referenceText&lt;br /&gt;
		content = content .. &amp;#039; &amp;#039; .. pencilText &lt;br /&gt;
		return content ----1. [[:ja:Sitelink(ja)|Sitelink(ja)]] style &lt;br /&gt;
	else&lt;br /&gt;
		currentLabel = p.getdata(item, &amp;quot;label_1&amp;quot;, nil )&lt;br /&gt;
		if currentLabel ~= nil then&lt;br /&gt;
			content = &amp;#039;[[:&amp;#039; .. lang1 .. &amp;#039;:&amp;#039; .. currentLabel .. &amp;#039;|&amp;#039; .. currentLabel .. &amp;#039;]]&amp;#039;&lt;br /&gt;
			currentLabel_temp = p.getdata(item, &amp;quot;sitelink_2&amp;quot;, nil )&lt;br /&gt;
			if currentLabel_temp ~= nil then&lt;br /&gt;
				content = content .. &amp;#039; ([[:&amp;#039; .. lang2 .. &amp;#039;:&amp;#039; .. currentLabel_temp .. &amp;#039;|&amp;#039; .. lang2 .. &amp;#039;]])&amp;#039; ----adding (en) link&lt;br /&gt;
			end&lt;br /&gt;
			content = content .. &amp;#039; &amp;#039; .. referenceText&lt;br /&gt;
			content = content .. &amp;#039; &amp;#039; .. pencilText &lt;br /&gt;
			return content ----2. [[:ja:Label(ja)|Label(ja)]](en link) style&lt;br /&gt;
		else&lt;br /&gt;
			currentLabel = p.getdata(item, &amp;quot;sitelink_2&amp;quot;, nil )&lt;br /&gt;
			if currentLabel ~= nil then&lt;br /&gt;
				content = currentLabel&lt;br /&gt;
				content = content .. &amp;#039; ([[:&amp;#039; .. lang2 .. &amp;#039;:&amp;#039; .. currentLabel .. &amp;#039;|&amp;#039; .. lang2 .. &amp;#039;]])&amp;#039; ----adding (en) link&lt;br /&gt;
				content = content .. &amp;#039; &amp;#039; .. referenceText&lt;br /&gt;
				content = content .. &amp;#039; &amp;#039; .. pencilText &lt;br /&gt;
				return content ----3. Sitelink(en) (en link) style&lt;br /&gt;
			else&lt;br /&gt;
				currentLabel = p.getdata(item, &amp;quot;label_2&amp;quot;, nil )&lt;br /&gt;
				if currentLabel ~= nil then&lt;br /&gt;
					content = currentLabel&lt;br /&gt;
					content = content .. &amp;#039; ([[:&amp;#039; .. lang2 .. &amp;#039;:&amp;#039; .. currentLabel .. &amp;#039;|&amp;#039; .. lang2 .. &amp;#039;]])&amp;#039; ----adding (en) link&lt;br /&gt;
					content = content .. &amp;#039; &amp;#039; .. referenceText&lt;br /&gt;
					content = content .. &amp;#039; &amp;#039; .. pencilText &lt;br /&gt;
					return content ----4. Label(en)(en link) style&lt;br /&gt;
				else&lt;br /&gt;
					content = item&lt;br /&gt;
					content = content .. &amp;#039; &amp;#039; .. referenceText&lt;br /&gt;
					content = content .. &amp;#039; &amp;#039; .. pencilText &lt;br /&gt;
					return content --5. Q123456 style&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
-------------------------------------------------------------------------------------------	&lt;br /&gt;
-- End definition of itemOutput function which returns text like &amp;#039;[[&amp;#039; .. link .. &amp;#039;|&amp;#039; .. label .. &amp;#039;]]&amp;#039;&lt;br /&gt;
-------------------------------------------------------------------------------------------	&lt;br /&gt;
&lt;br /&gt;
function p.outputTree( query, firstItemId, property, direction, maxdepth, sources, refStyle, pencil, lang1, lang2 )&lt;br /&gt;
-------------------------------------------------------------------------------------------	&lt;br /&gt;
-- Start preparation of functions&lt;br /&gt;
-------------------------------------------------------------------------------------------	&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
		----- topological sort and meta data of the DAG ( https://en.wikipedia.org/wiki/Topological_sorting )&lt;br /&gt;
		&lt;br /&gt;
	--This firstPass() function is called only once, at first&lt;br /&gt;
	local function firstPass( query, firstItemId, item_repr )&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
	    local content = p.itemOutput(firstItemId, lang1, lang2, datas, setdata, getdata, refStyle, pencil)&lt;br /&gt;
 &lt;br /&gt;
	    local opened = 1 -- This means the downstream branch of the item is under processing.&lt;br /&gt;
						 -- If encountering opened item through the processing, it indicates that infinite loop exists.   &lt;br /&gt;
						 &lt;br /&gt;
	    local closed = 2 -- This means the processing of all downstream branches of the item reached to its end.&lt;br /&gt;
						 -- The downest item is showed with &amp;quot; ? &amp;quot; in tree.&lt;br /&gt;
						 &lt;br /&gt;
	    local incomplete = 3 -- This means the processing of all downstream branches of the item reached to its end.&lt;br /&gt;
							 -- If more downstream branches exist but not processed (because of limitation from maxdepth),&lt;br /&gt;
							 -- then the downest item showed with &amp;quot;…&amp;quot; in tree.&lt;br /&gt;
							 &lt;br /&gt;
	    local marks = {} --opened(1) or closed(2) or incomplete(3) for each QID (e.g. marks[Q12234234] -&amp;gt; closed)&lt;br /&gt;
		&lt;br /&gt;
 &lt;br /&gt;
	    --local datas = {} --datas[QID][&amp;quot;nparents&amp;quot;], datas[QID][&amp;quot;symbol&amp;quot;], datas[QID][&amp;quot;looped&amp;quot;], datas[QID][&amp;quot;rank&amp;quot;], datas[QID][&amp;quot;status&amp;quot;]&lt;br /&gt;
						--datas[QID][&amp;quot;nparents&amp;quot;] represents &amp;quot;number of parents&amp;quot; of certain item. This is normally 1.&lt;br /&gt;
						--If this is 2 or more, it means that the exact same item appears several times in defferent branches of the tree repeatedly.&lt;br /&gt;
						&lt;br /&gt;
		local childrens = {} -- for example, childrens[Q1234] contains data like {Q123123, Q3984198237, Q1874138746}, three childrens of Q1234&lt;br /&gt;
 		&lt;br /&gt;
		--while there are unmarked nodes do&lt;br /&gt;
		--    select an unmarked node n&lt;br /&gt;
		--    visit(n) &lt;br /&gt;
		--function visit(node n)&lt;br /&gt;
		--    if n has a temporary mark then stop (not a DAG)&lt;br /&gt;
		--    if n is not marked (i.e. has not been visited yet) then&lt;br /&gt;
		--        mark n temporarily&lt;br /&gt;
		--        for each node m with an edge from n to m do&lt;br /&gt;
		--            visit(m)&lt;br /&gt;
		--        mark n permanently&lt;br /&gt;
		--        add n to head of L&lt;br /&gt;
 &lt;br /&gt;
		-- this function &lt;br /&gt;
		-- * visits and builds the tree, DAG or graph,&lt;br /&gt;
		-- * in the same pass, computes a topological ordering for the DAG&lt;br /&gt;
		-- * annotates the nodes with informations&lt;br /&gt;
	    function visit(n, depth, rank)&lt;br /&gt;
	    	--mw.log(&amp;quot;depth is &amp;quot; .. depth ..  &amp;quot;&amp;gt;=&amp;quot; .. &amp;quot;maxdepth is &amp;quot; .. maxdepth) &lt;br /&gt;
 &lt;br /&gt;
			--mw.log(n .. &amp;quot;: mark is &amp;quot; .. tostring(marks[n]))&lt;br /&gt;
			if marks[n] == opened then &lt;br /&gt;
				p.setdata(n, &amp;quot;status&amp;quot;, &amp;quot;loop&amp;quot;)&lt;br /&gt;
				p.setdata(n, &amp;quot;rank&amp;quot;, rank)&lt;br /&gt;
				return rank&lt;br /&gt;
			elseif marks[n] ~= closed then&lt;br /&gt;
				marks[n] = opened&lt;br /&gt;
 &lt;br /&gt;
				childrens[n] = p.children( query, n, property, direction, maxdepth, sources, refStyle, pencil, lang1, lang2)&lt;br /&gt;
				for _, node in ipairs(childrens[n]) do&lt;br /&gt;
					p.setdata(node, &amp;quot;nparents&amp;quot;, &lt;br /&gt;
						p.getdata(node, &amp;quot;nparents&amp;quot;, 0) + 1&lt;br /&gt;
					) &lt;br /&gt;
					--mw.log(node .. &amp;quot;: nparents is &amp;quot; .. tostring(p.getdata(node, &amp;quot;nparents&amp;quot;, &amp;quot;aaa&amp;quot;)))&lt;br /&gt;
					if depth &amp;lt;= maxdepth then&lt;br /&gt;
						rank = visit(node, depth + 1, rank + 1)&lt;br /&gt;
					end&lt;br /&gt;
				end&lt;br /&gt;
 &lt;br /&gt;
				if depth &amp;lt;= maxdepth then&lt;br /&gt;
					if p.getdata(n, &amp;quot;status&amp;quot;, &amp;quot;complete&amp;quot;) ~= &amp;quot;loop&amp;quot; then&lt;br /&gt;
						p.setdata(n, &amp;quot;status&amp;quot;, &amp;quot;complete&amp;quot;)&lt;br /&gt;
					end&lt;br /&gt;
					marks[n] = closed&lt;br /&gt;
				else&lt;br /&gt;
					p.setdata(n, &amp;quot;status&amp;quot;, &amp;quot;incomplete&amp;quot;)&lt;br /&gt;
					marks[n] = incomplete&lt;br /&gt;
				end&lt;br /&gt;
 &lt;br /&gt;
				p.setdata(n, &amp;quot;rank&amp;quot;, rank)&lt;br /&gt;
			end&lt;br /&gt;
			return rank + 1&lt;br /&gt;
		end&lt;br /&gt;
		p.setdata(firstItemId, &amp;quot;nparents&amp;quot;, 0)&lt;br /&gt;
		visit(firstItemId, 1, 0)&lt;br /&gt;
 &lt;br /&gt;
		return datas, childrens&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local langobj = mw.language.new(lang1)&lt;br /&gt;
	-- link inside tree &lt;br /&gt;
	local function formatSymbol(prefix)&lt;br /&gt;
		return &amp;#039;&amp;lt;span class=&amp;quot;Unicode&amp;quot;&amp;gt;&amp;lt;small&amp;gt;(&amp;#039; .. prefix .. &amp;quot;)&amp;lt;/small&amp;gt;&amp;lt;/span&amp;gt;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	local function genAnchor(id)&lt;br /&gt;
		return &amp;#039;&amp;lt;span id=&amp;quot;&amp;#039; .. firstItemId .. id .. &amp;#039;&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&amp;#039; &lt;br /&gt;
	end&lt;br /&gt;
	local function anchorLink(id, content)&lt;br /&gt;
		return &amp;quot;[[#&amp;quot; .. firstItemId .. id .. &amp;quot;|&amp;quot; .. content .. &amp;quot;]]&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
	local function fmtTreeLinks(content)&lt;br /&gt;
		return mw.text.tag(&amp;quot;sup&amp;quot;, {}, content)&lt;br /&gt;
	end&lt;br /&gt;
 &lt;br /&gt;
	local function renderTree( itemId, datas, children, alreadyOuted, gs )&lt;br /&gt;
		local content = p.itemOutput( itemId, lang1, lang2, datas, setdata, getdata, refStyle, pencil)&lt;br /&gt;
		local state&lt;br /&gt;
 &lt;br /&gt;
		if datas[itemId][&amp;quot;status&amp;quot;] ~= nil then&lt;br /&gt;
			state = datas[itemId][&amp;quot;status&amp;quot;] &lt;br /&gt;
		end&lt;br /&gt;
		--mw.log(itemId .. &amp;quot;: status is &amp;quot; .. state )&lt;br /&gt;
 &lt;br /&gt;
		if 	datas[itemId] [&amp;quot;nparents&amp;quot;] &amp;gt; 1 and datas[itemId][&amp;quot;symbol&amp;quot;] == nil then&lt;br /&gt;
			p.setdata(itemId, &amp;quot;symbol&amp;quot;, gs() )&lt;br /&gt;
		end&lt;br /&gt;
		-- prevent infinite loops in non DAGs&lt;br /&gt;
		if state == &amp;quot;loop&amp;quot; then&lt;br /&gt;
			if datas[itemId][&amp;quot;looped&amp;quot;] == nil then&lt;br /&gt;
				datas[itemId][&amp;quot;looped&amp;quot;] = &amp;quot;treated&amp;quot;&lt;br /&gt;
				content =  fmtTreeLinks(&amp;quot; ∞&amp;quot;) .. &amp;quot; &amp;quot; .. content .. genAnchor(itemId)&lt;br /&gt;
			else&lt;br /&gt;
				return content ..  fmtTreeLinks(&amp;quot;∞&amp;quot; ..	&amp;quot; ↑ &amp;quot; .. anchorLink(itemId, formatSymbol( datas[itemId][&amp;quot;symbol&amp;quot;] )))&lt;br /&gt;
			end&lt;br /&gt;
		elseif state == &amp;quot;incomplete&amp;quot; or state == &amp;quot;unvisited&amp;quot; then&lt;br /&gt;
			content = content .. &amp;quot; &amp;quot; .. fmtTreeLinks(&amp;quot;…&amp;quot;)&lt;br /&gt;
			return content&lt;br /&gt;
		end&lt;br /&gt;
 &lt;br /&gt;
		-- has no chilren ? display as leaf&lt;br /&gt;
		if children[itemId] ~= nil and table.getn(children[itemId]) == 0 then&lt;br /&gt;
			--return &amp;quot; &amp;quot; .. content .. &amp;quot; ? &amp;quot; -- would be great to use &amp;quot;?b, but font problem&lt;br /&gt;
			return &amp;quot; &amp;quot; .. content&lt;br /&gt;
		end&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
		datas[itemId] [&amp;quot;nparents&amp;quot;] = datas[itemId][&amp;quot;nparents&amp;quot;] - 1&lt;br /&gt;
 &lt;br /&gt;
	    local parts = {}&lt;br /&gt;
 &lt;br /&gt;
		-- sort children topologycally&lt;br /&gt;
		if alreadyOuted[itemId] == nil then&lt;br /&gt;
 &lt;br /&gt;
 			local langobj = mw.language.new(lang1)&lt;br /&gt;
		    local prefix = &amp;quot; &amp;quot;&lt;br /&gt;
		    if datas[itemId] [&amp;quot;nparents&amp;quot;] &amp;gt; 0 then&lt;br /&gt;
		    	local arrow = langobj:getArrow()&lt;br /&gt;
		    	prefix = fmtTreeLinks(genAnchor(itemId) .. &amp;quot; &amp;quot; .. arrow .. &amp;quot; &amp;quot; .. formatSymbol(datas[itemId][&amp;quot;symbol&amp;quot;]))&lt;br /&gt;
	    	end&lt;br /&gt;
				&lt;br /&gt;
			order = children[itemId]&lt;br /&gt;
	    	table.sort(order, function (a, b) return datas[a][&amp;quot;rank&amp;quot;] &amp;lt; datas[b][&amp;quot;rank&amp;quot;] end )&lt;br /&gt;
 &lt;br /&gt;
		    for i, childId in ipairs(order) do&lt;br /&gt;
		        table.insert( parts, renderTree( childId, datas, children, alreadyOuted, gs ) )&lt;br /&gt;
		    end&lt;br /&gt;
	 &lt;br /&gt;
			if direction == &amp;quot;child&amp;quot; or direction == &amp;quot;brother&amp;quot; then &lt;br /&gt;
				local l = table.maxn( parts )&lt;br /&gt;
				for i = 1,(l - 1) do&lt;br /&gt;
					parts[i] = mw.text.tag( &amp;#039;li&amp;#039;, {}, parts[i] )&lt;br /&gt;
				end&lt;br /&gt;
				parts[l] = mw.text.tag( &amp;#039;li&amp;#039;, { class = &amp;#039;lastline&amp;#039; }, parts[l] )&lt;br /&gt;
				alreadyOuted[itemId] = prefix .. &amp;quot; &amp;quot; .. content  .. mw.text.tag( &amp;#039;ul&amp;#039;, {}, table.concat( parts ) )&lt;br /&gt;
			elseif direction == &amp;quot;parent&amp;quot; then 				&lt;br /&gt;
				local texttmp&lt;br /&gt;
				local matchCount = 0&lt;br /&gt;
				--mw.log(&amp;quot;parts is: &amp;quot;)&lt;br /&gt;
				--mw.logObject(parts)&lt;br /&gt;
				texttmp = mw.text.tag( &amp;#039;ul&amp;#039;, {}, mw.text.tag( &amp;#039;li&amp;#039;, { class = &amp;#039;lastline&amp;#039; }, content ) )&lt;br /&gt;
				&lt;br /&gt;
				alreadyOuted[itemId], matchCount = string.gsub(table.concat( parts ), &amp;quot;(&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;)&amp;quot;, texttmp .. &amp;quot;%1&amp;quot;, 1)&lt;br /&gt;
				--mw.log(&amp;#039;matchCount is:&amp;#039;)&lt;br /&gt;
				--mw.log(matchCount)&lt;br /&gt;
				if matchCount == 0 then&lt;br /&gt;
					alreadyOuted[itemId] = prefix .. &amp;quot; &amp;quot; .. string.gsub(table.concat( parts ), &amp;quot;$&amp;quot;, texttmp, 1)&lt;br /&gt;
				end&lt;br /&gt;
				alreadyOuted[itemId] = prefix .. &amp;quot; &amp;quot; .. alreadyOuted[itemId]&lt;br /&gt;
				--mw.log(&amp;quot;alreadyOuted[itemId] is: &amp;quot;)&lt;br /&gt;
				--mw.logObject(alreadyOuted[itemId])&lt;br /&gt;
			end&lt;br /&gt;
			&lt;br /&gt;
		end&lt;br /&gt;
 &lt;br /&gt;
		if datas[itemId] [&amp;quot;nparents&amp;quot;] &amp;lt;= 0 or state == &amp;quot;loop&amp;quot; then&lt;br /&gt;
			return alreadyOuted[itemId]&lt;br /&gt;
		else &lt;br /&gt;
			return content .. &amp;quot; &amp;quot; .. fmtTreeLinks(anchorLink(itemId, formatSymbol(datas[itemId][&amp;quot;symbol&amp;quot;])) .. &amp;quot; &amp;quot; .. langobj:getArrow(forward))&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
-------------------------------------------------------------------------------------------	&lt;br /&gt;
-- End preparation of functions&lt;br /&gt;
-------------------------------------------------------------------------------------------	&lt;br /&gt;
 &lt;br /&gt;
-------------------------&lt;br /&gt;
-- Start creating tree --&lt;br /&gt;
-------------------------&lt;br /&gt;
	-- gen = p.gensymbols(&amp;quot;??&amp;quot;)&lt;br /&gt;
	-- gen = p.gensymbols(&amp;quot;12&amp;quot;)&lt;br /&gt;
	-- gen = p.gensymbols(&amp;quot;★☆?&amp;quot;)&lt;br /&gt;
	-- These symbols are used as link button for &amp;quot;jumping&amp;quot; from certain branch to other branch.&lt;br /&gt;
	-- &amp;quot;jumping&amp;quot; functionality is used when same item appears multipul times in one tree.&lt;br /&gt;
	local gen = p.gensymbols(&amp;quot;@*#&amp;quot;) &lt;br /&gt;
 &lt;br /&gt;
	local children &lt;br /&gt;
	datas, children = firstPass( query, firstItemId, gen)&lt;br /&gt;
	-- alreadyOuted is the table which contains html code for each QID.&lt;br /&gt;
	-- For example, like this... alreadyOuted[Q1234] = &amp;quot;&amp;lt;li&amp;gt;[[Human body]]&amp;lt;/li&amp;gt;&amp;quot;&lt;br /&gt;
	-- Actually items which located more nearer to the root (to say, FirstItemId) contain html codes of their branchs within it. &lt;br /&gt;
	-- For example, like this... alreadyOuted[Q1234] = &amp;quot;&amp;lt;li&amp;gt;[[Human body]] &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[[Nervous system]]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&amp;lt;/li&amp;gt;&amp;quot;&lt;br /&gt;
	-- This is done by recursive calling of renderTree() function inside the renderTree() function.&lt;br /&gt;
	local alreadyOuted = {} &lt;br /&gt;
	rendering = {} -- What does this do?&lt;br /&gt;
	return  renderTree( firstItemId, datas, children, alreadyOuted, gen)&lt;br /&gt;
-----------------------&lt;br /&gt;
-- End creating tree --&lt;br /&gt;
-----------------------&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.main( frame )&lt;br /&gt;
------------------------------------&lt;br /&gt;
-- Start adjustments of arguments &lt;br /&gt;
------------------------------------&lt;br /&gt;
    local frame = frame:getParent()&lt;br /&gt;
    local query = frame.args&lt;br /&gt;
	local sources&lt;br /&gt;
	local refStyle -- Option for refrence style. &amp;quot;none&amp;quot; or &amp;quot;normal&amp;quot; or other&lt;br /&gt;
	local pencil -- if pencil == &amp;quot;none&amp;quot;, wikidata-linked pencil image is not shown &lt;br /&gt;
    local maxdepth&lt;br /&gt;
    local lang1&lt;br /&gt;
    local lang2&lt;br /&gt;
	local firstItemId = query.items&lt;br /&gt;
	&lt;br /&gt;
	if firstItemId == nil or firstItemId == &amp;#039;&amp;#039; then&lt;br /&gt;
		--If QID is not defined, using the QID of the item which is connected to the current page.&lt;br /&gt;
		firstItemId = mw.wikibase.getEntityIdForCurrentPage() &lt;br /&gt;
		if not firstItemId then&lt;br /&gt;
			return &amp;#039;No items passed as parameter&amp;#039; --If there is no connected wikidata page, abort.&lt;br /&gt;
		end&lt;br /&gt;
    end&lt;br /&gt;
    --local firstItemsId = mw.text.split( query.items, &amp;#039; &amp;#039; )&lt;br /&gt;
    --if firstItemsId[1] == nil or firstItemsId[1] == &amp;#039;&amp;#039; or table.maxn( firstItemsId ) == 0 then&lt;br /&gt;
	--	--If QID is not defined, using the QID of the item which is connected to the current page.&lt;br /&gt;
	--	firstItemsId[1] = mw.wikibase.getEntityIdForCurrentPage() &lt;br /&gt;
	--	if not firstItemsId[1] then&lt;br /&gt;
	--		return &amp;#039;No items passed as parameter&amp;#039; --If there is no connected wikidata page, abort.&lt;br /&gt;
	--	end&lt;br /&gt;
    --end&lt;br /&gt;
	&lt;br /&gt;
	--for i, item in pairs( firstItemsId ) do&lt;br /&gt;
	if tonumber(firstItemId) then --legacy format&lt;br /&gt;
		firstItemId = &amp;#039;Q&amp;#039;.. tostring(firstItemId)&lt;br /&gt;
	end&lt;br /&gt;
	--end&lt;br /&gt;
	&lt;br /&gt;
	if tonumber(query.childProperty) then --legacy format&lt;br /&gt;
		query.childProperty = &amp;#039;P&amp;#039;.. tostring(query.childProperty)&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if tonumber(query.parentProperty) then --legacy format&lt;br /&gt;
		query.parentProperty = &amp;#039;P&amp;#039;.. tostring(query.parentProperty)&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if query.childDepth and query.childDepth ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		query.childDepth = tonumber( query.childDepth )&lt;br /&gt;
	else&lt;br /&gt;
		query.childDepth = 4&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if query.parentDepth and query.parentDepth ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		query.parentDepth = tonumber( query.parentDepth )&lt;br /&gt;
	else&lt;br /&gt;
		query.parentDepth = 2&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if query.brotherDepth and query.brotherDepth ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		query.brotherDepth = tonumber( query.brotherDepth )&lt;br /&gt;
	else&lt;br /&gt;
		query.brotherDepth = 0&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if query.sources and query.sources ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		sources = mw.text.split( query.sources, &amp;#039; &amp;#039; )&lt;br /&gt;
	else&lt;br /&gt;
		sources = nil&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if query.refStyle and query.refStyle ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		if query.refStyle == &amp;#039;none&amp;#039; then&lt;br /&gt;
			refStyle = &amp;#039;none&amp;#039;&lt;br /&gt;
		elseif query.refStyle == &amp;#039;normal&amp;#039; then&lt;br /&gt;
			refStyle = &amp;#039;normal&amp;#039;&lt;br /&gt;
		else&lt;br /&gt;
			refStyle = mw.text.split( query.sources, &amp;#039; &amp;#039; )&lt;br /&gt;
		end&lt;br /&gt;
	else&lt;br /&gt;
		refStyle = &amp;#039;normal&amp;#039;&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if query.pencil and query.pencil ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		pencil = tostring(query.pencil)&lt;br /&gt;
	else&lt;br /&gt;
		pencil = nil&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if query.lang1 and query.lang1 ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		lang1 = query.lang1&lt;br /&gt;
	else&lt;br /&gt;
		lang1 = mw.language.getContentLanguage().code&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if query.lang2 and query.lang2 ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		lang2 = query.lang2&lt;br /&gt;
	else&lt;br /&gt;
		lang2 = &amp;#039;en&amp;#039;&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
----------------------------------&lt;br /&gt;
-- End adjustments of arguments &lt;br /&gt;
----------------------------------&lt;br /&gt;
&lt;br /&gt;
-------------------------&lt;br /&gt;
-- Start creating tree --&lt;br /&gt;
-------------------------&lt;br /&gt;
    local content = &amp;#039;&amp;#039;&lt;br /&gt;
    local parent_content = nil&lt;br /&gt;
    local child_content = nil&lt;br /&gt;
	local brother_content = nil&lt;br /&gt;
	local direction -- tree searching direction. &amp;quot;child&amp;quot;, &amp;quot;parent&amp;quot; or &amp;quot;brother&amp;quot;&lt;br /&gt;
	local property -- property used to search items. For example &amp;quot;P527&amp;quot; (has part) or &amp;quot;P361&amp;quot; (part of)&lt;br /&gt;
	&lt;br /&gt;
	--------------------------------------------------&lt;br /&gt;
	--Creating tree for parent side (upstream side)&lt;br /&gt;
	--------------------------------------------------&lt;br /&gt;
	if query.parentProperty and query.parentProperty ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		parent_content = &amp;#039;&amp;#039;&lt;br /&gt;
		direction = &amp;quot;parent&amp;quot;&lt;br /&gt;
		property = query.parentProperty&lt;br /&gt;
		maxdepth = query.parentDepth&lt;br /&gt;
		--for _, item in pairs( firstItemsId ) do&lt;br /&gt;
		   parent_content = mw.text.tag( &amp;#039;li&amp;#039;, { class = &amp;#039;lastline&amp;#039; }, p.outputTree( query, firstItemId, property, direction, maxdepth, sources, refStyle, pencil, lang1, lang2 ) )&lt;br /&gt;
		--end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	--------------------------------------------------&lt;br /&gt;
	--Creating tree for brother side (substream side)&lt;br /&gt;
	--------------------------------------------------&lt;br /&gt;
	-- Brother is, as definition, &amp;quot;parent&amp;#039;s child&amp;quot;. So both query.parentProperty and query.childProperty is needed.&lt;br /&gt;
	if query.brotherDepth and query.brotherDepth &amp;gt;= 0 and query.parentProperty and query.parentProperty ~= &amp;#039;&amp;#039; and query.parentDepth &amp;gt;= 1 and query.childProperty and query.childProperty ~= &amp;#039;&amp;#039; then&lt;br /&gt;
	&lt;br /&gt;
		brother_content = &amp;#039;&amp;#039;&lt;br /&gt;
		direction = &amp;quot;brother&amp;quot;&lt;br /&gt;
		maxdepth = query.brotherDepth&lt;br /&gt;
		property = query.parentProperty&lt;br /&gt;
		&lt;br /&gt;
		-- Get parent QID&lt;br /&gt;
		local parents = p.children(query, firstItemId, property, direction, maxdepth, sources, refStyle, pencil, lang1, lang2 )&lt;br /&gt;
		--local currentEntity = mw.wikibase.getEntityObject( firstItemId )&lt;br /&gt;
		--local claims = currentEntity[&amp;#039;claims&amp;#039;][query.parentProperty]&lt;br /&gt;
		--if claims then -- If firstItemId has parent&lt;br /&gt;
		--	local x = 1&lt;br /&gt;
		--	local parents = {}&lt;br /&gt;
		--	for  _,claim in pairs( claims ) do -- Check all parent claims and get QID from there. And stored it into parents[] table.&lt;br /&gt;
		--			local value = claim.mainsnak.datavalue.value&lt;br /&gt;
		--			local nextitem = &amp;#039;Q&amp;#039; .. value[&amp;#039;numeric-id&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
		--			parents[x] = nextitem&lt;br /&gt;
		--			x = x + 1&lt;br /&gt;
		--	end&lt;br /&gt;
			&lt;br /&gt;
			local currentEntity&lt;br /&gt;
			local claims&lt;br /&gt;
			local x = 1&lt;br /&gt;
			local brothers, brothersPart = {}&lt;br /&gt;
			property = query.childProperty&lt;br /&gt;
			mw.log(&amp;quot;parents is :&amp;quot;)&lt;br /&gt;
			mw.logObject(parents)&lt;br /&gt;
			-- Get brother QID&lt;br /&gt;
			for  _,parent in pairs( parents ) do&lt;br /&gt;
				&lt;br /&gt;
				brothersPart = p.children(query, parent, property, direction, maxdepth, sources, refStyle, pencil, lang1, lang2 )&lt;br /&gt;
				mw.log(&amp;quot;brothersPart is :&amp;quot;)&lt;br /&gt;
				mw.logObject(brothersPart)&lt;br /&gt;
				--currentEntity = mw.wikibase.getEntityObject( parent )&lt;br /&gt;
				--claims = currentEntity[&amp;#039;claims&amp;#039;][query.childProperty] -- Get child property in parent entity. Parent&amp;#039;s child is brother.&lt;br /&gt;
				for  _,bro in pairs( brothersPart ) do -- Check all  child claims and get QID from there. And stored it into brothers[] table.&lt;br /&gt;
					--local value = claim.mainsnak.datavalue.value&lt;br /&gt;
					--local nextitem = &amp;#039;Q&amp;#039; .. value[&amp;#039;numeric-id&amp;#039;]&lt;br /&gt;
					&lt;br /&gt;
					if bro ~= firstItemId then -- One of parent&amp;#039;s child is ownself. It is not brother. So exclude that.&lt;br /&gt;
						brothers[x] = bro&lt;br /&gt;
						x = x + 1&lt;br /&gt;
					end&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
			&lt;br /&gt;
			mw.log(&amp;quot;brothers is :&amp;quot;)&lt;br /&gt;
			mw.logObject(brothers)&lt;br /&gt;
			-- Creating trees for brother&lt;br /&gt;
			local l = table.maxn( brothers )&lt;br /&gt;
			if l &amp;gt;= 1 then&lt;br /&gt;
				for i = 1,(l - 1) do&lt;br /&gt;
					brother_content = brother_content .. mw.text.tag( &amp;#039;li&amp;#039;, {}, p.outputTree( query, brothers[i], property, direction, maxdepth, sources, refStyle, pencil, lang1, lang2 ) )&lt;br /&gt;
				end&lt;br /&gt;
				brother_content = brother_content .. mw.text.tag( &amp;#039;li&amp;#039;, {}, p.outputTree( query, brothers[l], property, direction, maxdepth, sources, refStyle, pencil, lang1, lang2 ) )&lt;br /&gt;
			end&lt;br /&gt;
				&lt;br /&gt;
		--end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	--------------------------------------------------&lt;br /&gt;
	--Creating tree for child side (downstream side)&lt;br /&gt;
	--------------------------------------------------&lt;br /&gt;
	if query.childProperty and query.childProperty ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		child_content = &amp;#039;&amp;#039;&lt;br /&gt;
		direction = &amp;quot;child&amp;quot;&lt;br /&gt;
		property = query.childProperty&lt;br /&gt;
		maxdepth = query.childDepth&lt;br /&gt;
		if brother_content and brother_content ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		   child_content = mw.text.tag( &amp;#039;li&amp;#039;, { class = &amp;#039;lastline&amp;#039; }, p.outputTree( query, firstItemId, property, direction, maxdepth, sources, refStyle, pencil, lang1, lang2 ) )&lt;br /&gt;
		else&lt;br /&gt;
		   child_content = mw.text.tag( &amp;#039;li&amp;#039;, { class = &amp;#039;lastline&amp;#039; }, p.outputTree( query, firstItemId, property, direction, maxdepth, sources, refStyle, pencil, lang1, lang2 ) )&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	mw.log(&amp;quot;parent_content is: &amp;quot;)&lt;br /&gt;
	mw.logObject(parent_content)&lt;br /&gt;
	mw.log(&amp;quot;&amp;quot;)&lt;br /&gt;
	mw.log(&amp;quot;&amp;quot;)&lt;br /&gt;
	mw.log(&amp;quot;child_content is: &amp;quot;)&lt;br /&gt;
	mw.logObject(child_content)&lt;br /&gt;
	mw.log(&amp;quot;&amp;quot;)&lt;br /&gt;
	mw.log(&amp;quot;&amp;quot;)&lt;br /&gt;
	mw.log(&amp;quot;brother_content is: &amp;quot;)&lt;br /&gt;
	mw.logObject(brother_content)&lt;br /&gt;
	mw.log(&amp;quot;&amp;quot;)&lt;br /&gt;
	mw.log(&amp;quot;&amp;quot;)&lt;br /&gt;
	&lt;br /&gt;
	--------------------------------------------------&lt;br /&gt;
	-- Combine parent and child (and brother) trees &lt;br /&gt;
	--------------------------------------------------&lt;br /&gt;
	if parent_content and parent_content ~= &amp;#039;&amp;#039; and child_content and child_content ~= &amp;#039;&amp;#039; then&lt;br /&gt;
		-- Append child-tree to parent-tree.&lt;br /&gt;
		-- What is doing here is only replacing the last item (final &amp;#039;&amp;lt;li class=\&amp;quot;lastline\&amp;quot;&amp;gt;...&amp;lt;/li&amp;gt;&amp;#039;) in &amp;quot;parent_content&amp;quot;, by &amp;quot;child_content&amp;quot;&lt;br /&gt;
		-- But because of Lua regex function is not high functioning, bit complicated process is done here.&lt;br /&gt;
		local a = 0&lt;br /&gt;
		local b = 0&lt;br /&gt;
		&lt;br /&gt;
		-- Finding start position of final item by searching &amp;lt;li class=\&amp;quot;lastline\&amp;quot;&amp;gt; from the left to the right through text. &lt;br /&gt;
		while string.find(parent_content, &amp;#039;&amp;lt;li class=\&amp;quot;lastline\&amp;quot;&amp;gt;&amp;#039;, b) do&lt;br /&gt;
			mw.log(a .. &amp;#039; &amp;#039; .. b)&lt;br /&gt;
			a, b = string.find(parent_content, &amp;#039;&amp;lt;li class=\&amp;quot;lastline\&amp;quot;&amp;gt;&amp;#039;, b)&lt;br /&gt;
		end&lt;br /&gt;
		mw.log(a .. &amp;#039; &amp;#039; .. b)&lt;br /&gt;
		&lt;br /&gt;
		local c = 0&lt;br /&gt;
		local d = 0&lt;br /&gt;
		-- Finding end position of final item by searching &amp;lt;/ul&amp;gt;&amp;lt;/li&amp;gt;. &lt;br /&gt;
		c, d = string.find(parent_content, &amp;#039;&amp;lt;/ul&amp;gt;&amp;lt;/li&amp;gt;.*$&amp;#039;) &lt;br /&gt;
		--mw.log(c .. &amp;#039; &amp;#039; .. d)&lt;br /&gt;
		&lt;br /&gt;
		-- If c, d == nil, it means &amp;quot;parent_content&amp;quot; is one level, is not multipul level tree.&lt;br /&gt;
		-- In other words, it contains only one item which is correponding to &amp;quot;firstItemId&amp;quot;.&lt;br /&gt;
		-- In such case, ignore &amp;quot;parent_content&amp;quot;.&lt;br /&gt;
		-- Ignore &amp;quot;brother_content&amp;quot; also. Because if no parent known, no brother known.&lt;br /&gt;
		if c ~= nil and d ~= nil then &lt;br /&gt;
			-- Replacing final item of &amp;quot;parent_content&amp;quot; by &amp;quot;child_content&amp;quot;.&lt;br /&gt;
			-- If &amp;quot;brother_content&amp;quot; exists, inserts it just after the &amp;quot;child_content&amp;quot;.&lt;br /&gt;
			if brother_content and brother_content ~= &amp;#039;&amp;#039; then&lt;br /&gt;
				content = string.sub(parent_content, 1, a - 1) .. brother_content .. child_content .. string.sub(parent_content, c)&lt;br /&gt;
			else&lt;br /&gt;
				content = string.sub(parent_content, 1, a - 1) .. child_content .. string.sub(parent_content, c)&lt;br /&gt;
			end&lt;br /&gt;
		else&lt;br /&gt;
			content = child_content&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	mw.log(&amp;quot;content after is: &amp;quot;)&lt;br /&gt;
	mw.logObject(content)&lt;br /&gt;
	&lt;br /&gt;
	--------------------------------------------------&lt;br /&gt;
	-- Final formatting and adding {{reflist}} like template at the bottom &lt;br /&gt;
	--------------------------------------------------&lt;br /&gt;
	if parent_content or child_content then&lt;br /&gt;
		res =  mw.text.tag( &amp;#039;div&amp;#039;, { class = &amp;#039;treeview&amp;#039; }, mw.text.tag( &amp;#039;ul&amp;#039;, {}, content ) )&lt;br /&gt;
		&lt;br /&gt;
		if sources then -- if sources are shown after each items like [1], then put {{reflist}} like template at the bottom &lt;br /&gt;
			res = res .. frame:expandTemplate{ title = &amp;#039;Hidden&amp;#039;, args = { &amp;#039;Sources&amp;#039;, frame:expandTemplate{ title = &amp;#039;notelist&amp;#039;, args = {} } } }&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
-----------------------&lt;br /&gt;
-- End creating tree --&lt;br /&gt;
-----------------------&lt;br /&gt;
    return res&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>imported&gt;Was a bee</name></author>
	</entry>
</feed>