示例#1
0
文件: Dump.php 项目: CeusMedia/Hymn
 public function run()
 {
     if (!Hymn_Command_Database_Test::test($this->client)) {
         return Hymn_Client::out("Database can NOT be connected.");
     }
     $arguments = $this->client->arguments;
     $fileName = $arguments->getArgument(0);
     if (!preg_match("/[a-z0-9]/i", $fileName)) {
         //  arguments has not valid value
         $fileName = 'config/sql/';
     }
     //  set default path
     if (substr($fileName, -1) == "/") {
         //  given argument is a path
         $fileName = $fileName . "dump_" . date("Y-m-d_H:i:s") . ".sql";
     }
     //  generate stamped file name
     if (dirname($fileName)) {
         //  path is not existing
         exec("mkdir -p " . dirname($fileName));
     }
     //  create path
     $dbc = $this->client->getDatabase();
     $username = $this->client->getDatabaseConfiguration('username');
     $password = $this->client->getDatabaseConfiguration('password');
     $name = $this->client->getDatabaseConfiguration('name');
     $prefix = $this->client->getDatabaseConfiguration('prefix');
     if ($this->prefix = $arguments->hasOption('prefix')) {
         $this->prefixPlaceholder = $arguments->getOption('prefix');
     }
     $tables = array();
     if ($prefix) {
         foreach ($dbc->query("SHOW TABLES LIKE '" . $prefix . "%'") as $table) {
             $tables[] = $table[0];
         }
     }
     $tables = join(' ', $tables);
     $command = "mysqldump -u%s -p%s %s %s > %s";
     $command = sprintf($command, $username, $password, $name, $tables, $fileName);
     exec($command);
     /*  --  REPLACE PREFIX  --  */
     $regExp = "@(EXISTS|FROM|INTO|TABLE|TABLES|for table)( `)(" . $prefix . ")(.+)(`)@U";
     //  build regular expression
     $callback = array($this, '_callbackReplacePrefix');
     //  create replace callback
     $contents = explode("\n", file_get_contents($fileName));
     //  read raw dump file
     foreach ($contents as $nr => $content) {
         //  iterate lines
         $contents[$nr] = preg_replace_callback($regExp, $callback, $content);
     }
     //  replace prefix by placeholder
     file_put_contents($fileName, implode("\n", $contents));
     //  save final dump file
     return Hymn_Client::out("Database dumped to " . $fileName);
 }
示例#2
0
文件: Load.php 项目: CeusMedia/Hymn
 public function run()
 {
     if (!Hymn_Command_Database_Test::test($this->client)) {
         return Hymn_Client::out("Database can NOT be connected.");
     }
     $pathName = $this->client->arguments->getArgument(0);
     if ($pathName && file_exists($pathName)) {
         if (is_dir($pathName)) {
             $fileName = $this->getLatestDump($pathName, TRUE);
         } else {
             $fileName = $pathName;
         }
     } else {
         $fileName = $this->getLatestDump(NULL, TRUE);
     }
     if (!($fileName && file_exists($fileName))) {
         return Hymn_Client::out("No loadable database file found.");
     }
     $username = $this->client->getDatabaseConfiguration('username');
     $password = $this->client->getDatabaseConfiguration('password');
     $name = $this->client->getDatabaseConfiguration('name');
     $prefix = $this->client->getDatabaseConfiguration('prefix');
     $tempName = $fileName . ".tmp";
     try {
         if (($content = @file_get_contents($fileName)) === FALSE) {
             throw new RuntimeException('Missing read access to SQL script');
         }
         $content = str_replace("<%?prefix%>", $prefix, $content);
         if (@file_put_contents($tempName, $content) === FALSE) {
             throw new RuntimeException('Missing write access to SQL scripts path');
         }
         $command = "mysql -u%s -p%s %s < %s";
         $command = sprintf($command, $username, $password, $name, $tempName);
         $fileSize = Hymn_Tool_FileSize::get($fileName);
         Hymn_Client::out("Importing " . $fileName . " (" . $fileSize . ") ...");
         exec($command);
         unlink($tempName);
         //			return Hymn_Client::out( "Database loaded from ".$fileName );
     } catch (Exception $e) {
         Hymn_Client::out("Importing " . $fileName . " failed: " . $e->getMessage());
     }
 }
示例#3
0
文件: Clear.php 项目: CeusMedia/Hymn
 public function run()
 {
     if (!Hymn_Command_Database_Test::test($this->client)) {
         return Hymn_Client::out("Database can NOT be connected.");
     }
     $force = $this->client->arguments->getOption('force');
     $verbose = $this->client->arguments->getOption('verbose');
     $quiet = $this->client->arguments->getOption('quiet');
     $prefix = $this->client->getDatabaseConfiguration('prefix');
     $dbc = $this->client->getDatabase();
     $result = $dbc->query("SHOW TABLES" . ($prefix ? " LIKE '" . $prefix . "%'" : ""));
     $tables = $result->fetchAll();
     if (!$tables) {
         if (!$quiet) {
             Hymn_Client::out("Database is empty");
         }
         return;
     }
     if (!$force) {
         if ($quiet) {
             return Hymn_Client::out("Quiet mode needs force mode (-f|--force)");
         }
         Hymn_Client::out("Database tables:");
         foreach ($tables as $table) {
             Hymn_Client::out("- " . $table[0]);
         }
         $answer = Hymn_Client::getInput("Do you really want to drop these tables?", NULL, array("y", "n"));
         if ($answer !== "y") {
             return;
         }
     }
     foreach ($tables as $table) {
         if (!$quiet && $verbose) {
             Hymn_Client::out("- Drop table '" . $table[0] . "'");
         }
         $dbc->query("DROP TABLE " . $table[0]);
     }
     if (!$quiet) {
         Hymn_Client::out("Database cleared");
     }
 }