06d15e90706e23957f5ee1d2b1feb99095f512f9
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', 'repository', 'repos' );
26 require_once( 'commandLine.inc' );
28 define('EXTINST_NOPATCH', 0);
29 define('EXTINST_WRITEPATCH', 6);
30 define('EXTINST_HOTPATCH', 10);
32 class InstallerRepository
{
35 function InstallerRepository( $path ) {
39 function printListing( ) {
40 trigger_error( 'override InstallerRepository::printListing()', E_USER_ERROR
);
43 function getResource( $name ) {
44 trigger_error( 'override InstallerRepository::getResource()', E_USER_ERROR
);
47 /*static*/ function makeRepository( $path, $type = NULL ) {
50 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
55 } else if ( ( $proto == 'http' ||
$proto == 'https' ) && preg_match( '!([^\w]svn|svn[^\w])!i', $path) ) {
62 if ( $type == 'dir' ||
$type == 'file' ) { return new LocalInstallerRepository( $path ); }
63 else if ( $type == 'http' ||
$type == 'http' ) { return new WebInstallerRepository( $path ); }
64 else { return new SVNInstallerRepository( $path ); }
68 class LocalInstallerRepository
extends InstallerRepository
{
70 function LocalInstallerRepository ( $path ) {
71 InstallerRepository
::InstallerRepository( $path );
74 function printListing( ) {
75 $ff = glob( "{$this->path}/*" );
76 if ( $ff === false ||
$ff === NULL ) {
77 ExtensionInstaller
::error( "listing directory {$this->path} failed!" );
81 foreach ( $ff as $f ) {
84 if ( !is_dir( $f ) ) {
86 if ( !preg_match( '/(.*)\.(tgz|tar\.gz|zip)/', $n, $m ) ) continue;
94 function getResource( $name ) {
95 $path = $this->path
. '/' . $name;
97 if ( !file_exists( $path ) ||
!is_dir( $path ) ) $path = $this->path
. '/' . $name . '.tgz';
98 if ( !file_exists( $path ) ) $path = $this->path
. '/' . $name . '.tar.gz';
99 if ( !file_exists( $path ) ) $path = $this->path
. '/' . $name . '.zip';
101 return new LocalInstallerResource( $path );
105 class WebInstallerRepository
extends InstallerRepository
{
107 function WebInstallerRepository ( $path ) {
108 InstallerRepository
::InstallerRepository( $path );
111 function printListing( ) {
112 ExtensionInstaller
::note( "listing index from {$this->path}..." );
114 $txt = @file_get_contents
( $this->path
. '/index.txt' );
120 $txt = file_get_contents( $this->path
);
122 ExtensionInstaller
::error( "listing index from {$this->path} failed!" );
128 $ok = preg_match_all( '!<a\s[^>]*href\s*=\s*['."'".'"]([^/'."'".'"]+)\.tgz['."'".'"][^>]*>.*?</a>!si', $txt, $m, PREG_SET_ORDER
);
130 ExtensionInstaller
::error( "listing index from {$this->path} does not match!" );
135 foreach ( $m as $l ) {
142 function getResource( $name ) {
143 $path = $this->path
. '/' . $name . '.tgz';
144 return new WebInstallerResource( $path );
148 class SVNInstallerRepository
extends InstallerRepository
{
150 function SVNInstallerRepository ( $path ) {
151 InstallerRepository
::InstallerRepository( $path );
154 function printListing( ) {
155 ExtensionInstaller
::note( "SVN list {$this->path}..." );
156 $code = null; // Shell Exec return value.
157 $txt = wfShellExec( 'svn ls ' . escapeshellarg( $this->path
), $code );
159 ExtensionInstaller
::error( "svn list for {$this->path} failed!" );
163 $ll = preg_split('/(\s*[\r\n]\s*)+/', $txt);
165 foreach ( $ll as $line ) {
167 if ( !preg_match('!^(.*)/$!', $line, $m) ) continue;
174 function getResource( $name ) {
175 $path = $this->path
. '/' . $name;
176 return new SVNInstallerResource( $path );
180 class InstallerResource
{
185 function InstallerResource( $path, $isdir, $islocal ) {
188 $this->isdir
= $isdir;
189 $this->islocal
= $islocal;
192 preg_match( '!([-+\w]+://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
194 $this->protocol
= @$m[1];
195 $this->extensions
= @$m[2];
197 if ( $this->extensions
) $this->extensions
= strtolower( $this->extensions
);
200 function fetch( $target ) {
201 trigger_error( 'override InstallerResource::fetch()', E_USER_ERROR
);
204 function extract( $file, $target ) {
206 if ( $this->extensions
== '.tgz' ||
$this->extensions
== '.tar.gz' ) { #tgz file
207 ExtensionInstaller
::note( "extracting $file..." );
208 $code = null; // shell Exec return value.
209 wfShellExec( 'tar zxvf ' . escapeshellarg( $file ) . ' -C ' . escapeshellarg( $target ), $code );
212 ExtensionInstaller
::error( "failed to extract $file!" );
216 else if ( $this->extensions
== '.zip' ) { #zip file
217 ExtensionInstaller
::note( "extracting $file..." );
218 $code = null; // shell Exec return value.
219 wfShellExec( 'unzip ' . escapeshellarg( $file ) . ' -d ' . escapeshellarg( $target ) , $code );
222 ExtensionInstaller
::error( "failed to extract $file!" );
227 ExtensionInstaller
::error( "unknown extension {$this->extensions}!" );
234 /*static*/ function makeResource( $url ) {
236 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $url, $m );
239 if ( $ext ) $ext = strtolower( $ext );
241 if ( !$proto ) { return new LocalInstallerResource( $url, $ext ?
false : true ); }
242 else if ( $ext && ( $proto == 'http' ||
$proto == 'http' ||
$proto == 'ftp' ) ) { return new WebInstallerResource( $url ); }
243 else { return new SVNInstallerResource( $url ); }
247 class LocalInstallerResource
extends InstallerResource
{
248 function LocalInstallerResource( $path ) {
249 InstallerResource
::InstallerResource( $path, is_dir( $path ), true );
252 function fetch( $target ) {
253 if ( $this->isdir
) return ExtensionInstaller
::copyDir( $this->path
, dirname( $target ) );
254 else return $this->extract( $this->path
, dirname( $target ) );
259 class WebInstallerResource
extends InstallerResource
{
260 function WebInstallerResource( $path ) {
261 InstallerResource
::InstallerResource( $path, false, false );
264 function fetch( $target ) {
265 $tmp = wfTempDir() . '/' . basename( $this->path
);
267 ExtensionInstaller
::note( "downloading {$this->path}..." );
268 $ok = copy( $this->path
, $tmp );
271 ExtensionInstaller
::error( "failed to download {$this->path}" );
275 $this->extract( $tmp, dirname( $target ) );
282 class SVNInstallerResource
extends InstallerResource
{
283 function SVNInstallerResource( $path ) {
284 InstallerResource
::InstallerResource( $path, true, false );
287 function fetch( $target ) {
288 ExtensionInstaller
::note( "SVN checkout of {$this->path}..." );
289 $code = null; // shell exec return val.
290 wfShellExec( 'svn co ' . escapeshellarg( $this->path
) . ' ' . escapeshellarg( $target ), $code );
293 ExtensionInstaller
::error( "checkout failed for {$this->path}!" );
301 class ExtensionInstaller
{
308 function ExtensionInstaller( $name, $source, $target ) {
309 if ( !is_object( $source ) ) $source = InstallerResource
::makeResource( $source );
312 $this->source
= $source;
313 $this->target
= realpath( $target );
314 $this->extdir
= "$target/extensions";
315 $this->dir
= "{$this->extdir}/$name";
316 $this->incpath
= "extensions/$name";
317 $this->tasks
= array();
319 #TODO: allow a subdir different from "extensions"
320 #TODO: allow a config file different from "LocalSettings.php"
323 function note( $msg ) {
327 function warn( $msg ) {
328 print "WARNING: $msg\n";
331 function error( $msg ) {
332 print "ERROR: $msg\n";
335 function prompt( $msg ) {
336 if ( function_exists( 'readline' ) ) {
337 $s = readline( $msg );
340 if ( !@$this->stdin
) $this->stdin
= fopen( 'php://stdin', 'r' );
341 if ( !$this->stdin
) die( "Failed to open stdin for user interaction!\n" );
346 $s = fgets( $this->stdin
);
353 function confirm( $msg ) {
355 $s = $this->prompt( $msg . " [yes/no]: ");
356 $s = strtolower( trim($s) );
358 if ( $s == 'yes' ||
$s == 'y' ) { return true; }
359 else if ( $s == 'no' ||
$s == 'n' ) { return false; }
360 else { print "bad response: $s\n"; }
364 function deleteContents( $dir ) {
365 $ff = glob( $dir . "/*" );
368 foreach ( $ff as $f ) {
369 if ( is_dir( $f ) && !is_link( $f ) ) $this->deleteContents( $f );
374 function copyDir( $dir, $tgt ) {
375 $d = $tgt . '/' . basename( $dir );
377 if ( !file_exists( $d ) ) {
380 ExtensionInstaller
::error( "failed to create director $d" );
385 $ff = glob( $dir . "/*" );
386 if ( $ff === false ||
$ff === NULL ) return false;
388 foreach ( $ff as $f ) {
389 if ( is_dir( $f ) && !is_link( $f ) ) {
390 $ok = ExtensionInstaller
::copyDir( $f, $d );
391 if ( !$ok ) return false;
394 $t = $d . '/' . basename( $f );
395 $ok = copy( $f, $t );
398 ExtensionInstaller
::error( "failed to copy $f to $t" );
407 function setPermissions( $dir, $dirbits, $filebits ) {
408 if ( !chmod( $dir, $dirbits ) ) ExtensionInstaller
::warn( "faield to set permissions for $dir" );
410 $ff = glob( $dir . "/*" );
411 if ( $ff === false ||
$ff === NULL ) return false;
413 foreach ( $ff as $f ) {
415 if ( $n{0} == '.' ) continue; #HACK: skip dot files
417 if ( is_link( $f ) ) continue; #skip link
419 if ( is_dir( $f ) ) {
420 ExtensionInstaller
::setPermissions( $f, $dirbits, $filebits );
423 if ( !chmod( $f, $filebits ) ) ExtensionInstaller
::warn( "faield to set permissions for $f" );
430 function fetchExtension( ) {
431 if ( $this->source
->islocal
&& $this->source
->isdir
&& realpath( $this->source
->path
) === $this->dir
) {
432 $this->note( "files are already in the extension dir" );
436 if ( file_exists( $this->dir
) && glob( $this->dir
. "/*" ) ) {
437 if ( $this->confirm( "{$this->dir} exists and is not empty.\nDelete all files in that directory?" ) ) {
438 $this->deleteContents( $this->dir
);
445 $ok = $this->source
->fetch( $this->dir
);
446 if ( !$ok ) return false;
448 if ( !file_exists( $this->dir
) && glob( $this->dir
. "/*" ) ) {
449 $this->error( "{$this->dir} does not exist or is empty. Something went wrong, sorry." );
453 if ( file_exists( $this->dir
. '/README' ) ) $this->tasks
[] = "read the README file in {$this->dir}";
454 if ( file_exists( $this->dir
. '/INSTALL' ) ) $this->tasks
[] = "read the INSTALL file in {$this->dir}";
455 if ( file_exists( $this->dir
. '/RELEASE-NOTES' ) ) $this->tasks
[] = "read the RELEASE-NOTES file in {$this->dir}";
457 #TODO: configure this smartly...?
458 $this->setPermissions( $this->dir
, 0755, 0644 );
460 $this->note( "fetched extension to {$this->dir}" );
464 function patchLocalSettings( $mode ) {
465 #NOTE: if we get a better way to hook up extensions, that should be used instead.
467 $f = $this->dir
. '/install.settings';
468 $t = $this->target
. '/LocalSettings.php';
470 #TODO: assert version ?!
471 #TODO: allow custom installer scripts + sql patches
473 if ( !file_exists( $f ) ) {
474 $this->warn( "No install.settings file provided!" );
475 $this->tasks
[] = "Please read the instructions and edit LocalSettings.php manually to activate the extension.";
479 $this->note( "applying settings patch..." );
482 $settings = file_get_contents( $f );
485 $this->error( "failed to read settings from $f!" );
489 $settings = str_replace( '{{path}}', $this->incpath
, $settings );
491 if ( $mode == EXTINST_NOPATCH
) {
492 $this->tasks
[] = "Please put the following into your LocalSettings.php:" . "\n$settings\n";
493 $this->note( "Skipping patch phase, automatic patching is off." );
497 if ( $mode == EXTINST_HOTPATCH
) {
498 #NOTE: keep php extension for backup file!
499 $bak = $this->target
. '/LocalSettings.install-' . $this->name
. '-' . wfTimestamp(TS_MW
) . '.bak.php';
501 $ok = copy( $t, $bak );
504 $this->warn( "failed to create backup of LocalSettings.php!" );
508 $this->note( "created backup of LocalSettings.php at $bak" );
512 $localsettings = file_get_contents( $t );
515 $this->error( "failed to read $t for patching!" );
519 $marker = "<@< extension {$this->name} >@>";
520 $blockpattern = "/\n\s*#\s*BEGIN\s*$marker.*END\s*$marker\s*/smi";
522 if ( preg_match( $blockpattern, $localsettings ) ) {
523 $localsettings = preg_replace( $blockpattern, "\n", $localsettings );
524 $this->warn( "removed old configuration block for extension {$this->name}!" );
527 $newblock= "\n# BEGIN $marker\n$settings\n# END $marker\n";
529 $localsettings = preg_replace( "/\?>\s*$/si", "$newblock?>", $localsettings );
531 if ( $mode != EXTINST_HOTPATCH
) {
532 $t = $this->target
. '/LocalSettings.install-' . $this->name
. '-' . wfTimestamp(TS_MW
) . '.php';
535 $ok = file_put_contents( $t, $localsettings );
538 $this->error( "failed to patch $t!" );
541 else if ( $mode == EXTINST_HOTPATCH
) {
542 $this->note( "successfully patched $t" );
545 $this->note( "created patched settings file $t" );
546 $this->tasks
[] = "Replace your current LocalSettings.php with ".basename($t);
552 function printNotices( ) {
553 if ( !$this->tasks
) {
554 $this->note( "Installation is complete, no pending tasks" );
558 $this->note( "PENDING TASKS:" );
561 foreach ( $this->tasks
as $t ) {
562 $this->note ( "* " . $t );
573 $tgt = isset ( $options['target'] ) ?
$options['target'] : $IP;
575 $repos = @$options['repository'];
576 if ( !$repos ) $repos = @$options['repos'];
577 if ( !$repos ) $repos = @$wgExtensionInstallerRepository;
579 if ( !$repos && file_exists("$tgt/.svn") && is_dir("$tgt/.svn") ) {
580 $svn = file_get_contents( "$tgt/.svn/entries" );
583 if ( preg_match( '!url="(.*?)"!', $svn, $m ) ) {
584 $repos = dirname( $m[1] ) . '/extensions';
588 if ( !$repos ) $repos = 'http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions';
590 if( !isset( $args[0] ) && !@$options['list'] ) {
591 die( "USAGE: installExtension.php [options] <name> [source]\n" .
593 " --list list available extensions. <name> is ignored / may be omitted.\n" .
594 " --repository <n> repository to fetch extensions from. May be a local directoy,\n" .
595 " an SVN repository or a HTTP directory\n" .
596 " --target <dir> mediawiki installation directory to use\n" .
597 " --nopatch don't create a patched LocalSettings.php\n" .
598 " --hotpatch patched LocalSettings.php directly (creates a backup)\n" .
599 "SOURCE: specifies the package source directly. If given, the repository is ignored.\n" .
600 " The source my be a local file (tgz or zip) or directory, the URL of a\n" .
601 " remote file (tgz or zip), or a SVN path.\n"
605 $repository = InstallerRepository
::makeRepository( $repos );
607 if ( isset( $options['list'] ) ) {
608 $repository->printListing();
614 $src = isset( $args[1] ) ?
$args[1] : $repository->getResource( $name );
616 #TODO: detect $source mismatching $name !!
618 $mode = EXTINST_WRITEPATCH
;
619 if ( isset( $options['nopatch'] ) ||
@$wgExtensionInstallerNoPatch ) { $mode = EXTINST_NOPATCH
; }
620 else if ( isset( $options['hotpatch'] ) ||
@$wgExtensionInstallerHotPatch ) { $mode = EXTINST_HOTPATCH
; }
622 if ( !file_exists( "$tgt/LocalSettings.php" ) ) {
623 die("can't find $tgt/LocalSettings.php\n");
626 if ( $mode == EXTINST_HOTPATCH
&& !is_writable( "$tgt/LocalSettings.php" ) ) {
627 die("can't write to $tgt/LocalSettings.php\n");
630 if ( !file_exists( "$tgt/extensions" ) ) {
631 die("can't find $tgt/extensions\n");
634 if ( !is_writable( "$tgt/extensions" ) ) {
635 die("can't write to $tgt/extensions\n");
638 $installer = new ExtensionInstaller( $name, $src, $tgt );
640 $installer->note( "Installing extension {$installer->name} from {$installer->source->path} to {$installer->dir}" );
643 print "\tTHIS TOOL IS EXPERIMENTAL!\n";
644 print "\tEXPECT THE UNEXPECTED!\n";
647 if ( !$installer->confirm("continue") ) die("aborted\n");
649 $ok = $installer->fetchExtension();
651 if ( $ok ) $ok = $installer->patchLocalSettings( $mode );
653 if ( $ok ) $ok = $installer->printNotices();
655 if ( $ok ) $installer->note( "$name extension installed." );