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
20 * @addtogroup Maintenance
23 $optionsWithArgs = array( 'target', 'repository', 'repos' );
25 require_once( 'commandLine.inc' );
27 define('EXTINST_NOPATCH', 0);
28 define('EXTINST_WRITEPATCH', 6);
29 define('EXTINST_HOTPATCH', 10);
31 class InstallerRepository
{
34 function InstallerRepository( $path ) {
38 function printListing( ) {
39 trigger_error( 'override InstallerRepository::printListing()', E_USER_ERROR
);
42 function getResource( $name ) {
43 trigger_error( 'override InstallerRepository::getResource()', E_USER_ERROR
);
46 /*static*/ function makeRepository( $path, $type = NULL ) {
49 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
54 } else if ( ( $proto == 'http' ||
$proto == 'https' ) && preg_match( '!([^\w]svn|svn[^\w])!i', $path) ) {
61 if ( $type == 'dir' ||
$type == 'file' ) { return new LocalInstallerRepository( $path ); }
62 else if ( $type == 'http' ||
$type == 'http' ) { return new WebInstallerRepository( $path ); }
63 else { return new SVNInstallerRepository( $path ); }
67 class LocalInstallerRepository
extends InstallerRepository
{
69 function LocalInstallerRepository ( $path ) {
70 InstallerRepository
::InstallerRepository( $path );
73 function printListing( ) {
74 $ff = glob( "{$this->path}/*" );
75 if ( $ff === false ||
$ff === NULL ) {
76 ExtensionInstaller
::error( "listing directory {$this->path} failed!" );
80 foreach ( $ff as $f ) {
83 if ( !is_dir( $f ) ) {
85 if ( !preg_match( '/(.*)\.(tgz|tar\.gz|zip)/', $n, $m ) ) continue;
93 function getResource( $name ) {
94 $path = $this->path
. '/' . $name;
96 if ( !file_exists( $path ) ||
!is_dir( $path ) ) $path = $this->path
. '/' . $name . '.tgz';
97 if ( !file_exists( $path ) ) $path = $this->path
. '/' . $name . '.tar.gz';
98 if ( !file_exists( $path ) ) $path = $this->path
. '/' . $name . '.zip';
100 return new LocalInstallerResource( $path );
104 class WebInstallerRepository
extends InstallerRepository
{
106 function WebInstallerRepository ( $path ) {
107 InstallerRepository
::InstallerRepository( $path );
110 function printListing( ) {
111 ExtensionInstaller
::note( "listing index from {$this->path}..." );
113 $txt = @file_get_contents
( $this->path
. '/index.txt' );
119 $txt = file_get_contents( $this->path
);
121 ExtensionInstaller
::error( "listing index from {$this->path} failed!" );
127 $ok = preg_match_all( '!<a\s[^>]*href\s*=\s*['."'".'"]([^/'."'".'"]+)\.tgz['."'".'"][^>]*>.*?</a>!si', $txt, $m, PREG_SET_ORDER
);
129 ExtensionInstaller
::error( "listing index from {$this->path} does not match!" );
134 foreach ( $m as $l ) {
141 function getResource( $name ) {
142 $path = $this->path
. '/' . $name . '.tgz';
143 return new WebInstallerResource( $path );
147 class SVNInstallerRepository
extends InstallerRepository
{
149 function SVNInstallerRepository ( $path ) {
150 InstallerRepository
::InstallerRepository( $path );
153 function printListing( ) {
154 ExtensionInstaller
::note( "SVN list {$this->path}..." );
155 $code = null; // Shell Exec return value.
156 $txt = wfShellExec( 'svn ls ' . escapeshellarg( $this->path
), $code );
158 ExtensionInstaller
::error( "svn list for {$this->path} failed!" );
162 $ll = preg_split('/(\s*[\r\n]\s*)+/', $txt);
164 foreach ( $ll as $line ) {
166 if ( !preg_match('!^(.*)/$!', $line, $m) ) continue;
173 function getResource( $name ) {
174 $path = $this->path
. '/' . $name;
175 return new SVNInstallerResource( $path );
179 class InstallerResource
{
184 function InstallerResource( $path, $isdir, $islocal ) {
187 $this->isdir
= $isdir;
188 $this->islocal
= $islocal;
191 preg_match( '!([-+\w]+://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
193 $this->protocol
= @$m[1];
194 $this->extensions
= @$m[2];
196 if ( $this->extensions
) $this->extensions
= strtolower( $this->extensions
);
199 function fetch( $target ) {
200 trigger_error( 'override InstallerResource::fetch()', E_USER_ERROR
);
203 function extract( $file, $target ) {
205 if ( $this->extensions
== '.tgz' ||
$this->extensions
== '.tar.gz' ) { #tgz file
206 ExtensionInstaller
::note( "extracting $file..." );
207 $code = null; // shell Exec return value.
208 wfShellExec( 'tar zxvf ' . escapeshellarg( $file ) . ' -C ' . escapeshellarg( $target ), $code );
211 ExtensionInstaller
::error( "failed to extract $file!" );
215 else if ( $this->extensions
== '.zip' ) { #zip file
216 ExtensionInstaller
::note( "extracting $file..." );
217 $code = null; // shell Exec return value.
218 wfShellExec( 'unzip ' . escapeshellarg( $file ) . ' -d ' . escapeshellarg( $target ) , $code );
221 ExtensionInstaller
::error( "failed to extract $file!" );
226 ExtensionInstaller
::error( "unknown extension {$this->extensions}!" );
233 /*static*/ function makeResource( $url ) {
235 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $url, $m );
238 if ( $ext ) $ext = strtolower( $ext );
240 if ( !$proto ) { return new LocalInstallerResource( $url, $ext ?
false : true ); }
241 else if ( $ext && ( $proto == 'http' ||
$proto == 'http' ||
$proto == 'ftp' ) ) { return new WebInstallerResource( $url ); }
242 else { return new SVNInstallerResource( $url ); }
246 class LocalInstallerResource
extends InstallerResource
{
247 function LocalInstallerResource( $path ) {
248 InstallerResource
::InstallerResource( $path, is_dir( $path ), true );
251 function fetch( $target ) {
252 if ( $this->isdir
) return ExtensionInstaller
::copyDir( $this->path
, dirname( $target ) );
253 else return $this->extract( $this->path
, dirname( $target ) );
258 class WebInstallerResource
extends InstallerResource
{
259 function WebInstallerResource( $path ) {
260 InstallerResource
::InstallerResource( $path, false, false );
263 function fetch( $target ) {
264 $tmp = wfTempDir() . '/' . basename( $this->path
);
266 ExtensionInstaller
::note( "downloading {$this->path}..." );
267 $ok = copy( $this->path
, $tmp );
270 ExtensionInstaller
::error( "failed to download {$this->path}" );
274 $this->extract( $tmp, dirname( $target ) );
281 class SVNInstallerResource
extends InstallerResource
{
282 function SVNInstallerResource( $path ) {
283 InstallerResource
::InstallerResource( $path, true, false );
286 function fetch( $target ) {
287 ExtensionInstaller
::note( "SVN checkout of {$this->path}..." );
288 $code = null; // shell exec return val.
289 wfShellExec( 'svn co ' . escapeshellarg( $this->path
) . ' ' . escapeshellarg( $target ), $code );
292 ExtensionInstaller
::error( "checkout failed for {$this->path}!" );
300 class ExtensionInstaller
{
307 function ExtensionInstaller( $name, $source, $target ) {
308 if ( !is_object( $source ) ) $source = InstallerResource
::makeResource( $source );
311 $this->source
= $source;
312 $this->target
= realpath( $target );
313 $this->extdir
= "$target/extensions";
314 $this->dir
= "{$this->extdir}/$name";
315 $this->incpath
= "extensions/$name";
316 $this->tasks
= array();
318 #TODO: allow a subdir different from "extensions"
319 #TODO: allow a config file different from "LocalSettings.php"
322 function note( $msg ) {
326 function warn( $msg ) {
327 print "WARNING: $msg\n";
330 function error( $msg ) {
331 print "ERROR: $msg\n";
334 function prompt( $msg ) {
335 if ( function_exists( 'readline' ) ) {
336 $s = readline( $msg );
339 if ( !@$this->stdin
) $this->stdin
= fopen( 'php://stdin', 'r' );
340 if ( !$this->stdin
) die( "Failed to open stdin for user interaction!\n" );
345 $s = fgets( $this->stdin
);
352 function confirm( $msg ) {
354 $s = $this->prompt( $msg . " [yes/no]: ");
355 $s = strtolower( trim($s) );
357 if ( $s == 'yes' ||
$s == 'y' ) { return true; }
358 else if ( $s == 'no' ||
$s == 'n' ) { return false; }
359 else { print "bad response: $s\n"; }
363 function deleteContents( $dir ) {
364 $ff = glob( $dir . "/*" );
367 foreach ( $ff as $f ) {
368 if ( is_dir( $f ) && !is_link( $f ) ) $this->deleteContents( $f );
373 function copyDir( $dir, $tgt ) {
374 $d = $tgt . '/' . basename( $dir );
376 if ( !file_exists( $d ) ) {
379 ExtensionInstaller
::error( "failed to create director $d" );
384 $ff = glob( $dir . "/*" );
385 if ( $ff === false ||
$ff === NULL ) return false;
387 foreach ( $ff as $f ) {
388 if ( is_dir( $f ) && !is_link( $f ) ) {
389 $ok = ExtensionInstaller
::copyDir( $f, $d );
390 if ( !$ok ) return false;
393 $t = $d . '/' . basename( $f );
394 $ok = copy( $f, $t );
397 ExtensionInstaller
::error( "failed to copy $f to $t" );
406 function setPermissions( $dir, $dirbits, $filebits ) {
407 if ( !chmod( $dir, $dirbits ) ) ExtensionInstaller
::warn( "faield to set permissions for $dir" );
409 $ff = glob( $dir . "/*" );
410 if ( $ff === false ||
$ff === NULL ) return false;
412 foreach ( $ff as $f ) {
414 if ( $n{0} == '.' ) continue; #HACK: skip dot files
416 if ( is_link( $f ) ) continue; #skip link
418 if ( is_dir( $f ) ) {
419 ExtensionInstaller
::setPermissions( $f, $dirbits, $filebits );
422 if ( !chmod( $f, $filebits ) ) ExtensionInstaller
::warn( "faield to set permissions for $f" );
429 function fetchExtension( ) {
430 if ( $this->source
->islocal
&& $this->source
->isdir
&& realpath( $this->source
->path
) === $this->dir
) {
431 $this->note( "files are already in the extension dir" );
435 if ( file_exists( $this->dir
) && glob( $this->dir
. "/*" ) ) {
436 if ( $this->confirm( "{$this->dir} exists and is not empty.\nDelete all files in that directory?" ) ) {
437 $this->deleteContents( $this->dir
);
444 $ok = $this->source
->fetch( $this->dir
);
445 if ( !$ok ) return false;
447 if ( !file_exists( $this->dir
) && glob( $this->dir
. "/*" ) ) {
448 $this->error( "{$this->dir} does not exist or is empty. Something went wrong, sorry." );
452 if ( file_exists( $this->dir
. '/README' ) ) $this->tasks
[] = "read the README file in {$this->dir}";
453 if ( file_exists( $this->dir
. '/INSTALL' ) ) $this->tasks
[] = "read the INSTALL file in {$this->dir}";
454 if ( file_exists( $this->dir
. '/RELEASE-NOTES' ) ) $this->tasks
[] = "read the RELEASE-NOTES file in {$this->dir}";
456 #TODO: configure this smartly...?
457 $this->setPermissions( $this->dir
, 0755, 0644 );
459 $this->note( "fetched extension to {$this->dir}" );
463 function patchLocalSettings( $mode ) {
464 #NOTE: if we get a better way to hook up extensions, that should be used instead.
466 $f = $this->dir
. '/install.settings';
467 $t = $this->target
. '/LocalSettings.php';
469 #TODO: assert version ?!
470 #TODO: allow custom installer scripts + sql patches
472 if ( !file_exists( $f ) ) {
473 $this->warn( "No install.settings file provided!" );
474 $this->tasks
[] = "Please read the instructions and edit LocalSettings.php manually to activate the extension.";
478 $this->note( "applying settings patch..." );
481 $settings = file_get_contents( $f );
484 $this->error( "failed to read settings from $f!" );
488 $settings = str_replace( '{{path}}', $this->incpath
, $settings );
490 if ( $mode == EXTINST_NOPATCH
) {
491 $this->tasks
[] = "Please put the following into your LocalSettings.php:" . "\n$settings\n";
492 $this->note( "Skipping patch phase, automatic patching is off." );
496 if ( $mode == EXTINST_HOTPATCH
) {
497 #NOTE: keep php extension for backup file!
498 $bak = $this->target
. '/LocalSettings.install-' . $this->name
. '-' . wfTimestamp(TS_MW
) . '.bak.php';
500 $ok = copy( $t, $bak );
503 $this->warn( "failed to create backup of LocalSettings.php!" );
507 $this->note( "created backup of LocalSettings.php at $bak" );
511 $localsettings = file_get_contents( $t );
514 $this->error( "failed to read $t for patching!" );
518 $marker = "<@< extension {$this->name} >@>";
519 $blockpattern = "/\n\s*#\s*BEGIN\s*$marker.*END\s*$marker\s*/smi";
521 if ( preg_match( $blockpattern, $localsettings ) ) {
522 $localsettings = preg_replace( $blockpattern, "\n", $localsettings );
523 $this->warn( "removed old configuration block for extension {$this->name}!" );
526 $newblock= "\n# BEGIN $marker\n$settings\n# END $marker\n";
528 $localsettings = preg_replace( "/\?>\s*$/si", "$newblock?>", $localsettings );
530 if ( $mode != EXTINST_HOTPATCH
) {
531 $t = $this->target
. '/LocalSettings.install-' . $this->name
. '-' . wfTimestamp(TS_MW
) . '.php';
534 $ok = file_put_contents( $t, $localsettings );
537 $this->error( "failed to patch $t!" );
540 else if ( $mode == EXTINST_HOTPATCH
) {
541 $this->note( "successfully patched $t" );
544 $this->note( "created patched settings file $t" );
545 $this->tasks
[] = "Replace your current LocalSettings.php with ".basename($t);
551 function printNotices( ) {
552 if ( !$this->tasks
) {
553 $this->note( "Installation is complete, no pending tasks" );
557 $this->note( "PENDING TASKS:" );
560 foreach ( $this->tasks
as $t ) {
561 $this->note ( "* " . $t );
572 $tgt = isset ( $options['target'] ) ?
$options['target'] : $IP;
574 $repos = @$options['repository'];
575 if ( !$repos ) $repos = @$options['repos'];
576 if ( !$repos ) $repos = @$wgExtensionInstallerRepository;
578 if ( !$repos && file_exists("$tgt/.svn") && is_dir("$tgt/.svn") ) {
579 $svn = file_get_contents( "$tgt/.svn/entries" );
582 if ( preg_match( '!url="(.*?)"!', $svn, $m ) ) {
583 $repos = dirname( $m[1] ) . '/extensions';
587 if ( !$repos ) $repos = 'http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions';
589 if( !isset( $args[0] ) && !@$options['list'] ) {
590 die( "USAGE: installExtension.php [options] <name> [source]\n" .
592 " --list list available extensions. <name> is ignored / may be omitted.\n" .
593 " --repository <n> repository to fetch extensions from. May be a local directoy,\n" .
594 " an SVN repository or a HTTP directory\n" .
595 " --target <dir> mediawiki installation directory to use\n" .
596 " --nopatch don't create a patched LocalSettings.php\n" .
597 " --hotpatch patched LocalSettings.php directly (creates a backup)\n" .
598 "SOURCE: specifies the package source directly. If given, the repository is ignored.\n" .
599 " The source my be a local file (tgz or zip) or directory, the URL of a\n" .
600 " remote file (tgz or zip), or a SVN path.\n"
604 $repository = InstallerRepository
::makeRepository( $repos );
606 if ( isset( $options['list'] ) ) {
607 $repository->printListing();
613 $src = isset( $args[1] ) ?
$args[1] : $repository->getResource( $name );
615 #TODO: detect $source mismatching $name !!
617 $mode = EXTINST_WRITEPATCH
;
618 if ( isset( $options['nopatch'] ) ||
@$wgExtensionInstallerNoPatch ) { $mode = EXTINST_NOPATCH
; }
619 else if ( isset( $options['hotpatch'] ) ||
@$wgExtensionInstallerHotPatch ) { $mode = EXTINST_HOTPATCH
; }
621 if ( !file_exists( "$tgt/LocalSettings.php" ) ) {
622 die("can't find $tgt/LocalSettings.php\n");
625 if ( $mode == EXTINST_HOTPATCH
&& !is_writable( "$tgt/LocalSettings.php" ) ) {
626 die("can't write to $tgt/LocalSettings.php\n");
629 if ( !file_exists( "$tgt/extensions" ) ) {
630 die("can't find $tgt/extensions\n");
633 if ( !is_writable( "$tgt/extensions" ) ) {
634 die("can't write to $tgt/extensions\n");
637 $installer = new ExtensionInstaller( $name, $src, $tgt );
639 $installer->note( "Installing extension {$installer->name} from {$installer->source->path} to {$installer->dir}" );
642 print "\tTHIS TOOL IS EXPERIMENTAL!\n";
643 print "\tEXPECT THE UNEXPECTED!\n";
646 if ( !$installer->confirm("continue") ) die("aborted\n");
648 $ok = $installer->fetchExtension();
650 if ( $ok ) $ok = $installer->patchLocalSettings( $mode );
652 if ( $ok ) $ok = $installer->printNotices();
654 if ( $ok ) $installer->note( "$name extension installed." );