Movatterモバイル変換


[0]ホーム

URL:


Jump to content
Outreach Wiki
Search

Module:Wd

From Outreach Wiki
Module documentation[create] [purge]
You might want tocreate a documentation page for thisScribunto module.
Editors can experiment in this module's sandbox(create |mirror) and testcases(create) pages.
Add categories to the/doc subpage.Subpages of this module.
localp={}State={}State.__index=State-- allows for recursive callsfunctionState.new()localstt={}setmetatable(stt,State)stt.outPreferred={}stt.outNormal={}stt.outDeprecated={}stt.bestRank=truestt.foundRank=3stt.maxRank=nilstt.minRank=nilstt.linked=falsestt.propertyWithQualifier=falsestt.withUnit=falsestt.shortName=falsestt.singleValue=falsestt.langCode=mw.language.getContentLanguage().codestt.langObj=mw.language.new(stt.langCode)stt:setRankBoundaries("best")returnsttendfunctionState:unknownDatatypeError(type)return"<strong class=\"error\">Unknown or unsupported datatype '"..type.."'</strong>"endfunctionState:parseDate(dateStr,precision)precision=precisionor"d"locali,j,ptr,year,month,daylocalfrom=1ifdateStr:sub(1,1)=="-"then-- this is a negative number, look further aheadfrom=2endi,j=dateStr:find("-",from)ifithenyear=tonumber(mw.ustring.gsub(dateStr:sub(1,i-1),"^\+(.+)$","%1"),10)-- apparently, 0 can have a negative sign in Lua... so do this to get rid of itifyear==0thenyear=0endptr=i+1elseyear=nilendifprecision=="y"thenreturnyearendi,j=dateStr:find("-",ptr)ifithenmonth=tonumber(dateStr:sub(ptr,i-1),10)ptr=i+1elsemonth=nilendifprecision=="m"thenreturnyear,monthendi,j=dateStr:find("T",ptr)ifithenday=tonumber(dateStr:sub(ptr,i-1),10)elseday=nilendreturnyear,month,dayendfunctionState:convertUnit(unit,addLink)addLink=addLinkorfalselocali,j,itemID,label,targetifunit==""orunit=="1"thenreturnnilendifunit:match('^http[s]?://')theni,j=unit:find("Q")ifithenitemID=unit:sub(i)ifitemID=="Q11229"then-- 'percentage'return"%"elselabel=mw.wikibase.label(itemID)target=nilifaddLinkorlabel==nilthentarget=mw.wikibase.sitelink(itemID)endifaddLinktheniftargetthenreturn" ".."[["..target.."|"..(labelortarget).."]]"endifnotlabelthenreturn" ".."[[:d:"..itemID.."|"..itemID.."]]"endendreturn" "..(labelortargetoritemID)endendendreturn" "..unitendfunctionState:getShortName(itemID)returnp.property({args={"single",itemID,"P1813"}})-- 'short name'endfunctionState:getOrdinalSuffix(num)iftostring(num):sub(-2,-2)=='1'thenreturn"th"-- 10th, 11th, 12th, 13th, ... 19thendnum=tostring(num):sub(-1)ifnum=='1'thenreturn"st"elseifnum=='2'thenreturn"nd"elseifnum=='3'thenreturn"rd"elsereturn"th"endendfunctionState:getValue(snak,addUnit,addLink)addUnit=addUnitorfalseaddLink=addLinkorfalseifsnak.snaktype=='value'thenifsnak.datavalue.type=='string'thenreturnsnak.datavalue.valueelseifsnak.datavalue.type=='monolingualtext'thenifsnak.datavalue.value['language']==self.langCodethenreturnsnak.datavalue.value['text']elsereturnnilendelseifsnak.datavalue.type=='quantity'then-- strip + signs from frontlocalvalue=mw.ustring.gsub(snak.datavalue.value['amount'],"^\+(.+)$","%1")ifaddUnitthenlocalunit=self:convertUnit(snak.datavalue.value['unit'],addLink)ifunitthenvalue=value..unitendendreturnvalueelseifsnak.datavalue.type=='time'thenlocaly,m,d,p,i,j,yDiv,yRound,yFull,value,calendarIDlocalyFactor=1localsign=1localsuffix=""localmayAddCalendar=falselocalcalendar=""localprecision=snak.datavalue.value['precision']ifprecision==11thenp="d"elseifprecision==10thenp="m"elsep="y"yFactor=10^(9-precision)endy,m,d=self:parseDate(snak.datavalue.value['time'],p)ify<0thensign=-1y=y*signend-- if precision is tens/hundreds/thousands/millions/billions of yearsifprecision<=8thenyDiv=y/yFactor-- if precision is tens/hundreds/thousands of yearsifprecision>=6then-- if unit added then round centuries/millenniums up (e.g. 21st century or 3rd millennium, but 2010s for decade)mayAddCalendar=trueifaddUnitthenifprecision<=7thenyRound=math.ceil(yDiv)ifprecision==6thensuffix=" millennium"elsesuffix=" century"endsuffix=self:getOrdinalSuffix(yRound)..suffixelseyRound=math.floor(yDiv)*yFactorsuffix="s"endelseyRound=math.floor(yDiv)*yFactorendelselocalyReFactor,yReDiv,yReRound-- round to nearest for tens of thousands of years or moreyRound=math.floor(yDiv+0.5)ifyRound==0thenifprecision<=2andy~=0thenyReFactor=1e6yReDiv=y/yReFactoryReRound=math.floor(yReDiv+0.5)ifyReDiv==yReRoundthen-- change precision to millions of years only if we have a whole number of themprecision=3yFactor=yReFactoryRound=yReRoundendendifyRound==0then-- otherwise, take the unrounded (original) number of yearsprecision=5yFactor=1yRound=ymayAddCalendar=trueendendifprecision>=1andy~=0thenyFull=yRound*yFactoryReFactor=1e9yReDiv=yFull/yReFactoryReRound=math.floor(yReDiv+0.5)ifyReDiv==yReRoundthen-- change precision to billions of years if we're in that rangeprecision=0yFactor=yReFactoryRound=yReRoundelseyReFactor=1e6yReDiv=yFull/yReFactoryReRound=math.floor(yReDiv+0.5)ifyReDiv==yReRoundthen-- change precision to millions of years if we're in that rangeprecision=3yFactor=yReFactoryRound=yReRoundendendendifaddUnitthenifprecision==3thensuffix=" million years"elseifprecision==0thensuffix=" billion years"elseyRound=yRound*yFactorifyRound==1thensuffix=" year"elsesuffix=" years"endendelseyRound=yRound*yFactorendendelseyRound=ymayAddCalendar=trueendifmayAddCalendarthencalendarID=snak.datavalue.value['calendarmodel']ifcalendarID:match('^http[s]?://')theni,j=calendarID:find("Q")ifithencalendarID=calendarID:sub(i)ifcalendarID=="Q1985786"then-- 'Proleptic Julian calendar'ifaddUnitthenifaddLinkthencalendar=" ([[Julian calendar|Julian]])"elsecalendar=" (Julian)"endelsecalendar="/Julian"endendendendendifaddUnitthenlocalce=nilifsign<0thence="BCE"elseifprecision<=5thence="CE"endifcethenifaddLinkthence="[[Common Era|"..ce.."]]"endsuffix=suffix.." "..ceendvalue=tostring(yRound)ifmthenvalue=self.langObj:formatDate("F","1-"..m.."-1").." "..valueifdthenvalue=d.." "..valueendendvalue=value..suffix..calendarelsevalue=tostring(yRound*sign)ifmthenvalue=value.."-"..mifdthenvalue=value.."-"..dendendvalue=value..calendarendreturnvalueelseifsnak.datavalue.type=='wikibase-entityid'thenlocalvalue=""localtarget=nillocalitemID="Q"..snak.datavalue.value['numeric-id']ifself.shortNamethenvalue=self:getShortName(itemID)endifvalue==""thenvalue=mw.wikibase.label(itemID)endifaddLinkorvalue==nilthentarget=mw.wikibase.sitelink(itemID)endifaddLinktheniftargetthenvalue="[["..target.."|"..(valueortarget).."]]"elseifnotvaluethenvalue="[[:d:"..itemID.."|"..itemID.."]]"endelseifnotvaluethenvalue=(targetoritemID)endreturnvalueelsereturnself:unknownDatatypeError(snak.datavalue.type)endelseifsnak.snaktype=='somevalue'thenreturn"unknown"elseifsnak.snaktype=='novalue'thenreturn"none"elsereturnnilendendfunctionState:getRawValue(snak)localtemp,valueifsnak.snaktype=='value'andsnak.datavalue.type=='wikibase-entityid'thenreturn"Q"..snak.datavalue.value['numeric-id']elsereturnself:getValue(snak,false,false)endendfunctionState:snakEqualsValue(snak,value)ifsnak.snaktype=='value'thenlocalsnakValue=self:getRawValue(snak)ifsnakValueandsnak.datavalue.type=='wikibase-entityid'thenvalue=value:upper()endreturnsnakValue==valueelseifsnak.snaktype=='somevalue'thenifvalue==" "then-- single space represents 'somevalue'returntrueelsereturnfalseendelseifsnak.snaktype=='novalue'thenifvalue==""then-- empty value represents 'novalue'returntrueelsereturnfalseendelsereturnfalseendendfunctionState:setRankBoundaries(rank)localrankPosif(rank=="best")thenself.bestRank=trueself.foundRank=3returnelseself.bestRank=falseendif(rank=="all")thenself.maxRank=1self.minRank=3returnendif(rank:sub(1,9)=="preferred")thenrankPos=1elseif(rank:sub(1,6)=="normal")thenrankPos=2elseif(rank:sub(1,10)=="deprecated")thenrankPos=3endif(rank:sub(-1)=="+")thenself.maxRank=1self.minRank=rankPoselseif(rank:sub(-1)=="-")thenself.maxRank=rankPosself.minRank=3elseself.maxRank=rankPosself.minRank=rankPosendendfunctionState:convertRank(rank)if(rank=="preferred")thenreturn1elseif(rank=="normal")thenreturn2elseif(rank=="deprecated")thenreturn3elsereturn4-- default (in its literal sense)endendfunctionState:rankMatches(rankPos)ifself.bestRankthenifself.foundRank>rankPosthenself.foundRank=rankPos-- found a better rank, reset worse rank outputsifself.foundRank==1thenself.outNormal={}self.outDeprecated={}elseifself.foundRank==2thenself.outDeprecated={}endendreturnself.foundRank>=rankPos-- == would also work hereelsereturn(self.maxRank<=rankPosandrankPos<=self.minRank)endendfunctionState:appendOutput(value,rankPos)ifrankPos==1thenself.outPreferred[#self.outPreferred+1]=valueelseifrankPos==2thenself.outNormal[#self.outNormal+1]=valueelseifrankPos==3thenself.outDeprecated[#self.outDeprecated+1]=valueendendfunctionState:out()localout=""ifself.outDeprecated[1]thenifself.singleValuethenout=self.outDeprecated[1]elseout=table.concat(self.outDeprecated,", ")endendifself.outNormal[1]thenifself.singleValuethenout=self.outNormal[1]elseifout~=""thenout="; "..outendout=table.concat(self.outNormal,", ")..outendendifself.outPreferred[1]thenifself.singleValuethenout=self.outPreferred[1]elseifout~=""thenout="; "..outendout=table.concat(self.outPreferred,", ")..outendendreturnoutendfunctionState:processFlag(flag)ifflag=="linked"thenself.linked=truereturntrueelseifflag=="unit"thenself.withUnit=truereturntrueelseifflag=="short"thenself.shortName=truereturntrueelseifflag=="single"thenself.singleValue=truereturntrueelseifflag=="best"orflag=="all"orflag:match('^preferred[+-]?$')orflag:match('^normal[+-]?$')orflag:match('^deprecated[+-]?$')thenself:setRankBoundaries(flag)returntrueelsereturnfalseendendp.property=function(frame)local_=State.new()localentity,propertyID,claims,rankPos,valuelocalnextArg=mw.text.trim(frame.args[1]or"")localnextIndex=2while_:processFlag(nextArg)donextArg=mw.text.trim(frame.args[nextIndex]or"")nextIndex=nextIndex+1endifnextArg:sub(1,1):upper()=="Q"thenentity=mw.wikibase.getEntity(nextArg)propertyID=mw.text.trim(frame.args[nextIndex]or"")elseentity=mw.wikibase.getEntity()propertyID=nextArgendifentityandentity.claimsthenclaims=entity.claims[propertyID]endifclaimsthenfork,vinpairs(claims)dorankPos=_:convertRank(v.rank)if_:rankMatches(rankPos)thenvalue=_:getValue(v.mainsnak,_.withUnit,_.linked)ifvaluethen_:appendOutput(value,rankPos)endendendreturn_:out()elsereturn""endendp.qualifier=function(frame,_)_=_orState.new()localentity,propertyID,propertyValue,qualifierID,claims,qualifiers,rankPos,outValue,outInter,outQualifierlocalnextArg=mw.text.trim(frame.args[1]or"")localnextIndex=2while_:processFlag(nextArg)donextArg=mw.text.trim(frame.args[nextIndex]or"")nextIndex=nextIndex+1endifnextArg:sub(1,1):upper()=="Q"thenentity=mw.wikibase.getEntity(nextArg)propertyID=mw.text.trim(frame.args[nextIndex]or"")nextIndex=nextIndex+1elseentity=mw.wikibase.getEntity()propertyID=nextArgendnextArg=frame.args[nextIndex]nextIndex=nextIndex+1qualifierID=nextArgnextArg=mw.text.trim(frame.args[nextIndex]or"")nextIndex=nextIndex+1ifnextArg==""thenpropertyValue=nilqualifierID=mw.text.trim(qualifierIDor"")elsepropertyValue=qualifierID-- cannot be nil when reached; empty value represents 'novalue'ifpropertyValue~=""andmw.text.trim(propertyValue)==""thenpropertyValue=" "-- single space represents 'somevalue'elsepropertyValue=mw.text.trim(propertyValue)endqualifierID=nextArgendifentityandentity.claimsthenclaims=entity.claims[propertyID]endifclaimsthenfork,vinpairs(claims)dorankPos=_:convertRank(v.rank)ifpropertyValue==nilor_:snakEqualsValue(v.mainsnak,propertyValue)thenif_:rankMatches(rankPos)thenoutValue=niloutInter=niloutQualifier={}if_.propertyWithQualifierthenoutValue=_:getValue(v.mainsnak,_.withUnit,_.linked)endifv.qualifiersthenqualifiers=v.qualifiers[qualifierID]endif(not_.propertyWithQualifieroroutValue)andqualifiersthenfork2,v2inpairs(v.qualifiers[qualifierID])dooutInter=_:getValue(v2,_.withUnit,_.linked)ifoutInterthenifnot_.propertyWithQualifierthen_:appendOutput(outInter,rankPos)elseoutQualifier[#outQualifier+1]=outInterendendendendifoutValueand_.propertyWithQualifierthenoutQualifier=table.concat(outQualifier,", ")ifoutQualifier~=""thenoutQualifier=" <span style=\"font-size:smaller\">("..outQualifier..")</span>"outValue=outValue..outQualifierend_:appendOutput(outValue,rankPos)endendendendreturn_:out()elsereturn""endendp.propertyWithQualifier=function(frame)local_=State.new()_.propertyWithQualifier=true_.withUnit=truereturnp.qualifier(frame,_)endp.label=function(frame)local_=State.new()locallabel=""localtarget=""localnextArg=mw.text.trim(frame.args[1]or"")localnextIndex=2while_:processFlag(nextArg)donextArg=mw.text.trim(frame.args[nextIndex]or"")nextIndex=nextIndex+1endifnextArgthenifnextArg:sub(1,1):upper()=="Q"thenif_.shortNamethenlabel=_:getShortName(nextArg)endiflabel==""thenlabel=mw.wikibase.label(nextArg)endif_.linkedorlabel==nilthentarget=mw.wikibase.sitelink(nextArg)endif_.linkedandtargetthenlabel="[["..target.."|"..(labelortarget).."]]"endelselabel=mw.wikibase.label(nextArg)endreturn(labelortarget)elsereturnmw.wikibase.label()endendreturnp
Retrieved from "https://outreach.wikimedia.org/w/index.php?title=Module:Wd&oldid=204407"

[8]ページ先頭

©2009-2025 Movatter.jp