Actions

API16

JInstallerComponent/update

From Joomla! Documentation

< API16:JInstallerComponent
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 update method for components

[<! removed edit link to red link >]

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

Syntax

update()


Returns

boolean True on success

Defined in

libraries/joomla/installer/adapters/component.php

Importing

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

Source Body

function update()
{
        // Get a database connector object
        $db = &$this->parent->getDbo();
 
        // set the overwrite setting
        $this->parent->setOverwrite(true);
 
        // Get the extension manifest object
        $this->manifest = $this->parent->getManifest();
 
        // Set the extensions name
        $name = strtolower(JFilterInput::getInstance()->clean((string)$this->manifest->name, 'cmd'));
        $check=substr($name, 0, 4);
        if ($check="com_") {
        $name = substr($name, 4); }
        $element = strtolower('com_'.$name);
        $this->set('name', $name);
        $this->set('element', $element);
 
        // Get the component description
        $description = (string)$this->manifest->description;
        if ($description) {
                $this->parent->set('message', JText::_($description));
        }
        else {
                $this->parent->set('message', '');
        }
 
        // Set the installation target paths
        $this->parent->setPath('extension_site', JPath::clean(JPATH_SITE.DS."components".DS.$this->get('element')));
        $this->parent->setPath('extension_administrator', JPath::clean(JPATH_ADMINISTRATOR.DS."components".DS.$this->get('element')));
        $this->parent->setPath('extension_root', $this->parent->getPath('extension_administrator')); // copy this as its used as a common base
 
        $oldmanifest = null;
        $tmpInstaller = new JInstaller(); // create a new installer because findManifest sets stuff
        // look in the administrator first
        $tmpInstaller->setPath('source', $this->parent->getPath('extension_administrator'));
 
        if (!$tmpInstaller->findManifest())
        {
                // then the site
                $tmpInstaller->setPath('source', $this->parent->getPath('extension_site'));
                if ($tmpInstaller->findManifest()) {
                        $old_manifest = $tmpInstaller->getManifest();
                }
        }
        else
        {
                $old_manifest = $tmpInstaller->getManifest();
        }
 
        // should do this above perhaps?
        if ($old_manifest)
        {
                $this->oldAdminFiles = $old_manifest->administration->files;
                $this->oldFiles = $old_manifest->files;
        }
        else
        {
                $this->oldAdminFiles = null;
                $this->oldFiles = null;
        }
 
        // Make sure that we have an admin element
        if ( ! $this->manifest->administration)
        {
                JError::raiseWarning(1, JText::_('Component').' '.JText::_('Update').': '.JText::_('The XML file did not contain an administration element'));
                return false;
        }
 
        // If there is an manifest class file, lets load it; we'll copy it later (don't have dest yet)
        $manifestScript = (string)$this->manifest->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 = $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('update', $this);
        }
        $msg = ob_get_contents(); // create msg object; first use here
        ob_end_clean();
 
        // 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::_('Update').': '.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::_('Update').': '.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
        if ($this->manifest->files)
        {
                if ($this->parent->parseFiles($this->manifest->files, 0, $this->oldFiles) === false)
                {
                        // Install failed, rollback any changes
                        $this->parent->abort();
                        return false;
                }
        }
 
        if ($this->manifest->administration->files)
        {
                if ($this->parent->parseFiles($this->manifest->administration->files, 1, $this->oldAdminFiles) === false)
                {
                        // Install failed, rollback any changes
                        $this->parent->abort();
                        return false;
                }
        }
 
        // Parse optional tags
        $this->parent->parseMedia($this->manifest->media);
        $this->parent->parseLanguages($this->manifest->languages);
        $this->parent->parseLanguages($this->manifest->administration->languages, 1);
 
        // Deprecated install, remove after 1.6
        // If there is an install file, lets copy it.
        $installFile = (string)$this->manifest->installfile;
        if ($installFile)
        {
                // Make sure it hasn't already been copied (this would be an error in the xml install file)
                if (!file_exists($this->parent->getPath('extension_administrator').DS.$installFile) || $this->parent->getOverwrite())
                {
                        $path['src']    = $this->parent->getPath('source').DS.$installFile;
                        $path['dest']   = $this->parent->getPath('extension_administrator').DS.$installFile;
                        if (!$this->parent->copyFiles(array ($path)))
                        {
                                // Install failed, rollback changes
                                $this->parent->abort(JText::_('Component').' '.JText::_('Update').': '.JText::_('Could_not_copy_PHP_install_file'));
                                return false;
                        }
                }
                $this->set('install_script', $installFile);
        }
 
        // Deprecated uninstall, remove after 1.6
        // If there is an uninstall file, lets copy it.
        $uninstallFile = (string)$this->manifest->uninstallfile;
        if ($uninstallFile)
        {
                // Make sure it hasn't already been copied (this would be an error in the xml install file)
                if (!file_exists($this->parent->getPath('extension_administrator').DS.$uninstallFile) || $this->parent->getOverwrite())
                {
                        $path['src']    = $this->parent->getPath('source').DS.$uninstallFile;
                        $path['dest']   = $this->parent->getPath('extension_administrator').DS.$uninstallFile;
                        if (!$this->parent->copyFiles(array ($path)))
                        {
                                // Install failed, rollback changes
                                $this->parent->abort(JText::_('Component').' '.JText::_('Update').': '.JText::_('Could_not_copy_PHP_uninstall_file'));
                                return false;
                        }
                }
        }
 
        // If there is a manifest script, lets copy it.
        if ($this->get('manifest_script'))
        {
                $path['src'] = $this->parent->getPath('source').DS.$this->get('manifest_script');
                $path['dest'] = $this->parent->getPath('extension_administrator').DS.$this->get('manifest_script');
 
                if (!file_exists($path['dest']) || $this->parent->getOverwrite())
                {
                        if (!$this->parent->copyFiles(array ($path)))
                        {
                                // Install failed, rollback changes
                                $this->parent->abort(JText::_('Component').' '.JText::_('Update').': '.JText::_('Could not copy PHP manifest file.'));
                                return false;
                        }
                }
        }
 
        /*
         * Let's run the install queries for the component
         *      If Joomla 1.5 compatible, with discreet sql files - execute appropriate
         *      file for utf-8 support or non-utf-8 support
         */
        // second argument is the utf compatible version attribute
        $utfresult = $this->parent->parseSQLFiles($this->manifest->update->sql);
        if ($utfresult === false)
        {
                // Install failed, rollback changes
                $this->parent->abort(JText::_('Component').' '.JText::_('Update').': '.JText::_('SQLERRORORFILE')." ".$db->stderr(true));
                return false;
        }
 
        // Time to build the admin menus
        $this->_buildAdminMenus();
 
        /*
         * If we have an update script, lets include it, execute the custom
         * update method, and append the return value from the custom update
         * method to the installation message.
         */
        // Start Joomla! 1.6
        ob_start();
        ob_implicit_flush(false);
        if ($this->parent->manifestClass && method_exists($this->parent->manifestClass,'update')) {
                $this->parent->manifestClass->update($this);
        }
        $msg .= ob_get_contents(); // append messages
        ob_end_clean();
 
        // Clobber any possible pending updates
        $update = &JTable::getInstance('update');
        $uid = $update->find(Array('element'=>$this->get('element'),
                                                        'type'=>'component',
                                                        'client_id'=>'',
                                                        'folder'=>''));
        if ($uid) $update->delete($uid);
 
        // Update an entry to the extension table
        $row = & JTable::getInstance('extension');
        $eid = $row->find(Array('element'=>strtolower($this->get('element')),
                                        'type'=>'component'));
 
        if ($eid) {
                $row->load($eid);
        }
        else
        {
                // set the defaults
                $row->folder = ''; // There is no folder for components
                $row->enabled = 1;
                $row->protected = 0;
                $row->access = 1;
                $row->client_id = 0;
                $row->params = $this->parent->getParams();
        }
        $row->name = $this->get('name');
        $row->type = 'component';
        $row->element = $this->get('element');
        $row->manifest_cache = $this->parent->generateManifestCache();
 
        if (!$row->store())
        {
                // Install failed, roll back changes
                $this->parent->abort(JText::_('Component').' '.JText::_('Update').': '.$db->stderr(true));
                return false;
        }
 
 
        // We will copy the manifest file to its appropriate place.
        if (!$this->parent->copyManifest())
        {
                // Install failed, rollback changes
                $this->parent->abort(JText::_('Component').' '.JText::_('Update').': '.JText::_('COULD_NOT_COPY_SETUP_FILE'));
                return false;
        }
 
        // And now we run the postflight
        ob_start();
        ob_implicit_flush(false);
        if ($this->parent->manifestClass && method_exists($this->parent->manifestClass,'postflight')) {
                $this->parent->manifestClass->postflight('update', $this);
        }
        $msg .= ob_get_contents(); // append messages
        ob_end_clean();
        if ($msg != '') {
                $this->parent->set('extension_message', $msg);
        }
 
        return $row->extension_id;
}

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

Examples

<CodeExamplesForm />