Skip to content

Instantly share code, notes, and snippets.

@AABoyles
Created October 4, 2012 16:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AABoyles/3834609 to your computer and use it in GitHub Desktop.
Save AABoyles/3834609 to your computer and use it in GitHub Desktop.
Quick-creator for MediaWiki Extensions. Creates a project directory, main file, body file, and internationalization file, all adhering to the standards of extension design described in http://www.mediawiki.org/wiki/Manual:Developing_extensions
<?php
class makeExt{
protected $project = "<project>";
protected $name = "<you>";
protected $desc = "<insert a short description here>";
public function __construct(){
global $argv, $argc;
if(!isset($argc)){
$argc = count($argv);
}
if($argc < 2){
echo "Usage: php makeExt.php <ProjectName> [<YourName>] ['<ProjectDescription>']\n";
echo " Will create a new Mediawiki extension skeleton.\n";
echo "Parameters:\n";
echo " <ProjectName> The Name of your project\n";
echo " <YourName> (Optional) Your Name\n";
echo " <ProjectDescription> (Optional) A Description for the project\n";
die();
}
if( $argc >= 2 ){
$this->project = $argv[1];
if(file_exists($this->project)){
die ("Project Directory already exists!");
}
mkdir($this->project);
mkdir($this->project . '/tests');
mkdir($this->project . '/modules');
mkdir($this->project . '/includes');
mkdir($this->project . '/specials');
}
if($argc >= 3){
$this->name = $argv[2];
} else {
$processUser = posix_getpwuid(posix_geteuid());
$this->name = $processUser['name'];
}
if($argc >= 4){
$this->desc = $argv[3];
}
$this->printBase();
$this->printBody();
$this->printi18n();
$this->printTest();
$this->printRdMe();
}
function printBase(){
$short = strtolower($this->project);
$base = $this->top('') . "
# Global Variables:
# File inclusion and registration:
\$wgAutoloadClasses['$this->project'] = __DIR__ . '/$this->project.body.php';
\$wgExtensionMessagesFiles[ '$this->project' ] = __DIR__.'/$this->project.i18n.php';
\$wgHooks['UnitTestsList'][] = 'ef{$this->project}Tests';
function ef{$this->project}Tests( &\$files ) {
\$files = array_merge( \$files, glob( __DIR__ . '/tests/*Test.php' ) );
return true;
}
# Extension Registration:
\$wgExtensionCredits[][] = array(
'path' => __FILE__,
'name' => '$this->project',
'version' => '0.1.0',
'author' => array( '[<your url> $this->name]' ),
'url' => '<extension url>',
'descriptionmsg' => '$short-desc',
);
";
if(file_put_contents("$this->project/$this->project.php", $base)===FALSE){
die("Error writing file $this->project.php");
}
return;
}
function printBody(){
$body = $this->top('body.') . "
class {$this->project}{
#Variables:
function __construct(){
}
#Functions:
}
";
if(file_put_contents("$this->project/$this->project.body.php", $body)===FALSE){
die("Error writing file $this->project.body.php");
}
return;
}
function printi18n(){
$short = strtolower($this->project);
$i18n = $this->top('i18n.') . "
\$messages = array();
/** English
* @author $this->name
*/
\$messages['en'] = array(
'$short-desc' => '$this->desc',
);
/** Message documentation (Message documentation)
* @author $this->name
*/
\$messages['qqq'] = array(
'$short-desc' => '{{desc}}',
);
";
if(file_put_contents("$this->project/$this->project.i18n.php", $i18n)===FALSE){
die("Error writing file $this->project.i18n.php");
}
return;
}
function printTest(){
$test = $this->top("tests/{$this->project}Test.") . "
class {$this->project}Test extends MediaWikiTestCase {
//Put your testing functions here
}";
if(file_put_contents("$this->project/tests/{$this->project}Test.php", $test)===FALSE){
die("Error writing file tests/test.php");
}
return;
}
function printRdMe(){
$year = date('Y');
$readMe = "$this->project
============
$this->desc
Copyright $year, by $this->name
";
if(file_put_contents("$this->project/README.md", $readMe)===FALSE){
die("Error writing file README.md");
}
return;
}
function top($file){
$top = "<?php
/*
* $this->project
* $this->desc
*
* @file {$this->project}.{$file}php
* @author $this->name
*/
if (!defined('MEDIAWIKI')) {
die('This file is an extension to the <a href=\'http://www.mediawiki.org/\'>MediaWiki Platform</a> and cannot be used alone.');
}
";
return $top;
}
}
$ext = new makeExt();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment