Actions

API16

JInstallerPlugin/uninstall

From Joomla! Documentation

< API16:JInstallerPlugin
Revision as of 17:53, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Custom uninstall method

[Edit Descripton]

Description:JInstallerPlugin/uninstall

Syntax

uninstall($id)
Parameter Name Default Value Description
$id $cid The id of the plugin to uninstall

Returns

boolean True on success

Defined in

libraries/joomla/installer/adapters/plugin.php

Importing

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

Source Body

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))
        {
                JError::raiseWarning(100, JText::_('ERRORUNKOWNEXTENSION'));
                return false;
        }
 
        // Is the plugin we are trying to uninstall a core one?
        // Because that is not a good idea...
        if ($row->protected)
        {
                JError::raiseWarning(100, JText::_('Plugin').' '.JText::_('Uninstall').': '.JText::sprintf('WARNCOREPLUGIN', $row->name)."<br />".JText::_('WARNCOREPLUGIN2'));
                return false;
        }
 
        // Get the plugin folder so we can properly build the plugin path
        if (trim($row->folder) == '')
        {
                JError::raiseWarning(100, JText::_('Plugin').' '.JText::_('Uninstall').': '.JText::_('Folder field empty, cannot remove files'));
                return false;
        }
 
        // Set the plugin root path
        if (is_dir(JPATH_ROOT.DS.'plugins'.DS.$row->folder.DS.$row->element)) {
                // Use 1.6 plugins
                $this->parent->setPath('extension_root', JPATH_ROOT.DS.'plugins'.DS.$row->folder.DS.$row->element);
        }
        else {
                // Use Legacy 1.5 plugins
                $this->parent->setPath('extension_root', JPATH_ROOT.DS.'plugins'.DS.$row->folder);
        }
 
        // Because plugins don't have their own folders we cannot use the standard method of finding an installation manifest
        // Since 1.6 they do, however until we move to 1.7 and remove 1.6 legacy we still need to use this method
        // when we get there it'll be something like "$manifest = &$this->parent->getManifest();"
        $manifestFile = $this->parent->getPath('extension_root').DS.$row->element.'.xml';
 
        if ( ! file_exists($manifestFile))
        {
                JError::raiseWarning(100, 'Plugin Uninstall: Manifest File invalid or not found');
                return false;
        }
 
        $xml = JFactory::getXML($manifestFile);
 
        $this->manifest = $xml;
 
        // If we cannot load the xml file return null
        if (!$xml)
        {
                JError::raiseWarning(100, JText::_('Plugin').' '.JText::_('Uninstall').': '.JText::_('Could not load manifest file'));
                return false;
        }
 
        /*
         * Check for a valid XML root tag.
         * @todo: Remove backwards compatability in a future version
         * Should be 'extension', but for backward compatability we will accept 'install'.
         */
        if ($xml->getName() != 'install' && $xml->getName() != 'extension')
        {
                JError::raiseWarning(100, JText::_('Plugin').' '.JText::_('Uninstall').': '.JText::_('Invalid manifest file'));
                return false;
        }
 
        // If there is an manifest class file, lets load it; we'll copy it later (don't have dest yet)
        $manifestScript = (string)$xml->scriptfile;
        if ($manifestScript)
        {
                $manifestScriptFile = $this->parent->getPath('source').DS.$manifestScript;
                if (is_file($manifestScriptFile)) {
                        // load the file
                        include_once $manifestScriptFile;
                }
                // Set the class name
                $classname = 'plg'.$row->folder.$row->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
                }
        }
 
        // run preflight if possible (since we know we're not an update)
        ob_start();
        ob_implicit_flush(false);
        if ($this->parent->manifestClass && method_exists($this->parent->manifestClass,'preflight')) {
                $this->parent->manifestClass->preflight($this->route, $this);
        }
        $msg = ob_get_contents(); // create msg object; first use here
        ob_end_clean();
 
        /*
         * Let's run the queries for the module
         *      If Joomla 1.5 compatible, with discreet sql files - execute appropriate
         *      file for utf-8 support or non-utf-8 support
         */
        // try for Joomla 1.5 type queries
        // second argument is the utf compatible version attribute
        $utfresult = $this->parent->parseSQLFiles($xml->{strtolower($this->route)}->sql);
        if ($utfresult === false)
        {
                // Install failed, rollback changes
                $this->parent->abort(JText::_('Plugin').' '.JText::_('Uninstall').': '.JText::_('SQLERRORORFILE')." ".$db->stderr(true));
                return false;
        }
 
        // Start Joomla! 1.6
        ob_start();
        ob_implicit_flush(false);
        if ($this->parent->manifestClass && method_exists($this->parent->manifestClass,'uninstall')) {
                $this->parent->manifestClass->uninstall($this);
        }
        $msg = ob_get_contents(); // append messages
        ob_end_clean();
 
 
        // Remove the plugin files
        $this->parent->removeFiles($xml->images, -1);
        $this->parent->removeFiles($xml->files, -1);
        JFile::delete($manifestFile);
 
        // Remove all media and languages as well
        $this->parent->removeFiles($xml->media);
        $this->parent->removeFiles($xml->languages,1);
 
        // Now we will no longer need the plugin object, so lets delete it
        $row->delete($row->extension_id);
        unset ($row);
 
        // If the folder is empty, let's delete it
        $files = JFolder::files($this->parent->getPath('extension_root'));
        JFolder::delete($this->parent->getPath('extension_root'));
 
 
        if ($msg) {
                $this->parent->set('extension_message',$msg);
        }
 
        return $retval;
}

[Edit See Also] SeeAlso:JInstallerPlugin/uninstall

Examples

<CodeExamplesForm />