Extension:Piwo
This extension is incompatible with MediaWiki 1.41 or any later release! You are advised against using this extension on a live site. Volunteer developers are invited to pledge their efforts to updating this extension to make it compatible with MediaWiki 1.41 by replacing the {{Incompatible }} template with {{Incompatible |version=1.41|pledge=~~~~}} . |
<translate> This extension is currently not actively maintained!</translate> <translate> Although it may still work, any bug reports or feature requests will more than likely be ignored.</translate> |
Piwo Release status: unmaintained |
|
---|---|
Implementation | Parser function |
Description | Allows executing of Python scripts on pages |
Author(s) | Kenny2wikitalk |
Latest version | 0.1.0 |
MediaWiki | 1.31+ |
License | MIT License |
Download | GitHub:
<translate> Note:</translate> |
NS_GRAM (NS_GRAM_TALK) |
|
gram-edit |
|
Quarterly downloads | Lua error in Module:Extension at line 172: bad argument #1 to 'inNamespace' (unrecognized namespace name 'skin'). |
Public wikis using | Lua error in Module:Extension at line 172: bad argument #1 to 'inNamespace' (unrecognized namespace name 'skin'). |
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.
Installation
- Install Python
- <translate> <tvar name=1>Download</tvar> and place the file(s) in a directory called <tvar name=name>
Piwo
</tvar> in your <tvar name=ext>extensions/
</tvar> folder.</translate> - <translate> Add the following code at the bottom of your <tvar name=1>LocalSettings.php </tvar> file:</translate>
wfLoadExtension( 'Piwo' ); $wgGroupPermissions['bureaucrat']['gram-edit'] = true;
- File:OOjs UI icon check-constructive.svg <translate> Done</translate> – <translate> Navigate to <tvar name=special>Special:Version</tvar> on your wiki to verify that the extension is successfully installed.</translate>
Usage
This extension defines a custom namespace, "Gram", that stores Python scripts. Editing the Gram namespace requires the gram-edit
right. To invoke a script, use the following parser function:
{{#piwo:name of script, without "Gram:"|parameter1|parameter2|...}}
In the Python script, the parameters can be accessed through mw.argv
(no import is needed); an extra function, mw.hsc
is provided as an equivalent to PHP's htmlspecialchars
; mw.MW_ROOT
is equivalent to $IP; mw.GRAM_NAME
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.
from sys import argv
from os import environ as env
from html import escape as hsc
MW_ROOT = env.get('MW_ROOT', None)
GRAM_NAME = env.get('MW_GRAM_NAME', None)
argv[0] = '#piwo:' + GRAM_NAME
Any and all of the Python standard library can be imported, as of January 2018. The accessibility 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:
- 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.
- We want to display the disk usage 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 disk space.
ASCII-ART
Prepare the usage of the pypi art library by
pip install art
in the user space of your MediaWiki environment.
Gram:Art
from art import *
print("<pre>")
tprint(mw.argv[1])
print("</pre>")
Template:Art
<noinclude> {{Art|Kenny}} see: [[Gram:Art]] </noinclude><includeonly> {{#Piwo:Art|{{{1|Hello world!}}}}} </includeonly>
Result
_ __ | |/ / ___ _ __ _ __ _ _ | ' / / _ \| '_ \ | '_ \ | | | | | . \ | __/| | | || | | || |_| | |_|\_\ \___||_| |_||_| |_| \__, | |___/
And you can use the {{Art|...}}
template anywhere in your wiki hiding the fact that the implementation is python based.
Diskusage
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.
import shutil
total, used, free = shutil.disk_usage("/")
print("Total: %d GiB" % (total // (2**30)))
print("Used: %d GiB" % (used // (2**30)))
print("Free: %d GiB" % (free // (2**30)))
Diskusage
A page in the main namespace may now call the python function
{{#piwo:Diskusage}} [[Category:piwo]]
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:
import pydevd
pydevd.settrace("<your ip or hostname here>", port=5678)
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.
See also
- Pages with script errors
- Pages with broken file links
- Extensions incompatible with 1.41
- Unmaintained extensions
- Extensions without an image
- Parser function extensions
- Extensions without a compatibility policy
- Extensions with manual MediaWiki version
- MIT licensed extensions
- Extensions in GitHub version control
- Extensions which add rights
- ParserFirstCallInit extensions
- ContentHandlerDefaultModelFor extensions
- All extensions
- Extensions not in ExtensionJson