API: (bug 17703) Fix regression from r47039 causing error code and error text to...
[lhc/web/wiklou.git] / maintenance / installExtension.php
index e6bed0b..d5c4f4b 100644 (file)
@@ -17,8 +17,8 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
- * @package MediaWiki
- * @subpackage Maintenance
+ * @file
+ * @ingroup Maintenance
  */
 
 $optionsWithArgs = array( 'target', 'repository', 'repos' );
@@ -29,6 +29,9 @@ define('EXTINST_NOPATCH', 0);
 define('EXTINST_WRITEPATCH', 6);
 define('EXTINST_HOTPATCH', 10);
 
+/**
+ * @ingroup Maintenance
+ */
 class InstallerRepository {
        var $path;
        
@@ -44,23 +47,30 @@ class InstallerRepository {
                trigger_error( 'override InstallerRepository::getResource()', E_USER_ERROR );
        }        
        
-       /*static*/ function makeRepository( $path, $type = NULL ) {
+       static function makeRepository( $path, $type = NULL ) {
                if ( !$type ) {
+                       $m = array();
                        preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
                        $proto = @$m[2];
                        
-                       if( !$proto ) $type = 'dir';
-                       else if ( ( $proto == 'http' || $proto == 'https' ) 
-                                  && preg_match( '!([^\w]svn|svn[^\w])!i', $path) ) $type = 'svn'; #HACK!
-                       else $type = $proto;
+                       if ( !$proto ) {
+                               $type = 'dir';
+                       } else if ( ( $proto == 'http' || $proto == 'https' ) && preg_match( '!([^\w]svn|svn[^\w])!i', $path) ) {
+                               $type = 'svn'; #HACK!
+                       } else  {
+                               $type = $proto;
+                       }
                }
                
-               if ( $type == 'dir' || $type == 'file' ) return new LocalInstallerRepository( $path );
-               else if ( $type == 'http' || $type == 'http' ) return new WebInstallerRepository( $path );
-               else return new SVNInstallerRepository( $path );
+               if ( $type == 'dir' || $type == 'file' ) { return new LocalInstallerRepository( $path ); }
+               else if ( $type == 'http' || $type == 'http' ) { return new WebInstallerRepository( $path ); }
+               else { return new SVNInstallerRepository( $path ); }
        }
 }
 
+/**
+ * @ingroup Maintenance
+ */
 class LocalInstallerRepository extends InstallerRepository {
 
        function LocalInstallerRepository ( $path ) {
@@ -70,7 +80,7 @@ class LocalInstallerRepository extends InstallerRepository {
        function printListing( ) {
                $ff = glob( "{$this->path}/*" );
                if ( $ff === false || $ff === NULL ) {
-                       ExtensionInstaller::error( "listing directory $repos failed!" );
+                       ExtensionInstaller::error( "listing directory {$this->path} failed!" );
                        return false;
                }
                
@@ -78,6 +88,7 @@ class LocalInstallerRepository extends InstallerRepository {
                        $n = basename($f);
                        
                        if ( !is_dir( $f ) ) {
+                               $m = array();
                                if ( !preg_match( '/(.*)\.(tgz|tar\.gz|zip)/', $n, $m ) ) continue;
                                $n = $m[1];
                        }
@@ -97,6 +108,9 @@ class LocalInstallerRepository extends InstallerRepository {
        }        
 }
 
+/**
+ * @ingroup Maintenance
+ */
 class WebInstallerRepository extends InstallerRepository {
 
        function WebInstallerRepository ( $path ) {
@@ -118,7 +132,8 @@ class WebInstallerRepository extends InstallerRepository {
                                print ( $txt );
                                return false;
                        }
-                       
+
+                       $m = array();
                        $ok = preg_match_all( '!<a\s[^>]*href\s*=\s*['."'".'"]([^/'."'".'"]+)\.tgz['."'".'"][^>]*>.*?</a>!si', $txt, $m, PREG_SET_ORDER ); 
                        if ( !$ok ) {
                                ExtensionInstaller::error( "listing index from {$this->path} does not match!" );
@@ -139,6 +154,9 @@ class WebInstallerRepository extends InstallerRepository {
        }        
 }
 
+/**
+ * @ingroup Maintenance
+ */
 class SVNInstallerRepository extends InstallerRepository {
 
        function SVNInstallerRepository ( $path ) {
@@ -147,6 +165,7 @@ class SVNInstallerRepository extends InstallerRepository {
 
        function printListing( ) {
                ExtensionInstaller::note( "SVN list {$this->path}..." );
+               $code = null; // Shell Exec return value.
                $txt = wfShellExec( 'svn ls ' . escapeshellarg( $this->path ), $code );
                if ( $code !== 0 ) {
                        ExtensionInstaller::error( "svn list for {$this->path} failed!" );
@@ -156,6 +175,7 @@ class SVNInstallerRepository extends InstallerRepository {
                $ll = preg_split('/(\s*[\r\n]\s*)+/', $txt);
                
                foreach ( $ll as $line ) {
+                       $m = array();
                        if ( !preg_match('!^(.*)/$!', $line, $m) ) continue;
                        $n = $m[1];
                                  
@@ -169,6 +189,9 @@ class SVNInstallerRepository extends InstallerRepository {
        }        
 }
 
+/**
+ * @ingroup Maintenance
+ */
 class InstallerResource {
        var $path;
        var $isdir;
@@ -180,6 +203,7 @@ class InstallerResource {
                $this->isdir= $isdir;
                $this->islocal = $islocal;
 
+               $m = array();
                preg_match( '!([-+\w]+://)?.*?(\.[-\w\d.]+)?$!', $path, $m );
 
                $this->protocol = @$m[1];
@@ -196,6 +220,7 @@ class InstallerResource {
                
                if ( $this->extensions == '.tgz' || $this->extensions == '.tar.gz' ) { #tgz file
                        ExtensionInstaller::note( "extracting $file..." );
+                       $code = null; // shell Exec return value.
                        wfShellExec( 'tar zxvf ' . escapeshellarg( $file ) . ' -C ' . escapeshellarg( $target ), $code );
                        
                        if ( $code !== 0 ) {
@@ -205,6 +230,7 @@ class InstallerResource {
                }
                else if ( $this->extensions == '.zip' ) { #zip file
                        ExtensionInstaller::note( "extracting $file..." );
+                       $code = null; // shell Exec return value.
                        wfShellExec( 'unzip ' . escapeshellarg( $file ) . ' -d ' . escapeshellarg( $target ) , $code );
                        
                        if ( $code !== 0 ) {
@@ -221,17 +247,21 @@ class InstallerResource {
        }        
 
        /*static*/ function makeResource( $url ) {
+               $m = array();
                preg_match( '!(([-+\w]+)://)?.*?(\.[-\w\d.]+)?$!', $url, $m );
                $proto = @$m[2];
                $ext = @$m[3];
                if ( $ext ) $ext = strtolower( $ext );
                
-               if ( !$proto ) return new LocalInstallerResource( $url, $ext ? false : true );
-               else if ( $ext && ( $proto == 'http' || $proto == 'http' || $proto == 'ftp' ) ) return new WebInstallerResource( $url );
-               else return new SVNInstallerResource( $url );
+               if ( !$proto ) { return new LocalInstallerResource( $url, $ext ? false : true ); }
+               else if ( $ext && ( $proto == 'http' || $proto == 'http' || $proto == 'ftp' ) ) { return new WebInstallerResource( $url ); }
+               else { return new SVNInstallerResource( $url ); }
        }
 }
 
+/**
+ * @ingroup Maintenance
+ */
 class LocalInstallerResource extends InstallerResource {
        function LocalInstallerResource( $path ) {
                InstallerResource::InstallerResource( $path, is_dir( $path ), true );
@@ -244,6 +274,9 @@ class LocalInstallerResource extends InstallerResource {
         
 }
 
+/**
+ * @ingroup Maintenance
+ */
 class WebInstallerResource extends InstallerResource {
        function WebInstallerResource( $path ) {
                InstallerResource::InstallerResource( $path, false, false );
@@ -267,6 +300,9 @@ class WebInstallerResource extends InstallerResource {
        }        
 }
 
+/**
+ * @ingroup Maintenance
+ */
 class SVNInstallerResource extends InstallerResource {
        function SVNInstallerResource( $path ) {
                InstallerResource::InstallerResource( $path, true, false );
@@ -274,6 +310,7 @@ class SVNInstallerResource extends InstallerResource {
         
        function fetch( $target ) {
                ExtensionInstaller::note( "SVN checkout of {$this->path}..." );
+               $code = null; // shell exec return val.
                wfShellExec( 'svn co ' . escapeshellarg( $this->path ) . ' ' . escapeshellarg( $target ), $code );
 
                if ( $code !== 0 ) {
@@ -285,6 +322,9 @@ class SVNInstallerResource extends InstallerResource {
        }        
 }
 
+/**
+ * @ingroup Maintenance
+ */
 class ExtensionInstaller {
        var $source;
        var $target;
@@ -307,15 +347,15 @@ class ExtensionInstaller {
                #TODO: allow a config file different from "LocalSettings.php"
        }
 
-       function note( $msg ) {
+       static function note( $msg ) {
                print "$msg\n";
        }
 
-       function warn( $msg ) {
+       static function warn( $msg ) {
                print "WARNING: $msg\n";
        }
 
-       function error( $msg ) {
+       static function error( $msg ) {
                print "ERROR: $msg\n";
        }
 
@@ -342,9 +382,9 @@ class ExtensionInstaller {
                        $s = $this->prompt( $msg . " [yes/no]: ");
                        $s = strtolower( trim($s) );
                        
-                       if ( $s == 'yes' || $s == 'y' ) return true;
-                       else if ( $s == 'no' || $s == 'n' ) return false;
-                       else print "bad response: $s\n";                        
+                       if ( $s == 'yes' || $s == 'y' ) { return true; }
+                       else if ( $s == 'no' || $s == 'n' ) { return false; }
+                       else { print "bad response: $s\n"; }
                }
        }
 
@@ -458,18 +498,18 @@ class ExtensionInstaller {
                #TODO: allow custom installer scripts + sql patches
                
                if ( !file_exists( $f ) ) {
-                       $this->warn( "No install.settings file provided!" );
+                       self::warn( "No install.settings file provided!" );
                        $this->tasks[] = "Please read the instructions and edit LocalSettings.php manually to activate the extension.";
                        return '?';
                }
                else {
-                       $this->note( "applying settings patch..." );
+                       self::note( "applying settings patch..." );
                }
                
                $settings = file_get_contents( $f );
                                
                if ( !$settings ) {
-                       $this->error( "failed to read settings from $f!" );
+                       self::error( "failed to read settings from $f!" );
                        return false;
                }
                                
@@ -477,7 +517,7 @@ class ExtensionInstaller {
                
                if ( $mode == EXTINST_NOPATCH ) {
                        $this->tasks[] = "Please put the following into your LocalSettings.php:" . "\n$settings\n";
-                       $this->note( "Skipping patch phase, automatic patching is off." );
+                       self::note( "Skipping patch phase, automatic patching is off." );
                        return true;
                }
                
@@ -488,18 +528,18 @@ class ExtensionInstaller {
                        $ok = copy( $t, $bak );
                                        
                        if ( !$ok ) {
-                               $this->warn( "failed to create backup of LocalSettings.php!" );
+                               self::warn( "failed to create backup of LocalSettings.php!" );
                                return false;
                        }
                        else {
-                               $this->note( "created backup of LocalSettings.php at $bak" );
+                               self::note( "created backup of LocalSettings.php at $bak" );
                        }
                }
                                
                $localsettings = file_get_contents( $t );
                                
                if ( !$settings ) {
-                       $this->error( "failed to read $t for patching!" );
+                       self::error( "failed to read $t for patching!" );
                        return false;
                }
                                
@@ -522,14 +562,14 @@ class ExtensionInstaller {
                $ok = file_put_contents( $t, $localsettings );
                
                if ( !$ok ) {
-                       $this->error( "failed to patch $t!" );
+                       self::error( "failed to patch $t!" );
                        return false;
                }
                else if ( $mode == EXTINST_HOTPATCH ) {
-                       $this->note( "successfully patched $t" );
+                       self::note( "successfully patched $t" );
                }
                else  {
-                       $this->note( "created patched settings file $t" );
+                       self::note( "created patched settings file $t" );
                        $this->tasks[] = "Replace your current LocalSettings.php with ".basename($t);
                }
                
@@ -566,6 +606,7 @@ if ( !$repos ) $repos = @$wgExtensionInstallerRepository;
 if ( !$repos && file_exists("$tgt/.svn") && is_dir("$tgt/.svn") ) {
        $svn = file_get_contents( "$tgt/.svn/entries" );
        
+       $m = array();
        if ( preg_match( '!url="(.*?)"!', $svn, $m ) ) {
                $repos = dirname( $m[1] ) . '/extensions';
        }
@@ -573,7 +614,7 @@ if ( !$repos && file_exists("$tgt/.svn") && is_dir("$tgt/.svn") ) {
 
 if ( !$repos ) $repos = 'http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions';
 
-if( !isset( $args[0] ) && !$options['list'] ) {
+if( !isset( $args[0] ) && !@$options['list'] ) {
        die( "USAGE: installExtension.php [options] <name> [source]\n" .
                "OPTIONS: \n" . 
                "    --list            list available extensions. <name> is ignored / may be omitted.\n" .
@@ -602,8 +643,8 @@ $src = isset( $args[1] ) ? $args[1] : $repository->getResource( $name );
 #TODO: detect $source mismatching $name !!
 
 $mode = EXTINST_WRITEPATCH;
-if ( isset( $options['nopatch'] ) || @$wgExtensionInstallerNoPatch ) $mode = EXTINST_NOPATCH;
-else if ( isset( $options['hotpatch'] ) || @$wgExtensionInstallerHotPatch ) $mode = EXTINST_HOTPATCH;
+if ( isset( $options['nopatch'] ) || @$wgExtensionInstallerNoPatch ) { $mode = EXTINST_NOPATCH; }
+else if ( isset( $options['hotpatch'] ) || @$wgExtensionInstallerHotPatch ) { $mode = EXTINST_HOTPATCH; }
 
 if ( !file_exists( "$tgt/LocalSettings.php" ) ) {
        die("can't find $tgt/LocalSettings.php\n");
@@ -639,4 +680,4 @@ if ( $ok ) $ok = $installer->patchLocalSettings( $mode );
 if ( $ok ) $ok = $installer->printNotices();
 
 if ( $ok ) $installer->note( "$name extension installed." );
-?>
+