-
Notifications
You must be signed in to change notification settings - Fork 0
/
createParserFunction.php
185 lines (146 loc) · 5.27 KB
/
createParserFunction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* This script bootstraps a ParserFunctionHelper-based extension
* May at some point add a parser function to an existing extension
*
* Usage:
* no parameters
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @author James Montalvo
* @ingroup Maintenance
*/
// Determine mediawiki install path
if ( ! isset( $IP ) ) {
$IP = __DIR__ . '/../../';
if ( getenv("MW_INSTALL_PATH") ) {
$IP = getenv("MW_INSTALL_PATH");
}
}
require_once( "$IP/maintenance/Maintenance.php" );
class CreateParserFunction extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = "Bootstrap a ParserFunctionHelper-based extension";
// addOption ($name, $description, $required=false, $withArg=false, $shortName=false)
$this->addOption(
'ext-name',
'Name of extension, like MyParserFunction',
true, true );
$this->addOption(
'function',
'English function wikitext as used in MediaWiki, like {{#func-name: ... }}. Spaces removed, forced lowercase.',
true, true );
$this->addOption(
'your-name',
'Your name, for docs.',
false, true );
}
public function execute() {
$extensionName = $this->getOption( 'ext-name' );
$noSpaceExtensionName = str_replace( ' ', '', $extensionName );
$lowerCaseNoSpaceExtensionName = strtolower( $noSpaceExtensionName );
// as subject, use wikitext parser function with spaces removed and capital first letter
$pfNameInit = ucfirst( str_replace( ' ', '', $this->getOption( 'function' ) ) );
// echo "\n\n\n Test: $pfNameInit\n\n\n";
$parserFunctionName = preg_replace_callback(
// find - character and immediately following character
'/-./',
// strip off - and capitalize character following
function( $matches ) {
return strtoupper( substr( $matches[0], 1, 1 ) );
},
$pfNameInit
);
// for function wikitext just make sure no capitals or spaces.
$parserFunctionWikitext = strtolower( str_replace( ' ', '', $this->getOption( 'function' ) ) );
$yourName = $this->getOption( 'your-name', "Your Name" );
// copy template files
$source = __DIR__ . '/example';
$dest = __DIR__ . '/../' . $noSpaceExtensionName;
if ( ! self::rcopy( $source, $dest ) ) {
$this->output( "\nDestination $dst already exists.\n" );
$this->output( "Set a new destination with --path=/path/to/new/extension\n" );
return false;
}
// replace templated info
self::replace( "$dest/i18n/en.json", array(
"extension-name" => $extensionName,
"lowercase-nospace-extension-name" => $lowerCaseNoSpaceExtensionName
) );
self::replace( "$dest/Magic.php", array(
"parser-function-wikitext" => $parserFunctionWikitext
) );
self::replace( "$dest/extension.json", array(
"extension-name" => $extensionName,
"nospace-extension-name" => $noSpaceExtensionName,
"lowercase-nospace-extension-name" => $lowerCaseNoSpaceExtensionName,
"your-name" => $yourName,
"parser-function-name" => $parserFunctionName
) );
self::replace( "$dest/README.md", array(
"extension-name" => $extensionName,
"parser-function-wikitext" => $parserFunctionWikitext
) );
self::replace( "$dest/includes/ParserFunctionName.php", array(
"your-name" => $yourName,
"parser-function-name" => $parserFunctionName,
"parser-function-wikitext" => $parserFunctionWikitext
) );
self::replace( "$dest/includes/Setup.php", array(
"your-name" => $yourName,
"nospace-extension-name" => $noSpaceExtensionName,
"parser-function-name" => $parserFunctionName
) );
// rename file
rename( "$dest/includes/ParserFunctionName.php", "$dest/includes/$parserFunctionName.php" );
$this->output( "\nExtension:$extensionName created at $dest\n" );
return true;
}
// copies files and non-empty directories
// adapted from: http://php.net/manual/en/function.copy.php#104020
static public function rcopy ( $src, $dst ) {
if ( file_exists( $dst ) ) {
return false;
}
if ( is_dir( $src ) ) {
mkdir( $dst );
$files = scandir( $src );
foreach ( $files as $file ) {
if ( $file != "." && $file != ".." ) {
self::rcopy( "$src/$file", "$dst/$file" );
}
}
}
else if ( file_exists( $src ) ) {
copy( $src, $dst );
}
return true;
}
static public function replace ( $file, $replaces ) {
$fileText = file_get_contents( $file );
foreach ( $replaces as $key => $replace ) {
$search = '{{' . $key . '}}';
// echo "find $search in $file, replace with $replace\n";
$fileText = str_replace( $search, $replace, $fileText );
}
file_put_contents( $file, $fileText );
return true;
}
}
$maintClass = "CreateParserFunction";
require_once( DO_MAINTENANCE );