diff options
author | mattpiwik <matthieu.aubry@gmail.com> | 2008-11-03 19:21:10 +0300 |
---|---|---|
committer | mattpiwik <matthieu.aubry@gmail.com> | 2008-11-03 19:21:10 +0300 |
commit | 412d71d0101880f96ade1e6047a3f5f6b4136747 (patch) | |
tree | 73acb80d53f1c3391b87e1d7c06aa9463f52ef39 /core/Updater.php | |
parent | 3ba202aeb55f42500edf4b07856c47c91d83b8c3 (diff) |
- cleaning FrontController.php
- Updater now works and looks nice + added unit tests
- adding version number in META tags
- now hiding the Core plugins (CoreHome, CoreAdminHome, CoreUpdater, CorePluginsAdmin)
git-svn-id: http://dev.piwik.org/svn/trunk@668 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'core/Updater.php')
-rw-r--r-- | core/Updater.php | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/core/Updater.php b/core/Updater.php new file mode 100644 index 0000000000..0363b71038 --- /dev/null +++ b/core/Updater.php @@ -0,0 +1,167 @@ +<?php +require_once "Option.php"; + +class Piwik_Updater +{ + const INDEX_CURRENT_VERSION = 0; + const INDEX_NEW_VERSION = 1; + + public $pathUpdateFileCore; + public $pathUpdateFilePlugins; + + private $componentsToCheck = array(); + + public function __construct() + { + $this->pathUpdateFileCore = PIWIK_INCLUDE_PATH . '/core/Updates/'; + $this->pathUpdateFilePlugins = PIWIK_INCLUDE_PATH . '/plugins/%s/Updates/'; + } + + /** + * @param string $name + * @param string $version + * @return void + */ + public function addComponentToCheck($name, $version) + { + $this->componentsToCheck[$name] = $version; + } + + /** + * @param string $name + * @param string $version + * @return void + */ + public function recordComponentSuccessfullyUpdated($name, $version) + { + Piwik_UpdateOption('version_'.$name, $version); + } + + /** + * Returns a list of components (core | plugin) that need to run through the upgrade process. + * + * @return array( componentName => array( updateFile1, [...]), [...]) + */ + public function getComponentsWithUpdateFile() + { + $this->componentsWithNewVersion = $this->loadComponentsWithNewVersion(); + $this->componentsWithUpdateFile = $this->loadComponentsWithUpdateFile(); + return $this->componentsWithUpdateFile; + } + + /** + * @param string $name + * @return array of warning strings if applicable + */ + public function update($name) + { + $warningMessages = array(); + foreach($this->componentsWithUpdateFile[$name] as $file) + { + try { + require_once $file; + } catch( UpdateErrorException $e) { + throw $e; + } catch( Exception $e) { + $warningMessages[] = $e->getMessage(); + } + } + + // to debug, create core/Updates/X.php, update the core/Version.php, throw an Exception in the try, and comment the following line + $this->recordComponentSuccessfullyUpdated($name, $this->componentsWithNewVersion[$name][self::INDEX_NEW_VERSION]); + return $warningMessages; + } + + /** + * @return array array( componentName => array( file1, [...]), [...]) + */ + private function loadComponentsWithUpdateFile() + { + $componentsWithUpdateFile = array(); + foreach($this->componentsWithNewVersion as $name => $versions) + { + $currentVersion = $versions[self::INDEX_CURRENT_VERSION]; + $newVersion = $versions[self::INDEX_NEW_VERSION]; + + if($name == 'core') + { + $pathToUpdates = $this->pathUpdateFileCore . '*'; + } + else + { + $pathToUpdates = sprintf($this->pathUpdateFilePlugins, $name) . '*'; + } + + foreach( glob( $pathToUpdates ) as $file) + { + $fileVersion = basename($file, '.php'); + if(version_compare($currentVersion, $fileVersion) == -1) + { + $componentsWithUpdateFile[$name][] = $file; + } + } + + if(isset($componentsWithUpdateFile[$name])) + { + // order the update files by version asc + usort($componentsWithUpdateFile[$name], "version_compare"); + } + else + { + // there are no update file => nothing to do, update to the new version is successful + $this->recordComponentSuccessfullyUpdated($name, $newVersion); + } + } + return $componentsWithUpdateFile; + } + + /** + * @return array array( componentName => array( oldVersion, newVersion), [...]) + */ + private function loadComponentsWithNewVersion() + { + $componentsToUpdate = array(); + + // we make sure core updates are processed before any plugin updates + if(isset($this->componentsToCheck['core'])) + { + $coreVersions = $this->componentsToCheck['core']; + unset($this->componentsToCheck['core']); + $this->componentsToCheck = array_merge( array('core' => $coreVersions), $this->componentsToCheck); + } + + foreach($this->componentsToCheck as $name => $version) + { + $currentVersion = Piwik_GetOption('version_'.$name); + + if($currentVersion === false) + { + if($name === 'core') + { + $currentVersion = '0.2.9'; + } + else + { + $currentVersion = '0.01'; + } + $this->recordComponentSuccessfullyUpdated($name, $currentVersion); + } + + $versionCompare = version_compare($currentVersion, $version); + if($versionCompare == -1) + { + $componentsToUpdate[$name] = array( + self::INDEX_CURRENT_VERSION => $currentVersion, + self::INDEX_NEW_VERSION => $version + ); + } + else if($versionCompare == 1) + { + // the version in the DB is newest.. we choose to ignore (for the time being) + } + } + return $componentsToUpdate; + } +} + +class UpdateErrorException extends Exception {} |