* Double-escaping, checkLabel, fixed another label
[mediawiki.git] / maintenance / installExtension.php
blob91732fe6e797b11f2f9d4d5cc5ccdc65013bded1
1 <?php
2 /**
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 {
32 var $path;
34 function InstallerRepository( $path ) {
35 $this->path = $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 ) {
47 if ( !$type ) {
48 $m = array();
49 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
50 $proto = @$m[2];
52 if ( !$proto ) {
53 $type = 'dir';
54 } else if ( ( $proto == 'http' || $proto == 'https' ) && preg_match( '!([^\w]svn|svn[^\w])!i', $path) ) {
55 $type = 'svn'; #HACK!
56 } else {
57 $type = $proto;
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!" );
77 return false;
80 foreach ( $ff as $f ) {
81 $n = basename($f);
83 if ( !is_dir( $f ) ) {
84 $m = array();
85 if ( !preg_match( '/(.*)\.(tgz|tar\.gz|zip)/', $n, $m ) ) continue;
86 $n = $m[1];
89 print "\t$n\n";
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' );
114 if ( $txt ) {
115 print $txt;
116 print "\n";
118 else {
119 $txt = file_get_contents( $this->path );
120 if ( !$txt ) {
121 ExtensionInstaller::error( "listing index from {$this->path} failed!" );
122 print ( $txt );
123 return false;
126 $m = array();
127 $ok = preg_match_all( '!<a\s[^>]*href\s*=\s*['."'".'"]([^/'."'".'"]+)\.tgz['."'".'"][^>]*>.*?</a>!si', $txt, $m, PREG_SET_ORDER );
128 if ( !$ok ) {
129 ExtensionInstaller::error( "listing index from {$this->path} does not match!" );
130 print ( $txt );
131 return false;
134 foreach ( $m as $l ) {
135 $n = $l[1];
136 print "\t$n\n";
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 );
157 if ( $code !== 0 ) {
158 ExtensionInstaller::error( "svn list for {$this->path} failed!" );
159 return false;
162 $ll = preg_split('/(\s*[\r\n]\s*)+/', $txt);
164 foreach ( $ll as $line ) {
165 $m = array();
166 if ( !preg_match('!^(.*)/$!', $line, $m) ) continue;
167 $n = $m[1];
169 print "\t$n\n";
173 function getResource( $name ) {
174 $path = $this->path . '/' . $name;
175 return new SVNInstallerResource( $path );
179 class InstallerResource {
180 var $path;
181 var $isdir;
182 var $islocal;
184 function InstallerResource( $path, $isdir, $islocal ) {
185 $this->path = $path;
187 $this->isdir= $isdir;
188 $this->islocal = $islocal;
190 $m = array();
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 );
210 if ( $code !== 0 ) {
211 ExtensionInstaller::error( "failed to extract $file!" );
212 return false;
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 );
220 if ( $code !== 0 ) {
221 ExtensionInstaller::error( "failed to extract $file!" );
222 return false;
225 else {
226 ExtensionInstaller::error( "unknown extension {$this->extensions}!" );
227 return false;
230 return true;
233 /*static*/ function makeResource( $url ) {
234 $m = array();
235 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $url, $m );
236 $proto = @$m[2];
237 $ext = @$m[3];
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 );
269 if ( !$ok ) {
270 ExtensionInstaller::error( "failed to download {$this->path}" );
271 return false;
274 $this->extract( $tmp, dirname( $target ) );
275 unlink($tmp);
277 return true;
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 );
291 if ( $code !== 0 ) {
292 ExtensionInstaller::error( "checkout failed for {$this->path}!" );
293 return false;
296 return true;
300 class ExtensionInstaller {
301 var $source;
302 var $target;
303 var $name;
304 var $dir;
305 var $tasks;
307 function ExtensionInstaller( $name, $source, $target ) {
308 if ( !is_object( $source ) ) $source = InstallerResource::makeResource( $source );
310 $this->name = $name;
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 static function note( $msg ) {
323 print "$msg\n";
326 static function warn( $msg ) {
327 print "WARNING: $msg\n";
330 static function error( $msg ) {
331 print "ERROR: $msg\n";
334 function prompt( $msg ) {
335 if ( function_exists( 'readline' ) ) {
336 $s = readline( $msg );
338 else {
339 if ( !@$this->stdin ) $this->stdin = fopen( 'php://stdin', 'r' );
340 if ( !$this->stdin ) die( "Failed to open stdin for user interaction!\n" );
342 print $msg;
343 flush();
345 $s = fgets( $this->stdin );
348 $s = trim( $s );
349 return $s;
352 function confirm( $msg ) {
353 while ( true ) {
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 . "/*" );
365 if ( !$ff ) return;
367 foreach ( $ff as $f ) {
368 if ( is_dir( $f ) && !is_link( $f ) ) $this->deleteContents( $f );
369 unlink( $f );
373 function copyDir( $dir, $tgt ) {
374 $d = $tgt . '/' . basename( $dir );
376 if ( !file_exists( $d ) ) {
377 $ok = mkdir( $d );
378 if ( !$ok ) {
379 ExtensionInstaller::error( "failed to create director $d" );
380 return false;
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;
392 else {
393 $t = $d . '/' . basename( $f );
394 $ok = copy( $f, $t );
396 if ( !$ok ) {
397 ExtensionInstaller::error( "failed to copy $f to $t" );
398 return false;
403 return true;
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 ) {
413 $n= basename( $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 );
421 else {
422 if ( !chmod( $f, $filebits ) ) ExtensionInstaller::warn( "faield to set permissions for $f" );
426 return true;
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" );
432 return true;
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 );
439 else {
440 return false;
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." );
449 return false;
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}" );
460 return true;
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 self::warn( "No install.settings file provided!" );
474 $this->tasks[] = "Please read the instructions and edit LocalSettings.php manually to activate the extension.";
475 return '?';
477 else {
478 self::note( "applying settings patch..." );
481 $settings = file_get_contents( $f );
483 if ( !$settings ) {
484 self::error( "failed to read settings from $f!" );
485 return false;
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 self::note( "Skipping patch phase, automatic patching is off." );
493 return true;
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 );
502 if ( !$ok ) {
503 self::warn( "failed to create backup of LocalSettings.php!" );
504 return false;
506 else {
507 self::note( "created backup of LocalSettings.php at $bak" );
511 $localsettings = file_get_contents( $t );
513 if ( !$settings ) {
514 self::error( "failed to read $t for patching!" );
515 return false;
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 );
536 if ( !$ok ) {
537 self::error( "failed to patch $t!" );
538 return false;
540 else if ( $mode == EXTINST_HOTPATCH ) {
541 self::note( "successfully patched $t" );
543 else {
544 self::note( "created patched settings file $t" );
545 $this->tasks[] = "Replace your current LocalSettings.php with ".basename($t);
548 return true;
551 function printNotices( ) {
552 if ( !$this->tasks ) {
553 $this->note( "Installation is complete, no pending tasks" );
555 else {
556 $this->note( "" );
557 $this->note( "PENDING TASKS:" );
558 $this->note( "" );
560 foreach ( $this->tasks as $t ) {
561 $this->note ( "* " . $t );
564 $this->note( "" );
567 return true;
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" );
581 $m = array();
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" .
591 "OPTIONS: \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();
608 exit(0);
611 $name = $args[0];
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}" );
641 print "\n";
642 print "\tTHIS TOOL IS EXPERIMENTAL!\n";
643 print "\tEXPECT THE UNEXPECTED!\n";
644 print "\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." );