00001 <?php
00024 $optionsWithArgs = array( 'target', 'repository', 'repos' );
00025
00026 require_once( 'commandLine.inc' );
00027
00028 define('EXTINST_NOPATCH', 0);
00029 define('EXTINST_WRITEPATCH', 6);
00030 define('EXTINST_HOTPATCH', 10);
00031
00035 class InstallerRepository {
00036 var $path;
00037
00038 function InstallerRepository( $path ) {
00039 $this->path = $path;
00040 }
00041
00042 function printListing( ) {
00043 trigger_error( 'override InstallerRepository::printListing()', E_USER_ERROR );
00044 }
00045
00046 function getResource( $name ) {
00047 trigger_error( 'override InstallerRepository::getResource()', E_USER_ERROR );
00048 }
00049
00050 static function makeRepository( $path, $type = NULL ) {
00051 if ( !$type ) {
00052 $m = array();
00053 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
00054 $proto = @$m[2];
00055
00056 if ( !$proto ) {
00057 $type = 'dir';
00058 } else if ( ( $proto == 'http' || $proto == 'https' ) && preg_match( '!([^\w]svn|svn[^\w])!i', $path) ) {
00059 $type = 'svn'; #HACK!
00060 } else {
00061 $type = $proto;
00062 }
00063 }
00064
00065 if ( $type == 'dir' || $type == 'file' ) { return new LocalInstallerRepository( $path ); }
00066 else if ( $type == 'http' || $type == 'http' ) { return new WebInstallerRepository( $path ); }
00067 else { return new SVNInstallerRepository( $path ); }
00068 }
00069 }
00070
00074 class LocalInstallerRepository extends InstallerRepository {
00075
00076 function LocalInstallerRepository ( $path ) {
00077 InstallerRepository::InstallerRepository( $path );
00078 }
00079
00080 function printListing( ) {
00081 $ff = glob( "{$this->path}/*" );
00082 if ( $ff === false || $ff === NULL ) {
00083 ExtensionInstaller::error( "listing directory {$this->path} failed!" );
00084 return false;
00085 }
00086
00087 foreach ( $ff as $f ) {
00088 $n = basename($f);
00089
00090 if ( !is_dir( $f ) ) {
00091 $m = array();
00092 if ( !preg_match( '/(.*)\.(tgz|tar\.gz|zip)/', $n, $m ) ) continue;
00093 $n = $m[1];
00094 }
00095
00096 print "\t$n\n";
00097 }
00098 }
00099
00100 function getResource( $name ) {
00101 $path = $this->path . '/' . $name;
00102
00103 if ( !file_exists( $path ) || !is_dir( $path ) ) $path = $this->path . '/' . $name . '.tgz';
00104 if ( !file_exists( $path ) ) $path = $this->path . '/' . $name . '.tar.gz';
00105 if ( !file_exists( $path ) ) $path = $this->path . '/' . $name . '.zip';
00106
00107 return new LocalInstallerResource( $path );
00108 }
00109 }
00110
00114 class WebInstallerRepository extends InstallerRepository {
00115
00116 function WebInstallerRepository ( $path ) {
00117 InstallerRepository::InstallerRepository( $path );
00118 }
00119
00120 function printListing( ) {
00121 ExtensionInstaller::note( "listing index from {$this->path}..." );
00122
00123 $txt = @file_get_contents( $this->path . '/index.txt' );
00124 if ( $txt ) {
00125 print $txt;
00126 print "\n";
00127 }
00128 else {
00129 $txt = file_get_contents( $this->path );
00130 if ( !$txt ) {
00131 ExtensionInstaller::error( "listing index from {$this->path} failed!" );
00132 print ( $txt );
00133 return false;
00134 }
00135
00136 $m = array();
00137 $ok = preg_match_all( '!<a\s[^>]*href\s*=\s*['."'".'"]([^/'."'".'"]+)\.tgz['."'".'"][^>]*>.*?</a>!si', $txt, $m, PREG_SET_ORDER );
00138 if ( !$ok ) {
00139 ExtensionInstaller::error( "listing index from {$this->path} does not match!" );
00140 print ( $txt );
00141 return false;
00142 }
00143
00144 foreach ( $m as $l ) {
00145 $n = $l[1];
00146 print "\t$n\n";
00147 }
00148 }
00149 }
00150
00151 function getResource( $name ) {
00152 $path = $this->path . '/' . $name . '.tgz';
00153 return new WebInstallerResource( $path );
00154 }
00155 }
00156
00160 class SVNInstallerRepository extends InstallerRepository {
00161
00162 function SVNInstallerRepository ( $path ) {
00163 InstallerRepository::InstallerRepository( $path );
00164 }
00165
00166 function printListing( ) {
00167 ExtensionInstaller::note( "SVN list {$this->path}..." );
00168 $code = null;
00169 $txt = wfShellExec( 'svn ls ' . escapeshellarg( $this->path ), $code );
00170 if ( $code !== 0 ) {
00171 ExtensionInstaller::error( "svn list for {$this->path} failed!" );
00172 return false;
00173 }
00174
00175 $ll = preg_split('/(\s*[\r\n]\s*)+/', $txt);
00176
00177 foreach ( $ll as $line ) {
00178 $m = array();
00179 if ( !preg_match('!^(.*)/$!', $line, $m) ) continue;
00180 $n = $m[1];
00181
00182 print "\t$n\n";
00183 }
00184 }
00185
00186 function getResource( $name ) {
00187 $path = $this->path . '/' . $name;
00188 return new SVNInstallerResource( $path );
00189 }
00190 }
00191
00195 class InstallerResource {
00196 var $path;
00197 var $isdir;
00198 var $islocal;
00199
00200 function InstallerResource( $path, $isdir, $islocal ) {
00201 $this->path = $path;
00202
00203 $this->isdir= $isdir;
00204 $this->islocal = $islocal;
00205
00206 $m = array();
00207 preg_match( '!([-+\w]+://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
00208
00209 $this->protocol = @$m[1];
00210 $this->extensions = @$m[2];
00211
00212 if ( $this->extensions ) $this->extensions = strtolower( $this->extensions );
00213 }
00214
00215 function fetch( $target ) {
00216 trigger_error( 'override InstallerResource::fetch()', E_USER_ERROR );
00217 }
00218
00219 function extract( $file, $target ) {
00220
00221 if ( $this->extensions == '.tgz' || $this->extensions == '.tar.gz' ) { #tgz file
00222 ExtensionInstaller::note( "extracting $file..." );
00223 $code = null;
00224 wfShellExec( 'tar zxvf ' . escapeshellarg( $file ) . ' -C ' . escapeshellarg( $target ), $code );
00225
00226 if ( $code !== 0 ) {
00227 ExtensionInstaller::error( "failed to extract $file!" );
00228 return false;
00229 }
00230 }
00231 else if ( $this->extensions == '.zip' ) { #zip file
00232 ExtensionInstaller::note( "extracting $file..." );
00233 $code = null;
00234 wfShellExec( 'unzip ' . escapeshellarg( $file ) . ' -d ' . escapeshellarg( $target ) , $code );
00235
00236 if ( $code !== 0 ) {
00237 ExtensionInstaller::error( "failed to extract $file!" );
00238 return false;
00239 }
00240 }
00241 else {
00242 ExtensionInstaller::error( "unknown extension {$this->extensions}!" );
00243 return false;
00244 }
00245
00246 return true;
00247 }
00248
00249 function makeResource( $url ) {
00250 $m = array();
00251 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $url, $m );
00252 $proto = @$m[2];
00253 $ext = @$m[3];
00254 if ( $ext ) $ext = strtolower( $ext );
00255
00256 if ( !$proto ) { return new LocalInstallerResource( $url, $ext ? false : true ); }
00257 else if ( $ext && ( $proto == 'http' || $proto == 'http' || $proto == 'ftp' ) ) { return new WebInstallerResource( $url ); }
00258 else { return new SVNInstallerResource( $url ); }
00259 }
00260 }
00261
00265 class LocalInstallerResource extends InstallerResource {
00266 function LocalInstallerResource( $path ) {
00267 InstallerResource::InstallerResource( $path, is_dir( $path ), true );
00268 }
00269
00270 function fetch( $target ) {
00271 if ( $this->isdir ) return ExtensionInstaller::copyDir( $this->path, dirname( $target ) );
00272 else return $this->extract( $this->path, dirname( $target ) );
00273 }
00274
00275 }
00276
00280 class WebInstallerResource extends InstallerResource {
00281 function WebInstallerResource( $path ) {
00282 InstallerResource::InstallerResource( $path, false, false );
00283 }
00284
00285 function fetch( $target ) {
00286 $tmp = wfTempDir() . '/' . basename( $this->path );
00287
00288 ExtensionInstaller::note( "downloading {$this->path}..." );
00289 $ok = copy( $this->path, $tmp );
00290
00291 if ( !$ok ) {
00292 ExtensionInstaller::error( "failed to download {$this->path}" );
00293 return false;
00294 }
00295
00296 $this->extract( $tmp, dirname( $target ) );
00297 unlink($tmp);
00298
00299 return true;
00300 }
00301 }
00302
00306 class SVNInstallerResource extends InstallerResource {
00307 function SVNInstallerResource( $path ) {
00308 InstallerResource::InstallerResource( $path, true, false );
00309 }
00310
00311 function fetch( $target ) {
00312 ExtensionInstaller::note( "SVN checkout of {$this->path}..." );
00313 $code = null;
00314 wfShellExec( 'svn co ' . escapeshellarg( $this->path ) . ' ' . escapeshellarg( $target ), $code );
00315
00316 if ( $code !== 0 ) {
00317 ExtensionInstaller::error( "checkout failed for {$this->path}!" );
00318 return false;
00319 }
00320
00321 return true;
00322 }
00323 }
00324
00328 class ExtensionInstaller {
00329 var $source;
00330 var $target;
00331 var $name;
00332 var $dir;
00333 var $tasks;
00334
00335 function ExtensionInstaller( $name, $source, $target ) {
00336 if ( !is_object( $source ) ) $source = InstallerResource::makeResource( $source );
00337
00338 $this->name = $name;
00339 $this->source = $source;
00340 $this->target = realpath( $target );
00341 $this->extdir = "$target/extensions";
00342 $this->dir = "{$this->extdir}/$name";
00343 $this->incpath = "extensions/$name";
00344 $this->tasks = array();
00345
00346 #TODO: allow a subdir different from "extensions"
00347 #TODO: allow a config file different from "LocalSettings.php"
00348 }
00349
00350 static function note( $msg ) {
00351 print "$msg\n";
00352 }
00353
00354 static function warn( $msg ) {
00355 print "WARNING: $msg\n";
00356 }
00357
00358 static function error( $msg ) {
00359 print "ERROR: $msg\n";
00360 }
00361
00362 function prompt( $msg ) {
00363 if ( function_exists( 'readline' ) ) {
00364 $s = readline( $msg );
00365 }
00366 else {
00367 if ( !@$this->stdin ) $this->stdin = fopen( 'php://stdin', 'r' );
00368 if ( !$this->stdin ) die( "Failed to open stdin for user interaction!\n" );
00369
00370 print $msg;
00371 flush();
00372
00373 $s = fgets( $this->stdin );
00374 }
00375
00376 $s = trim( $s );
00377 return $s;
00378 }
00379
00380 function confirm( $msg ) {
00381 while ( true ) {
00382 $s = $this->prompt( $msg . " [yes/no]: ");
00383 $s = strtolower( trim($s) );
00384
00385 if ( $s == 'yes' || $s == 'y' ) { return true; }
00386 else if ( $s == 'no' || $s == 'n' ) { return false; }
00387 else { print "bad response: $s\n"; }
00388 }
00389 }
00390
00391 function deleteContents( $dir ) {
00392 $ff = glob( $dir . "/*" );
00393 if ( !$ff ) return;
00394
00395 foreach ( $ff as $f ) {
00396 if ( is_dir( $f ) && !is_link( $f ) ) $this->deleteContents( $f );
00397 unlink( $f );
00398 }
00399 }
00400
00401 function copyDir( $dir, $tgt ) {
00402 $d = $tgt . '/' . basename( $dir );
00403
00404 if ( !file_exists( $d ) ) {
00405 $ok = mkdir( $d );
00406 if ( !$ok ) {
00407 ExtensionInstaller::error( "failed to create director $d" );
00408 return false;
00409 }
00410 }
00411
00412 $ff = glob( $dir . "/*" );
00413 if ( $ff === false || $ff === NULL ) return false;
00414
00415 foreach ( $ff as $f ) {
00416 if ( is_dir( $f ) && !is_link( $f ) ) {
00417 $ok = ExtensionInstaller::copyDir( $f, $d );
00418 if ( !$ok ) return false;
00419 }
00420 else {
00421 $t = $d . '/' . basename( $f );
00422 $ok = copy( $f, $t );
00423
00424 if ( !$ok ) {
00425 ExtensionInstaller::error( "failed to copy $f to $t" );
00426 return false;
00427 }
00428 }
00429 }
00430
00431 return true;
00432 }
00433
00434 function setPermissions( $dir, $dirbits, $filebits ) {
00435 if ( !chmod( $dir, $dirbits ) ) ExtensionInstaller::warn( "faield to set permissions for $dir" );
00436
00437 $ff = glob( $dir . "/*" );
00438 if ( $ff === false || $ff === NULL ) return false;
00439
00440 foreach ( $ff as $f ) {
00441 $n= basename( $f );
00442 if ( $n{0} == '.' ) continue; #HACK: skip dot files
00443
00444 if ( is_link( $f ) ) continue; #skip link
00445
00446 if ( is_dir( $f ) ) {
00447 ExtensionInstaller::setPermissions( $f, $dirbits, $filebits );
00448 }
00449 else {
00450 if ( !chmod( $f, $filebits ) ) ExtensionInstaller::warn( "faield to set permissions for $f" );
00451 }
00452 }
00453
00454 return true;
00455 }
00456
00457 function fetchExtension( ) {
00458 if ( $this->source->islocal && $this->source->isdir && realpath( $this->source->path ) === $this->dir ) {
00459 $this->note( "files are already in the extension dir" );
00460 return true;
00461 }
00462
00463 if ( file_exists( $this->dir ) && glob( $this->dir . "/*" ) ) {
00464 if ( $this->confirm( "{$this->dir} exists and is not empty.\nDelete all files in that directory?" ) ) {
00465 $this->deleteContents( $this->dir );
00466 }
00467 else {
00468 return false;
00469 }
00470 }
00471
00472 $ok = $this->source->fetch( $this->dir );
00473 if ( !$ok ) return false;
00474
00475 if ( !file_exists( $this->dir ) && glob( $this->dir . "/*" ) ) {
00476 $this->error( "{$this->dir} does not exist or is empty. Something went wrong, sorry." );
00477 return false;
00478 }
00479
00480 if ( file_exists( $this->dir . '/README' ) ) $this->tasks[] = "read the README file in {$this->dir}";
00481 if ( file_exists( $this->dir . '/INSTALL' ) ) $this->tasks[] = "read the INSTALL file in {$this->dir}";
00482 if ( file_exists( $this->dir . '/RELEASE-NOTES' ) ) $this->tasks[] = "read the RELEASE-NOTES file in {$this->dir}";
00483
00484 #TODO: configure this smartly...?
00485 $this->setPermissions( $this->dir, 0755, 0644 );
00486
00487 $this->note( "fetched extension to {$this->dir}" );
00488 return true;
00489 }
00490
00491 function patchLocalSettings( $mode ) {
00492 #NOTE: if we get a better way to hook up extensions, that should be used instead.
00493
00494 $f = $this->dir . '/install.settings';
00495 $t = $this->target . '/LocalSettings.php';
00496
00497 #TODO: assert version ?!
00498 #TODO: allow custom installer scripts + sql patches
00499
00500 if ( !file_exists( $f ) ) {
00501 self::warn( "No install.settings file provided!" );
00502 $this->tasks[] = "Please read the instructions and edit LocalSettings.php manually to activate the extension.";
00503 return '?';
00504 }
00505 else {
00506 self::note( "applying settings patch..." );
00507 }
00508
00509 $settings = file_get_contents( $f );
00510
00511 if ( !$settings ) {
00512 self::error( "failed to read settings from $f!" );
00513 return false;
00514 }
00515
00516 $settings = str_replace( '{{path}}', $this->incpath, $settings );
00517
00518 if ( $mode == EXTINST_NOPATCH ) {
00519 $this->tasks[] = "Please put the following into your LocalSettings.php:" . "\n$settings\n";
00520 self::note( "Skipping patch phase, automatic patching is off." );
00521 return true;
00522 }
00523
00524 if ( $mode == EXTINST_HOTPATCH ) {
00525 #NOTE: keep php extension for backup file!
00526 $bak = $this->target . '/LocalSettings.install-' . $this->name . '-' . wfTimestamp(TS_MW) . '.bak.php';
00527
00528 $ok = copy( $t, $bak );
00529
00530 if ( !$ok ) {
00531 self::warn( "failed to create backup of LocalSettings.php!" );
00532 return false;
00533 }
00534 else {
00535 self::note( "created backup of LocalSettings.php at $bak" );
00536 }
00537 }
00538
00539 $localsettings = file_get_contents( $t );
00540
00541 if ( !$settings ) {
00542 self::error( "failed to read $t for patching!" );
00543 return false;
00544 }
00545
00546 $marker = "<@< extension {$this->name} >@>";
00547 $blockpattern = "/\n\s*#\s*BEGIN\s*$marker.*END\s*$marker\s*/smi";
00548
00549 if ( preg_match( $blockpattern, $localsettings ) ) {
00550 $localsettings = preg_replace( $blockpattern, "\n", $localsettings );
00551 $this->warn( "removed old configuration block for extension {$this->name}!" );
00552 }
00553
00554 $newblock= "\n# BEGIN $marker\n$settings\n# END $marker\n";
00555
00556 $localsettings = preg_replace( "/\?>\s*$/si", "$newblock?>", $localsettings );
00557
00558 if ( $mode != EXTINST_HOTPATCH ) {
00559 $t = $this->target . '/LocalSettings.install-' . $this->name . '-' . wfTimestamp(TS_MW) . '.php';
00560 }
00561
00562 $ok = file_put_contents( $t, $localsettings );
00563
00564 if ( !$ok ) {
00565 self::error( "failed to patch $t!" );
00566 return false;
00567 }
00568 else if ( $mode == EXTINST_HOTPATCH ) {
00569 self::note( "successfully patched $t" );
00570 }
00571 else {
00572 self::note( "created patched settings file $t" );
00573 $this->tasks[] = "Replace your current LocalSettings.php with ".basename($t);
00574 }
00575
00576 return true;
00577 }
00578
00579 function printNotices( ) {
00580 if ( !$this->tasks ) {
00581 $this->note( "Installation is complete, no pending tasks" );
00582 }
00583 else {
00584 $this->note( "" );
00585 $this->note( "PENDING TASKS:" );
00586 $this->note( "" );
00587
00588 foreach ( $this->tasks as $t ) {
00589 $this->note ( "* " . $t );
00590 }
00591
00592 $this->note( "" );
00593 }
00594
00595 return true;
00596 }
00597
00598 }
00599
00600 $tgt = isset ( $options['target'] ) ? $options['target'] : $IP;
00601
00602 $repos = @$options['repository'];
00603 if ( !$repos ) $repos = @$options['repos'];
00604 if ( !$repos ) $repos = @$wgExtensionInstallerRepository;
00605
00606 if ( !$repos && file_exists("$tgt/.svn") && is_dir("$tgt/.svn") ) {
00607 $svn = file_get_contents( "$tgt/.svn/entries" );
00608
00609 $m = array();
00610 if ( preg_match( '!url="(.*?)"!', $svn, $m ) ) {
00611 $repos = dirname( $m[1] ) . '/extensions';
00612 }
00613 }
00614
00615 if ( !$repos ) $repos = 'http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions';
00616
00617 if( !isset( $args[0] ) && !@$options['list'] ) {
00618 die( "USAGE: installExtension.php [options] <name> [source]\n" .
00619 "OPTIONS: \n" .
00620 " --list list available extensions. <name> is ignored / may be omitted.\n" .
00621 " --repository <n> repository to fetch extensions from. May be a local directoy,\n" .
00622 " an SVN repository or a HTTP directory\n" .
00623 " --target <dir> mediawiki installation directory to use\n" .
00624 " --nopatch don't create a patched LocalSettings.php\n" .
00625 " --hotpatch patched LocalSettings.php directly (creates a backup)\n" .
00626 "SOURCE: specifies the package source directly. If given, the repository is ignored.\n" .
00627 " The source my be a local file (tgz or zip) or directory, the URL of a\n" .
00628 " remote file (tgz or zip), or a SVN path.\n"
00629 );
00630 }
00631
00632 $repository = InstallerRepository::makeRepository( $repos );
00633
00634 if ( isset( $options['list'] ) ) {
00635 $repository->printListing();
00636 exit(0);
00637 }
00638
00639 $name = $args[0];
00640
00641 $src = isset( $args[1] ) ? $args[1] : $repository->getResource( $name );
00642
00643 #TODO: detect $source mismatching $name !!
00644
00645 $mode = EXTINST_WRITEPATCH;
00646 if ( isset( $options['nopatch'] ) || @$wgExtensionInstallerNoPatch ) { $mode = EXTINST_NOPATCH; }
00647 else if ( isset( $options['hotpatch'] ) || @$wgExtensionInstallerHotPatch ) { $mode = EXTINST_HOTPATCH; }
00648
00649 if ( !file_exists( "$tgt/LocalSettings.php" ) ) {
00650 die("can't find $tgt/LocalSettings.php\n");
00651 }
00652
00653 if ( $mode == EXTINST_HOTPATCH && !is_writable( "$tgt/LocalSettings.php" ) ) {
00654 die("can't write to $tgt/LocalSettings.php\n");
00655 }
00656
00657 if ( !file_exists( "$tgt/extensions" ) ) {
00658 die("can't find $tgt/extensions\n");
00659 }
00660
00661 if ( !is_writable( "$tgt/extensions" ) ) {
00662 die("can't write to $tgt/extensions\n");
00663 }
00664
00665 $installer = new ExtensionInstaller( $name, $src, $tgt );
00666
00667 $installer->note( "Installing extension {$installer->name} from {$installer->source->path} to {$installer->dir}" );
00668
00669 print "\n";
00670 print "\tTHIS TOOL IS EXPERIMENTAL!\n";
00671 print "\tEXPECT THE UNEXPECTED!\n";
00672 print "\n";
00673
00674 if ( !$installer->confirm("continue") ) die("aborted\n");
00675
00676 $ok = $installer->fetchExtension();
00677
00678 if ( $ok ) $ok = $installer->patchLocalSettings( $mode );
00679
00680 if ( $ok ) $ok = $installer->printNotices();
00681
00682 if ( $ok ) $installer->note( "$name extension installed." );
00683