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