/**
 * Do general setup for a CLI script
 * @param array $pa_additional_parameters Additional command line parameters. You don't have to add
 * --log/-l for the log file and --log-level/-d for the Zend_Log log level. They're always set up automatically
 * @return Zend_Console_Getopt
 */
function caSetupCLIScript($pa_additional_parameters)
{
    require_once __CA_LIB_DIR__ . "/core/Zend/Console/Getopt.php";
    require_once __CA_LIB_DIR__ . "/core/Zend/Log.php";
    require_once __CA_LIB_DIR__ . "/core/Zend/Log/Writer/Stream.php";
    require_once __CA_LIB_DIR__ . "/core/Zend/Log/Writer/Syslog.php";
    require_once __CA_LIB_DIR__ . "/core/Zend/Log/Formatter/Simple.php";
    $va_available_cli_opts = array_merge(array("log|l-s" => "Path to log file. If omitted, we log into the system log. Note that we don't log DEBUG messages into the system log, even when the log level is set to DEBUG.", "log-level|d-s" => "Log level"), $pa_additional_parameters);
    try {
        $o_opts = new Zend_Console_Getopt($va_available_cli_opts);
        $o_opts->parse();
    } catch (Exception $e) {
        die("Invalid command line options: " . $e->getMessage() . PHP_EOL);
    }
    // set up logging
    $o_writer = null;
    if ($vs_log = $o_opts->getOption('log')) {
        // log to file
        try {
            $o_writer = new Zend_Log_Writer_Stream($vs_log);
            $o_writer->setFormatter(new Zend_Log_Formatter_Simple('%timestamp% %priorityName%: %message%' . PHP_EOL));
        } catch (Zend_Log_Exception $e) {
            // error while opening the file (usually permissions)
            $o_writer = null;
            print CLIUtils::textWithColor("Couldn't open log file. Now logging via system log.", "bold_red") . PHP_EOL . PHP_EOL;
        }
    }
    // default: log everything to syslog
    if (!$o_writer) {
        $o_writer = new Zend_Log_Writer_Syslog(array('application' => 'CollectiveAccess CLI', 'facility' => LOG_USER));
        // no need for timespamps in syslog ... the syslog itsself provides that
        $o_writer->setFormatter(new Zend_Log_Formatter_Simple('%priorityName%: %message%' . PHP_EOL));
    }
    // was a loglevel set via command line? -> add filter to Zend logger, otherwise use WARN
    $vs_level = $o_opts->getOption('log-level');
    switch ($vs_level) {
        case 'ERR':
            $o_filter = new Zend_Log_Filter_Priority(Zend_Log::ERR);
            break;
        case 'DEBUG':
            $o_filter = new Zend_Log_Filter_Priority(Zend_Log::DEBUG);
            break;
        case 'INFO':
            $o_filter = new Zend_Log_Filter_Priority(Zend_Log::INFO);
            break;
        case 'WARN':
        default:
            $o_filter = new Zend_Log_Filter_Priority(Zend_Log::WARN);
            break;
    }
    // set up global logger. can be used by importing 'global $g_logger' anywhere, but it's recommended to use the caCLILog() helper instead
    global $g_logger;
    $g_logger = new Zend_Log($o_writer);
    $g_logger->setTimestampFormat('D Y-m-d H:i:s');
    $g_logger->addFilter($o_filter);
    return $o_opts;
}
Beispiel #2
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;
             }
         }
     }
 }