/** * Log error to console and log facilities and exit * @param string $ps_message the error message */ function caCLILogCritError($ps_message) { CLIUtils::addError("\t" . $ps_message . PHP_EOL); caCLILog($ps_message, Zend_Log::ERR); exit(255); }
/** * 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 check_media_fixity($po_opts = null) { require_once __CA_LIB_DIR__ . "/core/Db.php"; require_once __CA_MODELS_DIR__ . "/ca_object_representations.php"; $ps_file_path = strtolower((string) $po_opts->getOption('file')); $ps_format = strtolower((string) $po_opts->getOption('format')); if (!in_array($ps_format, array('text', 'tab', 'csv'))) { $ps_format = 'text'; } $o_db = new Db(); $o_dm = Datamodel::load(); $t_rep = new ca_object_representations(); $vs_report_output = join($ps_format == 'tab' ? "\t" : ",", array(_t('Type'), _t('Error'), _t('Name'), _t('ID'), _t('Version'), _t('File path'), _t('Expected MD5'), _t('Actual MD5'))) . "\n"; // Verify object representations $qr_reps = $o_db->query("SELECT representation_id, idno, media FROM ca_object_representations WHERE deleted = 0"); print CLIProgressBar::start($vn_rep_count = $qr_reps->numRows(), _t('Checking object representations')) . "\n"; $vn_errors = 0; while ($qr_reps->nextRow()) { $vn_representation_id = $qr_reps->get('representation_id'); print CLIProgressBar::next(1, _t("Checking representation media %1", $vn_representation_id)); $va_media_versions = $qr_reps->getMediaVersions('media'); foreach ($va_media_versions as $vs_version) { $vs_path = $qr_reps->getMediaPath('media', $vs_version); $vs_database_md5 = $qr_reps->getMediaInfo('media', $vs_version, 'MD5'); $vs_file_md5 = md5_file($vs_path); if ($vs_database_md5 !== $vs_file_md5) { $t_rep->load($vn_representation_id); $vs_message = _t("[Object representation][MD5 mismatch] %1; version %2 [%3]", $t_rep->get("ca_objects.preferred_labels.name") . " (" . $t_rep->get("ca_objects.idno") . "); representation_id={$vn_representation_id}", $vs_version, $vs_path); switch ($ps_format) { case 'text': default: $vs_report_output .= "{$vs_message}\n"; break; case 'tab': case 'csv': $va_log = array(_t('Object representation'), "MD5 mismatch", caEscapeForDelimitedOutput($t_rep->get("ca_objects.preferred_labels.name") . " (" . $t_rep->get("ca_objects.idno") . ")"), $vn_representation_id, $vs_version, $vs_path, $vs_database_md5, $vs_file_md5); $vs_report_output .= join($ps_format == 'tab' ? "\t" : ",", $va_log) . "\n"; break; } CLIUtils::addError($vs_message); $vn_errors++; } } } print CLIProgressBar::finish(); CLIUtils::addMessage(_t('%1 errors for %2 representations', $vn_errors, $vn_rep_count)); // get all Media elements $va_elements = ca_metadata_elements::getElementsAsList(false, null, null, true, false, true, array(16)); // 16=media if (is_array($va_elements) && sizeof($va_elements)) { if (is_array($va_element_ids = caExtractValuesFromArrayList($va_elements, 'element_id', array('preserveKeys' => false))) && sizeof($va_element_ids)) { $qr_c = $o_db->query("\n\t\t\t\t\t\tSELECT count(*) c \n\t\t\t\t\t\tFROM ca_attribute_values\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\telement_id in (?)\n\t\t\t\t\t", array($va_element_ids)); if ($qr_c->nextRow()) { $vn_count = $qr_c->get('c'); } else { $vn_count = 0; } print CLIProgressBar::start($vn_count, _t('Checking attribute media')); $vn_errors = 0; foreach ($va_elements as $vs_element_code => $va_element_info) { $qr_vals = $o_db->query("SELECT value_id FROM ca_attribute_values WHERE element_id = ?", (int) $va_element_info['element_id']); $va_vals = $qr_vals->getAllFieldValues('value_id'); foreach ($va_vals as $vn_value_id) { $t_attr_val = new ca_attribute_values($vn_value_id); if ($t_attr_val->getPrimaryKey()) { $t_attr_val->setMode(ACCESS_WRITE); $t_attr_val->useBlobAsMediaField(true); print CLIProgressBar::next(1, _t("Checking attribute media %1", $vn_value_id)); $va_media_versions = $t_attr_val->getMediaVersions('value_blob'); foreach ($va_media_versions as $vs_version) { $vs_path = $t_attr_val->getMediaPath('value_blob', $vs_version); $vs_database_md5 = $t_attr_val->getMediaInfo('value_blob', $vs_version, 'MD5'); $vs_file_md5 = md5_file($vs_path); if ($vs_database_md5 !== $vs_file_md5) { $t_attr = new ca_attributes($vn_attribute_id = $t_attr_val->get('attribute_id')); $vs_label = "attribute_id={$vn_attribute_id}; value_id={$vn_value_id}"; if ($t_instance = $o_dm->getInstanceByTableNum($t_attr->get('table_num'), true)) { if ($t_instance->load($t_attr->get('row_id'))) { $vs_label = $t_instance->get($t_instance->tableName() . '.preferred_labels'); if ($vs_idno = $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD'))) { $vs_label .= " ({$vs_label})"; } } } $vs_message = _t("[Media attribute][MD5 mismatch] %1; value_id=%2; version %3 [%4]", $vs_label, $vn_value_id, $vs_version, $vs_path); switch ($ps_format) { case 'text': default: $vs_report_output .= "{$vs_message}\n"; break; case 'tab': case 'csv': $va_log = array(_t('Media attribute'), _t("MD5 mismatch"), caEscapeForDelimitedOutput($vs_label), $vn_value_id, $vs_version, $vs_path, $vs_database_md5, $vs_file_md5); $vs_report_output .= join($ps_format == 'tab' ? "\t" : ",", $va_log); break; } CLIUtils::addError($vs_message); $vn_errors++; } } } } } print CLIProgressBar::finish(); CLIUtils::addMessage(_t('%1 errors for %2 attributes', $vn_errors, $vn_rep_count)); } } // get all File elements $va_elements = ca_metadata_elements::getElementsAsList(false, null, null, true, false, true, array(15)); // 15=file if (is_array($va_elements) && sizeof($va_elements)) { if (is_array($va_element_ids = caExtractValuesFromArrayList($va_elements, 'element_id', array('preserveKeys' => false))) && sizeof($va_element_ids)) { $qr_c = $o_db->query("\n\t\t\t\t\t\tSELECT count(*) c \n\t\t\t\t\t\tFROM ca_attribute_values\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\telement_id in (?)\n\t\t\t\t\t", array($va_element_ids)); if ($qr_c->nextRow()) { $vn_count = $qr_c->get('c'); } else { $vn_count = 0; } print CLIProgressBar::start($vn_count, _t('Checking attribute files')); $vn_errors = 0; foreach ($va_elements as $vs_element_code => $va_element_info) { $qr_vals = $o_db->query("SELECT value_id FROM ca_attribute_values WHERE element_id = ?", (int) $va_element_info['element_id']); $va_vals = $qr_vals->getAllFieldValues('value_id'); foreach ($va_vals as $vn_value_id) { $t_attr_val = new ca_attribute_values($vn_value_id); if ($t_attr_val->getPrimaryKey()) { $t_attr_val->setMode(ACCESS_WRITE); $t_attr_val->useBlobAsFileField(true); print CLIProgressBar::next(1, _t("Checking attribute file %1", $vn_value_id)); $vs_path = $t_attr_val->getFilePath('value_blob'); $vs_database_md5 = $t_attr_val->getFileInfo('value_blob', 'MD5'); $vs_file_md5 = md5_file($vs_path); if ($vs_database_md5 !== $vs_file_md5) { $t_attr = new ca_attributes($vn_attribute_id = $t_attr_val->get('attribute_id')); $vs_label = "attribute_id={$vn_attribute_id}; value_id={$vn_value_id}"; if ($t_instance = $o_dm->getInstanceByTableNum($t_attr->get('table_num'), true)) { if ($t_instance->load($t_attr->get('row_id'))) { $vs_label = $t_instance->get($t_instance->tableName() . '.preferred_labels'); if ($vs_idno = $t_instance->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD'))) { $vs_label .= " ({$vs_label})"; } } } $vs_message = _t("[File attribute][MD5 mismatch] %1; value_id=%2; version %3 [%4]", $vs_label, $vn_value_id, $vs_version, $vs_path); switch ($ps_format) { case 'text': default: $vs_report_output .= "{$vs_message}\n"; break; case 'tab': case 'csv': $va_log = array(_t('File attribute'), _t("MD5 mismatch"), caEscapeForDelimitedOutput($vs_label), $vn_value_id, $vs_version, $vs_path, $vs_database_md5, $vs_file_md5); $vs_report_output .= join($ps_format == 'tab' ? "\t" : ",", $va_log); break; } CLIUtils::addError($vs_message); $vn_errors++; } } } } print CLIProgressBar::finish(); CLIUtils::addMessage(_t('%1 errors for %2 attributes', $vn_errors, $vn_rep_count)); } } if ($ps_file_path) { file_put_contents($ps_file_path, $vs_report_output); } return true; }