public function run(ChefContext $context) { $logger = $context->getLog(); $pieCrust = $context->getApp(); $result = $context->getResult(); // Validate options. if ($result->command->options['order_by_name'] && $result->command->options['order_by_count']) { throw new PieCrustException("Can't specify both '--order-name' and '--order-count'."); } $blogKeys = $pieCrust->getConfig()->getValue('site/blogs'); if ($result->command->args['blog']) { foreach ($result->command->args['blog'] as $blogKey) { if (!in_array($blogKey, $blogKeys)) { throw new PieCrustException("No such blog in the website : {$blogKey}"); } } $blogKeys = $result->command->args['blog']; } $categories = array(); foreach ($blogKeys as $blogKey) { $callback = function ($post) use(&$categories) { $c = $post->getConfig()->getValue('category'); if ($c) { if (!isset($categories[$c])) { $categories[$c] = 0; } $categories[$c] += 1; } }; PageHelper::processPosts($context->getApp(), $blogKey, $callback); } // Only print the count? if ($result->command->options['count']) { $logger->info(count($categories)); return 0; } // Sort appropriately. $reverse = $result->command->options['reverse']; if ($result->command->options['order_by_name']) { if ($reverse) { krsort($categories); } else { ksort($categories); } } else { if ($result->command->options['order_by_count']) { if ($reverse) { array_multisort($categories, SORT_DESC); } else { array_multisort($categories, SORT_ASC); } } } // Print the list. $logger->info(count($categories) . " categories."); foreach ($categories as $c => $count) { $logger->info("{$c} ({$count} posts)"); } }
public function run(ChefContext $context) { $logger = $context->getLog(); $pieCrust = $context->getApp(); $result = $context->getResult(); // Warn about deprecated stuff. if ($result->command->options['order_by_name_old']) { $context->getLog()->warning("The `--orderbyname` option has been renamed to `--order-name`."); $result->command->options['order_by_name'] = true; } if ($result->command->options['order_by_count_old']) { $context->getLog()->warning("The `--orderbycount` option has been renamed to `--order-count`."); $result->command->options['order_by_count'] = true; } // Validate options. if ($result->command->options['order_by_name'] && $result->command->options['order_by_count']) { throw new PieCrustException("Can't specify both '--order-name' and '--order-count'."); } $blogKeys = $pieCrust->getConfig()->getValue('site/blogs'); if ($result->command->args['blog']) { foreach ($result->command->args['blog'] as $blogKey) { if (!in_array($blogKey, $blogKeys)) { throw new PieCrustException("No such blog in the website : {$blogKey}"); } } $blogKeys = $result->command->args['blog']; } $tags = array(); foreach ($blogKeys as $blogKey) { $callback = function ($post) use(&$tags) { $postTags = $post->getConfig()->getValue('tags'); if ($postTags) { if (!is_array($postTags)) { $postTags = array($postTags); } foreach ($postTags as $t) { if (!isset($tags[$t])) { $tags[$t] = 0; } $tags[$t] += 1; } } }; PageHelper::processPosts($pieCrust, $blogKey, $callback); } // Only print the count? if ($result->command->options['count']) { $logger->info(count($tags)); return 0; } // Sort appropriately. $reverse = $result->command->options['reverse']; if ($result->command->options['order_by_name']) { if ($reverse) { krsort($tags); } else { ksort($tags); } } else { if ($result->command->options['order_by_count']) { if ($reverse) { array_multisort($tags, SORT_DESC); } else { array_multisort($tags, SORT_ASC); } } } // Print the list. $logger->info(count($tags) . " tags."); foreach ($tags as $t => $count) { $logger->info("{$t} ({$count} posts)"); } }