Making more MediaWiki extensions installable via Composer
To make MediaWiki extensions installable via Composer, the following steps must be followed:
- Modify the main extension file (e.g. ExtensionName.php) to change all $wgVariableName global variables to $GLOBALS['wgVariableName']
- Create a composer.json file with information pull from main extension file
- Submit patch to main repository (Gerrit or GitHub as required)
- Create corresponding Packagist.org package (e.g. mediawiki/extension-name)
Goal: Construct proper regex to convert variable naming
To find PHP variables, use the following regex:
\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)
However, we do NOT want to match any variables already defined using $GLOBALS, so we must exclude this particular variable. I think we also need to exclude any $_GET, $_POST, $_SERVER, or other super-globals, but for the initial version of this script I will not be handling them since these should not be present in MediaWiki extensions.
To exclude $GLOBALS the following regex can be used:
\$(?!GLOBALS)([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)
Each match can then be replaced with:
$GLOBALS['$1']
This works well for many variables, but fails for variables not intended to be global. Most notably, if functions are defined within the ExtensionName.php file then any variables within those functions should not be converted.
How to handle this scenario? TBD...
Composer.json file should look like the following, with proper values inserted based on main extension file.
{
"name": "mediawiki/<extension-name-with-hyphens>",
"type": "mediawiki-extension",
"description": "<try to pull from ExtensionName.php, else use dummy text>",
"keywords": [
"MediaWiki"
],
"homepage": "<attempt to pull from ExtensionName.php, else blank>",
"license": "<leave as ‘unknown’?>",
<for “authors”: $wgExtensionCredits variable, loop over the relevant “author” field>
"authors": [
{
"name": "Yaron Koren",
"role": "Author"
}
],
"support": {<leave blank if possible>},
"require": {
"composer/installers": ">=1.0.1"
},
"autoload": {
"files": ["<ExtensionName>.php"]
}
}
Should we automate this somehow? Is that desireable?
Should we automate this somehow? Is that desireable?