Example #1
0
 protected static function _updateModules($updates, $removals = array(), $optional_excludes = array(), $disables = array())
 {
     I2CE::raiseError("Updating Modules");
     //make sure everything is nice and fresh
     clearstatcache();
     $mod_factory = I2CE_ModuleFactory::instance();
     $exec = array('max_execution_time' => 20 * 60, 'memory_limit' => 256 * 1048576);
     I2CE::longExecution($exec);
     if (!is_array($updates)) {
         $updates = array($updates);
     }
     if (!is_array($removals)) {
         $removals = array($removals);
     }
     $msg = "Will attempt to update:\n";
     foreach (array('Updates' => $updates, 'Removals' => $removals, 'Disables' => $disables) as $k => $v) {
         if (count($v) > 0) {
             $msg .= "\t{$k}:\n\t\t" . implode(',', $v) . "\n";
         }
     }
     I2CE::raiseError($msg);
     $storage = I2CE::getConfig();
     $tmp_storage = I2CE_MagicData::instance("temp_ModuleFactory");
     $configurator = new I2CE_Configurator($tmp_storage);
     if ($storage->setIfIsSet($sitemodule, "config/site/module")) {
         I2CE::raiseError("Site is set at " . $storage->getPath() . ' to be ' . $sitemodule);
         //make sure the site direcotry is added in to the config path.
         $data = $configurator->checkRequirements($updates, $disables, $removals, $mod_factory->getEnabled(), $sitemodule);
     } else {
         I2CE::raiseError("Site is not set at " . $storage->getPath());
         $data = $configurator->checkRequirements($updates, $disables, $removals, $mod_factory->getEnabled());
     }
     //note that checkRequirements has the result of putting _all_ valid config module metadata under /config/data of $tmp_storage
     if (isset($data['failure'])) {
         $storage->clearCache();
         I2CE::raiseError("Installation failed: " . $data['failure']);
         return false;
     }
     foreach (array_keys($data['removals']) as $shortname) {
         if (!$mod_factory->disable($shortname)) {
             $storage->clearCache();
             I2CE::raiseError("Unable to disable {$shortname}", E_USER_NOTICE);
             return false;
         }
     }
     //now we remove from the requirements list anything that is already enabled and  up-to-date
     if (count($data['moved']) > 0) {
         I2CE::raiseError("Found the following in another location.  Attempting to move:" . implode(',', array_keys($data['moved'])));
         I2CE::setupFileSearch(array(), true);
         //reset the file search and clear its cache
         I2CE::getFileSearch()->addPath('MODULES', dirname(dirname(__FILE__)), 'EVEN_HIGHER');
     }
     $skipped = array();
     $moved = array();
     foreach ($data['requirements'] as $shortname => $file) {
         if (!$mod_factory->isEnabled($shortname)) {
             continue;
         }
         if ($mod_factory->isUpToDate($shortname, $file) && $mod_factory->isUpToDateModule($shortname)) {
             //everything is in the correct place and up to date.
             $skipped[] = $shortname;
             $mod_factory->loadPaths($shortname, null, true);
             //for the loading of all categories for this module
             $storage->config->data->{$shortname}->file = $data['requirements'][$shortname];
             I2CE::raiseError("Updated {$shortname} config file to be " . $data['requirements'][$shortname]);
             unset($data['requirements'][$shortname]);
             //this module is enabled and the config is up-to-date so we dont need to do anything
             continue;
         }
         //let us see if this module has been moved
         if (!$storage->__isset("config/data/{$shortname}")) {
             continue;
         }
         if (!$tmp_storage->__isset("config/data/{$shortname}")) {
             continue;
         }
         $meta = $storage->config->data->{$shortname};
         $tmp_meta = $tmp_storage->config->data->{$shortname};
         foreach (array("hash", "last_access") as $key) {
             if (!isset($meta->{$key}) || !isset($tmp_meta->{$key}) || $tmp_meta->{$key} !== $meta->{$key}) {
                 continue 2;
             }
         }
         $class_file = null;
         $tmp_meta->setIfIsSet($class_file, "class/file");
         if ($class_file && ($class_file = I2CE_FileSearch::realPath($class_file))) {
             if (!$meta->__isset("class/hash")) {
                 continue;
             }
             if (!is_readable($class_file)) {
                 continue;
             }
             $contents = file_get_contents($class_file);
             if (!$contents) {
                 continue;
             }
             if ($meta->class->hash !== md5($contents)) {
                 continue;
             }
         }
         $mtimes = array();
         foreach (array("class/file", "file") as $f) {
             if (!isset($tmp_meta->{$f})) {
                 continue;
             }
             @($mtimes[$f] = filemtime(I2CE_FileSearch::realPath($tmp_meta->{$f})));
             if (!$mtimes[$f]) {
                 continue 2;
             }
         }
         if (false == $mod_factory->checkLocalesUpToDate($shortname, $class_file)) {
             //the locales for this module are not up to date
             continue;
         }
         //we made it here.  we can skip the update.
         I2CE::raiseError("Able to move config file for {$shortname} from:\n  " . $meta->file . "\nto:\n  " . $tmp_meta->file);
         foreach (array("class/file" => "class/last_access", "file" => "last_access") as $f => $a) {
             $val = null;
             $tmp_meta->setIfIsSet($val, $f);
             if ($val === null) {
                 continue;
             }
             $meta->{$f} = $val;
             $meta->{$a} = $mtimes[$f];
             $tmp_meta->{$a} = $mtimes[$f];
         }
         unset($data['requirements'][$shortname]);
         //this module is enabled and the config is up-to-date so we dont need to do anything
         $mod_factory->loadPaths($shortname, null, true);
         //for the loading of all categories for this module
         $moved[] = $shortname;
     }
     if (count($skipped) > 0) {
         I2CE::raiseError("Skipping update on the following up-to-date modules:" . implode(',', $skipped));
     }
     if (count($moved) > 0) {
         I2CE::raiseError("Moved the following  modules:" . implode(',', $moved));
     }
     I2CE::raiseError("Attempting to update/enable the following out of date modules: " . implode(',', array_keys($data['requirements'])));
     //make sure all of our class paths for existing moduels are loaded.
     $good_modules = array_diff($mod_factory->getEnabled(), $data['removals'], array_keys($data['requirements']));
     I2CE::raiseError("The following modules class paths are being added:\n\t" . implode(',', $good_modules));
     $mod_factory->loadPaths($good_modules, 'CLASSES', true);
     if (!array_key_exists('optional', $data) || !is_array($data['optional'])) {
         $data['optional'] = array();
     }
     if (is_string($optional_excludes)) {
         $optional_excludes = array($optional_excludes);
     }
     if (!is_array($optional_excludes)) {
         $optional_excludes = array();
     }
     $to_enable = array_merge($data['requirements'], $data['optional']);
     //while (count ($data['requirements']) > 0) {
     I2CE::raiseError("Trying to enable the following required:\n" . implode(" ", array_keys($data['requirements'])));
     I2CE::raiseError("Trying to enable the following optional:\n" . implode(" ", array_keys($data['optional'])));
     I2CE::raiseError("Trying to enable the following:\n" . implode(" ", array_keys($to_enable)));
     while (count($to_enable) > 0) {
         $shortname = key($to_enable);
         // reset ($data['requirements']);
         // $shortname = key($data['requirements']);
         if (!is_string($shortname) || strlen($shortname) == 0) {
             I2CE::raiseError("Invalid Shortname");
             continue;
         }
         $file = array_shift($to_enable);
         if (array_key_exists($shortname, $data['optional']) && in_array($shortname, $optional_excludes)) {
             continue;
         }
         $old_vers = '0';
         $storage->setIfIsSet($old_vers, "/config/data/{$shortname}/version");
         $new_vers = null;
         $tmp_storage->setIfIsSet($new_vers, "/config/data/{$shortname}/version");
         $mod_config = $tmp_storage->config->data->{$shortname};
         $storage->__unset("/config/data/{$shortname}");
         //set the module's metadata to the new stuff.
         $storage->config->data->{$shortname} = $mod_config;
         //keep the old version set around until we know that the module was upgraded
         $storage->config->data->{$shortname}->version = $old_vers;
         if (!$tmp_storage->__isset("/config/data/{$shortname}/class/name")) {
             //there is no class associated in the new version of  this module.
             if ($storage->__isset("/config/data/{$shortname}/class/name")) {
                 //there was a class previously assoicated to this module -- remove its hooks/fuzzy methods,
                 $mod_factory->removeHooks($shortname);
                 unset($storage->config->data->{$shortname}->class);
             }
         }
         foreach (array('conflict' => 'conflict_external', 'requirement' => 'requirement_external') as $type => $key) {
             if ($mod_config->is_parent($key)) {
                 foreach ($mod_config->{$key} as $ext => $req_data) {
                     if ($req_data instanceof I2CE_MagicDataNode) {
                         $req_data = $req_data->getAsArray();
                     } else {
                         $req_data = array();
                     }
                     foreach ($req_data as $req_d) {
                         if (!is_array($req_d) || !array_key_exists('eval', $req_d) || !$req_d['eval']) {
                             continue;
                         }
                         $eval = null;
                         @eval('$eval = ' . $req_d['eval'] . ';');
                         if (is_bool($eval) && !$eval) {
                             if (self::failedRequiredUpdate($shortname, $data, "Could not verify external {$type} {$ext} for {$shortname}", $configurator)) {
                                 return false;
                             } else {
                                 continue 4;
                             }
                         }
                     }
                 }
             }
         }
         $mod_storage = I2CE_MagicData::instance("temp_ModuleFactory_" . $shortname);
         I2CE::getFileSearch()->addPath('MODULES', dirname(dirname(__FILE__)), 'EVEN_HIGHER');
         $r_file = I2CE_FileSearch::realPath($file);
         $mod_configurator = new I2CE_Configurator($mod_storage);
         $s = $mod_configurator->processConfigFile($r_file, false, true, true, false);
         if (!is_string($s)) {
             if (self::failedRequiredUpdate($shortname, $data, "Could load configuration file", $configurator)) {
                 return false;
             } else {
                 continue;
             }
         }
         if ($s != $shortname) {
             //be super safe
             if (self::failedRequiredUpdate($shortname, $data, "Configuration shortname mismatch ({$s}/{$shortname})", $configurator)) {
                 return false;
             } else {
                 continue;
             }
         }
         self::processErasers($mod_config, $old_vers);
         $loaded = self::loadModuleMagicData($shortname, $r_file, $old_vers, $new_vers, $mod_configurator);
         if ($loaded === false) {
             if (self::failedRequiredUpdate($shortname, $data, "Could not load magic data", $configurator)) {
                 return false;
             } else {
                 continue;
             }
         }
         $loaded_mod_config = $mod_storage->config->data->{$shortname};
         self::processErasers($loaded_mod_config, $old_vers);
         if (!self::preUpgradeModule($shortname, $old_vers, $new_vers, $mod_storage)) {
             if (self::failedRequiredUpdate($shortname, $data, "Could not pre-update module", $configurator)) {
                 return false;
             } else {
                 continue;
             }
         }
         //if $loaded === true, then there was no magic data to update, so we can skip the store.
         if (is_array($loaded) && !self::storeModuleMagicData($shortname, $old_vers, $new_vers, $mod_configurator, $loaded)) {
             if (self::failedRequiredUpdate($shortname, $data, "Could not store magic data", $configurator)) {
                 return false;
             } else {
                 continue;
             }
         }
         if (!self::upgradeModule($shortname, $old_vers, $new_vers)) {
             if (self::failedRequiredUpdate($shortname, $data, "Could not upgrade module", $configurator)) {
                 return false;
             } else {
                 continue;
             }
         }
         if (!self::postUpdateModule($shortname, $old_vers, $new_vers)) {
             if (self::failedRequiredUpdate($shortname, $data, "Could not post update module", $configurator)) {
                 return false;
             } else {
                 continue;
             }
         }
         $mod_factory->setModuleHash($shortname);
         $mod_factory->setModuleClassHash($shortname, false);
         $mod_configurator->__destruct();
         $mod_configurator = null;
         $mod_storage->erase();
         $mod_storage = null;
         $storage = I2CE::getConfig();
         //just to make sure that any upgrades did not change the storage.  this happens with i2ce install for example
         $storage->config->data->{$shortname}->version = $new_vers;
         //we updated this module.  update the permanent modules config data with the temporary
         $mod_factory->loadPaths($shortname, null, true);
         //for the loading of all categories for this module
     }
     I2CE::raiseError("Enabled Modules: " . implode(',', $mod_factory->getEnabled()));
     return true;
 }