 And the next session is from Tohaum, your modules training. Okay, hello. I am user Tohaum, active mostly in Ukrainian Wikipedia, Wikidata and Wikimedia Commons. Also occasionally make edits to English Wikipedia. In real life I am a game developer and I develop games in, you guessed it, in Lua. And I also write modules in Lua for Wikipedia. So what are modules and how are they used? Wikipedia uses a lot of templates which are either a standardised form of representing information or some black box function where you give it some input and it gives some output or it may be some often repeated text or anything else. There are two approaches to creating templates in Wikipedia. These are parser language and Lua modules. Parser language is a classical way of creating templates and it has parser functions which allow to use basic logical functions and basic Wikidata queries and so on. You probably have saw them. They have a lot of curly brackets. I would say way too many curly brackets. For example, can you tell me from the first glance what is written here? What does this code mean? Well, maybe you can't. Maybe you can do it after reading it but still you need some time to understand what is written here and here comes the first problem with parser language it's not human readable. If your template consists of several lines that will not be a big problem but if it is as big as here with several hundred lines it will be a problem and changing something here or just fixing something here becomes a big challenge. If you drop just one curly bracket or if you put one extra curly bracket it will break completely and you will not even know where exactly did it break where exactly did you miss a curly bracket or put an extra curly bracket it will not point to exact line where you did it and that renders them almost impossible to edit or add new features if templates are written in parser language and are bigger than several dozens of lines. Also, parser language is limited in its possibilities it provides only the most basic control structures if it exists and so on and it allows only the most basic call-to-viki data which gets only the first value of some property you can't get labels, descriptions the second value of a property you can't get qualifiers, sight links and so on but with Lua you can do this Lua is a full-scale programming language which has such things as control structures functions, variables, arrays and everything that a decent programming language should have of course it has some things that are not like in other languages for example, arrays in Lua are numbered from one not starting from zero like in other programming languages also everything in Lua is a table absolutely everything global variables are just values in a global table as well as functions and so on Lua has many similarities with JavaScript in the way it functions and many similarities with Python in syntax however, Lua has the disadvantage that it has a higher entrance point to it than a parser language a parser language could even be used by non-programming contributors because it is much easier and if some template is written in Lua and there is some mistake editors who are not programmers who don't have technical background they tend to turn a blind eye on problems with modules because it is some high-tech dark magic and they don't want to get involved in it because it's hard or otherwise they will not do it themselves and ask for help from some editor who is fluent in this and if you are the only or one of the few editors in some projects who can edit Lua modules you will have a range of questions and suggestions and reports of bugs pouring on you concerning Lua modules also Lua modules allow you to access wiki data and you can get any part of wiki data item with Lua wiki data items behind the scenes are JSONs which are tables inside of tables inside of tables again and so on for example let's take this item in wiki data which is about wiki media hackathon 2019 this is how it looks on the front end for simple users who go to wiki data to see it but how it looks like behind the scenes what is actually the code of this page so this is the source code of this item it has a master table with keys type, id, labels, descriptions, aliases and claims and site links somewhere in the end here are tables and inside of them are other tables and other tables and with Lua you can get this value but you can do it with parser language and also in Lua you can iterate over those values which you cannot do in parser language is everything clear for this moment? are there any questions? after I answer the questions I will continue there is no questions yet okay then I will go next so how it works let's start from my demonstration from test wiki imagine we have a page in article where we want to use a template based on Lua module in this page we just call a template and the call of the template will be irrespectful of whether it's written in classical parser language or in Lua and let's pass into this template string hello world same next we have page template this is the template that we actually call from this page it will then call a module which is done with so the template invoke you write a sharp symbol in double curly brackets you write a sharp symbol invoke colon and name of your module then vertical pipeline and the name of the function which you want to call from Lua usually it is main if it is a module for one purpose for one info box or a net box it is usually called main but it can be other things for example if you write in a module that is sort of library of functions and you just call whatever function you want and now to the module itself which starts with the prefix module colon semicolon which is a separate namespace let's create it since Lua is object oriented language there should be an object let's create it in Lua everything is a table including objects objects in Lua are also tables these curly brackets in Lua means that we create a table which can be an array if there are only numeric keys or it can be a hash array if there are non-numeric keys so we created here an object which is also a table we attach the function to this object we create a function main to which we pass the frame that is some sort of what we called in our article in this object there will be arguments passed to template and from template here to the module and we will return this object now let's do something meaningful in here first of all let's get the parameters which were passed from article to template and from template here to module this is frame get parent, arcs and we need the first value remember that Lua numbers arrays from one so here in variable str we will have this string and now what will return let's reverse the string so put the last character at the place of first and first in the place of the last and so on and we will return this string can you please increase the font size or zoom a little bit more now let's refresh this page and yes it reversed the text that we passed there here in this return value there can be any kind of Viki markup but if it is not a simple text you should preprocess it so that it is treated as Viki markup for example imagine that we put our answer into the table so table is not visible so I would better add class Viki table so something is wrong okay another way I should be this way okay I don't know why it doesn't work so imagine in this rest value you have not just a text but some Viki markup which can be tables function calls some Viki markup like italics or bold text or references or anything else without this it will return them as text and references tables and other things will not be treated as such without preprocess this will not be a table it will literally write opening curly bracket pipeline line break another pipeline closing curly bracket and not a table if we write it without frame preprocess but with frame preprocess it will be treated correctly also this editing interface needs some explanation so here is always usual okay so I was telling about function frame preprocess which turns text into which turn text with Viki markup into some visual elements if we don't use frame preprocess the text will not be treated as Viki markup and it will literally write open curly bracket vertical pipeline vertical pipeline closing curly bracket but with this function it will be treated as Viki table and table will be depicted next thing I have to explain to you some in some words about editing interface here you have window of text editing much like usually in articles however there are different buttons and also there is line numbering this button is used to indent or outdent here is summary and publishing show changes button like in usual article editing window here is preview page with this template this thing allows you to view the page how it will look like if you apply the changes here like show preview and it just shows us this this was supposed to be table but something went wrong well, oh yeah I forgot this right let's apply some changes here and preview how the page will look like we saved these changes here we wrote the name of page we want to preview and we clicked show preview and now we see how this page would look like with those edits that were made also we have here a console which is kind of sandbox where you can test things Lua modules and Lua programming language allows you to do some complicated logics some complicated mess it has control structures like if, else if, else it has force control structure it has while but that's not the only plus it has it also can work with wiki data items which are stored in form of JSON and let's see how it works while I did not forget here is documentation of Lua how it is used in wikimedia projects here is short description of the language itself and then there are functions that were added here in media wiki for the language to be more convenient to work with wikipedia articles and items and so on I will give a link to it also there is a sub-library which allows you to work with titles get labels, get ID numbers for example this function if it is given a QID it gives you the page itself this function does the same but for current page page from which the module is called this does it for title you give a title of a page it gives you corresponding wiki data item and so on it is also very useful and here is documentation of Lua programming language itself you can access the source code of wiki data items through API like here which will give you the content of the page or in the console let's access this item in the console to see its JSON well, something doesn't work here but this function gets the item itself and this is a console function that will write it content so if it is applied right you will see JSON with tables inside of tables inside of tables and so on and let's see how to access those well, first we get the item itself not here should be here and here we should have let's get organizer p664 we get the item we get sub table claims and it's value for p664 then let's get a list of organizers and iterate over this list so we get the table for this property 664 here it is we need the first value then main snack it is actually a first value then data value value which is inside the data value and ID ID number of organizer let's now get item for this ID to get its title and add it here to this table here we will add labels English and value and now let's concatenate this table and save it in the very same variable and return it just like this publish and if I wrote everything right here should be at least yes I forgot module sandbox or not well when it comes to action everything goes wrong as always it says I have an error here labels multiple you see now we got these two values from this item and successfully added them to the page with template so we used our template to add something in wikidata which is somewhere deep in this wikidata item and unlike parser language we added not one value but two values so questions ok thanks a lot for the session ok goodbye