/**
  * Checks for and applies schema upgrades for the module
  *
  */
 function update()
 {
     // list files in a directory
     $files = owa_lib::listDir(OWA_DIR . 'modules' . '/' . $this->name . '/' . 'updates', false);
     //print_r($files);
     $current_schema_version = $this->c->get($this->name, 'schema_version');
     // extract sequence
     foreach ($files as $k => $v) {
         // the use of %d casts the sequence number as an int which is critical for maintaining the
         // order of the keys in the array that we are going ot create that holds the update objs
         //$n = sscanf($v['name'], '%d_%s', $seq, $classname);
         $seq = substr($v['name'], 0, -4);
         settype($seq, "integer");
         if ($seq > $current_schema_version) {
             if ($seq <= $this->required_schema_version) {
                 $this->updates[$seq] = owa_coreAPI::updateFactory($this->name, substr($v['name'], 0, -4));
                 // if the cli update mode is required and we are not running via cli then return an error.
                 owa_coreAPI::debug('cli update mode required: ' . $this->updates[$seq]->isCliModeRequired());
                 if ($this->updates[$seq]->isCliModeRequired() === true && !defined('OWA_CLI')) {
                     //set flag in module
                     $this->update_from_cli_required = true;
                     owa_coreAPI::notice("Aborting update {$seq}. This update must be applied using the command line interface.");
                     return false;
                 }
                 // set schema version from sequence number in file name. This ensures that only one update
                 // class can ever be in use for a particular schema version
                 $this->updates[$seq]->schema_version = $seq;
             }
         }
     }
     // sort the array
     ksort($this->updates, SORT_NUMERIC);
     //print_r(array_keys($this->updates));
     foreach ($this->updates as $k => $obj) {
         $this->e->notice(sprintf("Applying Update %d (%s)", $k, get_class($obj)));
         $ret = $obj->apply();
         if ($ret == true) {
             $this->e->notice("Update Suceeded");
         } else {
             $this->e->notice("Update Failed");
             return false;
         }
     }
     return true;
 }
Example #2
0
 /**
  * Lists all files in a Directory
  *
  */
 public static function listDir($start_dir = '.', $recursive = true)
 {
     $files = array();
     if (is_dir($start_dir)) {
         $fh = opendir($start_dir);
         while (($file = readdir($fh)) !== false) {
             // loop through the files, skipping . and .., and recursing if necessary
             if (strcmp($file, '.') == 0 || strcmp($file, '..') == 0) {
                 continue;
             }
             $filepath = $start_dir . '/' . $file;
             if (is_dir($filepath)) {
                 if ($recursive === true) {
                     $files = array_merge($files, owa_lib::listDir($filepath));
                 }
             } else {
                 array_push($files, array('name' => $file, 'path' => $filepath));
             }
         }
         closedir($fh);
     } else {
         // false if the function was called with an invalid non-directory argument
         $files = false;
     }
     return $files;
 }