Sunday, 23 January 2011

Unistalling multimple extensions in Joomla!

Here's the uninstaller code. It depends on the extensions.txt file written by the installer script.

<?php
/**
 * Uninstall a set of previously installed extensions
 * @package Packager
 */
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
define( 'PACKAGER','Packager');
$exts = array();
$types = array();
// 1. read in the extensions.txt file
$extensions_file = $this->parent->_paths[
  'extension_administrator'].DS."extensions.txt";
$file_len = filesize( $extensions_file );
$handle = fopen( $extensions_file, "r" );
if ( $handle )
{
  $contents = fread( $handle, $file_len );
  if ( $contents )
  {
    $lines = split( "\n", $contents );
    foreach ( $lines as $line )
    {
      $extension = split( "\t", $line );
      if ( count($extension)==2 )
      {
        $types[] = $extension[0];
        $exts[] = $extension[1];
      }
    }
  }
  fclose( $handle );
  // 2. for each extension, retrieve its ID from the database
  $db = JFactory::getDBO();
  $prefix = $db->getPrefix();
  $installer = new JInstaller();
  for ( $i=0;$i<count($types);$i++ )
  {
    $table = $prefix.$types[$i]."s";
    $name = ($types[$i]=="module")?"title":"name";
    $query = "select id from $table where $name='".$exts[$i]."';";
    $db->setQuery( $query );
  $result = $db->loadObjectList();
  if ( count($result) == 1 )
  {
      // 3. uninstall it
      $installer->uninstall( $types[$i], $result[0]->id, 0 );
    }
  }
}
else
  error_log(PACKAGER.": failed to find extensions.txt");
?>

No comments:

Post a Comment