Module:CapacityExchange
Appearance
From Meta, a Wikimedia project coordination wiki
This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected.
This Lua module is part of the Capacity Exchange platform. It is designed to display user capacities on their user pages. The module retrieves user data and capacity information from toolforge:capx, process and generates the HTML content to present the user's known and wanted capacities.
Usage
[edit ]{{CapacityExchange}}
The above documentation is transcluded from Module:CapacityExchange/doc. (edit | history)
Editors can experiment in this module’s sandbox (edit | diff) and testcases (create) pages.
Please add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module’s sandbox (edit | diff) and testcases (create) pages.
Please add categories to the /doc subpage. Subpages of this module.
localusers=mw.ext.data.get("CapacityExchange/users.tab").data locallang=mw.getCurrentFrame():preprocess("{{Int:Lang}}") localcapacities=mw.ext.data.get("CapacityExchange/capacities.tab",lang).data localbadgesData=mw.ext.data.get("CapacityExchange/badges.tab").data localtmb=require'mw.ext.translate.messageBundle' localmb=tmb.new("Module:CapacityExchange/locale.json",lang) localis_rtl=require('Module:Is rtl') localp={} -- Get the text direction functionget_dir() ifis_rtl[lang]then return"rtl" else return"ltr" end end -- Create a mapping of capacity IDs to their names and descriptions. -- Export rows: [id:number, name:string, description:string, wikidata_item:string] localcapacityMap={} for_,capacityinipairs(capacities)do localid=tostring(capacity[1]) localnameVal=capacity[2]or'' localdescVal=capacity[3]or'' localkey=capacity[4]-- wikidata_item / QID capacityMap[id]={key=key,label=nameVal,description=descVal} end -- Create a mapping of badge IDs to their names and images localbadgeMap={} for_,badgeinipairs(badgesData)do -- badge schema: [id:number, name:string, image:string, base_url:string] localid=tostring(badge[1]) badgeMap[id]={name=badge[2],image=badge[3],base_url=badge[4]} end -- Function to get user data by username localfunctiongetUserData(username) for_,userDatainipairs(users)do ifuserData[1]==usernamethen return{ username=userData[1], skills_known=userData[2], skills_available=userData[3], badges=userData[4] } end end returnnil end -- Helper function to replace capacity IDs with names and descriptions localfunctionreplaceCapacities(capacityList,sectionPrefix) localresult={} -- Remove [] and split by comma capacityList=mw.ustring.gsub(capacityList,"%[","") capacityList=mw.ustring.gsub(capacityList,"%]","") for_,capacityIdinipairs(mw.text.split(capacityList,","))do localtrimmedId=mw.text.trim(capacityId) iftrimmedId~=""then localmeta=capacityMap[trimmedId] ifmetathen localtoggleId=sectionPrefix.."-"..meta.key table.insert(result, "<div class='capx-capacity'>".. "<div class='capx-name mw-customtoggle-"..toggleId.."'>"..meta.label.."</div>".. "<div class='capx-desc mw-collapsible mw-collapsed' id='mw-customcollapsible-"..toggleId.."'>"..meta.description.."</div>".. "</div>" ) else table.insert(result,"<div class='capx-unknown'>"..trimmedId.."</div>") end end end returnresult end -- Helper function to load badges localfunctionloadBadges(badgeList) localresult={} -- Remove [] and split by comma badgeList=mw.ustring.gsub(badgeList,"%[","") badgeList=mw.ustring.gsub(badgeList,"%]","") for_,badgeinipairs(mw.text.split(badgeList,","))do localparts=mw.text.split(mw.text.trim(badge),"§") localid=mw.text.trim(parts[1]or"") ifid~=""then localmeta=badgeMap[id] ifmetathen localhashCode=parts[2] localbadgeUrl=nil ifhashCodeandhashCode~=""andmeta.base_urlandmeta.base_url~=""then badgeUrl=meta.base_url..hashCode end ifnotbadgeUrlorbadgeUrl==""then badgeUrl="https://meta.wikimedia.org/wiki/Capacity_Exchange/User_Guide#Badges" end table.insert(result,"<div class='capx-badge'>") table.insert(result,"<table role='presentation'>") table.insert(result,"<tr>") table.insert(result,"<td>[[File:"..meta.image.."|45px]]</td>") table.insert(result,"<td>"..mw.getCurrentFrame():preprocess(mb:t("badge"):params(badgeUrl,meta.name,'{{PAGENAME}}'):plain()).."</td>") table.insert(result,"</tr>") table.insert(result,"</table>") table.insert(result,"</div>") else -- Unknown badge id table.insert(result,"<div class='capx-badge capx-unknown'>"..mb:t("unknown-badge"):params(id):plain().."</div>") end end end returnresult end -- Return the constructed table localfunctionconstructCapacitiesTable(knownCapacities,availableCapacities,badges) localcapacitiesTable={} locallocaleLink='https://meta.wikimedia.org/w/index.php?title=Special%3ATranslate&group=messagebundle-Module%3ACapacityExchange%2Flocale.json' localcapacitiesLink='https://meta.wikimedia.org/w/index.php?title=Special%3ATranslate&group=messagebundle-Module%3ACapacityExchange%2Fcapacities.json' table.insert(capacitiesTable,"<div class='capx-main' style='direction: "..get_dir()..";'>") table.insert(capacitiesTable,"<div class='capx-logo'>") table.insert(capacitiesTable,mb:t("help-translate-plus"):params(localeLink,capacitiesLink):plain()) table.insert(capacitiesTable,"</div>") if#availableCapacities>0then table.insert(capacitiesTable,"<div class='capx-available'>") table.insert(capacitiesTable,"<div class='capx-title-"..get_dir().."'>"..mb:t("available"):plain().."</div>") for_,capacityinipairs(availableCapacities)do table.insert(capacitiesTable,capacity) end table.insert(capacitiesTable,"<div class='capx-message'>"..mw.getCurrentFrame():preprocess(mb:t("you-available"):params('{{PAGENAME}}'):plain()).."</div>") table.insert(capacitiesTable,"</div>") end if#knownCapacities>0then table.insert(capacitiesTable,"<div class='capx-known'>") table.insert(capacitiesTable,"<div class='capx-title-"..get_dir().."'>"..mb:t("known"):plain().."</div>") for_,capacityinipairs(knownCapacities)do table.insert(capacitiesTable,capacity) end table.insert(capacitiesTable,"<div class='capx-message'>"..mw.getCurrentFrame():preprocess(mb:t("you-have"):params('{{PAGENAME}}'):plain()).."</div>") table.insert(capacitiesTable,"</div>") end table.insert(capacitiesTable,"<div class='capx-button'>[[:toolforge:capx|"..mb:t("access-capx"):plain().."]]</div>") table.insert(capacitiesTable,"<div class='capx-badges'>") table.insert(capacitiesTable,"<div class='capx-title-"..get_dir().."'>"..mb:t("recognition"):plain().."</div>") for_,badgeinipairs(badges)do table.insert(capacitiesTable,badge) end table.insert(capacitiesTable,"</div>") table.insert(capacitiesTable,"</div>") returntable.concat(capacitiesTable,"\n") end functionp.main(frame) -- Check if namespace is 2, else return a message ifmw.title.getCurrentTitle().namespace~=2then return"<div class='capx-error'>"..mb:t("wrong-namespace"):plain().."</div>" end -- Get the title of the page and remove the namespace localuser=mw.ustring.gsub(mw.title.getCurrentTitle().text,"User:","") -- Get the specific user data localuserData=getUserData(user) ifnotuserDatathen return"<div class='capx-error'>"..mb:t("not-found"):plain().."</div>" end -- Replace capacity IDs with names and descriptions (use unique section prefixes) localknownCapacities=replaceCapacities(userData.skills_known,"known") localavailableCapacities=replaceCapacities(userData.skills_available,"available") localbadges=loadBadges(userData.badges) returnconstructCapacitiesTable(knownCapacities,availableCapacities,badges) end returnp