API15

JInstallerComponent/install

From Joomla! Documentation

< API15:JInstallerComponent
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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 install method for components

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax[edit]

install()


Returns[edit]

boolean True on success

Defined in[edit]

libraries/joomla/installer/adapters/component.php

Importing[edit]

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

Source Body[edit]

function install()
{
        // Get a database connector object
        $db =& $this->parent->getDBO();

        // Get the extension manifest object
        $manifest =& $this->parent->getManifest();
        $this->manifest =& $manifest->document;

        // Set the extensions name
        $name =& $this->manifest->getElementByPath('name');
        $name = JFilterInput::clean($name->data(), 'cmd');
        $this->set('name', $name);

        // Get the component description
        $description = & $this->manifest->getElementByPath('description');
        if (is_a($description, 'JSimpleXMLElement')) {
                $this->parent->set('message', $description->data());
        } else {
                $this->parent->set('message', '' );
        }

        // Get some important manifest elements
        $this->adminElement             =& $this->manifest->getElementByPath('administration');
        $this->installElement   =& $this->manifest->getElementByPath('install');
        $this->uninstallElement =& $this->manifest->getElementByPath('uninstall');

        // Set the installation target paths
        $this->parent->setPath('extension_site', JPath::clean(JPATH_SITE.DS."components".DS.strtolower("com_".str_replace(" ", "", $this->get('name')))));
        $this->parent->setPath('extension_administrator', JPath::clean(JPATH_ADMINISTRATOR.DS."components".DS.strtolower("com_".str_replace(" ", "", $this->get('name')))));

        // Make sure that we have an admin element
        if ( ! is_a($this->adminElement, 'JSimpleXMLElement') )
        {
                JError::raiseWarning(1, JText::_('Component').' '.JText::_('Install').': '.JText::_('The XML file did not contain an administration element'));
                return false;
        }

        /*
         * If the component site or admin directory already exists, then we will assume that the component is already
         * installed or another component is using that directory.
         */
        $exists = false;
        if ( file_exists($this->parent->getPath('extension_site')) && !$this->parent->getOverwrite()) {
                $exists = true;
                JError::raiseWarning(1, JText::_('Component').' '.JText::_('Install').': '.JText::_('Another component is already using directory').': "'.$this->parent->getPath('extension_site').'"');
        }
        if ( file_exists($this->parent->getPath('extension_administrator')) && !$this->parent->getOverwrite()) {
                $exists = true;
                JError::raiseWarning(1, JText::_('Component').' '.JText::_('Install').': '.JText::_('Another component is already using directory').': "'.$this->parent->getPath('extension_administrator').'"');
        }
        if ( $exists )
        {
                return false;
        }

        // If the component directory does not exist, lets create it
        $created = false;
        if (!file_exists($this->parent->getPath('extension_site'))) {
                if (!$created = JFolder::create($this->parent->getPath('extension_site'))) {
                        JError::raiseWarning(1, JText::_('Component').' '.JText::_('Install').': '.JText::_('Failed to create directory').': "'.$this->parent->getPath('extension_site').'"');
                        return false;
                }
        }

        /*
         * Since we created the component directory and will want to remove it if we have to roll back
         * the installation, lets add it to the installation step stack
         */
        if ($created) {
                $this->parent->pushStep(array ('type' => 'folder', 'path' => $this->parent->getPath('extension_site')));
        }

        // If the component admin directory does not exist, lets create it
        $created = false;
        if (!file_exists($this->parent->getPath('extension_administrator'))) {
                if (!$created = JFolder::create($this->parent->getPath('extension_administrator'))) {
                        JError::raiseWarning(1, JText::_('Component').' '.JText::_('Install').': '.JText::_('Failed to create directory').': "'.$this->parent->getPath('extension_administrator').'"');
                        // Install failed, rollback any changes
                        $this->parent->abort();
                        return false;
                }
        }

        /*
         * Since we created the component admin directory and we will want to remove it if we have to roll
         * back the installation, lets add it to the installation step stack
         */
        if ($created) {
                $this->parent->pushStep(array ('type' => 'folder', 'path' => $this->parent->getPath('extension_administrator')));
        }

        // Find files to copy
        foreach ($this->manifest->children() as $child)
        {
                if (is_a($child, 'JSimpleXMLElement') && $child->name() == 'files') {
                        if ($this->parent->parseFiles($child) === false) {
                                // Install failed, rollback any changes
                                $this->parent->abort();
                                return false;
                        }
                }
        }

        foreach ($this->adminElement->children() as $child)
        {
                if (is_a($child, 'JSimpleXMLElement') && $child->name() == 'files') {
                        if ($this->parent->parseFiles($child, 1) === false) {
                                // Install failed, rollback any changes
                                $this->parent->abort();
                                return false;
                        }
                }
        }

        // Parse optional tags
        $this->parent->parseMedia($this->manifest->getElementByPath('media'));
        $this->parent->parseLanguages($this->manifest->getElementByPath('languages'));
        $this->parent->parseLanguages($this->manifest->getElementByPath('administration/languages'), 1);

        // Parse deprecated tags
        $this->parent->parseFiles($this->manifest->getElementByPath('images'));
        $this->parent->parseFiles($this->manifest->getElementByPath('administration/images'), 1);

        // If there is an install file, lets copy it.
        $installScriptElement =& $this->manifest->getElementByPath('installfile');
        if (is_a($installScriptElement, 'JSimpleXMLElement')) {
                // check if it actually has a value
                $installScriptFilename = $installScriptElement->data();
                if(empty($installScriptFilename)) {
                        if(JDEBUG) JError::raiseWarning(43, JText::sprintf('BLANKSCRIPTELEMENT', JText::_('install')));
                } else {
                        // Make sure it hasn't already been copied (this would be an error in the xml install file)
                        // Only copy over an existing file when upgrading components
                        if (!file_exists($this->parent->getPath('extension_administrator').DS.$installScriptFilename) || $this->parent->getOverwrite())
                        {
                                $path['src']    = $this->parent->getPath('source').DS.$installScriptFilename;
                                $path['dest']   = $this->parent->getPath('extension_administrator').DS.$installScriptFilename;
                                if(file_exists($path['src']) && file_exists(dirname($path['dest']))) {
                                        if (!$this->parent->copyFiles(array ($path))) {
                                                // Install failed, rollback changes
                                                $this->parent->abort(JText::_('Component').' '.JText::_('Install').': '.JText::_('Could not copy PHP install file.'));
                                                return false;
                                        }
                                } else if(JDEBUG) {
                                        JError::raiseWarning(42, JText::sprintf('INVALIDINSTALLFILE', JText::_('install')));
                                }
                        }
                        $this->set('install.script', $installScriptFilename);
                }
        }

        // If there is an uninstall file, lets copy it.
        $uninstallScriptElement =& $this->manifest->getElementByPath('uninstallfile');
        if (is_a($uninstallScriptElement, 'JSimpleXMLElement')) {
                // check it actually has a value
                $uninstallScriptFilename = $uninstallScriptElement->data();
                if(empty($uninstallScriptFilename)) {
                        // display a warning when we're in debug mode
                        if(JDEBUG) JError::raiseWarning(43, JText::sprintf('BLANKSCRIPTELEMENT', JText::_('uninstall')));
                } else {
                        // Make sure it hasn't already been copied (this would be an error in the xml install file)
                        // Only copy over an existing file when upgrading components
                        if (!file_exists($this->parent->getPath('extension_administrator').DS.$uninstallScriptFilename) || $this->parent->getOverwrite())
                        {
                                $path['src']    = $this->parent->getPath('source').DS.$uninstallScriptFilename;
                                $path['dest']   = $this->parent->getPath('extension_administrator').DS.$uninstallScriptFilename;
                                if(file_exists($path['src']) && file_exists(dirname($path['dest']))) {
                                        if (!$this->parent->copyFiles(array ($path))) {
                                                // Install failed, rollback changes
                                                $this->parent->abort(JText::_('Component').' '.JText::_('Install').': '.JText::_('Could not copy PHP install file.'));
                                                return false;
                                        }
                                } else if(JDEBUG) {
                                        JError::raiseWarning(42, JText::sprintf('INVALIDINSTALLFILE', JText::_('uninstall')));
                                }
                        }
                }
        }

        /*
         * Let's run the install 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-8 support
         */
        $result = $this->parent->parseQueries($this->manifest->getElementByPath('install/queries'));
        if ($result === false) {
                // Install failed, rollback changes
                $this->parent->abort(JText::_('Component').' '.JText::_('Install').': '.JText::_('SQL Error')." ".$db->stderr(true));
                return 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('install/sql'));
                if ($utfresult === false) {
                        // Install failed, rollback changes
                        $this->parent->abort(JText::_('Component').' '.JText::_('Install').': '.JText::_('SQLERRORORFILE')." ".$db->stderr(true));
                        return false;
                }
        }

        // Time to build the admin menus
        $this->_buildAdminMenus();

        /*
         * If we have an install script, lets include it, execute the custom
         * install method, and append the return value from the custom install
         * method to the installation message.
         */
        if ($this->get('install.script')) {
                if (is_file($this->parent->getPath('extension_administrator').DS.$this->get('install.script'))) {
                        ob_start();
                        ob_implicit_flush(false);
                        require_once ($this->parent->getPath('extension_administrator').DS.$this->get('install.script'));
                        if (function_exists('com_install')) {
                                if (com_install() === false) {
                                        $this->parent->abort(JText::_('Component').' '.JText::_('Install').': '.JText::_('Custom install routine failure'));
                                        return false;
                                }
                        }
                        $msg = ob_get_contents();
                        ob_end_clean();
                        if ($msg != '') {
                                $this->parent->set('extension.message', $msg);
                        }
                }
        }

        // Lastly, we will copy the manifest file to its appropriate place.
        if (!$this->parent->copyManifest()) {
                // Install failed, rollback changes
                $this->parent->abort(JText::_('Component').' '.JText::_('Install').': '.JText::_('Could not copy setup file'));
                return false;
        }

        // Load component lang file
        $lang =& JFactory::getLanguage();
        $lang->load(strtolower("com_".str_replace(" ", "", $this->get('name'))));

        return true;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples[edit]

Code Examples[edit]