Jump to content

User:RexxS/GCI-2019-Task01

From Wikipedia, the free encyclopedia

Lua Task 1 - Introduction to Lua in Wikipedia

[edit]

Setting up

[edit]

Lua is a lightweight programming language that is particularly suited for embedding within other software. The software used by Wikipedia, called MediaWiki, has an extension that provides a version of Lua that can be used within Wikipedia pages. The extension is called Scribunto.

In these tasks you will be working on Wikipedia in two places:

  • (a) your module sandbox, which will contain the Lua code to be run;
  • (b) your user sandbox, which will contain the wiki-text that runs the code and displays the results.

1. Create a Wikipedia account if you don't already have one – see: Wikipedia:Why create an account? Adding an email address allows you to reset your password if you forget it.

2. After you have logged in to your account, use the link at the top right of your page that has your username to create your user page. In the edit box, write a sentence, noting that you’re taking part in GCI, but don't add personal information. Preview your edit and save it.

3. Use the link at the top right of your page to create your sandbox. Write a brief note saying that this is your user sandbox, preview it and save it. Remember that what you type can potentially be seen by anybody, so be sensible!

4. Read about Scribunto, the Lua implementation embedded in WikiMedia software: Wikipedia:Lua

5. Create an empty module sandbox for yourself as a subfolder of Module:Sandbox For example, if your user name is "Joe Bloggs", then create your module at Module:Sandbox/Joe Bloggs Note that any spaces will be converted to underscores _ in the page url that you can see in your browser's address bar. This is called urlencoding.

6. In your module sandbox add a line of text starting with two hyphens: --

7. After the two hyphens, type your username followed by Google Code-in 2019, Introduction to Lua in Wikipedia

Text beginning with two hyphens is used in Lua to designate a comment.

8. Save your module sandbox.

Running Lua code in Wikipedia

[edit]

A Wikipedia page can call a Lua module to do calculations, process text, format citations, fetch information from Wikidata, and many other jobs where a programming language is needed to get a result.

A Lua module is used inside a Wikipedia page by using a call something like {{#invoke:RexxS|carousel}}

9. In your user sandbox (not your module sandbox), leave a blank line after your brief note, then type this:

== Task 1 ==

Putting == around a phrase creates a level 2 html heading. We can use that to break up our user sandbox into sections for different tasks.

10. On the line below that, type or copy {{#invoke:RexxS|carousel}} and save it.

Please don't miss out saving your user sandbox each time you are instructed to.

You should see the filename of a JPG image. The line makes use of a module called Module:RexxS. Modules can contain many functions and the line you have entered calls a function in that module called "carousel".

11. Add another new line in your user sandbox that reads

[[File:{{#invoke:RexxS|carousel}} | thumb]]

and save it.

This uses the standard Wikipedia image syntax to display the image. You can read a lot more about image syntax at Wikipedia:Extended image syntax if you are interested.

12. Look at Module:RexxS and examine the code. See if you can work out how the function picks a filename. You may find the documentation at http://www.lua.org/manual/5.3/ helps you to learn how Lua works.

You should be able to work out that the filenames of the images which can be returned are kept in a list. In Lua, lists are tables. The table is the only data structure used and all other types of data structures, arrays, sequences, objects, etc. are created from tables.

13. In your user sandbox, leave a blank line, then on a new line write down the name of the variable that is set equal to the list of filenames. Write just the name, one word, not the contents of the list or anything else. (Hint: it is not the word 'local' which isn't a variable, but a qualifier that tells us something about the variable.)

14. Save your user sandbox.