/** * 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; }
/** * Reset user password */ public static function reset_password($po_opts = null) { if ($vs_user_name = (string) $po_opts->getOption('user')) { if (!($vs_password = (string) $po_opts->getOption('password'))) { CLIUtils::addError(_t("You must specify a password")); return false; } $t_user = new ca_users(); if (!$t_user->load(array("user_name" => $vs_user_name))) { CLIUtils::addError(_t("User name %1 does not exist", $vs_user_name)); return false; } $t_user->setMode(ACCESS_WRITE); $t_user->set('password', $vs_password); $t_user->update(); if ($t_user->numErrors()) { CLIUtils::addError(_t("Password change for user %1 failed: %2", $vs_user_name, join("; ", $t_user->getErrors()))); return false; } CLIUtils::addMessage(_t('Changed password for user %1', $vs_user_name), array('color' => 'bold_green')); return true; } CLIUtils::addError(_t("You must specify a user")); return false; }
/** * Process queued tasks */ public static function fix_permissions($po_opts = null) { // Guess web server user if (!($vs_user = $po_opts->getOption("user"))) { $vs_user = caDetermineWebServerUser(); if (!$po_opts->getOption("quiet") && $vs_user) { CLIUtils::addMessage(_t("Determined web server user to be \"%1\"", $vs_user)); } } if (!$vs_user) { $vs_user = caGetProcessUserName(); CLIUtils::addError(_t("Cannot determine web server user. Using %1 instead.", $vs_user)); } if (!$vs_user) { CLIUtils::addError(_t("Cannot determine the user. Please specify one with the --user option.")); return false; } if (!($vs_group = $po_opts->getOption("group"))) { $vs_group = caGetProcessGroupName(); if (!$po_opts->getOption("quiet") && $vs_group) { CLIUtils::addMessage(_t("Determined web server group to be \"%1\"", $vs_group)); } } if (!$vs_group) { CLIUtils::addError(_t("Cannot determine the group. Please specify one with the --group option.")); return false; } if (!$po_opts->getOption("quiet")) { CLIUtils::addMessage(_t("Fixing permissions for the temporary directory (app/tmp) for ownership by \"%1\"...", $vs_user)); } $va_files = caGetDirectoryContentsAsList($vs_path = __CA_APP_DIR__ . '/tmp', true, false, false, true); foreach ($va_files as $vs_path) { chown($vs_path, $vs_user); chgrp($vs_path, $vs_group); chmod($vs_path, 0770); } if (!$po_opts->getOption("quiet")) { CLIUtils::addMessage(_t("Fixing permissions for the media directory (media) for ownership by \"%1\"...", $vs_user)); } $va_files = caGetDirectoryContentsAsList($vs_path = __CA_BASE_DIR__ . '/media', true, false, false, true); foreach ($va_files as $vs_path) { chown($vs_path, $vs_user); chgrp($vs_path, $vs_group); chmod($vs_path, 0775); } if (!$po_opts->getOption("quiet")) { CLIUtils::addMessage(_t("Fixing permissions for the HTMLPurifier definition cache directory (app/lib/core/Parsers/htmlpurifier/standalone/HTMLPurifier/DefinitionCache) for ownership by \"%1\"...", $vs_user)); } $va_files = caGetDirectoryContentsAsList($vs_path = __CA_LIB_DIR__ . '/core/Parsers/htmlpurifier/standalone/HTMLPurifier/DefinitionCache', true, false, false, true); foreach ($va_files as $vs_path) { chown($vs_path, $vs_user); chgrp($vs_path, $vs_group); chmod($vs_path, 0770); } return true; }
/** * @param Zend_Console_Getopt|null $po_opts * @return bool */ public static function reload_ulan_records($po_opts = null) { require_once __CA_MODELS_DIR__ . '/ca_data_importers.php'; if (!($vs_mapping = $po_opts->getOption('mapping'))) { CLIUtils::addError("\t\tNo mapping found. Please use the -m parameter to specify a ULAN mapping."); return false; } if (!ca_data_importers::mappingExists($vs_mapping)) { CLIUtils::addError("\t\tMapping {$vs_mapping} does not exist"); return false; } $vs_log_dir = $po_opts->getOption('log'); $vn_log_level = CLIUtils::getLogLevel($po_opts); $o_db = new Db(); $qr_items = $o_db->query("\n\t\t\t\tSELECT DISTINCT source FROM ca_data_import_events WHERE type_code = 'ULAN'\n\t\t\t"); $va_sources = array(); while ($qr_items->nextRow()) { $vs_source = $qr_items->get('source'); if (!isURL($vs_source)) { continue; } if (!preg_match("/http\\:\\/\\/vocab\\.getty\\.edu\\/ulan\\//", $vs_source)) { continue; } $va_sources[] = $vs_source; } ca_data_importers::importDataFromSource(join(',', $va_sources), $vs_mapping, array('format' => 'ULAN', 'showCLIProgressBar' => true, 'logDirectory' => $vs_log_dir, 'logLevel' => $vn_log_level)); return true; }
/** * */ public static function reload_object_current_locations($po_opts = null) { require_once __CA_LIB_DIR__ . "/core/Db.php"; require_once __CA_MODELS_DIR__ . "/ca_objects.php"; $o_db = new Db(); $t_object = new ca_objects(); $qr_res = $o_db->query("SELECT * FROM ca_objects"); print CLIProgressBar::start($qr_res->numRows(), _t('Starting...')); $vn_c = 0; while ($qr_res->nextRow()) { $vn_object_id = $qr_res->get('object_id'); if ($t_object->load($vn_object_id)) { print CLIProgressBar::next(1, _t('Processing %1', $t_object->getWithTemplate("^ca_objects.preferred_labels.name (^ca_objects.idno)"))); $t_object->deriveCurrentLocationForBrowse(); } else { print CLIProgressBar::next(1, _t('Cannot load object %1', $vn_object_id)); } $vn_c++; } print CLIProgressBar::finish(); CLIUtils::addMessage(_t('Processed %1 objects', $vn_c)); return true; }