API15

JInstallerComponent/uninstall

From Joomla! Documentation

< API15:JInstallerComponent
Revision as of 17:16, 22 March 2010 by Doxiki (talk | contribs) (New page: ===Description=== Custom uninstall method for components <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>[[Description:JInstallerComponent/uninstall|Edit Descripto...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The "API15" 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 for components

[Edit Descripton]

Template:Description:JInstallerComponent/uninstall

Syntax[edit]

uninstall($id, $clientId)
Parameter Name Default Value Description
$id $cid The id of the component to uninstall
$clientId $clientId The id of the client (unused)

Returns[edit]

mixed Return value for uninstall method in component uninstall file

Defined in[edit]

libraries/joomla/installer/adapters/component.php

Importing[edit]

jimport( 'joomla.installer.adapters.component' );

Source Body[edit]

function uninstall($id, $clientId)
{
        // Initialize variables
        $db =& $this->parent->getDBO();
        $row    = null;
        $retval = true;

        // First order of business will be to load the component object table from the database.
        // This should give us the necessary information to proceed.
        $row = & JTable::getInstance('component');
        if ( !$row->load((int) $id) || !trim($row->option) ) {
                JError::raiseWarning(100, JText::_('ERRORUNKOWNEXTENSION'));
                return false;
        }

        // Is the component we are trying to uninstall a core one?
        // Because that is not a good idea...
        if ($row->iscore) {
                JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::sprintf('WARNCORECOMPONENT', $row->name)."<br />".JText::_('WARNCORECOMPONENT2'));
                return false;
        }

        // Get the admin and site paths for the component
        $this->parent->setPath('extension_administrator', JPath::clean(JPATH_ADMINISTRATOR.DS.'components'.DS.$row->option));
        $this->parent->setPath('extension_site', JPath::clean(JPATH_SITE.DS.'components'.DS.$row->option));

        // Find and load the XML install file for the component
        $this->parent->setPath('source', $this->parent->getPath('extension_administrator'));

        // Get the package manifest objecct
        $manifest =& $this->parent->getManifest();
        if (!is_a($manifest, 'JSimpleXML')) {
                // Make sure we delete the folders if no manifest exists
                JFolder::delete($this->parent->getPath('extension_administrator'));
                JFolder::delete($this->parent->getPath('extension_site'));

                // Remove the menu
                $this->_removeAdminMenus($row);

                // Raise a warning
                JError::raiseWarning(100, JText::_('ERRORREMOVEMANUALLY'));

                // Return
                return false;
        }

        // Get the root node of the manifest document
        $this->manifest =& $manifest->document;

        // Now lets load the uninstall file if there is one and execute the uninstall function if it exists.
        $uninstallfileElement =& $this->manifest->getElementByPath('uninstallfile');
        if (is_a($uninstallfileElement, 'JSimpleXMLElement')) {
                // Element exists, does the file exist?
                if (is_file($this->parent->getPath('extension_administrator').DS.$uninstallfileElement->data())) {
                        ob_start();
                        ob_implicit_flush(false);
                        require_once ($this->parent->getPath('extension_administrator').DS.$uninstallfileElement->data());
                        if (function_exists('com_uninstall')) {
                                if (com_uninstall() === false) {
                                        JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::_('Custom Uninstall script unsuccessful'));
                                        $retval = false;
                                }
                        }
                        $msg = ob_get_contents();
                        ob_end_clean();
                        if ($msg != '') {
                                $this->parent->set('extension.message', $msg);
                        }
                }
        }

        /*
         * Let's run the uninstall queries for the component
         *      If backward compatibility is required - run queries in xml file
         *      If Joomla 1.5 compatible, with discreet sql files - execute appropriate
         *      file for utf-8 support or non-utf support
         */
        $result = $this->parent->parseQueries($this->manifest->getElementByPath('uninstall/queries'));
        if ($result === false) {
                // Install failed, rollback changes
                JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::_('SQL Error')." ".$db->stderr(true));
                $retval = false;
        } elseif ($result === 0) {
                // no backward compatibility queries found - try for Joomla 1.5 type queries
                // second argument is the utf compatible version attribute
                $utfresult = $this->parent->parseSQLFiles($this->manifest->getElementByPath('uninstall/sql'));
                if ($utfresult === false) {
                        // Install failed, rollback changes
                        JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::_('SQLERRORORFILE')." ".$db->stderr(true));
                        $retval = false;
                }
        }

        $this->_removeAdminMenus($row);

        // Let's remove language files and media in the JROOT/images/ folder that are
        // associated with the component we are uninstalling
        $this->parent->removeFiles($this->manifest->getElementByPath('media'));
        $this->parent->removeFiles($this->manifest->getElementByPath('languages'));
        $this->parent->removeFiles($this->manifest->getElementByPath('administration/languages'), 1);

        // Now we need to delete the installation directories.  This is the final step in uninstalling the component.
        if (trim($row->option)) {
                // Delete the component site directory
                if (is_dir($this->parent->getPath('extension_site'))) {
                        if (!JFolder::delete($this->parent->getPath('extension_site'))) {
                                JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::_('Unable to remove the component site directory'));
                                $retval = false;
                        }
                }

                // Delete the component admin directory
                if (is_dir($this->parent->getPath('extension_administrator'))) {
                        if (!JFolder::delete($this->parent->getPath('extension_administrator'))) {
                                JError::raiseWarning(100, JText::_('Component').' '.JText::_('Uninstall').': '.JText::_('Unable to remove the component admin directory'));
                                $retval = false;
                        }
                }
                return $retval;
        } else {
                // No component option defined... cannot delete what we don't know about
                JError::raiseWarning(100, 'Component Uninstall: Option field empty, cannot remove files');
                return false;
        }
}

[Edit See Also] Template:SeeAlso:JInstallerComponent/uninstall

Examples[edit]

<CodeExamplesForm />