added experimental installer for extensions
[lhc/web/wiklou.git] / maintenance / installExtension.php
1 <?php
2 /**
3 * Copyright (C) 2006 Daniel Kinzler, brightbyte.de
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @package MediaWiki
21 * @subpackage Maintenance
22 */
23
24 $optionsWithArgs = array( 'target' );
25
26 require_once( 'commandLine.inc' );
27
28 class ExtensionInstaller {
29 var $source;
30 var $target;
31 var $name;
32 var $dir;
33
34 function ExtensionInstaller( $name, $source, $target ) {
35 $this->name = $name;
36 $this->source = $source;
37 $this->target = realpath( $target );
38 $this->extdir = "$target/extensions";
39 $this->dir = "{$this->extdir}/$name";
40 $this->incpath = "extensions/$name";
41
42 #TODO: allow a subdir different from "extensions"
43 #TODO: allow a config file different from "LocalSettings.php"
44 }
45
46 function note( $msg ) {
47 print "$msg\n";
48 }
49
50 function warn( $msg ) {
51 print "WARNING: $msg\n";
52 }
53
54 function error( $msg ) {
55 print "ERROR: $msg\n";
56 }
57
58 function prompt( $msg ) {
59 if ( function_exists( 'readline' ) ) {
60 $s = readline( $msg );
61 }
62 else {
63 if ( !@$this->stdin ) $this->stdin = fopen( 'php://stdin', 'r' );
64 if ( !$this->stdin ) die( "Failed to open stdin for user interaction!\n" );
65
66 print $msg;
67 flush();
68
69 $s = fgets( $this->stdin );
70 }
71
72 $s = trim( $s );
73 return $s;
74 }
75
76 function confirm( $msg ) {
77 while ( true ) {
78 $s = $this->prompt( $msg . " [yes/no]: ");
79 $s = strtolower( trim($s) );
80
81 if ( $s == 'yes' || $s == 'y' ) return true;
82 else if ( $s == 'no' || $s == 'n' ) return false;
83 else print "bad response: $s\n";
84 }
85 }
86
87 function deleteContents( $dir ) {
88 $ff = glob( $dir . "/*" );
89 if ( !$ff ) return;
90
91 foreach ( $ff as $f ) {
92 if ( is_dir( $f ) ) $this->deleteContents( $f );
93 unlink( $f );
94 }
95 }
96
97 function copyDir( $dir, $tgt ) {
98 $d = $tgt . '/' . basename( $dir );
99
100 if ( !file_exists( $d ) ) {
101 $ok = mkdir( $d );
102 if ( !$ok ) {
103 $this->error( "failed to create director $d" );
104 return false;
105 }
106 }
107
108 $ff = glob( $dir . "/*" );
109 if ( $ff === false || $ff === NULL ) return false;
110
111 foreach ( $ff as $f ) {
112 if ( is_dir( $f ) ) {
113 $ok = $this->copyDir( $f, $d );
114 if ( !$ok ) return false;
115 }
116 else {
117 $t = $d . '/' . basename( $f );
118 $ok = copy( $f, $t );
119
120 if ( !$ok ) {
121 $this->error( "failed to copy $f to $t" );
122 return false;
123 }
124 }
125 }
126
127 return true;
128 }
129
130 function fetchExtension( ) {
131 if ( file_exists( $this->dir ) && glob( $this->dir . "/*" )
132 && realpath( $this->source ) != $this->dir ) {
133
134 if ( $this->confirm( "{$this->dir} exists and is not empty.\nDelete all files in that directory?" ) ) {
135 $this->deleteContents( $this->dir );
136 }
137 else {
138 return false;
139 }
140 }
141
142 preg_match( '!([-\w]+://)?.*?(\.[-\w\d.]+)?$!', $this->source, $m );
143 $proto = @$m[1];
144 $ext = @$m[2];
145 if ( $ext ) $ext = strtolower( $ext );
146
147 $src = $this->source;
148
149 if ( $proto && $ext ) { #remote file
150 $tmp = wfTempDir() . '/' . basename( $src );
151
152 $this->note( "fetching {$this->source}..." );
153 $ok = copy( $src, $tmp );
154
155 if ( !$ok ) {
156 $this->error( "failed to download {$src}" );
157 return false;
158 }
159
160 $src = $tmp;
161 $proto = NULL;
162 }
163
164 if ( $proto ) { #assume SVN repository
165 $this->note( "SVN checkout of $src..." );
166 wfShellExec( 'svn co ' . escapeshellarg( $src ) . ' ' . escapeshellarg( $this->dir ), $code );
167
168 if ( $code !== 0 ) {
169 $this->error( "checkout failed for $src!" );
170 return false;
171 }
172 }
173 else { #local file or directory
174 $src = realpath ( $src );
175
176 if ( !file_exists( $src ) ) {
177 $this->error( "file not found: {$this->source}" );
178 return false;
179 }
180
181 if ( $ext === NULL || $ext === '') { #local dir
182 if ( $src == $this->dir ) {
183 $this->note( "files are already in the extension dir" );
184 return true;
185 }
186
187 $this->copyDir( $src, $this->extdir );
188 }
189 else if ( $ext == '.tgz' || $ext == '.tar.gz' ) { #tgz file
190 $this->note( "extracting $src..." );
191 wfShellExec( 'tar zxvf ' . escapeshellarg( $src ) . ' -C ' . escapeshellarg( $this->extdir ), $code );
192
193 if ( $code !== 0 ) {
194 $this->error( "failed to extract $src!" );
195 return false;
196 }
197 }
198 else if ( $ext == '.zip' ) { #zip file
199 $this->note( "extracting $src..." );
200 wfShellExec( 'unzip ' . escapeshellarg( $src ) . ' -d ' . escapeshellarg( $this->extdir ) , $code );
201
202 if ( $code !== 0 ) {
203 $this->error( "failed to extract $src!" );
204 return false;
205 }
206 }
207 else {
208 $this->error( "unknown file extension: $ext" );
209 return false;
210 }
211 }
212
213 if ( !file_exists( $this->dir ) && glob( $this->dir . "/*" ) ) {
214 $this->error( "{$this->dir} does not exist or is empty. Something went wrong, sorry." );
215 return false;
216 }
217
218 #TODO: set permissions.... somehow. Copy from extension dir??
219
220 $this->note( "fetched extension to {$this->dir}" );
221 return true;
222 }
223
224 function patchLocalSettings( ) {
225 $f = $this->dir . '/install.settings';
226 $t = $this->target . '/LocalSettings.php';
227
228 #TODO: assert version ?!
229 #TODO: allow custom installer scripts
230
231 if ( !file_exists( $f ) ) {
232 $this->warn( "No install.settings file provided! Please read the instructions and edit LocalSettings.php manually." );
233 return '?';
234 }
235
236 $settings = file_get_contents( $f );
237
238 if ( !$settings ) {
239 $this->error( "failed to read settings from $f!" );
240 return false;
241 }
242
243 $settings = str_replace( '{{path}}', $this->incpath, $settings );
244
245 #NOTE: keep php extension for backup file!
246 $bak = $this->target . '/LocalSettings.install-' . $this->name . '-' . wfTimestamp(TS_MW) . '.bak.php';
247
248 $ok = copy( $t, $bak );
249
250 if ( !$ok ) {
251 $this->warn( "failed to create backup of LocalSettings.php!" );
252 return false;
253 }
254 else {
255 $this->note( "created backup of LocalSettings.php at $bak" );
256 }
257
258 $localsettings = file_get_contents( $t );
259
260 if ( !$settings ) {
261 $this->error( "failed to read $t for patching!" );
262 return false;
263 }
264
265 $marker = "<@< extension {$this->name} >@>";
266 $blockpattern = "/\n\s*#\s*BEGIN\s*$marker.*END\s*$marker\s*/smi";
267
268 if ( preg_match( $blockpattern, $localsettings ) ) {
269 $localsettings = preg_replace( $blockpattern, "\n", $localsettings );
270 $this->warn( "removed old configuration block for extension {$this->name}!" );
271 }
272
273 $newblock= "\n# BEGIN $marker\n$settings\n# END $marker\n";
274
275 $localsettings = preg_replace( "/\?>\s*$/si", "$newblock?>", $localsettings );
276
277 $ok = file_put_contents( $t, $localsettings );
278
279 if ( !$ok ) {
280 $this->error( "failed to patch $t!" );
281 return false;
282 }
283 else {
284 $this->note( "successfully patched LocalSettings.php" );
285 }
286
287 return true;
288 }
289
290 function printNotices( ) {
291 $files = array();
292
293 if ( file_exists( $this->dir . '/README' ) ) $files[] = 'README';
294 if ( file_exists( $this->dir . '/INSTALL' ) ) $files[] = 'INSTALL';
295
296 if ( !$files ) {
297 $this->note( "no information files found in {$this->dir}" );
298 }
299 else {
300 $this->note( "" );
301
302 $this->note( "Please have a look at the following files in {$this->dir}," );
303 $this->note( "they may contain important information about {$this->name}." );
304
305 $this->note( "" );
306
307 foreach ( $files as $f ) {
308 $this->note ( "\t* $f" );
309 }
310
311 $this->note( "" );
312 }
313
314 return true;
315 }
316 }
317
318 if( !isset( $args[0] ) ) {
319 die( "USAGE: installExtension.php [options] name [source]\n" .
320 "OPTIONS: \n" .
321 " --target <dir> mediawiki installation directory\n" .
322 "SOURCE: \n" .
323 " May be a local file (tgz or zip) or directory.\n" .
324 " May be the URL of a remote file (tgz or zip).\n" .
325 " May be a SVN repository\n"
326 );
327 }
328
329 $name = $args[0];
330
331 # Default to SVN trunk. Perhaps change that to use the version of the present install,
332 # and/or use bundles at an official download location.
333 # Also, perhaps use the local systems versioin to select the right branch
334 $defsrc = "http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/" . urlencode($name);
335
336 $src = isset ( $args[1] ) ? $args[1] : $defsrc;
337
338 $tgt = isset ( $options['target'] ) ? $options['target'] : $IP;
339
340 if ( !file_exists( "$tgt/LocalSettings.php" ) ) {
341 die("can't find $tgt/LocalSettings.php\n");
342 }
343
344 if ( !is_writable( "$tgt/LocalSettings.php" ) ) {
345 die("can't write to $tgt/LocalSettings.php\n");
346 }
347
348 if ( !file_exists( "$tgt/extensions" ) ) {
349 die("can't find $tgt/extensions\n");
350 }
351
352 if ( !is_writable( "$tgt/extensions" ) ) {
353 die("can't write to $tgt/extensions\n");
354 }
355
356 $installer = new ExtensionInstaller( $name, $src, $tgt );
357
358 $installer->note( "Installing extension {$installer->name} from {$installer->source} to {$installer->dir}" );
359
360 print "\n";
361 print "\tTHIS TOOL IS EXPERIMENTAL!\n";
362 print "\tEXPECT THE UNEXPECTED!\n";
363 print "\n";
364
365 if ( !$installer->confirm("continue") ) die("aborted\n");
366
367 $ok = $installer->fetchExtension();
368 if ( $ok ) $ok = $installer->patchLocalSettings();
369 if ( $ok ) $ok = $installer->printNotices();
370 if ( $ok ) $installer->note( "$name extension was installed successfully" );
371 ?>