/** * Set the maximum value of the progress bar * * @param int $pn_total The maximum value of the progress bar. * @return bool True if value was valid and set, false if not. */ public function setTotal($pn_total, $pa_options = null) { if ($pn_total >= 0) { $this->opn_total = $pn_total; switch ($vs_mode = $this->getMode()) { case 'CLI': CLIProgressBar::setTotal($this->opn_total, $pa_options); break; case 'WebUI': // noop break; default: $vs_output = _t("Invalid mode %1", $vs_mode); break; } return true; } return false; }
/** * */ public static function validate_using_metadata_dictionary_rules($po_opts = null) { require_once __CA_MODELS_DIR__ . '/ca_metadata_dictionary_rules.php'; require_once __CA_MODELS_DIR__ . '/ca_metadata_dictionary_rule_violations.php'; $o_dm = Datamodel::load(); $t_violation = new ca_metadata_dictionary_rule_violations(); $va_rules = ca_metadata_dictionary_rules::getRules(); print CLIProgressBar::start(sizeof($va_rules), _t('Evaluating')); $vn_total_rows = $vn_rule_num = 0; $vn_num_rules = sizeof($va_rules); foreach ($va_rules as $va_rule) { $vn_rule_num++; $va_expression_tags = caGetTemplateTags($va_rule['expression']); $va_tmp = explode(".", $va_rule['bundle_name']); if (!($t_instance = $o_dm->getInstanceByTableName($va_tmp[0]))) { CLIUtils::addError(_t("Table for bundle %1 is not valid", $va_tmp[0])); continue; } $vs_bundle_name_proc = str_replace("{$vs_table_name}.", "", $va_rule['bundle_name']); $vn_table_num = $t_instance->tableNum(); $qr_records = call_user_func_array(($vs_table_name = $t_instance->tableName()) . "::find", array(array('deleted' => 0), array('returnAs' => 'searchResult'))); if (!$qr_records) { continue; } $vn_total_rows += $qr_records->numHits(); CLIProgressBar::setTotal($vn_total_rows); $vn_count = 0; while ($qr_records->nextHit()) { $vn_count++; print CLIProgressBar::next(1, _t("Rule %1 [%2/%3]: record %4", $va_rule['rule_settings']['label'], $vn_rule_num, $vn_num_rules, $vn_count)); $t_violation->clear(); $vn_id = $qr_records->getPrimaryKey(); $vb_skip = !$t_instance->hasBundle($va_rule['bundle_name'], $qr_records->get('type_id')); if (!$vb_skip) { // create array of values present in rule $va_row = array($va_rule['bundle_name'] => $vs_val = $qr_records->get($va_rule['bundle_name'])); foreach ($va_expression_tags as $vs_tag) { $va_row[$vs_tag] = $qr_records->get($vs_tag); } } // is there a violation recorded for this rule and row? if ($t_found = ca_metadata_dictionary_rule_violations::find(array('rule_id' => $va_rule['rule_id'], 'row_id' => $vn_id, 'table_num' => $vn_table_num), array('returnAs' => 'firstModelInstance'))) { $t_violation = $t_found; } if (!$vb_skip && ExpressionParser::evaluate($va_rule['expression'], $va_row)) { // violation if ($t_violation->getPrimaryKey()) { $t_violation->setMode(ACCESS_WRITE); $t_violation->update(); } else { $t_violation->setMode(ACCESS_WRITE); $t_violation->set('rule_id', $va_rule['rule_id']); $t_violation->set('table_num', $t_instance->tableNum()); $t_violation->set('row_id', $qr_records->getPrimaryKey()); $t_violation->insert(); } } else { if ($t_violation->getPrimaryKey()) { $t_violation->delete(true); // remove violation } } } } print CLIProgressBar::finish(); }