Пример #1
0
 /**
  * return the list of objects which are responsible to upgrade the component
  * from the current installed version of the component.
  *
  * this method should be called after verifying and resolving
  * dependencies. Needed components (modules or plugins) should be
  * installed/upgraded before calling this method
  *
  * @param EntryPoint $ep the entry point
  * @throw \Jelix\Installer\Exception  if an error occurs during the install.
  * @return ModuleInstaller[]
  */
 function getUpgraders(EntryPoint $ep)
 {
     $epId = $ep->getEpId();
     if ($this->moduleUpgraders === null) {
         $this->moduleUpgraders = array();
         $p = $this->moduleInfos->getPath() . 'install/';
         if (!file_exists($p) || $this->moduleStatuses[$epId]->skipInstaller) {
             return array();
         }
         // we get the list of files for the upgrade
         $fileList = array();
         if ($handle = opendir($p)) {
             while (false !== ($f = readdir($handle))) {
                 if (!is_dir($p . $f)) {
                     if (preg_match('/^upgrade_to_([^_]+)_([^\\.]+)\\.php$/', $f, $m)) {
                         $fileList[] = array($f, $m[1], $m[2]);
                     } else {
                         if (preg_match('/^upgrade_([^\\.]+)\\.php$/', $f, $m)) {
                             $fileList[] = array($f, '', $m[1]);
                         }
                     }
                 }
             }
             closedir($handle);
         }
         if (!count($fileList)) {
             return array();
         }
         // now we order the list of file
         foreach ($fileList as $fileInfo) {
             require_once $p . $fileInfo[0];
             $cname = $this->moduleInfos->name . 'ModuleUpgrader_' . $fileInfo[2];
             if (!class_exists($cname)) {
                 throw new Exception("module.upgrader.class.not.found", array($cname, $this->name));
             }
             $upgrader = new $cname($this->moduleInfos->name, $fileInfo[2], $this->moduleInfos->getPath(), $fileInfo[1], false);
             if ($fileInfo[1] && count($upgrader->targetVersions) == 0) {
                 $upgrader->targetVersions = array($fileInfo[1]);
             }
             $this->moduleUpgraders[] = $upgrader;
         }
     }
     $list = array();
     foreach ($this->moduleUpgraders as $upgrader) {
         $foundVersion = '';
         // check the version
         foreach ($upgrader->targetVersions as $version) {
             if (VersionComparator::compareVersion($this->moduleStatuses[$epId]->version, $version) >= 0) {
                 // we don't execute upgraders having a version lower than the installed version (they are old upgrader)
                 continue;
             }
             if (VersionComparator::compareVersion($this->moduleInfos->version, $version) < 0) {
                 // we don't execute upgraders having a version higher than the version indicated in the module.xml/jelix-module.json
                 continue;
             }
             $foundVersion = $version;
             // when multiple version are specified, we take the first one which is ok
             break;
         }
         if (!$foundVersion) {
             continue;
         }
         $upgrader->version = $foundVersion;
         // we have to check now the date of versions
         // we should not execute the updater in some case.
         // for example, we have an updater for the 1.2 and 2.3 version
         // we have the 1.4 installed, and want to upgrade to the 2.5 version
         // we should not execute the update for 2.3 since modifications have already been
         // made into the 1.4. The only way to now that, is to compare date of versions
         if ($upgrader->date != '' && $this->mainInstaller) {
             $upgraderDate = $this->_formatDate($upgrader->date);
             // the date of the first version installed into the application
             $firstVersionDate = $this->_formatDate($this->mainInstaller->installerIni->getValue($this->moduleInfos->name . '.firstversion.date', $epId));
             if ($firstVersionDate !== null) {
                 if ($firstVersionDate >= $upgraderDate) {
                     continue;
                 }
             }
             // the date of the current installed version
             $currentVersionDate = $this->_formatDate($this->mainInstaller->installerIni->getValue($this->moduleInfos->name . '.version.date', $epId));
             if ($currentVersionDate !== null) {
                 if ($currentVersionDate >= $upgraderDate) {
                     continue;
                 }
             }
         }
         $upgrader->setParameters($this->moduleStatuses[$epId]->parameters);
         $class = get_class($upgrader);
         if (!isset($this->upgradersContexts[$class])) {
             $this->upgradersContexts[$class] = array();
         }
         $upgrader->setEntryPoint($ep, $this->moduleStatuses[$epId]->dbProfile, $this->upgradersContexts[$class]);
         $list[] = $upgrader;
     }
     // now let's sort upgrader, to execute them in the right order (oldest before newest)
     usort($list, function ($upgA, $upgB) {
         return VersionComparator::compareVersion($upgA->version, $upgB->version);
     });
     return $list;
 }
Пример #2
0
 public function checkVersion($versionExpression)
 {
     return \Jelix\Version\VersionComparator::compareVersionRange($this->moduleInfos->version, $versionExpression);
 }
Пример #3
0
 /**
  * check dependencies of an item.
  *
  * @param Item   $item
  * @param string $epId
  */
 protected function _checkDependencies(Item $item)
 {
     if (isset($this->circularDependencyTracker[$item->getName()])) {
         throw new ItemException('Circular dependency! Cannot process the item ' . $item->getName(), $item, 1);
     }
     $this->circularDependencyTracker[$item->getName()] = true;
     $missingItems = array();
     foreach ($item->getDependencies() as $depItemName => $depItemVersion) {
         $depItem = null;
         if (isset($this->items[$depItemName])) {
             $depItem = $this->items[$depItemName];
         } else {
             $missingItems[] = $depItemName;
             continue;
         }
         if ($depItem->getAction() == self::ACTION_REMOVE) {
             throw new ItemException('Item ' . $depItemName . ', needed by item ' . $item->getName() . ', should be removed at the same time', $item, 3, $depItem);
         }
         if (isset($this->checkedItems[$depItemName])) {
             continue;
         }
         if ($depItem->getAction() == self::ACTION_NONE) {
             $version = $depItem->getCurrentVersion();
             if (!VersionComparator::compareVersionRange($version, $depItemVersion)) {
                 throw new ItemException("Version of item '" . $depItemName . "' does not match required version by item " . $item->getName(), $item, 2, $depItem);
             }
             if (!$depItem->isInstalled()) {
                 $depItem->setAction(self::ACTION_INSTALL);
                 $this->_checkDependencies($depItem);
                 $this->chain[] = $depItem;
             }
         } elseif ($depItem->getAction() == self::ACTION_INSTALL) {
             $version = $depItem->getCurrentVersion();
             if (!VersionComparator::compareVersionRange($version, $depItemVersion)) {
                 throw new ItemException("Version of item '" . $depItemName . "' does not match required version by item " . $item->getName(), $item, 2, $depItem);
             }
             $this->_checkDependencies($depItem);
             $this->chain[] = $depItem;
         } elseif ($depItem->getAction() == self::ACTION_UPGRADE) {
             $version = $depItem->getNextVersion();
             if (!VersionComparator::compareVersionRange($version, $depItemVersion)) {
                 throw new ItemException("Version of item '" . $depItemName . "' does not match required version by item " . $item->getName(), $item, 2, $depItem);
             }
             $this->_checkDependencies($depItem);
             $this->chain[] = $depItem;
         }
     }
     $this->checkedItems[$item->getName()] = true;
     unset($this->circularDependencyTracker[$item->getName()]);
     if ($missingItems) {
         throw new ItemException('For item ' . $item->getName() . ', some items are missing :' . implode(',', $missingItems), $item, 6, $missingItems);
     }
 }