Archived

Difference between revisions of "Managing Component Updates (Script.php)"

From Joomla! Documentation

Line 81: Line 81:
 
The postflight method is a good place to set the default values of component parameters during installs, and to set the values of hidden component parameters during installs and updates.  Postflight can be used for any other action that must follow the install, update or discover_install actions.
 
The postflight method is a good place to set the default values of component parameters during installs, and to set the values of hidden component parameters during installs and updates.  Postflight can be used for any other action that must follow the install, update or discover_install actions.
  
This example defines component parameters in postflight.  The value of ''my_param0'' is displayed in the component front-end.  Both ''my_param0'' and ''my_param1'' are overwritten in updates. This is how you would change the component revision number during an update. The example ''myparam2'' is how you would treat parameters that you expect users to define, and you do not want to overwrite during an update. Parameters that define component operation would be treated this way.
+
This example defines component parameters in postflight.  The value of ''my_param0'' is displayed in the component front-end.  Both ''my_param0'' and ''my_param1'' are overwritten in updates. This is how you would change the component revision number during an update. The examples ''myparam2'' and ''myparam3'' show how you would treat parameters that you expect users to set, and you do not want to overwrite during an update. Parameters that define component operation would be treated this way.
 
<source lang="php">
 
<source lang="php">
 
function postflight( $type, $parent ) {
 
function postflight( $type, $parent ) {

Revision as of 21:24, 1 February 2012

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Copyedit.png
This Page Needs Your Help

This page is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.


This tutorial is for Joomla Joomla 1.6 Joomla 1.7

Articles in this series[edit]

Script.php[edit]

This file contains the component-specific methods preflight, install, update, postflight and uninstall. This file is where the component developer implements all of the unique code for these actions. For this example, in addition to various checks, HTML is inserted into the back-end output to show which code has been executed. You may choose to display different HTML, or to display none for your component.

preflight[edit]

This is where most of the checking should be done before install, update or discover_install. Preflight is executed prior to any Joomla install, update or discover_install actions. Preflight is not executed on uninstall. A string denoting the type of action (install, update or discover_install) is passed to preflight in the $type operand. Your code can use this string to execute different checks and responses for the three cases.

It is appropriate to check the Joomla version and the existing component version in the preflight module. A failure of either of these two should cause an abort of the action. An abort is triggered by returning a false value from this preflight method. On receiving a false return value, Joomla will reverse any actions already completed.

The PHP function version_compare is useful to compare version strings. Joomla also uses version_compare to define the sequence of SQL file execution, when an update is being executed that spans multiple versions.

In this example, the preflight method checks that Joomla version 1.6 or greater is in use. If an earlier Joomla version is in use, the install or update will abort. Since this script.php file only works in Joomla version 1.6 or greater, this check is currently not very useful. As subsequent Joomla versions become available, this check will be useful. Try changing the version string in the script.php file to a value greater than the Joomla version that you are using. Installs and updates should fail. This is what the version democompupdate_10j3.zip demonstrates.

In addition to failing, it is important to give the web site administrator useful information as to why the component installation failed. See the example below.

function preflight( $type, $parent ) {
	$jversion = new JVersion();

        // Extract the version number from the manifest file
        $this->release = $parent->get( "manifest" )->version;

        // Find mimimum required joomla version from the manifest file
        $this->minimum_joomla_release = $parent->get( "manifest" )->attributes()->version;        

	if( version_compare( $jversion->getShortVersion(), $this->minimum_joomla_release, 'lt' ) ) {
		Jerror::raiseWarning(null, 'Cannot install com_democompupdate in a Joomla release prior to '. $this->minimum_joomla_release);
		return false;
	}

	// abort if the component being installed is not newer than the currently installed version
	if ( $type == 'update' ) {
		$oldRelease = $this->getParam('version');
		$rel = $oldRelease . ' to ' . $this->release;
		if ( version_compare( $this->release, $oldRelease, 'le' ) ) {
			Jerror::raiseWarning(null, 'Incorrect version sequence. Cannot upgrade ' . $rel);
			return false;
		}
	}
	else { $rel = $this->release; }
		
	echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_PREFLIGHT_' . $type . ' ' . $rel) . '</p>';
}

install[edit]

Install is executed after the Joomla install database scripts have completed. Returning 'false' will abort the install and undo any changes already made. It is cleaner to abort the install during preflight, if possible. Since fewer install actions have occurred at preflight, there is less risk that that their reversal may be done incorrectly.

function install( $parent ) {
	echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_INSTALL to ' . $this->release) . '</p>';
	// Following an install, the backend can jump directly to the newly installed component configuration page
	$parent->getParent()->setRedirectURL('index.php?option=com_democompupdate');
}

update[edit]

Update is executed after the Joomla update database scripts have completed. Returning 'false' will abort the update and undo any changes already made. It is cleaner to abort the update during preflight, if possible. Since fewer update actions have occurred at preflight, there is less risk that that their reversal may be done incorrectly.

function update( $parent ) {
	echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_UPDATE_ to ' . $this->release) . '</p>';
	// Following an update, the backend can jump directly to the newly installed component configuration page
	$parent->getParent()->setRedirectURL('index.php?option=com_democompupdate');
}

postflight[edit]

Postflight is executed after the Joomla install, update or discover_update actions have completed. It is not executed after uninstall. Postflight is executed after the extension is registered in the database. The type of action (install, update or discover_install) is passed to postflight in the $type operand. Postflight cannot cause an abort of the Joomla install, update or discover_install action.

The postflight method is a good place to set the default values of component parameters during installs, and to set the values of hidden component parameters during installs and updates. Postflight can be used for any other action that must follow the install, update or discover_install actions.

This example defines component parameters in postflight. The value of my_param0 is displayed in the component front-end. Both my_param0 and my_param1 are overwritten in updates. This is how you would change the component revision number during an update. The examples myparam2 and myparam3 show how you would treat parameters that you expect users to set, and you do not want to overwrite during an update. Parameters that define component operation would be treated this way.

function postflight( $type, $parent ) {
	// always create or modify these parameters
	$params['my_param0'] = 'Component version ' . $this->release;
	$params['my_param1'] = 'Another value';

	// define the following parameters only if it is an original install
	if ( $type == 'install' ) {
		$params['my_param2'] = '4';
		$params['my_param3'] = 'Star';
	}

	$this->setParams( $params );

	echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_POSTFLIGHT ' . $type . ' to ' . $this->release) . '</p>';
	}

uninstall[edit]

The uninstall method is executed before any Joomla uninstall action, such as file removal or database changes. Uninstall cannot cause an abort of the Joomla uninstall action, so returning false would be a waste of time.

function uninstall( $parent ) {
	echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_UNINSTALL ' . $this->release) . '</p>';
}

The entire script.php file[edit]

This is the entire script.php file for version 1.0. Note the definition of the variable $release.

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

//the name of the class must be the name of your component + InstallerScript
//for example: com_contentInstallerScript for com_content.
class com_democompupdateInstallerScript
{
	/*
	 * The release value to be displayed and checked against throughout this file.
	 */
	private $release = '1.0';

        /*
         * Find mimimum required joomla version for this extension. It will be read from the version attribute (install tag) in the manifest file
         */
        private $minimum_joomla_release = '1.6.0';    

	/*
	 * $parent is the class calling this method.
	 * $type is the type of change (install, update or discover_install, not uninstall).
	 * preflight runs before anything else and while the extracted files are in the uploaded temp folder.
	 * If preflight returns false, Joomla will abort the update and undo everything already done.
	 */
	function preflight( $type, $parent ) {
		// this component does not work with Joomla releases prior to 1.6
		// abort if the current Joomla release is older
		$jversion = new JVersion();

                // Extract the version number from the manifest. This will overwrite the 1.0 value set above 
                $this->release=$parent->get("manifest")->version;
                
                // Find mimimum required joomla version
                $this->minimum_joomla_release=$parent->get("manifest")->attributes()->version;    

		if( version_compare( $jversion->getShortVersion(), $this->minimum_joomla_release, 'lt' ) ) {
			Jerror::raiseWarning(null, 'Cannot install com_democompupdate in a Joomla release prior to '.$this->minimum_joomla_release);
			return false;
		}

		// abort if the component being installed is not newer than the currently installed version
		if ( $type == 'update' ) {
			$oldRelease = $this->getParam('version');
			$rel = $oldRelease . ' to ' . $this->release;
			if ( version_compare( $this->release, $oldRelease, 'le' ) ) {
				Jerror::raiseWarning(null, 'Incorrect version sequence. Cannot upgrade ' . $rel);
				return false;
			}
		}
		else { $rel = $this->release; }
		
		echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_PREFLIGHT_' . $type . ' ' . $rel) . '</p>';
	}

	/*
	 * $parent is the class calling this method.
	 * install runs after the database scripts are executed.
	 * If the extension is new, the install method is run.
	 * If install returns false, Joomla will abort the install and undo everything already done.
	 */
	function install( $parent ) {
		echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_INSTALL to ' . $this->release) . '</p>';
		// You can have the backend jump directly to the newly installed component configuration page
		// $parent->getParent()->setRedirectURL('index.php?option=com_democompupdate');
	}

	/*
	 * $parent is the class calling this method.
	 * update runs after the database scripts are executed.
	 * If the extension exists, then the update method is run.
	 * If this returns false, Joomla will abort the update and undo everything already done.
	 */
	function update( $parent ) {
		echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_UPDATE_ to ' . $this->release) . '</p>';
	}

	/*
	 * $parent is the class calling this method.
	 * $type is the type of change (install, update or discover_install, not uninstall).
	 * postflight is run after the extension is registered in the database.
	 */
	function postflight( $type, $parent ) {
		// always create or modify these parameters
		$params['my_param0'] = 'Component version ' . $this->release;
		$params['my_param1'] = 'Another value';
 
		// define the following parameters only if it is an original install
		if ( $type == 'install' ) {
			$params['my_param2'] = '4';
			$params['my_param3'] = 'Star';
		}
 
		$this->setParams( $params );
 
		echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_POSTFLIGHT ' . $type . ' to ' . $this->release) . '</p>';
	}

	/*
	 * $parent is the class calling this method
	 * uninstall runs before any other action is taken (file removal or database processing).
	 */
	function uninstall( $parent ) {
		echo '<p>' . JText::_('COM_DEMOCOMPUPDATE_UNINSTALL ' . $this->release) . '</p>';
	}

	/*
	 * get a variable from the manifest file (actually, from the manifest cache).
	 */
	function getParam( $name ) {
		$db = JFactory::getDbo();
		$db->setQuery('SELECT manifest_cache FROM #__extensions WHERE name = "com_democompupdate"');
		$manifest = json_decode( $db->loadResult(), true );
		return $manifest[ $name ];
	}

	/*
	 * sets parameter values in the component's row of the extension table
	 */
	function setParams($param_array) {
		if ( count($param_array) > 0 ) {
			// read the existing component value(s)
			$db = JFactory::getDbo();
			$db->setQuery('SELECT params FROM #__extensions WHERE name = "com_democompupdate"');
			$params = json_decode( $db->loadResult(), true );
			// add the new variable(s) to the existing one(s)
			foreach ( $param_array as $name => $value ) {
				$params[ (string) $name ] = (string) $value;
			}
			// store the combined new and existing values back as a JSON string
			$paramsString = json_encode( $params );
			$db->setQuery('UPDATE #__extensions SET params = ' .
				$db->quote( $paramsString ) .
				' WHERE name = "com_democompupdate"' );
				$db->query();
		}
	}
}

Contributors[edit]