local string s=mw.ustring.format("%s",S102)
if party_list[s] ~=nil then
party_list[s]=tonumber(party_list[s])+1;
else
party_list[s]=1;
end  
end  
end  
local max_val, key = -math.huge  
for k, v in pairs(party_list) do
if v > max_val then
max_val, key = v, k
end
end  
if key~=nil then
_shortname=getShortName('Q'..key);
if mw.wikibase.sitelink('Q'..key)~=nil then
party=mw.ustring.format("%s", mw.wikibase.sitelink('Q'..key), _shortname or mw.wikibase.label('Q'..key) or '')
elseif mw.wikibase.label('Q'..key)~=nil then
party=mw.ustring.format("%s", mw.wikibase.label('Q'..key))
else
party=mw.ustring.format(":d:%s", 'Q'..key)
end  
return mw.ustring.format("%s \(%s %s\)",party, max_val,label.Times)
else
return ""
end
end  
function getImage(_entity)
local property=getPropertyByEntity(_entity, 'P242' )
if property~=nil then
return mw.ustring.format( "", property)
else
return ''
end
end  
function getImageLegend(_entity)  
--lang=mw.language.getContentLanguage.code  
local propertyID='P242' --locator map image
local qualifierID='P2096' --image legend
local legend ="Image Legend qualifier
missing at d:".. _entity.id ..":P242:P2096";
if _entity.claims[propertyID] then
for k, v in pairs(_entity.claims[propertyID]) do
if v.qualifiers~=nil and v.qualifiers[qualifierID] then
for k2, v2 in pairs(v.qualifiers[qualifierID]) do
if v2.datatype=='monolingualtext' and v2.datavalue ~=nill and v2.datavalue.value['language']==lang then
legend=v2.datavalue.value['text'];
end
end
end
end
end
return legend;
end  
local function getElectors(_entity)
local property=getPropertyByEntity(_entity, 'P1867' )--eligible voters
if property~=nil then
return mw.ustring.format( "%s", property)
else
return ''
end
end  
local function assemblylist(_entity)
--has part P527
--local Qid="Q7785493"
local propertyID ="P527"--has part
--local entity = mw.wikibase.getEntityObject(Qid)
local str=""
if _entity.claims[propertyID] ~= nil then
for k, v in pairs(_entity.claims[propertyID]) do
if str ~="" then str= str.."
" end
local id='Q' ..v.mainsnak.datavalue.value['numeric-id']
local series=getProperty(id,'P1545')
if series~=nil then series=series ..". " end
-- str =str .. (series or '') .. mw.ustring.format( "%s", mw.wikibase.sitelink(id), mw.wikibase.label(id))  
if mw.wikibase.sitelink(id)~=nil and mw.wikibase.label(id) ~=nil then
str =str .. mw.ustring.format( "%s", mw.wikibase.sitelink(id), mw.wikibase.label(id))
elseif mw.wikibase.sitelink(id)~=nil then
str =str .. mw.ustring.format( "%s", mw.wikibase.sitelink(id))
elseif mw.wikibase.label(id)~=nil then
str =str .. mw.ustring.format( "%s", mw.wikibase.label(id))
else
str =str .. mw.ustring.format( ":d:%s", id)
end  
end
-- print(str)
end
return str;  
end  
function getShortName(_id)
local _entity = mw.wikibase.getEntityObject(_id)
if _entity==nil then return ;
elseif _entity.claims==nil then return end;
local propertyID='P1813'
if _entity.claims[propertyID] then
for k, v in pairs(_entity.claims[propertyID]) do
--print(mw.dumpObject(v.mainsnak.datavalue.value))
if(v.mainsnak.datavalue.value.language==lang)then
return v.mainsnak.datavalue.value.text;
end  
end  
end
end
local function setarg(k, v)
if(v and v ~= '') then args[k] = v end
end  
local function loadLokSabha(,s)  
local yearstr = string.sub(s,1,4)
local Elected_Year =.args.Elected_Year
QId=string.sub(s,5) or QId
local _entity = mw.wikibase.getEntityObject(QId)
local lokassmlist = assemblylist(_entity)
local propertyID='P585' --point in time
local elec_year
if _entity.claims[propertyID] then
local elyear={}
local pattern = "+(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)Z"  
pos=1
for k, v in pairs(_entity.claims[propertyID]) do
local val=mw.ustring.format("%s%s%s",v.mainsnak.datavalue.value.time:match(pattern))
elyear[pos]=val
pos=pos+1
end  
local max_val, key = -math.huge  
for k, v in pairs(elyear) do  
if tonumber(v) > tonumber(max_val) then
max_val, key = v, k
end
end
P585Qual = _entity.claims[propertyID][key]['qualifiers']
elec_year = string.sub(max_val,1,4)
if P585Qual['P2502']~=nil then P2502=P585Qual['P2502'][1].datavalue.value['numeric-id']; end
if P585Qual['P102'] ~=nil then P102 =P585Qual['P102'][1].datavalue.value['numeric-id']; end
if P585Qual['P805'] ~=nil then P805 ='Q'..P585Qual['P805'][1].datavalue.value['numeric-id']; end
if P585Qual['P991'] ~=nil then P991 ='Q'..P585Qual['P991'][1].datavalue.value['numeric-id']; end
if P585Qual['P1867'] ~=nil then P1867 =P585Qual['P1867'][1].datavalue.value['amount']; end
if P585Qual['P1868'] ~=nil then P1868 =P585Qual['P1868'][1].datavalue.value['amount']; end
end  
setarg('title',getwikidatalabel(QId))
setarg('image', getImage(_entity) )
setarg('caption', getImageLegend(_entity) or '')
setarg('Existence', getExistence(_entity) or '')
setarg('Reservation', getReserved(P2502) or'')
setarg('CurrMP', getCurrentMP(P991))
setarg('Party', getPartyLink(P102))
setarg('Yr', getElectionYear(P805, elec_year) or Elected_Year or '')
setarg('State', getPropertyByEntity(_entity,'P131'))--dargs['State']
setarg('TotElec', comma_value(P1867))--dargs['TotElec']
setarg('SuccParty', getMostSuccessfulParty(_entity) or'')
--setarg('Category', dargs['Category'] or '')
setarg('AssList',lokassmlist or '')  
end  
local function infobox
local abovestr =args['title'] or ''
local bgcolor = args['background'] or args['bgcolour'] or ''  
return :expandTemplate{ title = 'infobox', args = {
["bodyclass"] = "vevent",
["bodystyle"] = "width:25em; text-align:; font-size:90%;",
["above"] = mw.ustring.format( "%s [ edit ]",abovestr ,QId),
["aboveclass"] = "summary",
["abovestyle"] = "padding-bottom:0.25em; background:" .. bgcolor .. "; line-height:1.2em; text-align:; font-size:115%;",
------------------ Images and maps ------------------
["image"] = args['image'] or '',
["imagestyle"] = "padding-bottom:0.5em;",
["caption"] = args['caption'] or '',
["captionstyle"] = "padding-top:0.1em; line-height:1.2em; font-size:90%;",
["headerstyle"] = "background:#eee; font-size:105%;",
["labelstyle"] = "padding:0 0.5em 0 0; line-height:1.1em;",
["datastyle"] = "padding:0; line-height:1.2em; vertical-align:middle;",
-- ["label1"] = 'Title',
-- ["data1"] = args['title'] or '',  
["label3"] = label.Existence,
["data3"] = args['Existence'] or '',  
["label4"] = label.Reservation,
["data4"] = args['Reservation'] or '',  
["label5"] = label.CurrentMP,
["data5"] = args['CurrMP'] or '',  
["label6"] = label.Party,
["data6"] = args['Party'] or '',  
["label7"] = label.ElectedYear,
["data7"] = args['Yr'] or '',  
["label8"] = label.State,
["data8"] = args['State'] or '',  
["label9"] = label.TotalElectors,
["data9"] = args['TotElec'] or '',  
["label10"] = label.MostSuccessfulParty,
["data10"] = args['SuccParty'] or '',  
["label11"] = label.Category,
["data11"] = args['Category'] or '',  
["label12"] = label.AssemblyConstituencies,
["data12"] = args['AssList'] or '',  
} }  
end  
--fetch current elected member
function loksabha.memberpartyref
args = require('Module:Arguments').getArgs(, {
wrappers = 'Template:Infobox lok sabha constituency1' --test
})  
if( args['2'] and args['2'] ~= '') then
loadloksabhadb(,.args.year .. args['1'])
elseif( args['1'] and args['1'] ~= '') then  
local entity=mw.wikibase.getEntityObject
if entity then
--return entity.metatable.id
--return mw.dumpObject(entity)
end
--page id
if(entity ) then
if entity.id then QId = entity.id; end
end  
loadLokSabha(,.args.year .. args['1'])
end  
if(args['CurrMP'] and args['CurrMP'] ~="") then
return (args['CurrMP'] or *) .. '(<small>'.. (args['Party'] or *) ..'</small>)'
else
return ""
end
--infobox
end  
--fetch current elected member
function loksabha.currentmember
args = require('Module:Arguments').getArgs(, {
wrappers = 'Template:Infobox lok sabha constituency1' --test
})  
if( args['2'] and args['2'] ~= '') then
loadloksabhadb(,.args.year .. args['1'])
elseif( args['1'] and args['1'] ~= '') then
--loadloksabhadb(,.args.year .. args['1'])
local entity=mw.wikibase.getEntityObject
if entity then
--return entity.metatable.id