Fix for r55810: oly set $optionsWithArgs if it's not defined, as in the old version...
[lhc/web/wiklou.git] / maintenance / installExtension.php
1 <?php
2 /**
3 * Copyright (C) 2006 Daniel Kinzler, brightbyte.de
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 $optionsWithArgs = array( 'target', 'repository', 'repos' );
25
26 require_once( dirname(__FILE__) . '/commandLine.inc' );
27
28 define('EXTINST_NOPATCH', 0);
29 define('EXTINST_WRITEPATCH', 6);
30 define('EXTINST_HOTPATCH', 10);
31
32 /**
33 * @ingroup Maintenance
34 */
35 class InstallerRepository {
36 var $path;
37
38 function InstallerRepository( $path ) {
39 $this->path = $path;
40 }
41
42 function printListing( ) {
43 trigger_error( 'override InstallerRepository::printListing()', E_USER_ERROR );
44 }
45
46 function getResource( $name ) {
47 trigger_error( 'override InstallerRepository::getResource()', E_USER_ERROR );
48 }
49
50 static function makeRepository( $path, $type = NULL ) {
51 if ( !$type ) {
52 $m = array();
53 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
54 $proto = @$m[2];
55
56 if ( !$proto ) {
57 $type = 'dir';
58 } else if ( ( $proto == 'http' || $proto == 'https' ) && preg_match( '!([^\w]svn|svn[^\w])!i', $path) ) {
59 $type = 'svn'; #HACK!
60 } else {
61 $type = $proto;
62 }
63 }
64
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 ); }
68 }
69 }
70
71 /**
72 * @ingroup Maintenance
73 */
74 class LocalInstallerRepository extends InstallerRepository {
75
76 function LocalInstallerRepository ( $path ) {
77 InstallerRepository::InstallerRepository( $path );
78 }
79
80 function printListing( ) {
81 $ff = glob( "{$this->path}/*" );
82 if ( $ff === false || $ff === NULL ) {
83 ExtensionInstaller::error( "listing directory {$this->path} failed!" );
84 return false;
85 }
86
87 foreach ( $ff as $f ) {
88 $n = basename($f);
89
90 if ( !is_dir( $f ) ) {
91 $m = array();
92 if ( !preg_match( '/(.*)\.(tgz|tar\.gz|zip)/', $n, $m ) ) continue;
93 $n = $m[1];
94 }
95
96 print "\t$n\n";
97 }
98 }
99
100 function getResource( $name ) {
101 $path = $this->path . '/' . $name;
102
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';
106
107 return new LocalInstallerResource( $path );
108 }
109 }
110
111 /**
112 * @ingroup Maintenance
113 */
114 class WebInstallerRepository extends InstallerRepository {
115
116 function WebInstallerRepository ( $path ) {
117 InstallerRepository::InstallerRepository( $path );
118 }
119
120 function printListing( ) {
121 ExtensionInstaller::note( "listing index from {$this->path}..." );
122
123 $txt = @file_get_contents( $this->path . '/index.txt' );
124 if ( $txt ) {
125 print $txt;
126 print "\n";
127 }
128 else {
129 $txt = file_get_contents( $this->path );
130 if ( !$txt ) {
131 ExtensionInstaller::error( "listing index from {$this->path} failed!" );
132 print ( $txt );
133 return false;
134 }
135
136 $m = array();
137 $ok = preg_match_all( '!<a\s[^>]*href\s*=\s*['."'".'"]([^/'."'".'"]+)\.tgz['."'".'"][^>]*>.*?</a>!si', $txt, $m, PREG_SET_ORDER );
138 if ( !$ok ) {
139 ExtensionInstaller::error( "listing index from {$this->path} does not match!" );
140 print ( $txt );
141 return false;
142 }
143
144 foreach ( $m as $l ) {
145 $n = $l[1];
146 print "\t$n\n";
147 }
148 }
149 }
150
151 function getResource( $name ) {
152 $path = $this->path . '/' . $name . '.tgz';
153 return new WebInstallerResource( $path );
154 }
155 }
156
157 /**
158 * @ingroup Maintenance
159 */
160 class SVNInstallerRepository extends InstallerRepository {
161
162 function SVNInstallerRepository ( $path ) {
163 InstallerRepository::InstallerRepository( $path );
164 }
165
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 );
170 if ( $code !== 0 ) {
171 ExtensionInstaller::error( "svn list for {$this->path} failed!" );
172 return false;
173 }
174
175 $ll = preg_split('/(\s*[\r\n]\s*)+/', $txt);
176
177 foreach ( $ll as $line ) {
178 $m = array();
179 if ( !preg_match('!^(.*)/$!', $line, $m) ) continue;
180 $n = $m[1];
181
182 print "\t$n\n";
183 }
184 }
185
186 function getResource( $name ) {
187 $path = $this->path . '/' . $name;
188 return new SVNInstallerResource( $path );
189 }
190 }
191
192 /**
193 * @ingroup Maintenance
194 */
195 class InstallerResource {
196 var $path;
197 var $isdir;
198 var $islocal;
199
200 function InstallerResource( $path, $isdir, $islocal ) {
201 $this->path = $path;
202
203 $this->isdir= $isdir;
204 $this->islocal = $islocal;
205
206 $m = array();
207 preg_match( '!([-+\w]+://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
208
209 $this->protocol = @$m[1];
210 $this->extensions = @$m[2];
211
212 if ( $this->extensions ) $this->extensions = strtolower( $this->extensions );
213 }
214
215 function fetch( $target ) {
216 trigger_error( 'override InstallerResource::fetch()', E_USER_ERROR );
217 }
218
219 function extract( $file, $target ) {
220
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 );
225
226 if ( $code !== 0 ) {
227 ExtensionInstaller::error( "failed to extract $file!" );
228 return false;
229 }
230 }
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 );
235
236 if ( $code !== 0 ) {
237 ExtensionInstaller::error( "failed to extract $file!" );
238 return false;
239 }
240 }
241 else {
242 ExtensionInstaller::error( "unknown extension {$this->extensions}!" );
243 return false;
244 }
245
246 return true;
247 }
248
249 /*static*/ function makeResource( $url ) {
250 $m = array();
251 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $url, $m );
252 $proto = @$m[2];
253 $ext = @$m[3];
254 if ( $ext ) $ext = strtolower( $ext );
255
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 ); }
259 }
260 }
261
262 /**
263 * @ingroup Maintenance
264 */
265 class LocalInstallerResource extends InstallerResource {
266 function LocalInstallerResource( $path ) {
267 InstallerResource::InstallerResource( $path, is_dir( $path ), true );
268 }
269
270 function fetch( $target ) {
271 if ( $this->isdir ) return ExtensionInstaller::copyDir( $this->path, dirname( $target ) );
272 else return $this->extract( $this->path, dirname( $target ) );
273 }
274
275 }
276
277 /**
278 * @ingroup Maintenance
279 */
280 class WebInstallerResource extends InstallerResource {
281 function WebInstallerResource( $path ) {
282 InstallerResource::InstallerResource( $path, false, false );
283 }
284
285 function fetch( $target ) {
286 $tmp = wfTempDir() . '/' . basename( $this->path );
287
288 ExtensionInstaller::note( "downloading {$this->path}..." );
289 $ok = copy( $this->path, $tmp );
290
291 if ( !$ok ) {
292 ExtensionInstaller::error( "failed to download {$this->path}" );
293 return false;
294 }
295
296 $this->extract( $tmp, dirname( $target ) );
297 unlink($tmp);
298
299 return true;
300 }
301 }
302
303 /**
304 * @ingroup Maintenance
305 */
306 class SVNInstallerResource extends InstallerResource {
307 function SVNInstallerResource( $path ) {
308 InstallerResource::InstallerResource( $path, true, false );
309 }
310
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 );
315
316 if ( $code !== 0 ) {
317 ExtensionInstaller::error( "checkout failed for {$this->path}!" );
318 return false;
319 }
320
321 return true;
322 }
323 }
324
325 /**
326 * @ingroup Maintenance
327 */
328 class ExtensionInstaller {
329 var $source;
330 var $target;
331 var $name;
332 var $dir;
333 var $tasks;
334
335 function ExtensionInstaller( $name, $source, $target ) {
336 if ( !is_object( $source ) ) $source = InstallerResource::makeResource( $source );
337
338 $this->name = $name;
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();
345
346 #TODO: allow a subdir different from "extensions"
347 #TODO: allow a config file different from "LocalSettings.php"
348 }
349
350 static function note( $msg ) {
351 print "$msg\n";
352 }
353
354 static function warn( $msg ) {
355 print "WARNING: $msg\n";
356 }
357
358 static function error( $msg ) {
359 print "ERROR: $msg\n";
360 }
361
362 function prompt( $msg ) {
363 if ( function_exists( 'readline' ) ) {
364 $s = readline( $msg );
365 }
366 else {
367 if ( !@$this->stdin ) $this->stdin = fopen( 'php://stdin', 'r' );
368 if ( !$this->stdin ) die( "Failed to open stdin for user interaction!\n" );
369
370 print $msg;
371 flush();
372
373 $s = fgets( $this->stdin );
374 }
375
376 $s = trim( $s );
377 return $s;
378 }
379
380 function confirm( $msg ) {
381 while ( true ) {
382 $s = $this->prompt( $msg . " [yes/no]: ");
383 $s = strtolower( trim($s) );
384
385 if ( $s == 'yes' || $s == 'y' ) { return true; }
386 else if ( $s == 'no' || $s == 'n' ) { return false; }
387 else { print "bad response: $s\n"; }
388 }
389 }
390
391 function deleteContents( $dir ) {
392 $ff = glob( $dir . "/*" );
393 if ( !$ff ) return;
394
395 foreach ( $ff as $f ) {
396 if ( is_dir( $f ) && !is_link( $f ) ) $this->deleteContents( $f );
397 unlink( $f );
398 }
399 }
400
401 function copyDir( $dir, $tgt ) {
402 $d = $tgt . '/' . basename( $dir );
403
404 if ( !file_exists( $d ) ) {
405 $ok = mkdir( $d );
406 if ( !$ok ) {
407 ExtensionInstaller::error( "failed to create director $d" );
408 return false;
409 }
410 }
411
412 $ff = glob( $dir . "/*" );
413 if ( $ff === false || $ff === NULL ) return false;
414
415 foreach ( $ff as $f ) {
416 if ( is_dir( $f ) && !is_link( $f ) ) {
417 $ok = ExtensionInstaller::copyDir( $f, $d );
418 if ( !$ok ) return false;
419 }
420 else {
421 $t = $d . '/' . basename( $f );
422 $ok = copy( $f, $t );
423
424 if ( !$ok ) {
425 ExtensionInstaller::error( "failed to copy $f to $t" );
426 return false;
427 }
428 }
429 }
430
431 return true;
432 }
433
434 function setPermissions( $dir, $dirbits, $filebits ) {
435 if ( !chmod( $dir, $dirbits ) ) ExtensionInstaller::warn( "faield to set permissions for $dir" );
436
437 $ff = glob( $dir . "/*" );
438 if ( $ff === false || $ff === NULL ) return false;
439
440 foreach ( $ff as $f ) {
441 $n= basename( $f );
442 if ( $n{0} == '.' ) continue; #HACK: skip dot files
443
444 if ( is_link( $f ) ) continue; #skip link
445
446 if ( is_dir( $f ) ) {
447 ExtensionInstaller::setPermissions( $f, $dirbits, $filebits );
448 }
449 else {
450 if ( !chmod( $f, $filebits ) ) ExtensionInstaller::warn( "faield to set permissions for $f" );
451 }
452 }
453
454 return true;
455 }
456
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" );
460 return true;
461 }
462
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 );
466 }
467 else {
468 return false;
469 }
470 }
471
472 $ok = $this->source->fetch( $this->dir );
473 if ( !$ok ) return false;
474
475 if ( !file_exists( $this->dir ) && glob( $this->dir . "/*" ) ) {
476 $this->error( "{$this->dir} does not exist or is empty. Something went wrong, sorry." );
477 return false;
478 }
479
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}";
483
484 #TODO: configure this smartly...?
485 $this->setPermissions( $this->dir, 0755, 0644 );
486
487 $this->note( "fetched extension to {$this->dir}" );
488 return true;
489 }
490
491 function patchLocalSettings( $mode ) {
492 #NOTE: if we get a better way to hook up extensions, that should be used instead.
493
494 $f = $this->dir . '/install.settings';
495 $t = $this->target . '/LocalSettings.php';
496
497 #TODO: assert version ?!
498 #TODO: allow custom installer scripts + sql patches
499
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.";
503 return '?';
504 }
505 else {
506 self::note( "applying settings patch..." );
507 }
508
509 $settings = file_get_contents( $f );
510
511 if ( !$settings ) {
512 self::error( "failed to read settings from $f!" );
513 return false;
514 }
515
516 $settings = str_replace( '{{path}}', $this->incpath, $settings );
517
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." );
521 return true;
522 }
523
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';
527
528 $ok = copy( $t, $bak );
529
530 if ( !$ok ) {
531 self::warn( "failed to create backup of LocalSettings.php!" );
532 return false;
533 }
534 else {
535 self::note( "created backup of LocalSettings.php at $bak" );
536 }
537 }
538
539 $localsettings = file_get_contents( $t );
540
541 if ( !$settings ) {
542 self::error( "failed to read $t for patching!" );
543 return false;
544 }
545
546 $marker = "<@< extension {$this->name} >@>";
547 $blockpattern = "/\n\s*#\s*BEGIN\s*$marker.*END\s*$marker\s*/smi";
548
549 if ( preg_match( $blockpattern, $localsettings ) ) {
550 $localsettings = preg_replace( $blockpattern, "\n", $localsettings );
551 $this->warn( "removed old configuration block for extension {$this->name}!" );
552 }
553
554 $newblock= "\n# BEGIN $marker\n$settings\n# END $marker\n";
555
556 $localsettings = preg_replace( "/\?>\s*$/si", "$newblock?>", $localsettings );
557
558 if ( $mode != EXTINST_HOTPATCH ) {
559 $t = $this->target . '/LocalSettings.install-' . $this->name . '-' . wfTimestamp(TS_MW) . '.php';
560 }
561
562 $ok = file_put_contents( $t, $localsettings );
563
564 if ( !$ok ) {
565 self::error( "failed to patch $t!" );
566 return false;
567 }
568 else if ( $mode == EXTINST_HOTPATCH ) {
569 self::note( "successfully patched $t" );
570 }
571 else {
572 self::note( "created patched settings file $t" );
573 $this->tasks[] = "Replace your current LocalSettings.php with ".basename($t);
574 }
575
576 return true;
577 }
578
579 function printNotices( ) {
580 if ( !$this->tasks ) {
581 $this->note( "Installation is complete, no pending tasks" );
582 }
583 else {
584 $this->note( "" );
585 $this->note( "PENDING TASKS:" );
586 $this->note( "" );
587
588 foreach ( $this->tasks as $t ) {
589 $this->note ( "* " . $t );
590 }
591
592 $this->note( "" );
593 }
594
595 return true;
596 }
597
598 }
599
600 $tgt = isset ( $options['target'] ) ? $options['target'] : $IP;
601
602 $repos = @$options['repository'];
603 if ( !$repos ) $repos = @$options['repos'];
604 if ( !$repos ) $repos = @$wgExtensionInstallerRepository;
605
606 if ( !$repos && file_exists("$tgt/.svn") && is_dir("$tgt/.svn") ) {
607 $svn = file_get_contents( "$tgt/.svn/entries" );
608
609 $m = array();
610 if ( preg_match( '!url="(.*?)"!', $svn, $m ) ) {
611 $repos = dirname( $m[1] ) . '/extensions';
612 }
613 }
614
615 if ( !$repos ) $repos = 'http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions';
616
617 if( !isset( $args[0] ) && !@$options['list'] ) {
618 die( "USAGE: installExtension.php [options] <name> [source]\n" .
619 "OPTIONS: \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"
629 );
630 }
631
632 $repository = InstallerRepository::makeRepository( $repos );
633
634 if ( isset( $options['list'] ) ) {
635 $repository->printListing();
636 exit(0);
637 }
638
639 $name = $args[0];
640
641 $src = isset( $args[1] ) ? $args[1] : $repository->getResource( $name );
642
643 #TODO: detect $source mismatching $name !!
644
645 $mode = EXTINST_WRITEPATCH;
646 if ( isset( $options['nopatch'] ) || @$wgExtensionInstallerNoPatch ) { $mode = EXTINST_NOPATCH; }
647 else if ( isset( $options['hotpatch'] ) || @$wgExtensionInstallerHotPatch ) { $mode = EXTINST_HOTPATCH; }
648
649 if ( !file_exists( "$tgt/LocalSettings.php" ) ) {
650 die("can't find $tgt/LocalSettings.php\n");
651 }
652
653 if ( $mode == EXTINST_HOTPATCH && !is_writable( "$tgt/LocalSettings.php" ) ) {
654 die("can't write to $tgt/LocalSettings.php\n");
655 }
656
657 if ( !file_exists( "$tgt/extensions" ) ) {
658 die("can't find $tgt/extensions\n");
659 }
660
661 if ( !is_writable( "$tgt/extensions" ) ) {
662 die("can't write to $tgt/extensions\n");
663 }
664
665 $installer = new ExtensionInstaller( $name, $src, $tgt );
666
667 $installer->note( "Installing extension {$installer->name} from {$installer->source->path} to {$installer->dir}" );
668
669 print "\n";
670 print "\tTHIS TOOL IS EXPERIMENTAL!\n";
671 print "\tEXPECT THE UNEXPECTED!\n";
672 print "\n";
673
674 if ( !$installer->confirm("continue") ) die("aborted\n");
675
676 $ok = $installer->fetchExtension();
677
678 if ( $ok ) $ok = $installer->patchLocalSettings( $mode );
679
680 if ( $ok ) $ok = $installer->printNotices();
681
682 if ( $ok ) $installer->note( "$name extension installed." );
683