Extension:Piwo

The Piwo extension allows executing of Python scripts on MediaWiki (Python In, Wikitext Out). Since it uses the shell framework, it can and should be sandboxed.

Usage
This extension defines a custom namespace, "Gram", that stores Python scripts. Editing the Gram namespace requires the  right. To invoke a script, use the following parser function:

In the Python script, the parameters can be accessed through  (no import is needed); an extra function,   is provided as an equivalent to PHP's  ;   is equivalent to $IP;   is the name of the script, without "Gram:".

mw.py
You might need the source code of mw.py for debugging and it's also copied here for reference.

Any and all of the Python standard library can be imported, as of January 2018. The accessiblity depends on the user you run your Mediawiki with.

Examples
Obviously we need to be careful what to disclose via the python code. In principle even the jailboxed environment might have access to security relevant data.

Take the use cases:
 * 1) We want to display some text as ASCII-Art and are to lazy to look for a PHP / extension based solution. There is https://pypi.org/project/art/ which looks nice for the Job. There seems to be no security risk involved.
 * 2) We want to display the diskusage in our wiki. This info might not be too security sensible and e.g. useful in an intranet to show when we run out of diskspace.

ASCII-ART
prepare the usage of the pypi art library by in the user space of your mediawiki enviroment.

Template:Art
see: Gram:Art

Result
_ __                                                      |___/
 * |/ / ___  _ __   _ __   _   _
 * ' / / _ \| '_ \ | '_ \ | | | |
 * . \ | __/| | | || | | || |_| |
 * _|\_\ \___||_| |_||_| |_| \__, |

And you can use the template anywhere in your wiki hiding the fact that the implementaton is python based.

Gram:Diskusage
Just get some python code which does the relevant task e.g. see https://stackoverflow.com/a/48929832/1497139 and put into the Gram namespace.

Diskusage
A page in the main namespace may now call the python function

Result
Total: 187 GiB Used: 140 GiB Free: 37 GiB

Debugging
If you'd like to remote debug your code you might want to add: to your python code. You'll then be able to debug the python code execution with your favorite Debug environment/IDE. This feature has e.g. been tested with Eclipse/Liclipse. see
 * https://stackoverflow.com/questions/35066784/how-to-setup-remote-debugging-with-eclipse-and-pydev for setting up things
 * https://github.com/fabioz/PyDev.Debugger/issues/184 for the necessary path mapping.