local loksabha = {}
local args = {}
local QId;
local label =mw.loadData('Module:Lok_sabha_constituency/labels')
local lang=mw.language.getContentLanguage.code  
local function ifnotempty(s,a,b)
if (s and s ~= '') then
return a
else
return b
end
end  
local function ifexist(page)
if not page then return false end
if mw.title.new(page).exists then return true end
return false
end  
local function getwikidatalabel(id)
--wikidata label
return mw.wikibase.label(id) or id
end  
local function getwikilink(id)
if ifexist(mw.wikibase.sitelink(id)) then
return *.. mw.wikibase.label(id) ..*
else
return mw.wikibase.label(id) or id
end
end  
--comma
function comma_value(amount)
if amount==nil then return end
local formatted = tonumber(amount)
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k==0) then
break
end
end
return formatted
end  
--print(getProperty('Q1255973','P159'))
function getProperty(id, propertyName )
local entity = mw.wikibase.getEntityObject(id)
return getPropertyByEntity(entity,propertyName);  
end --getproperty
function getPropertyByEntity(_entity, propertyName )
local entity = _entity; -- mw.wikibase.getEntityObject(id)
if not entity or not entity.claims then return end--the entity doesnt exist or have no claims
local property = entity.claims[propertyName]
if not property then return end--no such property for this item  
--lang=mw.language.getContentLanguage
i=1
default_lang='en'
local default_text=''
for _ in ipairs(property) do  
propy = property[i]
i=i+1
local propValue = propy.mainsnak and propy.mainsnak.datavalue
local propType = propy.mainsnak and propy.mainsnak.datatype
if not propValue then return end --property doesnt exist  
if propValue and propValue['type'] == 'monolingualtext' and propValue.value['language'] == default_lang then
default_text = propValue.value['text']..mw.ustring.format( "update at :%s", _entity.id, _entity.id..':'.. propertyName )
end  
if propValue['type'] == 'wikibase-entityid' then
local linkTarget = mw.wikibase.sitelink( "Q" .. propValue.value['numeric-id'] )
local linkTitle = mw.wikibase.label( "Q" ..propValue.value['numeric-id'] )
return linkTarget and linkTitle and mw.ustring.format( "%s", linkTarget, linkTitle )
--return linkTitle  
elseif propValue and propValue['type'] == 'monolingualtext' and propValue.value['language'] == lang then
return propValue.value['text']
elseif propType and (propType == 'commonsMedia' or propType=='string') then
return propValue['value']
elseif propType and propType == 'quantity' then
return tonumber(propValue.value['amount'])
end --if  
end --for  
return default_text; --incase value for other language missing  
end --getproperty  
function getQualifierDateValue(_entity,propertyID,qualifierID)
local pattern = "+(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)Z"
if _entity.claims[propertyID] ~= nil then
local out = {}
for k, v in pairs(_entity.claims[propertyID]) do
if v.qualifiers and v.qualifiers[qualifierID] ~= nil then
for k2, v2 in pairs(v.qualifiers[qualifierID]) do
if v2.datatype=='time' and v2.datavalue ~=nill then
local xyear=v2.datavalue.value['time']:match(pattern)
--print(xyear)  
table.insert(out ,xyear)
elseif v2.datatype=="time" and v2.snaktype=="novalue" then  
table.insert(out, v2.snaktype)
end
end
end
end
return out
else
return ""
end  
end  
function getExistence(_entity)
local a={}
local b={}
a=getQualifierDateValue(_entity,'P31','P580'); --instance of / start time
b=getQualifierDateValue(_entity,'P31','P582');--instance of / end time  
table.sort(a)
table.sort(b)  
local str="";  
if a~=nil then
for k,v in ipairs(a) do
if str ~="" then str= str.."
" end
if tonumber(v) and tonumber(b[k]) and tonumber(v)<tonumber(b[k]) then
str=tostring(str) .. tostring(v) .." - ".. tostring(b[k])
elseif tonumber(b[k]) ==nil then
if b[k]=="novalue" or b[k]==nil then b[k]= label.Present; end --mw.wikibase.label("Q193168") or "[[d:Q193168|]]" end --present
str=tostring(str) .. tostring(v) .." - ".. tostring(b[k])
end
end
else
str="Start Time Property value
missing at d:".. _entity.id ..":P31:P580"
end  
--print(str)
return(str)
end  
--print(getPropertyValue('Q7785493','P991'))
function getPropertyValue(id, propertyName )
local _entity = mw.wikibase.getEntityObject(id)  
return getPropertyValueByEntity(_entity,propertyName)  
end --getPropertyValueById  
function getPropertyValueByEntity(_entity, propertyName )
if not _entity or not _entity.claims then return end--the entity doesnt exist or have no claims
local property = _entity.claims[propertyName]
if not property then return end--no such property for this item  
--local lang=mw.language.getContentLanguage.code
i=1  
for _ in ipairs(property) do  
propy = property[i]
i=i+1
local propValue = propy.mainsnak and propy.mainsnak.datavalue
local propType = propy.mainsnak and propy.mainsnak.datatype
if not propValue then return end --property doesnt exist  
-- print(mw.dumpObject(propValue))
if propValue['type'] == 'wikibase-entityid' then
local numericid = "Q" .. propValue.value['numeric-id']  
return numericid
elseif propValue and propValue['type'] == 'monolingualtext' and propValue.value['language'] == lang then
return propValue.value['text']
else
return
end --if  
end --for  
return default_text; --incase value for other language missing  
end --getPropertyValueByEntity  
local function getReserved(classification)
--local classification=getPropertyValueByEntity(_entity,'P2502') --P2502:classification
if classification == nill then return end
classification='Q'..classification
local scst=getPropertyValue(classification,'P361') --P361 :partof
--print(scst)
if ifexist(mw.wikibase.sitelink(scst)) then
_shortname=getShortName(classification);
if _shortname~=nil then --P1813:short name
return mw.ustring.format( "%s", mw.wikibase.sitelink(scst), mw.wikibase.label(classification))
else
return mw.ustring.format( "%s", mw.wikibase.sitelink(scst))
end
else
return mw.wikibase.label(classification) or classification
end
end
--print(getReserved('22813282'))  
function getPartyLink(partyid)--party of successful candidate
local str="Qualifier Political party (102) is missing under P585 in d:".. QId .."";
if partyid==nil then return str end;
--local partyid=getPropertyValue(id,'P102')--member of political party
partyid='Q'..partyid
if ifexist(mw.wikibase.sitelink(partyid)) then
_shortname=getShortName(partyid);
if _shortname~=nil then --short name
str= mw.ustring.format( "%s", mw.wikibase.sitelink(partyid), _shortname)
else
str= mw.ustring.format( "%s", mw.wikibase.sitelink(partyid))
end
else
str= mw.wikibase.label(partyid) or "Political party (P102) is missing at d:".. QId .."";
end  
return str
end
function getCurrentMP(_id)
local propertyID ="P991" --successful candidate
local str="Current MP (Successful candidate - P991) name is missing at d:".. QId .."";
if _id==nil then return str;  
elseif ifexist(mw.wikibase.sitelink(_id)) then
str=mw.ustring.format( "%s", mw.wikibase.sitelink(_id), mw.wikibase.label(_id))  
elseif mw.wikibase.label(_id) then
str= mw.ustring.format( "%s", mw.wikibase.label(_id))
else
str=_id
end  
return str;
end  
function getElectionYear(_id,elec_year)
if _id==nil or _id=='Q' then
return nil;
elseif mw.wikibase.sitelink(_id)~=nil then
return mw.ustring.format( "%s", mw.wikibase.sitelink(_id), elec_year)
elseif mw.wikibase.label(_id) then
return mw.ustring.format( "%s", mw.wikibase.label(_id), elec_year)
else
return nil
end
end  
function getMostSuccessfulParty(_entity)  
party_list={}
local propertyID='P585' --point in time
if _entity.claims[propertyID] then
local pattern="+(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)Z"  
for k, v in pairs(_entity.claims[propertyID]) do
P585Qual = v['qualifiers'];  
if P585Qual~=nil and P585Qual['P102']~=nil then S102=P585Qual['P102'][1].datavalue.value['numeric-id']; end --classification
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