Actions

API16

JInstallerModule/install

From Joomla! Documentation

< API16:JInstallerModule
Revision as of 22:02, 13 May 2013 by JoomlaWikiBot (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 install method

[<! removed edit link to red link >]

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

Syntax

install()


Returns

boolean True on success

Defined in

libraries/joomla/installer/adapters/module.php

Importing

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

Source Body

public function install()
{
        // if this is an update, set the route accordingly
        /*if ($this->parent->getUpgrade()) {
                $this->route = 'Update';
        }*/
 
        // Get a database connector object
        $db = &$this->parent->getDbo();
 
        // Get the extension manifest object
        $this->manifest = $this->parent->getManifest();
 
        // Set the extensions name
        $name = (string)$this->manifest->name;
        $name = JFilterInput::getInstance()->clean($name, 'string');
        $this->set('name', $name);
 
        // Get the component description
        $description = (string)$this->manifest->description;
        if ($description) {
                $this->parent->set('message', JText::_($description));
        }
        else {
                $this->parent->set('message', '');
        }
 
        // Get the target application
        if ($cname = (string)$this->manifest->attributes()->client)
        {
                // Attempt to map the client to a base path
                jimport('joomla.application.helper');
                $client = &JApplicationHelper::getClientInfo($cname, true);
                if ($client === false)
                {
                        $this->parent->abort(JText::_('Module').' '.JText::_($this->route).': '.JText::_('Unknown client type').' ['.$client->name.']');
                        return false;
                }
                $basePath = $client->path;
                $clientId = $client->id;
        }
        else
        {
                // No client attribute was found so we assume the site as the client
                $cname = 'site';
                $basePath = JPATH_SITE;
                $clientId = 0;
        }
 
        // Set the installation path
        $element = '';
        if (count($this->manifest->files->children()))
        {
                foreach ($this->manifest->files->children() as $file)
                {
                        if ((string)$file->attributes()->module)
                        {
                                $element = (string)$file->attributes()->module;
                                $this->set('element',$element);
                                break;
                        }
                }
        }
        if (!empty ($element)) {
                $this->parent->setPath('extension_root', $basePath.DS.'modules'.DS.$element);
        }
        else {
                $this->parent->abort(JText::_('Module').' '.JText::_($this->route).': '.JText::_('No module file specified'));
                return false;
        }
 
        // Check to see if a module by the same name is already installed
        // If it is, then update the table because if the files aren't there
        // we can assume that it was (badly) uninstalled
        // If it isn't, add an entry to extensions
        $query = 'SELECT `extension_id`' .
                        ' FROM `#__extensions` ' .
                        ' WHERE element = '.$db->Quote($element) .
                        ' AND client_id = '.(int)$clientId;
        $db->setQuery($query);
        try {
                $db->Query();
        }
        catch(JException $e)
        {
                // Install failed, roll back changes
                $this->parent->abort(JText::_('Module').' '.JText::_($this->route).': '.$db->stderr(true));
                return false;
        }
        $id = $db->loadResult();
 
        /*
         * If the module directory already exists, then we will assume that the
         * module is already installed or another module is using that
         * directory.
         * Check that this is either an issue where its not overwriting or it is
         * set to upgrade anyway
         */
        if (file_exists($this->parent->getPath('extension_root')) && (!$this->parent->getOverwrite() || $this->parent->getUpgrade()))
        {
                // look for an update function or update tag
                $updateElement = $this->manifest->update;
                // upgrade manually set
                // update function available
                // update tag detected
                if ($this->parent->getUpgrade() || ($this->parent->manifestClass && method_exists($this->parent->manifestClass,'update')) || is_a($updateElement, 'JXMLElement'))
                {
                        // force these one
                        $this->parent->setOverwrite(true);
                        $this->parent->setUpgrade(true);
                        if ($id) { // if there is a matching extension mark this as an update; semantics really
                                $this->route = 'Update';
                        }
                }
                else if (!$this->parent->getOverwrite())
                {
                        // overwrite is set
                        // we didn't have overwrite set, find an udpate function or find an update tag so lets call it safe
                        $this->parent->abort(JText::_('Module').' '.JText::_($this->route).': '.JText::_('Another module is already using directory').': "'.$this->parent->getPath('extension_root').'"');
                        return false;
                }
        }
 
        // If there is an manifest class file, lets load it; we'll copy it later (don't have dest yet)
        $this->scriptElement = $this->manifest->scriptfile;
        $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($this->route, $this);
        }
        $msg = ob_get_contents(); // create msg object; first use here
        ob_end_clean();
 
        // If the module directory does not exist, lets create it
        $created = false;
        if (!file_exists($this->parent->getPath('extension_root')))
        {
                if (!$created = JFolder::create($this->parent->getPath('extension_root')))
                {
                        $this->parent->abort(JText::_('Module').' '.JText::_($this->route).': '.JText::_('FAILED_TO_CREATE_DIRECTORY').': "'.$this->parent->getPath('extension_root').'"');
                        return false;
                }
        }
 
        /*
         * Since we created the module 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_root')));
        }
 
        // Copy all necessary files
        if ($this->parent->parseFiles($this->manifest->files, -1) === false)
        {
                // Install failed, roll back changes
                $this->parent->abort();
                return false;
        }
 
        // Parse optional tags
        $this->parent->parseMedia($this->manifest->media, $clientId);
        $this->parent->parseLanguages($this->manifest->languages, $clientId);
 
        // Parse deprecated tags
        $this->parent->parseFiles($this->manifest->images, -1);
 
        // Was there a module already installed with the same name?
        if ($id)
        {
                // load the entry and update the manifest_cache
                $row = &JTable::getInstance('extension');
                $row->load($id);
                $row->name = $this->get('name'); // update name
                $row->manifest_cache = $this->parent->generateManifestCache(); // update manifest
                if (!$row->store()) {
                        // Install failed, roll back changes
                        $this->parent->abort(JText::_('Module').' '.JText::_($this->route).': '.$db->stderr(true));
                        return false;
                }
        }
        else
        {
                $row = & JTable::getInstance('extension');
                $row->set('name', $this->get('name'));
                $row->set('type', 'module');
                $row->set('element', $this->get('element'));
                $row->set('folder', ''); // There is no folder for modules
                $row->set('enabled', 1);
                $row->set('protected', 0);
                $row->set('access', $clientId == 1 ? 2 : 0);
                $row->set('client_id', $clientId);
                $row->set('params', $this->parent->getParams());
                $row->set('custom_data', ''); // custom data
                $row->set('manifest_cache', $this->parent->generateManifestCache());
 
                if (!$row->store())
                {
                        // Install failed, roll back changes
                        $this->parent->abort(JText::_('Module').' '.JText::_($this->route).': '.$db->stderr(true));
                        return false;
                }
 
                // Since we have created a module item, we add it to the installation step stack
                // so that if we have to rollback the changes we can undo it.
                $this->parent->pushStep(array ('type' => 'extension', 'extension_id' => $row->extension_id));
        }
 
        /*
         * 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($this->manifest->{strtolower($this->route)}->sql);
        if ($utfresult === false)
        {
                // Install failed, rollback changes
                $this->parent->abort(JText::_('Module').' '.JText::_($this->route).': '.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,$this->route)) $this->parent->manifestClass->{$this->route}($this);
        $msg .= ob_get_contents(); // append messages
        ob_end_clean();
 
        // Lastly, we will copy the manifest file to its appropriate place.
        if (!$this->parent->copyManifest(-1))
        {
                // Install failed, rollback changes
                $this->parent->abort(JText::_('Module').' '.JText::_($this->route).': '.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($this->route, $this);
        }
        $msg .= ob_get_contents(); // append messages
        ob_end_clean();
        if ($msg != '') {
                $this->parent->set('extension_message', $msg);
        }
        return $row->get('extension_id');
}

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

Examples

<CodeExamplesForm />