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 * @ingroup Maintenance
24 $optionsWithArgs = array( 'target', 'repository', 'repos' );
26 require_once( dirname(__FILE__
) . '/commandLine.inc' );
28 define('EXTINST_NOPATCH', 0);
29 define('EXTINST_WRITEPATCH', 6);
30 define('EXTINST_HOTPATCH', 10);
33 * @ingroup Maintenance
35 class InstallerRepository
{
38 function InstallerRepository( $path ) {
42 function printListing( ) {
43 trigger_error( 'override InstallerRepository::printListing()', E_USER_ERROR
);
46 function getResource( $name ) {
47 trigger_error( 'override InstallerRepository::getResource()', E_USER_ERROR
);
50 static function makeRepository( $path, $type = NULL ) {
53 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
58 } else if ( ( $proto == 'http' ||
$proto == 'https' ) && preg_match( '!([^\w]svn|svn[^\w])!i', $path) ) {
65 if ( $type == 'dir' ||
$type == 'file' ) { return new LocalInstallerRepository( $path ); }
66 else if ( $type == 'http' ||
$type == 'http' ) { return new WebInstallerRepository( $path ); }
67 else { return new SVNInstallerRepository( $path ); }
72 * @ingroup Maintenance
74 class LocalInstallerRepository
extends InstallerRepository
{
76 function LocalInstallerRepository ( $path ) {
77 InstallerRepository
::InstallerRepository( $path );
80 function printListing( ) {
81 $ff = glob( "{$this->path}/*" );
82 if ( $ff === false ||
$ff === NULL ) {
83 ExtensionInstaller
::error( "listing directory {$this->path} failed!" );
87 foreach ( $ff as $f ) {
90 if ( !is_dir( $f ) ) {
92 if ( !preg_match( '/(.*)\.(tgz|tar\.gz|zip)/', $n, $m ) ) continue;
100 function getResource( $name ) {
101 $path = $this->path
. '/' . $name;
103 if ( !file_exists( $path ) ||
!is_dir( $path ) ) $path = $this->path
. '/' . $name . '.tgz';
104 if ( !file_exists( $path ) ) $path = $this->path
. '/' . $name . '.tar.gz';
105 if ( !file_exists( $path ) ) $path = $this->path
. '/' . $name . '.zip';
107 return new LocalInstallerResource( $path );
112 * @ingroup Maintenance
114 class WebInstallerRepository
extends InstallerRepository
{
116 function WebInstallerRepository ( $path ) {
117 InstallerRepository
::InstallerRepository( $path );
120 function printListing( ) {
121 ExtensionInstaller
::note( "listing index from {$this->path}..." );
123 $txt = @file_get_contents
( $this->path
. '/index.txt' );
129 $txt = file_get_contents( $this->path
);
131 ExtensionInstaller
::error( "listing index from {$this->path} failed!" );
137 $ok = preg_match_all( '!<a\s[^>]*href\s*=\s*['."'".'"]([^/'."'".'"]+)\.tgz['."'".'"][^>]*>.*?</a>!si', $txt, $m, PREG_SET_ORDER
);
139 ExtensionInstaller
::error( "listing index from {$this->path} does not match!" );
144 foreach ( $m as $l ) {
151 function getResource( $name ) {
152 $path = $this->path
. '/' . $name . '.tgz';
153 return new WebInstallerResource( $path );
158 * @ingroup Maintenance
160 class SVNInstallerRepository
extends InstallerRepository
{
162 function SVNInstallerRepository ( $path ) {
163 InstallerRepository
::InstallerRepository( $path );
166 function printListing( ) {
167 ExtensionInstaller
::note( "SVN list {$this->path}..." );
168 $code = null; // Shell Exec return value.
169 $txt = wfShellExec( 'svn ls ' . escapeshellarg( $this->path
), $code );
171 ExtensionInstaller
::error( "svn list for {$this->path} failed!" );
175 $ll = preg_split('/(\s*[\r\n]\s*)+/', $txt);
177 foreach ( $ll as $line ) {
179 if ( !preg_match('!^(.*)/$!', $line, $m) ) continue;
186 function getResource( $name ) {
187 $path = $this->path
. '/' . $name;
188 return new SVNInstallerResource( $path );
193 * @ingroup Maintenance
195 class InstallerResource
{
200 function InstallerResource( $path, $isdir, $islocal ) {
203 $this->isdir
= $isdir;
204 $this->islocal
= $islocal;
207 preg_match( '!([-+\w]+://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
209 $this->protocol
= @$m[1];
210 $this->extensions
= @$m[2];
212 if ( $this->extensions
) $this->extensions
= strtolower( $this->extensions
);
215 function fetch( $target ) {
216 trigger_error( 'override InstallerResource::fetch()', E_USER_ERROR
);
219 function extract( $file, $target ) {
221 if ( $this->extensions
== '.tgz' ||
$this->extensions
== '.tar.gz' ) { #tgz file
222 ExtensionInstaller
::note( "extracting $file..." );
223 $code = null; // shell Exec return value.
224 wfShellExec( 'tar zxvf ' . escapeshellarg( $file ) . ' -C ' . escapeshellarg( $target ), $code );
227 ExtensionInstaller
::error( "failed to extract $file!" );
231 else if ( $this->extensions
== '.zip' ) { #zip file
232 ExtensionInstaller
::note( "extracting $file..." );
233 $code = null; // shell Exec return value.
234 wfShellExec( 'unzip ' . escapeshellarg( $file ) . ' -d ' . escapeshellarg( $target ) , $code );
237 ExtensionInstaller
::error( "failed to extract $file!" );
242 ExtensionInstaller
::error( "unknown extension {$this->extensions}!" );
249 /*static*/ function makeResource( $url ) {
251 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $url, $m );
254 if ( $ext ) $ext = strtolower( $ext );
256 if ( !$proto ) { return new LocalInstallerResource( $url, $ext ?
false : true ); }
257 else if ( $ext && ( $proto == 'http' ||
$proto == 'http' ||
$proto == 'ftp' ) ) { return new WebInstallerResource( $url ); }
258 else { return new SVNInstallerResource( $url ); }
263 * @ingroup Maintenance
265 class LocalInstallerResource
extends InstallerResource
{
266 function LocalInstallerResource( $path ) {
267 InstallerResource
::InstallerResource( $path, is_dir( $path ), true );
270 function fetch( $target ) {
271 if ( $this->isdir
) return ExtensionInstaller
::copyDir( $this->path
, dirname( $target ) );
272 else return $this->extract( $this->path
, dirname( $target ) );
278 * @ingroup Maintenance
280 class WebInstallerResource
extends InstallerResource
{
281 function WebInstallerResource( $path ) {
282 InstallerResource
::InstallerResource( $path, false, false );
285 function fetch( $target ) {
286 $tmp = wfTempDir() . '/' . basename( $this->path
);
288 ExtensionInstaller
::note( "downloading {$this->path}..." );
289 $ok = copy( $this->path
, $tmp );
292 ExtensionInstaller
::error( "failed to download {$this->path}" );
296 $this->extract( $tmp, dirname( $target ) );
304 * @ingroup Maintenance
306 class SVNInstallerResource
extends InstallerResource
{
307 function SVNInstallerResource( $path ) {
308 InstallerResource
::InstallerResource( $path, true, false );
311 function fetch( $target ) {
312 ExtensionInstaller
::note( "SVN checkout of {$this->path}..." );
313 $code = null; // shell exec return val.
314 wfShellExec( 'svn co ' . escapeshellarg( $this->path
) . ' ' . escapeshellarg( $target ), $code );
317 ExtensionInstaller
::error( "checkout failed for {$this->path}!" );
326 * @ingroup Maintenance
328 class ExtensionInstaller
{
335 function ExtensionInstaller( $name, $source, $target ) {
336 if ( !is_object( $source ) ) $source = InstallerResource
::makeResource( $source );
339 $this->source
= $source;
340 $this->target
= realpath( $target );
341 $this->extdir
= "$target/extensions";
342 $this->dir
= "{$this->extdir}/$name";
343 $this->incpath
= "extensions/$name";
344 $this->tasks
= array();
346 #TODO: allow a subdir different from "extensions"
347 #TODO: allow a config file different from "LocalSettings.php"
350 static function note( $msg ) {
354 static function warn( $msg ) {
355 print "WARNING: $msg\n";
358 static function error( $msg ) {
359 print "ERROR: $msg\n";
362 function prompt( $msg ) {
363 if ( function_exists( 'readline' ) ) {
364 $s = readline( $msg );
367 if ( !@$this->stdin
) $this->stdin
= fopen( 'php://stdin', 'r' );
368 if ( !$this->stdin
) die( "Failed to open stdin for user interaction!\n" );
373 $s = fgets( $this->stdin
);
380 function confirm( $msg ) {
382 $s = $this->prompt( $msg . " [yes/no]: ");
383 $s = strtolower( trim($s) );
385 if ( $s == 'yes' ||
$s == 'y' ) { return true; }
386 else if ( $s == 'no' ||
$s == 'n' ) { return false; }
387 else { print "bad response: $s\n"; }
391 function deleteContents( $dir ) {
392 $ff = glob( $dir . "/*" );
395 foreach ( $ff as $f ) {
396 if ( is_dir( $f ) && !is_link( $f ) ) $this->deleteContents( $f );
401 function copyDir( $dir, $tgt ) {
402 $d = $tgt . '/' . basename( $dir );
404 if ( !file_exists( $d ) ) {
407 ExtensionInstaller
::error( "failed to create director $d" );
412 $ff = glob( $dir . "/*" );
413 if ( $ff === false ||
$ff === NULL ) return false;
415 foreach ( $ff as $f ) {
416 if ( is_dir( $f ) && !is_link( $f ) ) {
417 $ok = ExtensionInstaller
::copyDir( $f, $d );
418 if ( !$ok ) return false;
421 $t = $d . '/' . basename( $f );
422 $ok = copy( $f, $t );
425 ExtensionInstaller
::error( "failed to copy $f to $t" );
434 function setPermissions( $dir, $dirbits, $filebits ) {
435 if ( !chmod( $dir, $dirbits ) ) ExtensionInstaller
::warn( "faield to set permissions for $dir" );
437 $ff = glob( $dir . "/*" );
438 if ( $ff === false ||
$ff === NULL ) return false;
440 foreach ( $ff as $f ) {
442 if ( $n{0} == '.' ) continue; #HACK: skip dot files
444 if ( is_link( $f ) ) continue; #skip link
446 if ( is_dir( $f ) ) {
447 ExtensionInstaller
::setPermissions( $f, $dirbits, $filebits );
450 if ( !chmod( $f, $filebits ) ) ExtensionInstaller
::warn( "faield to set permissions for $f" );
457 function fetchExtension( ) {
458 if ( $this->source
->islocal
&& $this->source
->isdir
&& realpath( $this->source
->path
) === $this->dir
) {
459 $this->note( "files are already in the extension dir" );
463 if ( file_exists( $this->dir
) && glob( $this->dir
. "/*" ) ) {
464 if ( $this->confirm( "{$this->dir} exists and is not empty.\nDelete all files in that directory?" ) ) {
465 $this->deleteContents( $this->dir
);
472 $ok = $this->source
->fetch( $this->dir
);
473 if ( !$ok ) return false;
475 if ( !file_exists( $this->dir
) && glob( $this->dir
. "/*" ) ) {
476 $this->error( "{$this->dir} does not exist or is empty. Something went wrong, sorry." );
480 if ( file_exists( $this->dir
. '/README' ) ) $this->tasks
[] = "read the README file in {$this->dir}";
481 if ( file_exists( $this->dir
. '/INSTALL' ) ) $this->tasks
[] = "read the INSTALL file in {$this->dir}";
482 if ( file_exists( $this->dir
. '/RELEASE-NOTES' ) ) $this->tasks
[] = "read the RELEASE-NOTES file in {$this->dir}";
484 #TODO: configure this smartly...?
485 $this->setPermissions( $this->dir
, 0755, 0644 );
487 $this->note( "fetched extension to {$this->dir}" );
491 function patchLocalSettings( $mode ) {
492 #NOTE: if we get a better way to hook up extensions, that should be used instead.
494 $f = $this->dir
. '/install.settings';
495 $t = $this->target
. '/LocalSettings.php';
497 #TODO: assert version ?!
498 #TODO: allow custom installer scripts + sql patches
500 if ( !file_exists( $f ) ) {
501 self
::warn( "No install.settings file provided!" );
502 $this->tasks
[] = "Please read the instructions and edit LocalSettings.php manually to activate the extension.";
506 self
::note( "applying settings patch..." );
509 $settings = file_get_contents( $f );
512 self
::error( "failed to read settings from $f!" );
516 $settings = str_replace( '{{path}}', $this->incpath
, $settings );
518 if ( $mode == EXTINST_NOPATCH
) {
519 $this->tasks
[] = "Please put the following into your LocalSettings.php:" . "\n$settings\n";
520 self
::note( "Skipping patch phase, automatic patching is off." );
524 if ( $mode == EXTINST_HOTPATCH
) {
525 #NOTE: keep php extension for backup file!
526 $bak = $this->target
. '/LocalSettings.install-' . $this->name
. '-' . wfTimestamp(TS_MW
) . '.bak.php';
528 $ok = copy( $t, $bak );
531 self
::warn( "failed to create backup of LocalSettings.php!" );
535 self
::note( "created backup of LocalSettings.php at $bak" );
539 $localsettings = file_get_contents( $t );
542 self
::error( "failed to read $t for patching!" );
546 $marker = "<@< extension {$this->name} >@>";
547 $blockpattern = "/\n\s*#\s*BEGIN\s*$marker.*END\s*$marker\s*/smi";
549 if ( preg_match( $blockpattern, $localsettings ) ) {
550 $localsettings = preg_replace( $blockpattern, "\n", $localsettings );
551 $this->warn( "removed old configuration block for extension {$this->name}!" );
554 $newblock= "\n# BEGIN $marker\n$settings\n# END $marker\n";
556 $localsettings = preg_replace( "/\?>\s*$/si", "$newblock?>", $localsettings );
558 if ( $mode != EXTINST_HOTPATCH
) {
559 $t = $this->target
. '/LocalSettings.install-' . $this->name
. '-' . wfTimestamp(TS_MW
) . '.php';
562 $ok = file_put_contents( $t, $localsettings );
565 self
::error( "failed to patch $t!" );
568 else if ( $mode == EXTINST_HOTPATCH
) {
569 self
::note( "successfully patched $t" );
572 self
::note( "created patched settings file $t" );
573 $this->tasks
[] = "Replace your current LocalSettings.php with ".basename($t);
579 function printNotices( ) {
580 if ( !$this->tasks
) {
581 $this->note( "Installation is complete, no pending tasks" );
585 $this->note( "PENDING TASKS:" );
588 foreach ( $this->tasks
as $t ) {
589 $this->note ( "* " . $t );
600 $tgt = isset ( $options['target'] ) ?
$options['target'] : $IP;
602 $repos = @$options['repository'];
603 if ( !$repos ) $repos = @$options['repos'];
604 if ( !$repos ) $repos = @$wgExtensionInstallerRepository;
606 if ( !$repos && file_exists("$tgt/.svn") && is_dir("$tgt/.svn") ) {
607 $svn = file_get_contents( "$tgt/.svn/entries" );
610 if ( preg_match( '!url="(.*?)"!', $svn, $m ) ) {
611 $repos = dirname( $m[1] ) . '/extensions';
615 if ( !$repos ) $repos = 'http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions';
617 if( !isset( $args[0] ) && !@$options['list'] ) {
618 die( "USAGE: installExtension.php [options] <name> [source]\n" .
620 " --list list available extensions. <name> is ignored / may be omitted.\n" .
621 " --repository <n> repository to fetch extensions from. May be a local directoy,\n" .
622 " an SVN repository or a HTTP directory\n" .
623 " --target <dir> mediawiki installation directory to use\n" .
624 " --nopatch don't create a patched LocalSettings.php\n" .
625 " --hotpatch patched LocalSettings.php directly (creates a backup)\n" .
626 "SOURCE: specifies the package source directly. If given, the repository is ignored.\n" .
627 " The source my be a local file (tgz or zip) or directory, the URL of a\n" .
628 " remote file (tgz or zip), or a SVN path.\n"
632 $repository = InstallerRepository
::makeRepository( $repos );
634 if ( isset( $options['list'] ) ) {
635 $repository->printListing();
641 $src = isset( $args[1] ) ?
$args[1] : $repository->getResource( $name );
643 #TODO: detect $source mismatching $name !!
645 $mode = EXTINST_WRITEPATCH
;
646 if ( isset( $options['nopatch'] ) ||
@$wgExtensionInstallerNoPatch ) { $mode = EXTINST_NOPATCH
; }
647 else if ( isset( $options['hotpatch'] ) ||
@$wgExtensionInstallerHotPatch ) { $mode = EXTINST_HOTPATCH
; }
649 if ( !file_exists( "$tgt/LocalSettings.php" ) ) {
650 die("can't find $tgt/LocalSettings.php\n");
653 if ( $mode == EXTINST_HOTPATCH
&& !is_writable( "$tgt/LocalSettings.php" ) ) {
654 die("can't write to $tgt/LocalSettings.php\n");
657 if ( !file_exists( "$tgt/extensions" ) ) {
658 die("can't find $tgt/extensions\n");
661 if ( !is_writable( "$tgt/extensions" ) ) {
662 die("can't write to $tgt/extensions\n");
665 $installer = new ExtensionInstaller( $name, $src, $tgt );
667 $installer->note( "Installing extension {$installer->name} from {$installer->source->path} to {$installer->dir}" );
670 print "\tTHIS TOOL IS EXPERIMENTAL!\n";
671 print "\tEXPECT THE UNEXPECTED!\n";
674 if ( !$installer->confirm("continue") ) die("aborted\n");
676 $ok = $installer->fetchExtension();
678 if ( $ok ) $ok = $installer->patchLocalSettings( $mode );
680 if ( $ok ) $ok = $installer->printNotices();
682 if ( $ok ) $installer->note( "$name extension installed." );