public function test_column_value_too_long() { $constraint_width = 80; $table = new cli\Table(); $renderer = new cli\Table\Ascii(); $renderer->setConstraintWidth($constraint_width); $table->setRenderer($renderer); $table->setHeaders(array('Field', 'Value')); $table->addRow(array('description', 'The 2012 theme for WordPress is a fully responsive theme that looks great on any device. Features include a front page template with its own widgets, an optional display font, styling for post formats on both index and single views, and an optional no-sidebar page template. Make it yours with a custom menu, header image, and background.')); $table->addRow(array('author', '<a href="http://wordpress.org/" title="Visit author homepage">the WordPress team</a>')); $out = $table->getDisplayLines(); // "+ 1" accommodates "\n" $this->assertCount(12, $out); $this->assertEquals($constraint_width, strlen($out[0]) + 1); $this->assertEquals($constraint_width, strlen($out[1]) + 1); $this->assertEquals($constraint_width, strlen($out[2]) + 1); $this->assertEquals($constraint_width, strlen($out[3]) + 1); $this->assertEquals($constraint_width, strlen($out[4]) + 1); $this->assertEquals($constraint_width, strlen($out[5]) + 1); $this->assertEquals($constraint_width, strlen($out[6]) + 1); $this->assertEquals($constraint_width, strlen($out[7]) + 1); $this->assertEquals($constraint_width, strlen($out[8]) + 1); $this->assertEquals($constraint_width, strlen($out[9]) + 1); $this->assertEquals($constraint_width, strlen($out[10]) + 1); $this->assertEquals($constraint_width, strlen($out[11]) + 1); }
/** * List users. * * @subcommand list * @synopsis [--role=<role>] [--ids] */ public function _list($args, $assoc_args) { global $blog_id; $params = array('blog_id' => $blog_id, 'fields' => isset($assoc_args['ids']) ? 'ids' : 'all_with_meta'); if (array_key_exists('role', $assoc_args)) { $params['role'] = $assoc_args['role']; } $users = get_users($params); if (isset($assoc_args['ids'])) { WP_CLI::out(implode(' ', $users)); return; } $fields = array('ID', 'user_login', 'display_name', 'user_email', 'user_registered'); $table = new \cli\Table(); $table->setHeaders(array_merge($fields, array('roles'))); foreach ($users as $user) { $line = array(); foreach ($fields as $field) { $line[] = $user->{$field}; } $line[] = implode(',', $user->roles); $table->addRow($line); } $table->display(); WP_CLI::line('Total: ' . count($users) . ' users'); }
public static function print_profile_list() { $configuration = PS_CLI_CONFIGURE::getConfigurationInstance(); $profiles = Profile::getProfiles($configuration->lang); $table = new cli\Table(); $table->setHeaders(array('ID', 'Name')); foreach ($profiles as $profile) { $table->addRow(array($profile['id_profile'], $profile['name'])); } $table->display(); return true; }
public static function list_customers($lang = null) { // TODO: check if lang exists before using it if ($lang === null) { $lang = Configuration::get('PS_LANG_DEFAULT'); } $customers = Customer::getCustomers(); $table = new cli\Table(); $table->setHeaders(array('ID', 'email', 'First name', 'Last name')); foreach ($customers as $customer) { // print_r($customer); $table->addRow(array($customer['id_customer'], $customer['email'], $customer['firstname'], $customer['lastname'])); } $table->display(); }
/** * List all of the currently configured redirects * * @subcommand list */ public function _list() { global $safe_redirect_manager; $fields = array('ID', 'redirect_from', 'redirect_to', 'status_code', 'enable_regex', 'post_status'); $table = new \cli\Table(); $table->setHeaders($fields); $redirects = $safe_redirect_manager->get_redirects(array('post_status' => 'any')); foreach ($redirects as $redirect) { $line = array(); foreach ($fields as $field) { if ('enable_regex' == $field) { $line[] = $redirect[$field] ? 'true' : 'false'; } else { $line[] = $redirect[$field]; } } $table->addRow($line); } $table->display(); WP_CLI::line("Total of " . count($redirects) . " redirects"); }
/** * Constructs table for data going to STDOUT * TODO: Complexity too high. Refactor. * * @param [mixed] $data Object or hash of data for output * @param [array] $headers Array of strings for table headers * @return [void] */ protected function constructTableForResponse($data, $headers = array()) { $table = new \cli\Table(); if (is_object($data)) { $data = (array) $data; } if (\Terminus\Utils\result_is_multiobj($data)) { if (!empty($headers)) { $table->setHeaders($headers); } elseif (property_exists($this, '_headers') && !empty($this->_headers[$this->func])) { if (is_array($this->_headers[$this->func])) { $table->setHeaders($this->_headers[$this->func]); } } else { $table->setHeaders(\Terminus\Utils\result_get_response_fields($data)); } foreach ($data as $row => $row_data) { $row = array(); foreach ($row_data as $key => $value) { if (is_array($value) || is_object($value)) { $value = join(', ', (array) $value); } $row[] = $value; } $table->addRow($row); } } else { if (!empty($headers)) { $table->setHeaders($headers); } foreach ($data as $key => $value) { if (is_array($value) || is_object($value)) { $value = implode(', ', (array) $value); } $table->addRow(array($key, $value)); } } $table->display(); }
/** * @param $data * @param $headers */ private function formatTable($data, $headers = null) { $table = new \cli\Table(); if ($headers) { $table->setHeaders($headers); } foreach ($data as $row_data) { $row = array(); foreach ((array) $row_data as $key => $value) { $value = PrettyFormatter::flattenValue($value); $row[] = $value; } $table->addRow($row); } // @TODO: This does not test well. PHPUnit uses output buffering. ob_start(); $table->display(); $out = ob_get_contents(); ob_end_clean(); return $out; }
/** * Show items in a \cli\Table. * * @param array $items * @param array $fields */ private static function show_table($items, $fields) { $table = new \cli\Table(); $enabled = \cli\Colors::shouldColorize(); if ($enabled) { \cli\Colors::disable(true); } $table->setHeaders($fields); foreach ($items as $item) { $table->addRow(array_values(\WP_CLI\Utils\pick_fields($item, $fields))); } foreach ($table->getDisplayLines() as $line) { \WP_CLI::line($line); } if ($enabled) { \cli\Colors::enable(true); } }
public static function print_module_list($status = 'all', $onDiskOnly = true) { $_GET['controller'] = 'AdminModules'; $_GET['tab'] = 'AdminModules'; $modulesOnDisk = Module::getModulesOnDisk(); switch ($status) { case 'all': $table = new cli\Table(); $table->setHeaders(array('ID', 'Name', 'Installed', 'Active', 'Upgradable')); foreach ($modulesOnDisk as $module) { if ($onDiskOnly && isset($module->not_on_disk)) { continue; } Module::isInstalled($module->name) ? $iStat = 'Yes' : ($iStat = 'No'); Module::isEnabled($module->name) ? $aStat = 'Yes' : ($aStat = 'No'); // check for updates if (isset($module->version_addons) && $module->version_addons) { $uStat = 'Yes'; } else { $uStat = 'No'; } $table->addRow(array($module->id, $module->name, $iStat, $aStat, $uStat)); } break; case 'installed': foreach ($modulesOnDisk as $module) { if ($module->installed) { echo "{$module->id};" . "{$module->name};" . "{$module->installed};" . "{$module->active}\n"; } } break; case 'active': foreach ($modulesOnDisk as $module) { if ($module->active) { echo "{$module->id};" . "{$module->name};" . "{$module->installed};" . "{$module->active}\n"; } } break; default: return false; break; } $interface = PS_CLI_Interface::getInterface(); if ($table) { //$table->display(); $interface->add_table($table); } return true; }
$certificate = new Joelvardy\Certificate(__DIR__ . '/cache'); $table = new \cli\Table(); $table->setHeaders(['Domain', 'Domain Expiry', 'Certificate Expiry']); $formatColour = function ($expires) { $oneMonth = new DateTime(); $oneMonth->modify('+1 month'); $oneWeek = new DateTime(); $oneWeek->modify('+1 week'); return $expires < $oneWeek ? '%r' : ($expires < $oneMonth ? '%y' : '%g'); }; print "Checking domains:\n"; Joelvardy\Progress::bar(1, count($domains)); foreach ($domains as $i => $domain) { if ($domainDetails = $whois->check($domain->domain)) { $domainExpiresText = $formatColour($domainDetails->domain->expires) . $domainDetails->domain->expires->format('jS F Y') . '%n'; } else { $domainExpiresText = '%rUnable to load whois data%n'; } if (isset($domain->certificate) && $domain->certificate) { if ($certificateDetails = $certificate->check($domain->domain)) { $certificateExpiresText = $formatColour($certificateDetails->domain->expires) . $certificateDetails->domain->expires->format('jS F Y') . '%n'; } else { $certificateExpiresText = '%rUnable to load certificate data%n'; } } else { $certificateExpiresText = 'N/A'; } $table->addRow([$domain->domain, $domainExpiresText, $certificateExpiresText]); Joelvardy\Progress::bar($i + 1, count($domains)); } $table->display();
/** * Show items in a \cli\Table. * * @param array $items * @param array $fields */ private static function show_table($items, $fields) { $table = new \cli\Table(); $table->setHeaders($fields); foreach ($items as $item) { $table->addRow(array_values(\WP_CLI\Utils\pick_fields($item, $fields))); } $table->display(); }
/** * Get a list of posts. * * @subcommand list * @synopsis [--<field>=<value>] [--ids] */ public function _list($_, $assoc_args) { $query_args = array('posts_per_page' => -1); foreach ($assoc_args as $key => $value) { if (true === $value) { continue; } $query_args[$key] = $value; } if (isset($assoc_args['ids'])) { $query_args['fields'] = 'ids'; } $query = new WP_Query($query_args); if (isset($assoc_args['ids'])) { WP_CLI::out(implode(' ', $query->posts)); } else { $fields = array('ID', 'post_title', 'post_name', 'post_date'); $table = new \cli\Table(); $table->setHeaders($fields); foreach ($query->posts as $post) { $line = array(); foreach ($fields as $field) { $line[] = $post->{$field}; } $table->addRow($line); } $table->display(); } }
public static function list_employees($lang = NULL) { // TODO: check if lang exists before using it if ($lang === NULL) { $lang = Configuration::get('PS_LANG_DEFAULT'); } $profiles = Profile::getProfiles($lang); $table = new cli\Table(); $table->setHeaders(array('ID', 'email', 'profile', 'First name', 'Last name', 'Active')); foreach ($profiles as $profile) { $employees = Employee::getEmployeesByProfile($profile['id_profile']); if (!$employees) { continue; } foreach ($employees as $employee) { //print_r($employee); $enabled = $employee['active'] == 1 ? 'Active' : 'Inactive'; $table->addRow(array($employee['id_employee'], $employee['email'], $profile['name'], $employee['firstname'], $employee['lastname'], $enabled)); } } $table->display(); }
/** * @inheritdoc */ public function call($args, $flags) { // Check version and operations on remote server if (in_array('-c', $flags)) { $client = self::loadClient($args); \cli\line('%_Base URI:%n ' . $client->getBaseURI()); \cli\line('%_Version:%n ' . $client->serverVersion()); \cli\line('%_Operations:%n'); $table = new \cli\Table(); $table->setHeaders(array('Class', 'Methods', 'Path')); foreach ($client->request('GET', 'operations')->body as $data) { $table->addRow(array_values((array) $data)); } $table->display(); } elseif (isset($args['-r'])) { $servers = $this->loadStoredServerInfo(); if (isset($servers[$args['-r']])) { unset($servers[$args['-r']]); file_put_contents($this->infoFile, serialize($servers)); \cli\line('Server removed (add flag -l to list servers)'); } } elseif (in_array('-l', $flags)) { $servers = $this->loadStoredServerInfo(); $this->listServers($servers); } elseif (isset($args['-d'])) { $servers = $this->loadStoredServerInfo(); if (empty($servers[$args['-d']])) { \cli\line('Server does not exist....'); $this->listServers($servers); } else { $servers['__default'] = $args['-d']; file_put_contents($this->infoFile, serialize($servers)); \cli\line('%gServer "' . $args['-d'] . '" set as default %n'); } } else { \cli\line('Add remote server'); \cli\line('--------------------'); $name = \cli\prompt('Server name (any name of your choice)'); $address = rtrim(\cli\prompt('Server address'), '/') . '/'; $user = \cli\prompt('Admin e-mail'); $pass = Utils::promptPassword('Admin password: '******'Secret (leave empty if not used)')); try { // Try to request server $auth = $user . ':' . $pass; if ($secret) { $auth = 'RC4 ' . base64_encode(RC4Cipher::encrypt($secret, $auth)); } else { $auth = 'Basic ' . base64_encode($auth); } $client = new Client($address); $client->setAuthString($auth); $user = $client->me(); // just to check that auth is correct if (!$user) { throw new \Exception('Could not authenticate'); } $version = $client->serverVersion(); $this->addServer($name, $address, $auth); \cli\line('%gSuccessfully added server "' . $name . '" (v' . $version . ')%n'); \cli\line('... add flag -l to list all added servers'); } catch (\Exception $e) { \cli\err('Failed adding server with message "' . $e->getMessage() . '"'); } } }