JInstallerModule/uninstall
From Joomla! Documentation
< API16:JInstallerModule
The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Description[edit]
Custom uninstall method
<! removed transcluded page call, red link never existed >
Syntax[edit]
uninstall($id)
Parameter Name | Default Value | Description |
---|---|---|
$id | $id The id of the module to uninstall |
Returns[edit]
boolean True on success
Defined in[edit]
libraries/joomla/installer/adapters/module.php
Importing[edit]
jimport( 'joomla.installer.adapters.module' );
Source Body[edit]
public function uninstall($id)
{
// Initialise variables.
$row = null;
$retval = true;
$db = &$this->parent->getDbo();
// First order of business will be to load the module object table from the database.
// This should give us the necessary information to proceed.
$row = & JTable::getInstance('extension');
if (!$row->load((int) $id) || !strlen($row->element))
{
JError::raiseWarning(100, JText::_('ERRORUNKOWNEXTENSION'));
return false;
}
// Is the module we are trying to uninstall a core one?
// Because that is not a good idea...
if ($row->protected)
{
JError::raiseWarning(100, JText::_('Module').' '.JText::_('Uninstall').': '.JText::sprintf('WARNCOREMODULE', $row->name)."<br />".JText::_('WARNCOREMODULE2'));
return false;
}
// Get the extension root path
jimport('joomla.application.helper');
$element = $row->element;
$client = &JApplicationHelper::getClientInfo($row->client_id);
if ($client === false)
{
$this->parent->abort(JText::_('Module').' '.JText::_('Uninstall').': '.JText::_('Unknown client type').' ['.$row->client_id.']');
return false;
}
$this->parent->setPath('extension_root', $client->path.DS.'modules'.DS.$element);
$this->parent->setPath('source', $this->parent->getPath('extension_root'));
// Get the package manifest objecct
$this->manifest = $this->parent->getManifest();
// If there is an manifest class file, lets load it
$this->scriptElement = $this->manifest->scriptfile;
$manifestScript = (string)$this->manifest->scriptfile;
if ($manifestScript)
{
$manifestScriptFile = $this->parent->getPath('extension_root').DS.$manifestScript;
if (is_file($manifestScriptFile))
{
// load the file
include_once $manifestScriptFile;
}
// Set the class name
$classname = $element.'InstallerScript';
if (class_exists($classname))
{
// create a new instance
$this->parent->manifestClass = new $classname($this);
// and set this so we can copy it later
$this->set('manifest_script', $manifestScript);
// Note: if we don't find the class, don't bother to copy the file
}
}
ob_start();
ob_implicit_flush(false);
// run uninstall if possible
if ($this->parent->manifestClass && method_exists($this->parent->manifestClass,'uninstall')) {
$this->parent->manifestClass->uninstall($this);
}
$msg = ob_get_contents();
ob_end_clean();
if (!$this->manifest INSTANCEOF JXMLElement)
{
// Make sure we delete the folders
JFolder::delete($this->parent->getPath('extension_root'));
JError::raiseWarning(100, 'Module Uninstall: Package manifest file invalid or not found');
return false;
}
/*
* Let's run the uninstall queries for the component
* If Joomla 1.5 compatible, with discreet sql files - execute appropriate
* file for utf-8 support or non-utf support
*/
// try for Joomla 1.5 type queries
// second argument is the utf compatible version attribute
$utfresult = $this->parent->parseSQLFiles($this->manifest->uninstall->sql);
if ($utfresult === false)
{
// Install failed, rollback changes
JError::raiseWarning(100, JText::_('Module').' '.JText::_('Uninstall').': '.JText::_('SQLERRORORFILE')." ".$db->stderr(true));
$retval = false;
}
// Remove other files
$this->parent->removeFiles($this->manifest->media);
$this->parent->removeFiles($this->manifest->languages, $row->client_id);
// Lets delete all the module copies for the type we are uninstalling
$query = 'SELECT `id`' .
' FROM `#__modules`' .
' WHERE module = '.$db->Quote($row->element) .
' AND client_id = '.(int)$row->client_id;
$db->setQuery($query);
try {
$modules = $db->loadResultArray();
}
catch(JException $e) {
$modules = array();
}
// Do we have any module copies?
if (count($modules))
{
// Ensure the list is sane
JArrayHelper::toInteger($modules);
$modID = implode(',', $modules);
// Wipe out any items assigned to menus
$query = 'DELETE' .
' FROM #__modules_menu' .
' WHERE moduleid IN ('.$modID.')';
$db->setQuery($query);
try {
$db->query();
}
catch(JException $e)
{
JError::raiseWarning(100, JText::_('Module').' '.JText::_('Uninstall').': '.$db->stderr(true));
$retval = false;
}
// Wipe out any instances in the modules table
$query = 'DELETE' .
' FROM #__modules' .
' WHERE id IN ('.$modID.')';
$db->setQuery($query);
try {
$db->query();
}
catch (JException $e)
{
JError::raiseWarning(100, JText::_('Module').' '.JText::_('Uninstall').': '.$db->stderr(true));
$retval = false;
}
}
// Now we will no longer need the module object, so lets delete it and free up memory
$row->delete($row->extension_id);
$query = 'DELETE FROM `#__modules` WHERE module = '.$db->Quote($row->element) . ' AND client_id = ' . $row->client_id;
$db->setQuery($query);
try {
$db->Query(); // clean up any other ones that might exist as well
}
catch(JException $e) {
//Ignore the error...
}
unset ($row);
// Remove the installation folder
if (!JFolder::delete($this->parent->getPath('extension_root')))
{
// JFolder should raise an error
$retval = false;
}
return $retval;
}
<! removed transcluded page call, red link never existed >
Examples[edit]
Code Examples[edit]