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