Module:Video game wikidata/sandbox
Appearance
From Wikipedia, the free encyclopedia
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Warning This Lua module is used on approximately 17,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.
Usage
[edit ]{{#invoke:Video game wikidata/sandbox|getSystemAlias|<numeric-id>}}
{{#invoke:Video game wikidata/sandbox|getSystemID|<alias>}}
{{#invoke:Video game wikidata/sandbox|getAggregatorAlias|<numeric-id>}}
{{#invoke:Video game wikidata/sandbox|getAggregatorID|<alias>}}
{{#invoke:Video game wikidata/sandbox|getReviewerAlias|<numeric-id>}}
{{#invoke:Video game wikidata/sandbox|getReviewerID|<alias>}}
The above documentation is transcluded from Module:Video game wikidata/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
localDate=require('Module:Date')._Date localyesno=require('Module:Yesno') localp={} -- Local variables. localreviewer=nil; localdf="mdy"; localentity=nil; localgenRefs=true; localshowSystem=true; localshowUpdateLink=true; localsystem=nil; localsystemId=nil; localsystemFormat="colon"; localupdateLinkStyle=nil; localentities={}; -- Translation table for converting numeric-IDs to shorthand aliases. localsystemAliases={ [10677]='PS1', [1323662]='PS1',-- Placeholder, this is actually the series but could be mistakenly used for PS1. [10680]='PS2', [10683]='PS3', [5014725]='PS4', [16338]='PC', [8079]='Wii', [56942]='WiiU', [132020]='XBOX', [48263]='X360', [13361286]='XONE', [203597]='3DS', [188808]='PSV', [170323]='DS',-- Sometimes has been NDS [170325]='PSP', [48493]='IOS',-- iOS, iPhone, iPad [94]='AND',-- Android [186437]='GB', [188642]='GBA', [203992]='GBC', [184198]='DC', [200912]='SAT', [172742]='NES', [183259]='SNES', [184839]='N64', [182172]='GC',-- Sometimes has been NGC [19610114]='NS',-- Nintendo Switch [98973368]='XSX/S',-- Xbox Series X and Series S (Output label differs from the input parameter, XSXS, per RFC) [63184502]='PS5' } -- Translation table for converting system aliases to QIDs localsystemIDs={ ['PS1']=10677, ['PS2']=10680, ['PS3']=10683, ['PS4']=5014725, ['PC']=16338, ['WII']=8079, ['WIIU']=56942, ['XBOX']=132020, ['X360']=48263, ['XONE']=13361286, ['3DS']=203597, ['PSV']=188808, ['DS']=170323, ['NDS']=170323, ['PSP']=170325, ['IOS']=48493, ['AND']=94, ['GB']=186437, ['GBA']=188642, ['GBC']=203992, ['DC']=184198, ['SAT']=200912, ['NES']=172742, ['SNES']=183259, ['N64']=184839, ['GC']=182172, ['NGC']=182172, ['NS']=19610114, ['XSXS']=98973368,-- This is the template parameter, which cannot contain a / or | character. ['PS5']=63184502 } -- List of accepted aggregator arguments and their related QID. localaggregatorAliases={ [150248]='MC', [40160]='GR', [21039459]='OC' } -- List of accepted aggregator arguments and their related QID. localaggregatorIDs={ ['MC']=150248, ['GR']=40160, ['OC']=21039459 } -- List of accepted reviewer arguments and their related QID. localreviewerAliases={ [591573]='FAM', [207708]='IGN' } -- List of accepted reviewer arguments and their related QID. localreviewerIDs={ ['FAM']=591573, ['IGN']=207708 } localfunctionsortByPlatform(a,b) localplatformA=""; localplatformB=""; if(a['qualifiers']['P400']~=nilanda['qualifiers']['P400'][1]~=nil)then platformA=p.getSystemAlias(a['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); if(platformA==nil)then platformA=mw.wikibase.label('Q'..a['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); end; end; if(b['qualifiers']['P400']~=nilandb['qualifiers']['P400'][1]~=nil)then platformB=p.getSystemAlias(b['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); if(platformB==nil)then platformB=mw.wikibase.label('Q'..b['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); end; end; returnplatformA<platformB end; localfunctionbuildCite(reference) localreferenceUrl=nil; localcite=nil; if(reference['snaks']['P854']~=nilandreference['snaks']['P854'][1]~=nil)then referenceUrl=reference['snaks']['P854'][1]['datavalue']['value']; end; if(referenceUrl~=nilandreferenceUrl~="")then cite="{{cite web|url="..referenceUrl; localpubdate=nil; localaccessdate=nil; localpublisher=nil; localwork=nil; localtitle=nil; localarchiveUrl=nil; localarchiveDate=nil; localauthors={}; if(reference['snaks']['P577']~=nilandreference['snaks']['P577'][1]~=nil)then pubdate=reference['snaks']['P577'][1]['datavalue']['value']['time']; end; if(reference['snaks']['P813']~=nilandreference['snaks']['P813'][1]~=nil)then accessdate=reference['snaks']['P813'][1]['datavalue']['value']['time']; end; if(reference['snaks']['P123']~=nilandreference['snaks']['P123'][1]~=nil)then publisher=mw.wikibase.label('Q'..reference['snaks']['P123'][1]['datavalue']['value']['numeric-id']); end; if(reference['snaks']['P1433']~=nilandreference['snaks']['P1433'][1]~=nil)then work=mw.wikibase.label('Q'..reference['snaks']['P1433'][1]['datavalue']['value']['numeric-id']); end; if(reference['snaks']['P1476']~=nilandreference['snaks']['P1476'][1]~=nil)then title=reference['snaks']['P1476'][1]['datavalue']['value']['text']; end; if(reference['snaks']['P1065']~=nilandreference['snaks']['P1065'][1]~=nil)then archiveUrl=reference['snaks']['P1065'][1]['datavalue']['value']; end; if(reference['snaks']['P2960']~=nilandreference['snaks']['P2960'][1]~=nil)then archiveDate=reference['snaks']['P2960'][1]['datavalue']['value']['time']; end; if(reference['snaks']['P50']~=niland#reference['snaks']['P50']>0)then fori,authorDatinpairs(reference['snaks']['P50'])do localauthorQid='Q'..authorDat['datavalue']['value']['numeric-id']; if(entities[authorQid]==nil)then entities[authorQid]=mw.wikibase.getEntity(authorQid); end; localauthor={}; author['fullname']=mw.wikibase.label(authorQid);-- Default to label author['first']=nil; author['last']=nil; if(entities[authorQid]['claims']['P735']~=nilandentities[authorQid]['claims']['P735'][1]~=nil)then author['first']=mw.wikibase.label('Q'..entities[authorQid]['claims']['P735'][1]['mainsnak']['datavalue']['value']['numeric-id']); end; if(entities[authorQid]['claims']['P734']~=nilandentities[authorQid]['claims']['P734'][1]~=nil)then author['last']=mw.wikibase.label('Q'..entities[authorQid]['claims']['P734'][1]['mainsnak']['datavalue']['value']['numeric-id']); end; table.insert(authors,author); end; end; if(title~=nilandtitle~="")then cite=cite.."|title="..title; end; if(publisher~=nilandpublisher~="")then cite=cite.."|publisher="..publisher; end; if(work~=nilandwork~="")then cite=cite.."|work="..work; end; if(pubdate~=nilandpubdate~="")then localpubdateText=Date(pubdate):text(df); cite=cite.."|date="..pubdateText; end; if(accessdate~=nilandaccessdate~="")then localaccessdateText=Date(accessdate):text(df); cite=cite.."|accessdate="..accessdateText; end; if(archiveUrl~=nilandarchiveUrl~=""andarchiveDate~=nilandarchiveDate~="")then localarchivedateText=Date(archiveDate):text(df); cite=cite.."|archiveurl="..archiveUrl; cite=cite.."|archivedate="..archivedateText; end; if(#authors>0)then fori,authorinpairs(authors)do if(author['first']~=nilandauthor['last']~=nilandauthor['first']~=""andauthor['last']~="")then if(#authors==1)then cite=cite.."|last="..author['last'].."|first="..author['first']; else cite=cite.."|last"..i.."="..author['last'].."|first"..i.."="..author['first']; end; else if(#authors==1)then cite=cite.."|author="..author['fullname']; else cite=cite.."|author"..i.."="..author['fullname']; end; end; end; end; cite=cite..'}}'; end; returncite; end; localfunctionprintReviewRow(frame,reviewscore) localscore=nil; if(reviewscore['mainsnak']['datavalue']~=nilandreviewscore['mainsnak']['datavalue']['value']~=nil)then score=reviewscore['mainsnak']['datavalue']['value']; else return""; end; localret="" localsystem=nil; localreference=nil; if(reviewscore['qualifiers']['P400']~=nilandreviewscore['qualifiers']['P400'][1]~=nil)then system=p.getSystemAlias(reviewscore['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']); end if(system~=nilandsystem~=""andshowSystem)then if(systemFormat=="para")then ret=ret.."("..system..") "; else ret=ret..system..": "; end; end; ret=ret..score; if(reviewscore['references']~=nilandreviewscore['references'][1]~=nilandgenRefs)then localcite=buildCite(reviewscore['references'][1]); if(cite~=nil)then localscoreBy=p.getAggregatorAlias(reviewscore['qualifiers']['P447'][1]['datavalue']['value']['numeric-id']); if(scoreBy==nil)then scoreBy=p.getReviewerAlias(reviewscore['qualifiers']['P447'][1]['datavalue']['value']['numeric-id']); end; localname=entity:getLabel()..'-'..scoreBy; if(system~=nilandsystem~="")then name=name..system; end; cite=frame:extensionTag{name="ref",args={name=name},content=cite}; ret=ret..cite; end; end; returnret.."<br />"; end functionp.getSystemAlias(numericId) returnsystemAliases[numericId]; end functionp.getSystemID(system) returnsystemIDs[system]; end functionp.getAggregatorAlias(numericId) returnaggregatorAliases[numericId]; end functionp.getAggregatorID(system) returnaggregatorIDs[system]; end functionp.getReviewerAlias(numericId) returnreviewerAliases[numericId]; end functionp.getReviewerID(system) returnreviewerIDs[system]; end functionp.setReviewer(iReviewer) -- No reviewer, stop. Must have reviewer at least. if(iReviewer==niloriReviewer=="")then return"Missing reviewer"; end; -- See if supplied reviewer is in the aggregator table. iReviewer=string.upper(iReviewer) reviewer=p.getAggregatorID(iReviewer); if(reviewer==nilorreviewer=="")then -- No? Maybe in the reviewer table. reviewer=p.getReviewerID(iReviewer); if(reviewer==nilorreviewer=="")then return"Invalid reviewer"; end; end; returnnil; end; functionp.setDateFormat(iDf) -- Check for a date format parameter. Default to mdy if missing. if(iDf~=nilandiDf~="")then df=string.lower(iDf); end; end; functionp.setSystemFormat(iSf) if(iSf~=nilandiSf~="")then systemFormat=string.lower(iSf); end; end; functionp.setUpdateLinkStyle(iStyle) if(iStyle~=nilandiStyle~="")then updateLinkStyle=string.lower(iStyle); end; end; functionp.setGame(iGame) -- Check for a game parameter. If missing, default to current article. if(iGame~=nilandiGame~="")then if(entities[iGame]==nilandmw.wikibase~=nil)then entities[iGame]=mw.wikibase.getEntity(iGame); end; entity=entities[iGame] else -- Need to research if we can determine the entity's ID before retrieving it. if(mw.wikibase~=nil)then entity=mw.wikibase.getEntity(); if(entity~=nil)then entities[entity['id']]=entity; end; end; end; if(entity==nil)then return"No matching wikidata entity found"; end; returnnil; end; functionp.setSystem(iSystem) -- Check for system parameter, and resolve it's QID if possible. if(iSystem~=nilandiSystem~="")then system=string.upper(iSystem); systemId=p.getSystemID(system); elseif(notshowSystem)then -- If no system was specified, force showSystem on. showSystem=true; end; end; functionp.setGenerateReferences(iGenRefs) -- Reference suppression. if(iGenRefs~=nilandiGenRefs~="")then genRefs=yesno(iGenRefs,true); end; end; functionp.setShowSystem(iShowSystem) -- Suppression of system aliases in front of score, i.e. (XBOX) xx/100. if(iShowSystem~=nilandiShowSystem~="")then showSystem=yesno(iShowSystem,false); end; if(system==nilorsystem=='')then -- If no system was specified, force showSystem on. showSystem=true; end; end; functionp.setShowUpdateLink(iShowUpdateLink) -- Suppression of update link to Wikidata at the end of the score, i.e. (XBOX) xx/100[+]. if(iShowUpdateLink~=nilandiShowUpdateLink~="")then showUpdateLink=yesno(iShowUpdateLink,false); end; end; functionp.getUpdateLink() if(updateLinkStyle=="pen")then return"[[File:Blue pencil.svg|frameless|text-top|10px|alt=Edit this on Wikidata|link=https://www.wikidata.org/wiki/"..entity['id'].."?uselang="..mw.language.getContentLanguage().code.."#P444|Edit this on Wikidata]]"; elseif(updateLinkStyle=="noSub")then return'[[d:'..entity['id']..'#P444|[±]]]'; elseif(updateLinkStyle=="text and pen")then return'<span style="position: relative;"><span style="position: absolute; right: 0;">[[File:Blue pencil.svg|10px|baseline|link=|alt=]]</span>[[d:'..entity['id']..'#P444|<span style="position: relative; padding-right: 14px;">Edit on Wikidata</span>]]</span>' end; return'<sub>[[d:'..entity['id']..'#P444|[±]]]</sub>'; end; functionp.getSitelink() returnmw.wikibase.sitelink(entity['id']); end; functionp.getLabel() returnmw.wikibase.label(entity['id']); end; functionp.getParts() localret={}; -- Loop all of "has Part" for this title localparts=entity['claims']['P527']; if(parts)then fori,partinpairs(parts)do table.insert(ret,"Q"..part['mainsnak']['datavalue']['value']['numeric-id']); end; end; returnret; end; functionp.getEarliestPublicationDate() localret={}; localpubDates=entity['claims']['P577']; if(pubDates)then fori,pubDateinpairs(pubDates)do if(pubDate['mainsnak']['datavalue'])then localtimestamp=pubDate['mainsnak']['datavalue']['value']['time']; localaccessdate=Date(timestamp); table.insert(ret,accessdate); end; end; end; if(#ret<1)then returnnil; end; table.sort(ret); returnret[1]; end; functionp.printReviewScores(frame) localret=""; -- Loop all of "review scores" for this title localreviewscores=entity['claims']['P444']; if(reviewscores)then -- Find reviews that qualify for printing and insert into array. localreviewsToPrint={} fori,reviewinpairs(reviewscores)do if(review['qualifiers']~=nil)then localscoreBy=nil if(review['qualifiers']['P447']~=nilandreview['qualifiers']['P447'][1]~=nil)then scoreBy=review['qualifiers']['P447'][1]['datavalue']['value']['numeric-id']; end; if(scoreBy==reviewer)then -- If template specified a system, we need to check for the specific system and only output that one. if(system==nilorsystem=="")then -- No system specified, so output each one found. table.insert(reviewsToPrint,review); else -- Get platform if it exists. if(review['qualifiers']['P400']~=nilandreview['qualifiers']['P400'][1]~=nil)then -- Try to match based on QID. localreviewSysId=review['qualifiers']['P400'][1]['datavalue']['value']['numeric-id']; if(systemId==reviewSysId)then table.insert(reviewsToPrint,review); else -- If that failed, try to match based on label. localsystemName=mw.wikibase.label('Q'..reviewSysId); if(systemName~=nilandstring.upper(systemName)==system)then table.insert(reviewsToPrint,review); end; end; end; end; end; end; end; -- Sort the array by platform label. table.sort(reviewsToPrint,sortByPlatform); -- If a system was not specified, showSystem has defaulted to true. If this title only has one platform and one review, we will turn it off. -- Note: If the title has zero or more platforms defined, we leave showSystem on. We are unable to determine if this is a single-platform game. --if((system == nil or system == "") and #reviewsToPrint == 1 and entity['claims']['P400'] ~= nil and #entity['claims']['P400'] == 1) then -- Simplifying this based on discussion at [Template:Video game reviews]. If there's only one review, don't display system unless explicitly requested. if((system==nilorsystem=="")and#reviewsToPrint==1)then showSystem=false; end; -- Print the reviews fori,reviewinipairs(reviewsToPrint)do ret=ret..printReviewRow(frame,review); end; end; if(ret~="")then ret=string.sub(ret,1,-7); elseif(notshowUpdateLink)then ret=nil; end; -- Add edit link at end if showUpdateLink is on. if(showUpdateLink)then ret=ret..p.getUpdateLink(); end; returnret; end; returnp