3 * Copyright (C) 2006 Daniel Kinzler, brightbyte.de
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.
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.
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
21 * @subpackage Maintenance
24 $optionsWithArgs = array( 'target' );
26 require_once( 'commandLine.inc' );
28 class ExtensionInstaller
{
34 function ExtensionInstaller( $name, $source, $target ) {
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";
42 #TODO: allow a subdir different from "extensions"
43 #TODO: allow a config file different from "LocalSettings.php"
46 function note( $msg ) {
50 function warn( $msg ) {
51 print "WARNING: $msg\n";
54 function error( $msg ) {
55 print "ERROR: $msg\n";
58 function prompt( $msg ) {
59 if ( function_exists( 'readline' ) ) {
60 $s = readline( $msg );
63 if ( !@$this->stdin
) $this->stdin
= fopen( 'php://stdin', 'r' );
64 if ( !$this->stdin
) die( "Failed to open stdin for user interaction!\n" );
69 $s = fgets( $this->stdin
);
76 function confirm( $msg ) {
78 $s = $this->prompt( $msg . " [yes/no]: ");
79 $s = strtolower( trim($s) );
81 if ( $s == 'yes' ||
$s == 'y' ) return true;
82 else if ( $s == 'no' ||
$s == 'n' ) return false;
83 else print "bad response: $s\n";
87 function deleteContents( $dir ) {
88 $ff = glob( $dir . "/*" );
91 foreach ( $ff as $f ) {
92 if ( is_dir( $f ) ) $this->deleteContents( $f );
97 function copyDir( $dir, $tgt ) {
98 $d = $tgt . '/' . basename( $dir );
100 if ( !file_exists( $d ) ) {
103 $this->error( "failed to create director $d" );
108 $ff = glob( $dir . "/*" );
109 if ( $ff === false ||
$ff === NULL ) return false;
111 foreach ( $ff as $f ) {
112 if ( is_dir( $f ) ) {
113 $ok = $this->copyDir( $f, $d );
114 if ( !$ok ) return false;
117 $t = $d . '/' . basename( $f );
118 $ok = copy( $f, $t );
121 $this->error( "failed to copy $f to $t" );
130 function fetchExtension( ) {
131 if ( file_exists( $this->dir
) && glob( $this->dir
. "/*" )
132 && realpath( $this->source
) != $this->dir
) {
134 if ( $this->confirm( "{$this->dir} exists and is not empty.\nDelete all files in that directory?" ) ) {
135 $this->deleteContents( $this->dir
);
142 preg_match( '!([-\w]+://)?.*?(\.[-\w\d.]+)?$!', $this->source
, $m );
145 if ( $ext ) $ext = strtolower( $ext );
147 $src = $this->source
;
149 #TODO: check that the required program is available.
150 #may be used: tar, unzip, svn
152 if ( $proto && $ext ) { #remote file
153 $tmp = wfTempDir() . '/' . basename( $src );
155 $this->note( "fetching {$this->source}..." );
156 $ok = copy( $src, $tmp );
159 $this->error( "failed to download {$src}" );
167 if ( $proto ) { #assume SVN repository
168 $this->note( "SVN checkout of $src..." );
169 wfShellExec( 'svn co ' . escapeshellarg( $src ) . ' ' . escapeshellarg( $this->dir
), $code );
172 $this->error( "checkout failed for $src!" );
176 else { #local file or directory
177 $src = realpath ( $src );
179 if ( !file_exists( $src ) ) {
180 $this->error( "file not found: {$this->source}" );
184 if ( $ext === NULL ||
$ext === '') { #local dir
185 if ( $src == $this->dir
) {
186 $this->note( "files are already in the extension dir" );
190 $this->copyDir( $src, $this->extdir
);
192 else if ( $ext == '.tgz' ||
$ext == '.tar.gz' ) { #tgz file
193 $this->note( "extracting $src..." );
194 wfShellExec( 'tar zxvf ' . escapeshellarg( $src ) . ' -C ' . escapeshellarg( $this->extdir
), $code );
197 $this->error( "failed to extract $src!" );
201 else if ( $ext == '.zip' ) { #zip file
202 $this->note( "extracting $src..." );
203 wfShellExec( 'unzip ' . escapeshellarg( $src ) . ' -d ' . escapeshellarg( $this->extdir
) , $code );
206 $this->error( "failed to extract $src!" );
211 $this->error( "unknown file extension: $ext" );
216 if ( !file_exists( $this->dir
) && glob( $this->dir
. "/*" ) ) {
217 $this->error( "{$this->dir} does not exist or is empty. Something went wrong, sorry." );
221 #TODO: set permissions.... somehow. Copy from extension dir??
223 $this->note( "fetched extension to {$this->dir}" );
227 function patchLocalSettings( ) {
228 #NOTE: if we get a better way to hook up extensions, that should be used instead.
230 $f = $this->dir
. '/install.settings';
231 $t = $this->target
. '/LocalSettings.php';
233 #TODO: assert version ?!
234 #TODO: allow custom installer scripts + sql patches
236 if ( !file_exists( $f ) ) {
237 $this->warn( "No install.settings file provided! Please read the instructions and edit LocalSettings.php manually." );
241 $settings = file_get_contents( $f );
244 $this->error( "failed to read settings from $f!" );
248 $settings = str_replace( '{{path}}', $this->incpath
, $settings );
250 #NOTE: keep php extension for backup file!
251 $bak = $this->target
. '/LocalSettings.install-' . $this->name
. '-' . wfTimestamp(TS_MW
) . '.bak.php';
253 $ok = copy( $t, $bak );
256 $this->warn( "failed to create backup of LocalSettings.php!" );
260 $this->note( "created backup of LocalSettings.php at $bak" );
263 $localsettings = file_get_contents( $t );
266 $this->error( "failed to read $t for patching!" );
270 $marker = "<@< extension {$this->name} >@>";
271 $blockpattern = "/\n\s*#\s*BEGIN\s*$marker.*END\s*$marker\s*/smi";
273 if ( preg_match( $blockpattern, $localsettings ) ) {
274 $localsettings = preg_replace( $blockpattern, "\n", $localsettings );
275 $this->warn( "removed old configuration block for extension {$this->name}!" );
278 $newblock= "\n# BEGIN $marker\n$settings\n# END $marker\n";
280 $localsettings = preg_replace( "/\?>\s*$/si", "$newblock?>", $localsettings );
282 $ok = file_put_contents( $t, $localsettings );
285 $this->error( "failed to patch $t!" );
289 $this->note( "successfully patched LocalSettings.php" );
295 function printNotices( ) {
298 if ( file_exists( $this->dir
. '/README' ) ) $files[] = 'README';
299 if ( file_exists( $this->dir
. '/INSTALL' ) ) $files[] = 'INSTALL';
302 $this->note( "no information files found in {$this->dir}" );
307 $this->note( "Please have a look at the following files in {$this->dir}," );
308 $this->note( "they may contain important information about {$this->name}." );
312 foreach ( $files as $f ) {
313 $this->note ( "\t* $f" );
322 /* static */ function listRepository( $repos ) {
323 preg_match( '!([-\w]+://)?.*?(\.[-\w\d.]+)?$!', $repos, $m );
326 #TODO: right now, this basically lists filenames, so it's not terribly useful.
327 #In future, there should be a "repository + logical name" scheme
329 if ( $proto == 'http://' ) { #HTML directory listing
330 ExtensionInstaller
::note( "listing index from $repos..." );
332 $txt = file_get_contents( $repos );
334 $ok = preg_match_all( '!<a\s[^>]*href\s*=\s*['."'".'"]([^/'."'".'"]+)['."'".'"][^>]*>.*?</a>!si', $txt, $m, PREG_SET_ORDER
);
336 ExtensionInstaller
::error( "listing index from $repos failed!" );
341 foreach ( $m as $l ) {
344 if ( preg_match('!^[./?]!', $n) ) continue;
346 ExtensionInstaller
::note( "\t$n" );
349 else if ( !$proto ) { #local directory
350 ExtensionInstaller
::note( "listing directory $repos..." );
352 $ff = glob( "$repos/*" );
353 if ( $ff === false ||
$ff === NULL ) {
354 ExtensionInstaller
::error( "listing directory $repos failed!" );
358 foreach ( $ff as $f ) {
361 ExtensionInstaller
::note( "\t$n" );
365 ExtensionInstaller
::note( "SVN list $repos..." );
366 $txt = wfShellExec( 'svn ls ' . escapeshellarg( $repos ), $code );
369 ExtensionInstaller
::error( "svn list for $repos failed!" );
373 $ll = preg_split('/(\s*[\r\n]\s*)+/', $txt);
375 foreach ( $ll as $line ) {
376 if ( !preg_match('!^(.*)/$!', $line, $m) ) continue;
378 ExtensionInstaller
::note( "\t{$m[1]}" );
384 if ( isset( $options['list'] ) ) {
385 $repos = $options['list'];
386 if ( $repos === true ||
$repos === 1 ) {
387 # Default to SVN trunk. Perhaps change that to use the version of the present install,
388 # and/or use bundles at an official download location.
389 $repos = 'http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/';
392 ExtensionInstaller
::listRepository( $repos );
397 if( !isset( $args[0] ) ) {
398 die( "USAGE: installExtension.php [options] name [source]\n" .
400 " --target=<dir> mediawiki installation directory\n" .
401 " --nopatch don't touch LocalSettings.php\n" .
403 " May be a local file (tgz or zip) or directory.\n" .
404 " May be the URL of a remote file (tgz or zip).\n" .
405 " May be a SVN repository\n"
411 # Default to SVN trunk. Perhaps change that to use the version of the present install,
412 # and/or use bundles at an official download location.
413 $defsrc = "http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/" . urlencode($name);
415 $src = isset ( $args[1] ) ?
$args[1] : $defsrc;
417 $tgt = isset ( $options['target'] ) ?
$options['target'] : $IP;
419 if ( !file_exists( "$tgt/LocalSettings.php" ) ) {
420 die("can't find $tgt/LocalSettings.php\n");
423 if ( !is_writable( "$tgt/LocalSettings.php" ) ) {
424 die("can't write to $tgt/LocalSettings.php\n");
427 if ( !file_exists( "$tgt/extensions" ) ) {
428 die("can't find $tgt/extensions\n");
431 if ( !is_writable( "$tgt/extensions" ) ) {
432 die("can't write to $tgt/extensions\n");
435 $installer = new ExtensionInstaller( $name, $src, $tgt );
437 $installer->note( "Installing extension {$installer->name} from {$installer->source} to {$installer->dir}" );
440 print "\tTHIS TOOL IS EXPERIMENTAL!\n";
441 print "\tEXPECT THE UNEXPECTED!\n";
444 if ( !$installer->confirm("continue") ) die("aborted\n");
446 $ok = $installer->fetchExtension();
449 if ( isset( $options['nopatch'] ) ) $installer->note( "skipping patch phase; Please edit LocalSettings.php manually to activate the extension." );
450 else $ok = $installer->patchLocalSettings();
453 $ok = $installer->printNotices();
455 if ( $ok ) $installer->note( "$name extension was installed successfully" );