/**
  * Determines what action should be performed and takes that action.
  *
  * @uses MpmListController::displayHelp()
  * @uses MpmListHelper::getFullList()
  * @uses MpmListHelper::getTotalMigrations()
  * @uses MpmCommandLineWriter::getInstance()
  * @uses MpmCommandLineWriter::addText()
  * @uses MpmCommandLineWriter::write()
  * 
  * @return void
  */
 public function doAction()
 {
     $page = 1;
     $per_page = 30;
     if (isset($this->arguments[0])) {
         $page = $this->arguments[0];
     }
     if (isset($this->arguments[1])) {
         $per_page = $this->arguments[1];
     }
     if (!is_numeric($per_page)) {
         $per_page = 30;
     }
     if (!is_numeric($page)) {
         $page = 1;
     }
     $start_idx = ($page - 1) * $per_page;
     $list = MpmListHelper::getFullList($start_idx, $per_page);
     $total = MpmListHelper::getTotalMigrations();
     $total_pages = ceil($total / $per_page);
     $clw = MpmCommandLineWriter::getInstance();
     if ($total == 0) {
         $clw->addText('No migrations exist.');
     } else {
         $clw->addText("WARNING: Migration numbers may not be in order due to interleaving.", 4);
         $clw->addText(" ");
         $clw->addText("#\t\tTimestamp", 6);
         $clw->addText("=========================================", 4);
         foreach ($list as $obj) {
             if (strlen($obj->id) > 1) {
                 $clw->addText($obj->id . "\t" . $obj->timestamp, 6);
             } else {
                 $clw->addText($obj->id . "\t\t" . $obj->timestamp, 6);
             }
         }
         $clw->addText(" ");
         $clw->addText("Page {$page} of {$total_pages}, {$total} migrations in all.", 4);
     }
     $clw->write();
 }
 /**
  * Determines what action should be performed and takes that action.
  *
  * @uses MpmListController::displayHelp()
  * @uses MpmListHelper::getFullList()
  * @uses MpmListHelper::getTotalMigrations()
  * @uses MpmCommandLineWriter::getInstance()
  * @uses MpmCommandLineWriter::addText()
  * @uses MpmCommandLineWriter::write()
  * 
  * @return void
  */
 public function doAction()
 {
     $page = 1;
     $per_page = 30;
     if (isset($this->arguments[0])) {
         $page = $this->arguments[0];
     }
     if (isset($this->arguments[1])) {
         $per_page = $this->arguments[1];
     }
     if (!is_numeric($per_page)) {
         $per_page = 30;
     }
     if (!is_numeric($page)) {
         $page = 1;
     }
     $start_idx = ($page - 1) * $per_page;
     $list = MpmListHelper::getFullList($start_idx, $per_page, true);
     $total = MpmListHelper::getTotalMigrations();
     $total_pages = ceil($total / $per_page);
     $clw = MpmCommandLineWriter::getInstance();
     if ($total == 0) {
         $clw->addText('No migrations exist.');
     } else {
         $clw->addText("WARNING: Migration numbers may not be in order due to interleaving.", 4);
         $clw->addText(" ");
         $clw->addText("#\t\tTimestamp\t\tDescription", 6);
         $clw->addText(str_repeat('=', 80), 4);
         foreach ($list as $obj) {
             // "highlight" the current migration
             $id = $obj->is_current ? "*" . $obj->id . "*" : $obj->id;
             $id_indent = strlen($id) > 1 ? "\t" : "\t\t";
             $clw->addText($id . $id_indent . $obj->timestamp . "\t" . $obj->info, 6);
         }
         $clw->addText(" ");
         $clw->addText("Page {$page} of {$total_pages}, {$total} migrations in all.", 4);
     }
     $clw->write();
 }
 /**
  * Fetches a list of files and adds migrations to the database migrations table.
  *
  * @uses MpmListHelper::getListOfFiles()
  * @uses MpmListHelper::getTotalMigrations()
  * @uses MpmListHelper::getFullList()
  * @uses MpmListHelper::getTimestampArray()
  * @uses MpmDbHelper::getMethod()
  * @uses MpmDbHelper::getPdoObj()
  * @uses MpmDbHelper::getMysqliObj()
  * @uses MPM_METHOD_PDO
  *
  * @return void
  */
 static function mergeFilesWithDb()
 {
     $db_config = $GLOBALS['db_config'];
     $migrations_table = $db_config->migrations_table;
     $files = MpmListHelper::getListOfFiles();
     $total_migrations = MpmListHelper::getTotalMigrations();
     $db_list = MpmListHelper::getFullList(0, $total_migrations);
     $file_timestamps = MpmListHelper::getTimestampArray($files);
     if (MpmDbHelper::getMethod() == MPM_METHOD_PDO) {
         if (count($files) > 0) {
             $pdo = MpmDbHelper::getPdoObj();
             $pdo->beginTransaction();
             try {
                 foreach ($files as $file) {
                     $sql = "INSERT IGNORE INTO `{$migrations_table}` ( `timestamp`, `active`, `is_current` ) VALUES ( '{$file->timestamp}', 0, 0 )";
                     $pdo->exec($sql);
                 }
             } catch (Exception $e) {
                 $pdo->rollback();
                 echo "\n\nError: " . $e->getMessage();
                 echo "\n\n";
                 exit;
             }
             $pdo->commit();
         }
         if (count($db_list)) {
             $pdo->beginTransaction();
             try {
                 foreach ($db_list as $obj) {
                     if (!in_array($obj->timestamp, $file_timestamps) && $obj->active == 0) {
                         $sql = "DELETE FROM `{$migrations_table}` WHERE `id` = '{$obj->id}'";
                         $pdo->exec($sql);
                     }
                 }
             } catch (Exception $e) {
                 $pdo->rollback();
                 echo "\n\nError: " . $e->getMessage();
                 echo "\n\n";
                 exit;
             }
             $pdo->commit();
         }
     } else {
         $mysqli = MpmDbHelper::getMysqliObj();
         $mysqli->autocommit(false);
         if (count($files) > 0) {
             try {
                 $stmt = $mysqli->prepare('INSERT IGNORE INTO `' . $migrations_table . '` ( `timestamp`, `active`, `is_current` ) VALUES ( ?, 0, 0 )');
                 foreach ($files as $file) {
                     $stmt->bind_param('s', $file->timestamp);
                     $result = $stmt->execute();
                     if ($result === false) {
                         throw new Exception('Unable to execute query to update file list.');
                     }
                 }
             } catch (Exception $e) {
                 $mysqli->rollback();
                 $mysqli->close();
                 echo "\n\nError:", $e->getMessage(), "\n\n";
                 exit;
             }
             $mysqli->commit();
         }
         if (count($db_list)) {
             try {
                 $stmt = $mysqli->prepare('DELETE FROM `' . $migrations_table . '` WHERE `id` = ?');
                 foreach ($db_list as $obj) {
                     if (!in_array($obj->timestamp, $file_timestamps) && $obj->active == 0) {
                         $stmt->bind_param('i', $obj->id);
                         $result = $stmt->execute();
                         if ($result === false) {
                             throw new Exception('Unable to execute query to remove stale files from the list.');
                         }
                     }
                 }
             } catch (Exception $e) {
                 $mysqli->rollback();
                 $mysqli->close();
                 echo "\n\nError: " . $e->getMessage();
                 echo "\n\n";
                 exit;
             }
             $mysqli->commit();
         }
         $mysqli->close();
     }
 }