/** * @param string $filename * @return int */ private function getCountLines(string $filename) : int { $linecount = 0; /** @var Progress $progress */ $progress = $this->climate->progress()->total(10000000); $handle = fopen($filename, "r"); while (!feof($handle)) { $progress->advance(); fgets($handle); $linecount++; } fclose($handle); return $linecount; }
/** * Tests each record to see if the 'filetype' propery is not 'cpd', * and then tests to see whether the object has no parent. * * @param array $all_records * All of the records from the fetcher. * @return array $filtered_records * An array of records that pass the test(s) defined in the * fetcher manipulator. */ public function manipulate($all_records) { $numRecs = count($all_records); echo "Filtering {$numRecs} records through the CdmNoParent fetcher manipulator.\n"; // Instantiate the progress bar if we're not running on Windows. if (!$this->onWindows) { $climate = new \League\CLImate\CLImate(); $progress = $climate->progress()->total($numRecs); } $record_num = 0; $filtered_records = array(); foreach ($all_records as $record) { if (property_exists($record, 'key') && property_exists($record, 'filetype') && is_string($record->filetype) && $record->filetype != 'cpd') { $pointer = $record->key; // And that have no parent. if (!$this->getCdmParent($pointer)) { $filtered_records[] = $record; } $record_num++; if ($this->onWindows) { print '.'; } else { $progress->current($record_num); } } } if ($this->onWindows) { print "\n"; } return $filtered_records; }
/** * Tests each record to see if it has a .cpd file, and if so, * what the value of the CPD <type> element is. * * @param array $all_records * All of the records from the fetcher. * @return array $filtered_records * An array of records that pass the test(s) defined in the fetcher manipulator. */ public function manipulate($all_records) { $numRecs = count($all_records); echo "Fetching {$numRecs} records, filitering them.\n"; // Instantiate the progress bar if we're not running on Windows. if (!$this->onWindows) { $climate = new \League\CLImate\CLImate(); $progress = $climate->progress()->total($numRecs); } $record_num = 0; $filtered_records = array(); foreach ($all_records as $record) { $structure = $this->getDocumentStructure($record->pointer); if ($record->filetype == 'cpd' && $structure['type'] == $this->type) { $filtered_records[] = $record; } $record_num++; if ($this->onWindows) { print '.'; } else { $progress->current($record_num); } } if ($this->onWindows) { print "\n"; } return $filtered_records; }
public function run($args) { $cli = new CLImate(); $file_name = 'db_' . time() . '.sql'; $remote_file = Remote::getHomePath() . $file_name; $local_file = Local::getDocRoot() . "{$file_name}"; $ssh = SSHConnection::getInstance(); $ssh->exec('mysqldump -h ' . Setting::getSetting('mysql:host') . ' -u ' . Setting::getSetting('mysql:user') . ' -p' . addslashes(Setting::getSetting('mysql:pass')) . ' ' . Setting::getSetting('mysql:db') . " > " . $remote_file); $ssh->scpRemoteLocal($remote_file, $local_file); $ssh->rmRemoteFile($remote_file); $sql = file($local_file); $db = new LocalDB(); $templine = ''; $size = count($sql); $cli->out("Importing database"); $progress = $cli->progress()->total($size); $current = 0; foreach ($sql as $line) { $current++; // Skip it if it's a comment if (substr($line, 0, 2) == '--' || $line == '') { continue; } $templine .= $line; if (substr(trim($line), -1, 1) == ';') { $db->execute($templine); $progress->current($current); $templine = ''; } } unlink($local_file); }
/** * Tests each record to see if it has one of the extensions in * $this->allowed_extensions. * * @param array $all_records * All of the records from the fetcher. * @return array $filtered_records * An array of records that pass the test(s) defined in this function. */ public function manipulate($records) { // var_dump($records); $numRecs = count($records); echo "Fetching {$numRecs} records, filitering them.\n"; // Instantiate the progress bar. if (!$this->onWindows) { $climate = new \League\CLImate\CLImate(); $progress = $climate->progress()->total($numRecs); } $record_num = 0; $filtered_records = array(); foreach ($records as $record) { // var_dump($record); $ext = pathinfo($record->{$this->file_name_field}, PATHINFO_EXTENSION); if (in_array($ext, $this->allowed_extensions)) { $filtered_records[] = $record; } $record_num++; if ($this->onWindows) { print '.'; } else { $progress->current($record_num); } } if ($this->onWindows) { print "\n"; } return $filtered_records; }
/** * The body of the command. * * @return void */ public function mysqldumper() { if (!$this->mysqldumpExists()) { throw new \Exception('mysqldump command not found. Please check your path.'); } $this->createArchiveFolder(); // Get a list of the tables we are going to dump $table_list = $this->listTables(); // Count tables $table_count = count($table_list); // Create a progress bar $progress = $this->cli->progress()->total($table_count); // Loop of tables and create dump for ($i = 0; $i < $table_count; $i++) { $table_name = $table_list[$i]['Tables_in_' . $this->config->db_name]; $progress->advance(1, $this->parseString('(%s of %s) Dumping %s', [$i + 1, $table_count, $table_name], 'light_green')); $command = $this->buildCommand($table_name); exec($command); } $this->cli->br(); $this->out('Dump complete', 'success'); if (!$this->skip_remote) { $this->out('Uploading to remote', 'success'); $this->deployToRemote(); } else { $this->out('Skipping remote upload', 'warning'); } // Clean up $this->cleanupLocal(); $this->cleanupRemote(); }
/** * Tests each record to see if the extension of the file named in * the 'find' field has an extension matching any in the list of * extensions defined as manipulator paramters. * * @param array $all_records * All of the records from the fetcher. * @return array $filtered_records * An array of records that pass the test(s) defined in the * fetcher manipulator. */ public function manipulate($all_records) { $numRecs = count($all_records); echo "Filtering {$numRecs} records through the CdmSingleFileByExtension fetcher manipulator.\n"; // Instantiate the progress bar if we're not running on Windows. if (!$this->onWindows) { $climate = new \League\CLImate\CLImate(); $progress = $climate->progress()->total($numRecs); } $record_num = 0; $filtered_records = array(); foreach ($all_records as $record) { if (property_exists($record, 'find') && is_string($record->find) && strlen($record->find)) { $ext = pathinfo($record->find, PATHINFO_EXTENSION); if (in_array($ext, $this->extensions)) { $filtered_records[] = $record; } $record_num++; if ($this->onWindows) { print '.'; } else { $progress->current($record_num); } } } if ($this->onWindows) { print "\n"; } return $filtered_records; }
/** * Selects a random sample of records. * * @param array $all_records * All of the records from the fetcher. * @return array $filtered_records * An array of records that pass the test(s) defined in the fetcher manipulator. */ public function manipulate($all_records) { $numRecs = count($all_records); echo "Filtering {$numRecs} records through the RandomSet fetcher manipulator.\n"; // Instantiate the progress bar if we're not running on Windows. if (!$this->onWindows) { $climate = new \League\CLImate\CLImate(); $progress = $climate->progress()->total($numRecs); } $randomSet = $this->getRandomSet($numRecs); $record_num = 0; $filtered_records = array(); foreach ($all_records as $record) { if (in_array($record_num, $randomSet)) { $filtered_records[] = $record; } $record_num++; if ($this->onWindows) { print '.'; } else { $progress->current($record_num); } } if ($this->onWindows) { print "\n"; } return $filtered_records; }
/** * Filter on pattern in file name. * * @param array $records * All of the records from the fetcher. * @return array $filtered_records * An array of records that pass the test(s) defined in this function. */ public function manipulate($records) { $numRecs = count($records); echo "Filtering {$numRecs} records through the CsvSingleFileByFilename fetcher manipulator.\n"; // Instantiate the progress bar. if (!$this->onWindows) { $climate = new \League\CLImate\CLImate(); $progress = $climate->progress()->total($numRecs); } $record_num = 0; $filtered_records = array(); foreach ($records as $record) { $filename = pathinfo($record->{$this->file_name_field}, PATHINFO_FILENAME); if (preg_match('/' . $this->allowed_pattern . '/', $filename)) { $filtered_records[] = $record; } $record_num++; if ($this->onWindows) { print '.'; } else { $progress->current($record_num); } } if ($this->onWindows) { print "\n"; } return $filtered_records; }
public function run($args) { $cli = new CLImate(); $backup_dir = Setting::getSetting('consh:db_backup_folder'); if (!file_exists($backup_dir)) { if (!mkdir($backup_dir, 0777, true)) { $cli->error("Could not create database backup folder: {$backup_dir}"); return false; } } if (count($args) != 1) { $cli->error("Please pass along the filename to import"); if ($handle = opendir($backup_dir)) { $cli->out("possible files:"); while (false !== ($entry = readdir($handle))) { if ($entry != '.' && $entry != '..') { $cli->out($entry); } } } else { $cli->error("Could not open database backup folder"); } return false; } $file = $args[0]; $path = $backup_dir . "/" . $file; if (!file_exists($path)) { $cli->error("{$file} does not exist"); return false; } $sql = file($path); $db = new LocalDB(); $templine = ''; $size = count($sql); $cli->out("Restoring database"); $progress = $cli->progress()->total($size); $current = 0; foreach ($sql as $line) { $current++; // Skip it if it's a comment if (substr($line, 0, 2) == '--' || $line == '') { continue; } $templine .= $line; if (substr(trim($line), -1, 1) == ';') { $db->execute($templine); $progress->current($current); $templine = ''; } } }
/** * Selects a subset of records based on the value of their record keys. * * @param array $all_records * All of the records from the fetcher. * @return array $filtered_records * An array of records that pass the test(s) defined in the fetcher manipulator. */ public function manipulate($all_records) { $numRecs = count($all_records); echo "Filtering {$numRecs} records through the RangeSet fetcher manipulator.\n"; // Instantiate the progress bar if we're not running on Windows. if (!$this->onWindows) { $climate = new \League\CLImate\CLImate(); $progress = $climate->progress()->total($numRecs); } // Determine what type of range test we will apply. if (preg_match('/^>/', $this->range)) { $filter = 'greaterThan'; } if (preg_match('/^</', $this->range)) { $filter = 'lessThan'; } if (preg_match('/@/', $this->range)) { $filter = 'between'; } // The limit filter uses position in full record set, // not value of record key. if (preg_match('/,/', $this->range)) { $filter = 'limit'; } $record_num = 0; $filtered_records = array(); foreach ($all_records as $record) { $record_num++; if ($this->{$filter}($record->key, $this->range, $record_num)) { $filtered_records[] = $record; } if ($this->onWindows) { print '.'; } else { $progress->current($record_num); } } if ($this->onWindows) { print "\n"; } if (count($filtered_records) === 0) { $this->log->addError("RangeSet", array('Empty record set' => "The range " . $this->range . " has filtered out all records.")); } return $filtered_records; }
/** * Selects a specific subset of records. * * @param array $all_records * All of the records from the fetcher. * @return array $filtered_records * An array of records that pass the test(s) defined in the fetcher manipulator. */ public function manipulate($all_records) { $numRecs = count($all_records); echo "Filtering {$numRecs} records through the SpecificSet fetcher manipulator.\n"; // Instantiate the progress bar if we're not running on Windows. if (!$this->onWindows) { $climate = new \League\CLImate\CLImate(); $progress = $climate->progress()->total($numRecs); } $specificSet = $this->getSpecificSet(); $record_num = 0; $filtered_records = array(); foreach ($all_records as $record) { // If we're in 'exclude' mode, keep records that are not listed // in the input file. if ($this->exclude) { if (!in_array($record->key, $specificSet)) { $filtered_records[] = $record; } } else { if (in_array($record->key, $specificSet)) { $filtered_records[] = $record; } } $record_num++; if ($this->onWindows) { print '.'; } else { $progress->current($record_num); } } if ($this->onWindows) { print "\n"; } return $filtered_records; }
/** * Create a progressbar * @see http://climate.thephpleague.com/terminal-objects/progress-bar/ * * @param int $total Total progress * @return mixed */ public function progress($total = null) { if ($this->hasSttyAvailable()) { return $this->climate->progress($total); } }