/** * Update the database to the current version. */ private function __runDatabaseUpdate() { if (!$this->__createDb) { $this->__logMessage('Updating database'); $this->__pushLogIndent(); // Find out what version we're updating from $currentDbVersion = $this->__readDbVersion(); if ($currentDbVersion == null) { $this->__logMessage('Error: Could not read current database version'); return; } $this->__logMessage('DB Version: ' . $this->__versionToString($currentDbVersion)); // Find out which version we should be updating to $codeVersion = $this->__getCodeVersion(); if ($codeVersion == null) { $this->__logMessage('Error: Could not get code version'); return; } $this->__logMessage('Code Version: ' . $this->__versionToString($codeVersion)); if ($this->__compareVersions($currentDbVersion, $codeVersion) == 0) { $this->__logMessage('No update required'); $this->__popLogIndent(); return; } $this->__logMessage(sprintf('Updating from version: %s to version %s', $this->__versionToString($currentDbVersion), $this->__versionToString($codeVersion))); // Ok, lets get started. // First we find all the update files, and sort them by version $updatesPath = makeAbsolutePath('updates'); $updateFiles = array(); $files = glob($updatesPath . '/*.php'); foreach ($files as $file) { if (is_file($file)) { $fileParts = pathinfo($file); $fileVersion = $this->__stringToVersion($fileParts['filename']); if (!$this->__isValidVersion($fileVersion)) { $this->__logMessage("Warning: Found update php with a filename that is not a valid version {$file}"); continue; } $updateFiles[$this->__versionToString($fileVersion)] = $file; } } uksort($updateFiles, array($this, "__compareVersions")); // Then execute the version file for any version that's ahead of us // until we hit the code version foreach ($updateFiles as $updateVersion => $path) { $currentVersionDiff = $this->__compareVersions($updateVersion, $currentDbVersion); $codeVersionDiff = $this->__compareVersions($updateVersion, $codeVersion); if ($currentVersionDiff > 0 && $codeVersionDiff <= 0) { $this->__logMessage('Executing update ' . $path); if ($this->__executeUpdate($path)) { $this->__writeDbVersion($this->__stringToVersion($updateVersion)); $currentDbVersion = $updateVersion; $this->__logMessage('Updated to version: ' . $updateVersion); } else { $this->__logMessage('Error: Failed to execute update ' . $path); } } } // There will be some sql files that need to be ran even during an update $sqlFiles = array('mailinglists', 'mailinglist_subscriptions'); $conn = $this->__getDbConnection('default', true); foreach ($sqlFiles as $filename) { $schemaFiles = $this->__getSqlFilesContaining($filename . '_schema'); // Kill the databases that are in thiese files foreach ($schemaFiles as $file) { $tableName = $this->__getTableNameFromSchemaFile($file); if ($tableName == null) { $this->__logMessage("Error: Unable to parse table name from file: {$file}"); } $this->__logMessage("Dropping table `{$tableName}`"); $this->__runQuery($conn, "DROP TABLE `{$tableName}`"); $this->__runQueryFromFile($conn, $file); } // And add the data $dataFiles = $this->__getSqlFilesContaining($filename . '_data'); foreach ($dataFiles as $file) { $this->__runQueryFromFile($conn, $file); } } $this->__popLogIndent(); } }
/** * Parse a CSV file, adding the data to the stockData array. * * @param $filepath string Path to the .csv file to try and parse. */ private function __parseCsv($filepath) { $csvReader = new CsvReader(); if ($csvReader->readFile(makeAbsolutePath($filepath))) { $numLines = $csvReader->getNumLines(); // If the .csv is sane, the first line is the headers $headers = $csvReader->getLine(0); if ($headers != null) { for ($i = 1; $i < $numLines; $i++) { // For every line, convert the indexed array to an associated array // using the headers as keys $line = $csvReader->getLine($i); if ($line != null && count($line) == count($headers)) { $assocLine = array(); $numLineParts = count($line); for ($j = 0; $j < $numLineParts; $j++) { $assocLine[$headers[$j]] = $line[$j]; } array_push($this->__stockData, $assocLine); } } } } }