/** * Returns the requested template file as a string * * @uses MPM_PATH * * @param string $file the filename of the template being requested * @param array $vars an array of key value pairs that correspond to variables that should be replaced in the template file * * @return string */ public static function getTemplate($file, $vars = array()) { // don't raise exception $db_config = MpmDbHelper::get_db_config(false); if (!$db_config) { $db_config = new stdClass(); $db_config->db_path = MPM_PATH . '/lib/templates/'; } // has the file been customized? if (file_exists($db_config->db_path . 'templates/' . $file)) { $contents = file_get_contents($db_config->db_path . 'templates/' . $file); } else { $contents = file_get_contents(MPM_PATH . '/lib/templates/' . $file); } foreach ($vars as $key => $val) { $contents = str_replace('@@' . $key . '@@', $val, $contents); } return $contents; }
/** * Returns a migration object; this object contains all data stored in the DB for the particular migration ID. * * @uses MpmDbHelper::getMethod() * @uses MpmDbHelper::getDbObj() * @uses MPM_METHOD_MYSQLI * @uses MPM_METHOD_PDO * * @param int $id the ID of the migration * * @return object */ public static function getMigrationObject($id) { $db_config = MpmDbHelper::get_db_config(); $migrations_table = $db_config->migrations_table; $sql = "SELECT * FROM `{$migrations_table}` WHERE `id` = '{$id}'"; $obj = null; try { switch (MpmDbHelper::getMethod()) { case MPM_METHOD_PDO: $pdo = MpmDbHelper::getDbObj(); $stmt = $pdo->internal_query($sql); $obj = $stmt->fetch(PDO::FETCH_OBJ); break; case MPM_METHOD_MYSQLI: $mysqli = MpmDbHelper::getDbObj(); $stmt = $mysqli->internal_query($sql); $obj = $stmt->fetch_object(); break; } } catch (Exception $e) { echo "\n\nERROR: " . $e->getMessage() . "\n\n"; exit; } return $obj; }
/** * Fetches a list of migrations which have already been run. * * @uses MpmDbHelper::doSingleRowSelect() * @uses MpmDbHelper::doMultiRowSelect() * * @param string $latestTimestamp the current timestamp of the migration run last * @param string $direction the way we are migrating; should either be up or down * * @return array */ public static function getListFromDb($latestTimestamp, $direction = 'up') { $db_config = MpmDbHelper::get_db_config(); $migrations_table = $db_config->migrations_table; if ($direction == 'down') { $sql = "SELECT * FROM `{$migrations_table}` WHERE `timestamp` <= '{$latestTimestamp}' AND `active` = 1"; $countSql = "SELECT COUNT(*) as total FROM `{$migrations_table}` WHERE `timestamp` <= '{$latestTimestamp}' AND `active` = 1"; } else { $sql = "SELECT * FROM `{$migrations_table}` WHERE `timestamp` >= '{$latestTimestamp}' AND `active` = 1"; $countSql = "SELECT COUNT(*) as total FROM `{$migrations_table}` WHERE `timestamp` >= '{$latestTimestamp}' AND `active` = 1"; } $list = array(); $countObj = MpmDbHelper::doSingleRowSelect($countSql); if ($countObj->total > 0) { $results = MpmDbHelper::doMultiRowSelect($sql); foreach ($results as $obj) { $list[] = $obj->timestamp; } } return $list; }
/** * Clears the migrations table and then rebuilds it. * * @uses MpmListHelper::mergeFilesWithDb() * @uses MpmDbHelper::doSingleRowSelect() * * @return void */ public function reloadMigrations() { if ($this->dryrun) { echo "No clear out existing migration data for dry-run.\n"; } else { $db_config = MpmDbHelper::get_db_config(); $migrations_table = $db_config->migrations_table; echo 'Clearing out existing migration data... '; $this->dbObj->internal_exec('TRUNCATE TABLE `' . $migrations_table . '`'); echo 'done.', "\n\n", 'Rebuilding migration data... '; MpmListHelper::mergeFilesWithDb(); echo 'done.', "\n"; if ($this->initialMigrationTimestamp != null) { echo "\n", 'Updating initial migration timestamp to ', $this->initialMigrationTimestamp, '... '; $result = MpmDbHelper::doSingleRowSelect('SELECT COUNT(*) AS total FROM `' . $migrations_table . '` WHERE `timestamp` = "' . $this->initialMigrationTimestamp . '"', $this->dbObj); if ($result->total == 1) { $this->dbObj->internal_exec('UPDATE `' . $migrations_table . '` SET `is_current` = 0'); $this->dbObj->internal_exec('UPDATE `' . $migrations_table . '` SET `is_current` = 1 WHERE `timestamp` = "' . $this->initialMigrationTimestamp . '"'); $this->dbObj->internal_exec('UPDATE `' . $migrations_table . '` SET `active` = 1 WHERE `timestamp` <= "' . $this->initialMigrationTimestamp . '"'); } echo 'done.', "\n"; } } }
/** * Checks whether or not the migrations database table exists. * * @uses MpmDbHelper::getDbObj() * @uses MpmDbHelper::getMethod() * @uses MPM_METHOD_PDO * @uses MPM_METHOD_MYSQLI * * @return bool */ public static function checkForDbTable() { $db_config = MpmDbHelper::get_db_config(); if (isset($db_config->migrations_table)) { $migrations_table = $db_config->migrations_table; } $tables = MpmDbHelper::getTables(); if (count($tables) == 0 || !in_array($migrations_table, $tables)) { return false; } return true; }