Jump to content

Extension:Piwo

From mediawiki.org
Revision as of 10:46, 19 October 2020 by AbyxDev (talk | contribs) (updated after updates)
MediaWiki extensions manual
Piwo
Release status: experimental
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
NS_GRAM
(NS_GRAM_TALK)
gram-edit

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
  • Download and place the file(s) in a directory called Piwo in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'Piwo' );
    $wgGroupPermissions['bureaucrat']['gram-edit'] = true;
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

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

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 accessiblity depends on the user you run your Mediawiki with.

See Also