Scribunto: An Introduction/Background

What is Scribunto?
To know what Scribunto is, first you need to know about MediaWiki. MediaWiki is the software that powers Wikipedia, the popular online encyclopedia. It is a wiki engine, which means that generally anyone can edit any page at any time. MediaWiki was built for Wikipedia, and Wikipedia's success has meant that MediaWiki has also flourished. As of February 2014, over 9,000 websites are using MediaWiki, and over 2,000 extensions to the core MediaWiki software have been written.

Scribunto is one of these 2,000 extensions. The name Scribunto is Latin for they shall write, which is fitting, as Scribunto allows users to write computer programs on MediaWiki wikis. Specifically, it allows users to embed scripts written in the Lua programming language. The Wikimedia Foundation, the non-profit organisation who operate Wikipedia and oversee the development of the MediaWiki software, intends these scripts as an alternative to the existing system of MediaWiki templates. Lua scripts have a significant performance advantage over MediaWiki templates, particularly for more complex templates with many parameters and multiple sub-templates. Lua scripts are also more flexible than MediaWiki templates, in some cases allowing a dramatic reduction in code size for the same functionality.

What is Lua?
Lua is a programming language developed in 1993 by computer scientists at the Pontifical Catholic University of Rio de Janeiro, Brazil. It is fast and lightweight, making it ideal for use in embedded applications. It also has a relatively simple syntax, and is known for its short learning curve.

History
Scribunto has its origins in MediaWiki's template system. Originally, templates were intended as a way to reuse the same content on many different pages. For example, on Wikipedia they were used to make maintenance banners for articles needing improvement, and navigational boxes for linking related articles. This greatly simplified maintenance of articles, as users could make changes to hundreds of articles simply by making one edit to a template.

In version 1.3, the MediaWiki developers added the ability to use parameters with templates, meaning the same template could display different output on different pages. Furthermore, starting with MediaWiki 1.6, these parameters could accept a default value. This made templates a lot more flexible, but had an unintended downside. By manipulating parameter names and default values, it became possible to add conditional logic to templates.

The ability to use conditional logic enabled templates to be used as a kind of programming language. Templates were never designed to do this kind of work, and using them in this way had several disadvantages. One was that the display code became mixed with the logic code, with the result that templates could not easily be indented to aid understanding. Another was that they were slow; MediaWiki was already written in a programming language with conditional logic (PHP), and adding another quasi-programming-language on top of that had a significant performance cost, especially for complicated templates. One template in particular, the "qif" template, provided an easier way to use conditional logic than manipulating template parameters directly, and became quite widely used on Wikipedia, with a corresponding negative effect on performance.

To try and improve this situation, one of the lead MediaWiki developers, Tim Starling, wrote the ParserFunctions extension, which introduced logical operators including  and. This improved performance somewhat, but was never intended as a permanent fix. Sure enough, templates based on ParserFunctions became even more complicated than those that had used qif.

A further innovation to address template performance was introduced in MediaWiki 1.12, in the form of a new preprocessor for the MediaWiki parser. This new preprocessor was designed to only evaluate template code if it was necessary. So if a template used an  parser function, MediaWiki would only evaluate the "true" code if the test evaluated to , and would only evaluate the "false" code if the test evaluated to. Again, this preprocessor was successful in improving MediaWiki's parsing performance, but was still slow on pages that used many complex templates.

Scribunto is the latest effort to address the performance problems of MediaWiki templates. Rather than making more improvements to parser functions or to MediaWiki's core software, the MediaWiki developers have elected to allow users to use a fully-fledged scripting language on wiki pages. This means that Scribunto-based templates have a dramatic performance increase from their ParserFunctions-based counterparts. Depending on the template, the Scribunto version can be anything from twice as fast to 50 times as fast. Scribunto also provides proper separation of logic code and display code, meaning that Scribunto-based templates can be easily indented and commented for readability.