Marked authenticateUserData() as public
[mediawiki.git] / maintenance / installExtension.php
blobf6b2dff4abce42b257da32e3002e406f3711c61b
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 * @package MediaWiki
21 * @subpackage Maintenance
24 $optionsWithArgs = array( 'target', 'repository', 'repos' );
26 require_once( 'commandLine.inc' );
28 define('EXTINST_NOPATCH', 0);
29 define('EXTINST_WRITEPATCH', 6);
30 define('EXTINST_HOTPATCH', 10);
32 class InstallerRepository {
33 var $path;
35 function InstallerRepository( $path ) {
36 $this->path = $path;
39 function printListing( ) {
40 trigger_error( 'override InstallerRepository::printListing()', E_USER_ERROR );
43 function getResource( $name ) {
44 trigger_error( 'override InstallerRepository::getResource()', E_USER_ERROR );
47 /*static*/ function makeRepository( $path, $type = NULL ) {
48 if ( !$type ) {
49 preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
50 $proto = @$m[2];
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;
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 );
64 class LocalInstallerRepository extends InstallerRepository {
66 function LocalInstallerRepository ( $path ) {
67 InstallerRepository::InstallerRepository( $path );
70 function printListing( ) {
71 $ff = glob( "{$this->path}/*" );
72 if ( $ff === false || $ff === NULL ) {
73 ExtensionInstaller::error( "listing directory $repos failed!" );
74 return false;
77 foreach ( $ff as $f ) {
78 $n = basename($f);
80 if ( !is_dir( $f ) ) {
81 if ( !preg_match( '/(.*)\.(tgz|tar\.gz|zip)/', $n, $m ) ) continue;
82 $n = $m[1];
85 print "\t$n\n";
89 function getResource( $name ) {
90 $path = $this->path . '/' . $name;
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';
96 return new LocalInstallerResource( $path );
100 class WebInstallerRepository extends InstallerRepository {
102 function WebInstallerRepository ( $path ) {
103 InstallerRepository::InstallerRepository( $path );
106 function printListing( ) {
107 ExtensionInstaller::note( "listing index from {$this->path}..." );
109 $txt = @file_get_contents( $this->path . '/index.txt' );
110 if ( $txt ) {
111 print $txt;
112 print "\n";
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;
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;
129 foreach ( $m as $l ) {
130 $n = $l[1];
131 print "\t$n\n";
136 function getResource( $name ) {
137 $path = $this->path . '/' . $name . '.tgz';
138 return new WebInstallerResource( $path );
142 class SVNInstallerRepository extends InstallerRepository {
144 function SVNInstallerRepository ( $path ) {
145 InstallerRepository::InstallerRepository( $path );
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;
156 $ll = preg_split('/(\s*[\r\n]\s*)+/', $txt);
158 foreach ( $ll as $line ) {
159 if ( !preg_match('!^(.*)/$!', $line, $m) ) continue;
160 $n = $m[1];
162 print "\t$n\n";
166 function getResource( $name ) {
167 $path = $this->path . '/' . $name;
168 return new SVNInstallerResource( $path );
172 class InstallerResource {
173 var $path;
174 var $isdir;
175 var $islocal;
177 function InstallerResource( $path, $isdir, $islocal ) {
178 $this->path = $path;
180 $this->isdir= $isdir;
181 $this->islocal = $islocal;
183 preg_match( '!([-+\w]+://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
185 $this->protocol = @$m[1];
186 $this->extensions = @$m[2];
188 if ( $this->extensions ) $this->extensions = strtolower( $this->extensions );
191 function fetch( $target ) {
192 trigger_error( 'override InstallerResource::fetch()', E_USER_ERROR );
195 function extract( $file, $target ) {
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 );
201 if ( $code !== 0 ) {
202 ExtensionInstaller::error( "failed to extract $file!" );
203 return false;
206 else if ( $this->extensions == '.zip' ) { #zip file
207 ExtensionInstaller::note( "extracting $file..." );
208 wfShellExec( 'unzip ' . escapeshellarg( $file ) . ' -d ' . escapeshellarg( $target ) , $code );
210 if ( $code !== 0 ) {
211 ExtensionInstaller::error( "failed to extract $file!" );
212 return false;
215 else {
216 ExtensionInstaller::error( "unknown extension {$this->extensions}!" );
217 return false;
220 return true;
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 );
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 );
235 class LocalInstallerResource extends InstallerResource {
236 function LocalInstallerResource( $path ) {
237 InstallerResource::InstallerResource( $path, is_dir( $path ), true );
240 function fetch( $target ) {
241 if ( $this->isdir ) return ExtensionInstaller::copyDir( $this->path, dirname( $target ) );
242 else return $this->extract( $this->path, dirname( $target ) );
247 class WebInstallerResource extends InstallerResource {
248 function WebInstallerResource( $path ) {
249 InstallerResource::InstallerResource( $path, false, false );
252 function fetch( $target ) {
253 $tmp = wfTempDir() . '/' . basename( $this->path );
255 ExtensionInstaller::note( "downloading {$this->path}..." );
256 $ok = copy( $this->path, $tmp );
258 if ( !$ok ) {
259 ExtensionInstaller::error( "failed to download {$this->path}" );
260 return false;
263 $this->extract( $tmp, dirname( $target ) );
264 unlink($tmp);
266 return true;
270 class SVNInstallerResource extends InstallerResource {
271 function SVNInstallerResource( $path ) {
272 InstallerResource::InstallerResource( $path, true, false );
275 function fetch( $target ) {
276 ExtensionInstaller::note( "SVN checkout of {$this->path}..." );
277 wfShellExec( 'svn co ' . escapeshellarg( $this->path ) . ' ' . escapeshellarg( $target ), $code );
279 if ( $code !== 0 ) {
280 ExtensionInstaller::error( "checkout failed for {$this->path}!" );
281 return false;
284 return true;
288 class ExtensionInstaller {
289 var $source;
290 var $target;
291 var $name;
292 var $dir;
293 var $tasks;
295 function ExtensionInstaller( $name, $source, $target ) {
296 if ( !is_object( $source ) ) $source = InstallerResource::makeResource( $source );
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();
306 #TODO: allow a subdir different from "extensions"
307 #TODO: allow a config file different from "LocalSettings.php"
310 function note( $msg ) {
311 print "$msg\n";
314 function warn( $msg ) {
315 print "WARNING: $msg\n";
318 function error( $msg ) {
319 print "ERROR: $msg\n";
322 function prompt( $msg ) {
323 if ( function_exists( 'readline' ) ) {
324 $s = readline( $msg );
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" );
330 print $msg;
331 flush();
333 $s = fgets( $this->stdin );
336 $s = trim( $s );
337 return $s;
340 function confirm( $msg ) {
341 while ( true ) {
342 $s = $this->prompt( $msg . " [yes/no]: ");
343 $s = strtolower( trim($s) );
345 if ( $s == 'yes' || $s == 'y' ) return true;
346 else if ( $s == 'no' || $s == 'n' ) return false;
347 else print "bad response: $s\n";
351 function deleteContents( $dir ) {
352 $ff = glob( $dir . "/*" );
353 if ( !$ff ) return;
355 foreach ( $ff as $f ) {
356 if ( is_dir( $f ) && !is_link( $f ) ) $this->deleteContents( $f );
357 unlink( $f );
361 function copyDir( $dir, $tgt ) {
362 $d = $tgt . '/' . basename( $dir );
364 if ( !file_exists( $d ) ) {
365 $ok = mkdir( $d );
366 if ( !$ok ) {
367 ExtensionInstaller::error( "failed to create director $d" );
368 return false;
372 $ff = glob( $dir . "/*" );
373 if ( $ff === false || $ff === NULL ) return false;
375 foreach ( $ff as $f ) {
376 if ( is_dir( $f ) && !is_link( $f ) ) {
377 $ok = ExtensionInstaller::copyDir( $f, $d );
378 if ( !$ok ) return false;
380 else {
381 $t = $d . '/' . basename( $f );
382 $ok = copy( $f, $t );
384 if ( !$ok ) {
385 ExtensionInstaller::error( "failed to copy $f to $t" );
386 return false;
391 return true;
394 function setPermissions( $dir, $dirbits, $filebits ) {
395 if ( !chmod( $dir, $dirbits ) ) ExtensionInstaller::warn( "faield to set permissions for $dir" );
397 $ff = glob( $dir . "/*" );
398 if ( $ff === false || $ff === NULL ) return false;
400 foreach ( $ff as $f ) {
401 $n= basename( $f );
402 if ( $n{0} == '.' ) continue; #HACK: skip dot files
404 if ( is_link( $f ) ) continue; #skip link
406 if ( is_dir( $f ) ) {
407 ExtensionInstaller::setPermissions( $f, $dirbits, $filebits );
409 else {
410 if ( !chmod( $f, $filebits ) ) ExtensionInstaller::warn( "faield to set permissions for $f" );
414 return true;
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;
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 );
427 else {
428 return false;
432 $ok = $this->source->fetch( $this->dir );
433 if ( !$ok ) return false;
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;
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}";
444 #TODO: configure this smartly...?
445 $this->setPermissions( $this->dir, 0755, 0644 );
447 $this->note( "fetched extension to {$this->dir}" );
448 return true;
451 function patchLocalSettings( $mode ) {
452 #NOTE: if we get a better way to hook up extensions, that should be used instead.
454 $f = $this->dir . '/install.settings';
455 $t = $this->target . '/LocalSettings.php';
457 #TODO: assert version ?!
458 #TODO: allow custom installer scripts + sql patches
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 '?';
465 else {
466 $this->note( "applying settings patch..." );
469 $settings = file_get_contents( $f );
471 if ( !$settings ) {
472 $this->error( "failed to read settings from $f!" );
473 return false;
476 $settings = str_replace( '{{path}}', $this->incpath, $settings );
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;
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';
488 $ok = copy( $t, $bak );
490 if ( !$ok ) {
491 $this->warn( "failed to create backup of LocalSettings.php!" );
492 return false;
494 else {
495 $this->note( "created backup of LocalSettings.php at $bak" );
499 $localsettings = file_get_contents( $t );
501 if ( !$settings ) {
502 $this->error( "failed to read $t for patching!" );
503 return false;
506 $marker = "<@< extension {$this->name} >@>";
507 $blockpattern = "/\n\s*#\s*BEGIN\s*$marker.*END\s*$marker\s*/smi";
509 if ( preg_match( $blockpattern, $localsettings ) ) {
510 $localsettings = preg_replace( $blockpattern, "\n", $localsettings );
511 $this->warn( "removed old configuration block for extension {$this->name}!" );
514 $newblock= "\n# BEGIN $marker\n$settings\n# END $marker\n";
516 $localsettings = preg_replace( "/\?>\s*$/si", "$newblock?>", $localsettings );
518 if ( $mode != EXTINST_HOTPATCH ) {
519 $t = $this->target . '/LocalSettings.install-' . $this->name . '-' . wfTimestamp(TS_MW) . '.php';
522 $ok = file_put_contents( $t, $localsettings );
524 if ( !$ok ) {
525 $this->error( "failed to patch $t!" );
526 return false;
528 else if ( $mode == EXTINST_HOTPATCH ) {
529 $this->note( "successfully patched $t" );
531 else {
532 $this->note( "created patched settings file $t" );
533 $this->tasks[] = "Replace your current LocalSettings.php with ".basename($t);
536 return true;
539 function printNotices( ) {
540 if ( !$this->tasks ) {
541 $this->note( "Installation is complete, no pending tasks" );
543 else {
544 $this->note( "" );
545 $this->note( "PENDING TASKS:" );
546 $this->note( "" );
548 foreach ( $this->tasks as $t ) {
549 $this->note ( "* " . $t );
552 $this->note( "" );
555 return true;
560 $tgt = isset ( $options['target'] ) ? $options['target'] : $IP;
562 $repos = @$options['repository'];
563 if ( !$repos ) $repos = @$options['repos'];
564 if ( !$repos ) $repos = @$wgExtensionInstallerRepository;
566 if ( !$repos && file_exists("$tgt/.svn") && is_dir("$tgt/.svn") ) {
567 $svn = file_get_contents( "$tgt/.svn/entries" );
569 if ( preg_match( '!url="(.*?)"!', $svn, $m ) ) {
570 $repos = dirname( $m[1] ) . '/extensions';
574 if ( !$repos ) $repos = 'http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions';
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"
591 $repository = InstallerRepository::makeRepository( $repos );
593 if ( isset( $options['list'] ) ) {
594 $repository->printListing();
595 exit(0);
598 $name = $args[0];
600 $src = isset( $args[1] ) ? $args[1] : $repository->getResource( $name );
602 #TODO: detect $source mismatching $name !!
604 $mode = EXTINST_WRITEPATCH;
605 if ( isset( $options['nopatch'] ) || @$wgExtensionInstallerNoPatch ) $mode = EXTINST_NOPATCH;
606 else if ( isset( $options['hotpatch'] ) || @$wgExtensionInstallerHotPatch ) $mode = EXTINST_HOTPATCH;
608 if ( !file_exists( "$tgt/LocalSettings.php" ) ) {
609 die("can't find $tgt/LocalSettings.php\n");
612 if ( $mode == EXTINST_HOTPATCH && !is_writable( "$tgt/LocalSettings.php" ) ) {
613 die("can't write to $tgt/LocalSettings.php\n");
616 if ( !file_exists( "$tgt/extensions" ) ) {
617 die("can't find $tgt/extensions\n");
620 if ( !is_writable( "$tgt/extensions" ) ) {
621 die("can't write to $tgt/extensions\n");
624 $installer = new ExtensionInstaller( $name, $src, $tgt );
626 $installer->note( "Installing extension {$installer->name} from {$installer->source->path} to {$installer->dir}" );
628 print "\n";
629 print "\tTHIS TOOL IS EXPERIMENTAL!\n";
630 print "\tEXPECT THE UNEXPECTED!\n";
631 print "\n";
633 if ( !$installer->confirm("continue") ) die("aborted\n");
635 $ok = $installer->fetchExtension();
637 if ( $ok ) $ok = $installer->patchLocalSettings( $mode );
639 if ( $ok ) $ok = $installer->printNotices();
641 if ( $ok ) $installer->note( "$name extension installed." );