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