📄 component.php
字号:
<?php/** * @version $Id:component.php 6961 2007-03-15 16:06:53Z tcp $ * @package Joomla.Framework * @subpackage Installer * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved. * @license GNU/GPL, see LICENSE.php * Joomla! is free software. This version may have been modified pursuant * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. * See COPYRIGHT.php for copyright notices and details. */// Check to ensure this file is within the rest of the frameworkdefined('JPATH_BASE') or die();/** * Component installer * * @package Joomla.Framework * @subpackage Installer * @since 1.5 */class JInstallerComponent extends JObject{ /** * Constructor * * @access protected * @param object $parent Parent object [JInstaller instance] * @return void * @since 1.5 */ function __construct(&$parent) { $this->parent =& $parent; } /** * Custom install method for components * * @access public * @return boolean True on success * @since 1.5 */ function install() { // Get a database connector object $db =& $this->parent->getDBO(); // Get the extension manifest object $manifest =& $this->parent->getManifest(); $this->manifest =& $manifest->document; /** * --------------------------------------------------------------------------------------------- * Manifest Document Setup Section * --------------------------------------------------------------------------------------------- */ // 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'))))); /** * --------------------------------------------------------------------------------------------- * Basic Checks Section * --------------------------------------------------------------------------------------------- */ // 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; } /** * --------------------------------------------------------------------------------------------- * Filesystem Processing Section * --------------------------------------------------------------------------------------------- */ /* * 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')) { // 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.$installScriptElement->data()) || $this->parent->getOverwrite()) { $path['src'] = $this->parent->getPath('source').DS.$installScriptElement->data(); $path['dest'] = $this->parent->getPath('extension_administrator').DS.$installScriptElement->data(); 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; } } $this->set('install.script', $installScriptElement->data()); } // If there is an uninstall file, lets copy it. $uninstallScriptElement =& $this->manifest->getElementByPath('uninstallfile'); if (is_a($uninstallScriptElement, 'JSimpleXMLElement')) { // 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.$uninstallScriptElement->data()) || $this->parent->getOverwrite()) { $path['src'] = $this->parent->getPath('source').DS.$uninstallScriptElement->data(); $path['dest'] = $this->parent->getPath('extension_administrator').DS.$uninstallScriptElement->data(); if (!$this->parent->copyFiles(array ($path))) { // Install failed, rollback changes $this->parent->abort(JText::_('Component').' '.JText::_('Install').': '.JText::_('Could not copy PHP uninstall file.')); return false; } } } /** * --------------------------------------------------------------------------------------------- * Database Processing Section * --------------------------------------------------------------------------------------------- */ /* * 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(); /** * --------------------------------------------------------------------------------------------- * Custom Installation Script Section * --------------------------------------------------------------------------------------------- */ /* * 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); } } } /** * --------------------------------------------------------------------------------------------- * Finalization and Cleanup Section * --------------------------------------------------------------------------------------------- */ // 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; } return true; } /** * Custom uninstall method for components * * @access public * @param int $cid The id of the component to uninstall * @param int $clientId The id of the client (unused) * @return mixed Return value for uninstall method in component uninstall file * @since 1.0 */ 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)); /** * --------------------------------------------------------------------------------------------- * Manifest Document Setup Section * --------------------------------------------------------------------------------------------- */ // 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; /** * ---------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -