Jump to content

User:RexxS/GCI-2018-Task06

From Wikipedia, the free encyclopedia

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]

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 langlist is a Lua table and langs is a string which contains html to format the display.

p.langnames = function(frame)
	local langlist = mw.language.fetchLanguageNames()
	local langs = ""
	local count = 0
	for k, v in pairs(langlist) do
		langs = langs .. k .. " - " .. v .. "<br>"
		count = count + 1
	end
	return langs .. "<br>= " .. count .. " languages"
end

1. Copy the function p.langnames 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 langnames from your module sandbox. Save it and observe the result.

3. Based on the function above, create a new function p.fallbacks in your module sandbox that takes a parameter |langcode= (which represents a language code), and uses mw.language.getFallbacksFor( langcode ) to return a list of all of the fallback language codes for that code. Save your module sandbox.

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).

4. In your user sandbox call 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. Then save your user sandbox.

Mediawki title library

[edit]

5. Examine this code:

p.pagename = function( frame )
	local ttl = frame.args.title
	local ttlobj = mw.title.new( ttl )
	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.pagename 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 pagename function, supplying the parameter |title=mw:Extension:Scribunto/Lua_reference_manual #mw.title.new, and save it. 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 pageinfo 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.pageinfo function. If you need a start, copy and adapt the similar code from p.pagename to make the function required. Save your module sandbox when you are ready to test your work.

12. In your sandbox write six test cases (include "Color", "Colour" and "Colr") 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.