diff --git a/app/Console/Command/AdminShell.php b/app/Console/Command/AdminShell.php index a68d747c0..b65326a64 100644 --- a/app/Console/Command/AdminShell.php +++ b/app/Console/Command/AdminShell.php @@ -9,6 +9,7 @@ App::uses('JsonTool', 'Tools'); * @property Feed $Feed * @property Warninglist $warninglist * @property AdminSetting $AdminSetting + * @property Taxonomy $Taxonomy */ class AdminShell extends AppShell { @@ -23,6 +24,9 @@ class AdminShell extends AppShell $parser->addSubcommand('updateWarningLists', array( 'help' => __('Update the JSON definition of warninglists.'), )); + $parser->addSubcommand('updateTaxonomies', array( + 'help' => __('Update the JSON definition of taxonomies.'), + )); $parser->addSubcommand('setSetting', [ 'help' => __('Set setting in PHP config file.'), 'parser' => [ @@ -282,22 +286,27 @@ class AdminShell extends AppShell public function updateTaxonomies() { - $this->ConfigLoad->execute(); $result = $this->Taxonomy->update(); - $successes = count(!empty($result['success']) ? $result['success'] : []); - $fails = count(!empty($result['fails']) ? $result['fails'] : []); - $message = ''; - if ($successes == 0 && $fails == 0) { - $message = __('All taxonomies are up to date already.'); - } elseif ($successes == 0 && $fails > 0) { + $successes = empty($result['success']) ? 0 : count($result['success']); + $fails = empty($result['fails']) ? 0 : count($result['fails']); + + if ($successes === 0 && $fails === 0) { + $message = __('All taxonomies are up to date already.'); + } elseif ($successes === 0 && $fails > 0) { $message = __('Could not update any of the taxonomies.'); - } elseif ($successes > 0 ) { + } else { $message = __('Successfully updated %s taxonomies.', $successes); - if ($fails != 0) { + if ($fails !== 0) { $message .= __(' However, could not update %s taxonomies.', $fails); } } - echo $message . PHP_EOL; + $this->out($message); + if ($fails) { + $this->out(__('Fails:')); + foreach ($result['fails'] as $fail) { + $this->out("{$fail['namespace']}: {$fail['fail']}"); + } + } } public function enableTaxonomyTags() diff --git a/app/Controller/TaxonomiesController.php b/app/Controller/TaxonomiesController.php index 43c4ce1b2..d4dec12c2 100644 --- a/app/Controller/TaxonomiesController.php +++ b/app/Controller/TaxonomiesController.php @@ -270,9 +270,9 @@ class TaxonomiesController extends AppController $message = __('Could not update any of the taxonomy libraries'); } else { $flashType = 'success'; - $message = __('Successfully updated ') . $successes . __(' taxonomy libraries.'); + $message = __('Successfully updated %s taxonomy libraries.', $successes); if ($fails != 0) { - $message .= __(' However, could not update ') . $fails . __(' taxonomy libraries.'); + $message .= __(' However, could not update %s taxonomy libraries.', $fails); } } if ($this->_isRest()) { diff --git a/app/Model/Taxonomy.php b/app/Model/Taxonomy.php index 1f0c479f6..415ca795b 100644 --- a/app/Model/Taxonomy.php +++ b/app/Model/Taxonomy.php @@ -49,14 +49,15 @@ class Taxonomy extends AppModel continue; } - $file = new File(APP . 'files' . DS . 'taxonomies' . DS . $dir . DS . 'machinetag.json'); - if (!$file->exists()) { + $machineTagPath = APP . 'files' . DS . 'taxonomies' . DS . $dir . DS . 'machinetag.json'; + if (!file_exists($machineTagPath)) { continue; } + try { - $vocab = $this->jsonDecode($file->read()); + $vocab = FileAccessTool::readJsonFromFile($machineTagPath); } catch (Exception $e) { - $updated['fails'][] = array('namespace' => $dir, 'fail' => "File machinetag.json is not valid JSON."); + $updated['fails'][] = ['namespace' => $dir, 'fail' => $e->getMessage()]; continue; }