User:RexxS/GCI-2019-Task06
Lua Task 6 - MediaWiki libraries
[edit]Prerequisite: Lua Task 5 - Lua libraries. This task requires research and independent learning. It may be unsuitable for beginners to programming. The previous task showed how we can use the built-in libraries to handle strings. This task examines some of the specialised functions that are available in the Scribunto libraries that we can use for working in MediaWiki projects.
The Scribunto extension for MediaWiki provides a number of libraries specifically for use in MediaWiki and you can read about the common ones at Scribunto libraries. The library used to import Wikidata into other projects is documented at Extension:Wikibase Client/Lua.
Mediawiki language library
[edit]Read through the first four steps first, then carry out the instructions.
Here is a function that makes use of the mediawiki language (mw.language
) library to get a list of languages known to MediaWiki. Read mw.language.fetchLanguageNames to work out what it does. Note that langslist is a Lua table and out is a string which contains html to format the display.
function p.langs(frame)
local langslist = mw.language.fetchLanguageNames()
local out = ""
local count = 0
for k, v in pairs(langslist) do
out = out .. k .. " - " .. v .. "<br>"
count = count + 1
end
return out .. "<br>= " .. count .. " languages"
end
1. Copy the function p.langs
as a new function in your module sandbox and save it. The for
loop demonstrates an example of how we could extract all of the data in a table into a string that we can return to the wiki-page. The pairs
function is very useful in for
loops as it will iterate through any table passing in consecutive loops each key and value into the variables given (k and v in the example above). Please note however, that the order in which a table is constructed may not correspond to the order in which it will be iterated.
2. Write a call in a new section of your user sandbox called == Task 6 ==
that invokes langs
from your module sandbox. Save it and observe the result.
Fallback languages are used by MediaWiki software to supply text in other languages that may be usable when the text is not available in the user's first choice. So "sk" (Slovak) falls back to "cs" (Czech), which falls back to "en" (English). Each language will have a name and a code.
3. Based on the function above, create a new function p.fallbacklangs
in your module sandbox that takes a parameter |langcode=
(which represents a language code such as "en"), and uses mw.language.getFallbacksFor( langcode )
to return a list of all of the fallback language codes for that code. Save your module sandbox.
4. In your user sandbox invoke your function three times. The List of ISO 639-1 codes may be useful. Use the preview button to try different languages before you save, and try to find at least one language code that has more than two fallback language codes. For example, Slovak has two fallback language codes, but some languages have more. Then save your user sandbox.
Mediawiki title library
[edit]5. Examine this code:
p.pgtitle = function( frame )
local title = frame.args.title
local ttlobj = mw.title.new( title )
local txt = ttlobj.text
return txt
end
6. Read about the mw.title.new
function in the manual, working out what ttlobj.text
returns.
7. Copy the p.pgtitle
function into your module sandbox and save it.
8. Add a new subsection to your user sandbox called === Pageinfo ===
.
9. Add a line of code that calls the pgtitle
function, supplying the parameter |title=mw:Extension:Scribunto/Lua_reference_manual #mw.title.new
, and save it. Observe the result and make sure you understand how the function has transformed the page title that you supplied.
10. You are going to write another function that uses mw.title.new
(as described in the manual) to create a title object; we can use that to test whether the given title exists and whether it is a redirect. Read through the specification below before starting to write your function.
I want a function called pginfo
that takes a page title as a parameter and returns information about the corresponding Wikipedia page: whether it exists and whether it is a redirect. Here are the examples of what I want it to display when tested with the three page titles, "Color", "Colour" and "Colr":
Color exists and is not a redirect Colour exists and is a redirect Colr does not exist and is not a redirect
11. In your module sandbox write the p.pginfo
function. If you need a start, copy and adapt the similar code from p.pgtitle
to make the function required. Save your module sandbox when you are ready to test your work.
12. In your user sandbox write at least six test cases (include "Color", "Colour" and "Colr" as three of them) to show that your module works. Think about what would happen if the user missed out the parameter or gave different kinds of invalid values, like you did for Task 4. If you have errors, fix them and re-check your tests until you have the function working and save your user sandbox.