Exemplo n.º 1
0
 public static function export_data($po_opts = null)
 {
     require_once __CA_MODELS_DIR__ . "/ca_data_exporters.php";
     $vs_search = $po_opts->getOption('search');
     $vs_id = $po_opts->getOption('id');
     $vb_rdf = (bool) $po_opts->getOption('rdf');
     if (!$vb_rdf && !$vs_search && !$vs_id) {
         print _t('You must specify either an idno or a search expression to select a record or record set for export or activate RDF mode.') . "\n";
         return false;
     }
     if (!($vs_filename = $po_opts->getOption('file'))) {
         print _t('You must specify a file to write export output to.') . "\n";
         return false;
     }
     if (@file_put_contents($vs_filename, "") === false) {
         // probably a permission error
         print _t("Can't write to file %1. Check the permissions.", $vs_filename) . "\n";
         return false;
     }
     // RDF mode
     if ($vb_rdf) {
         if (!($vs_config = $po_opts->getOption('config'))) {
             print _t('You must specify a configuration file that contains the export definition for the RDF mode.') . "\n";
             return false;
         }
         // test config syntax
         if (!Configuration::load($vs_config)) {
             print _t('Syntax error in configuration file %s.', $vs_config) . "\n";
             return false;
         }
         if (ca_data_exporters::exportRDFMode($vs_config, $vs_filename, array('showCLIProgressBar' => true))) {
             print _t("Exported data to %1", CLIUtils::textWithColor($vs_filename, 'yellow'));
             return true;
         } else {
             print _t("Could not run RDF mode export") . "\n";
             return false;
         }
     }
     // Search or ID mode
     if (!($vs_mapping = $po_opts->getOption('mapping'))) {
         print _t('You must specify a mapping for export.') . "\n";
         return false;
     }
     if (!ca_data_exporters::loadExporterByCode($vs_mapping)) {
         print _t('Mapping %1 does not exist', $vs_mapping) . "\n";
         return false;
     }
     if (sizeof($va_errors = ca_data_exporters::checkMapping($vs_mapping)) > 0) {
         print _t("Mapping %1 has errors: %2", $vs_mapping, join("; ", $va_errors)) . "\n";
         return false;
     }
     if ($vs_search) {
         if (!ca_data_exporters::exportRecordsFromSearchExpression($vs_mapping, $vs_search, $vs_filename, array('showCLIProgressBar' => true, 'useNcurses' => true))) {
             print _t("Could not export mapping %1", $vs_mapping) . "\n";
             return false;
         } else {
             print _t("Exported data to %1", $vs_filename) . "\n";
         }
     } else {
         if ($vs_id) {
             if ($vs_export = ca_data_exporters::exportRecord($vs_mapping, $vs_id, $pa_options = array('singleRecord' => true))) {
                 file_put_contents($vs_filename, $vs_export);
                 print _t("Exported data to %1", CLIUtils::textWithColor($vs_filename, 'yellow'));
             } else {
                 print _t("Could not export mapping %1", $vs_mapping) . "\n";
                 return false;
             }
         }
     }
 }