public function mergeALL(ArrayObject $projectStructure, $renewPassword = false) { $query_job = "SELECT *\n FROM jobs\n WHERE id = %u\n ORDER BY job_first_segment"; $query_job = sprintf($query_job, $projectStructure['job_to_merge']); //$projectStructure[ 'job_to_split' ] $rows = $this->dbHandler->fetch_array($query_job); //get the min and $first_job = reset($rows); $job_first_segment = $first_job['job_first_segment']; //the max segment from job list $last_job = end($rows); $job_last_segment = $last_job['job_last_segment']; //change values of first job $first_job['job_first_segment'] = $job_first_segment; // redundant $first_job['job_last_segment'] = $job_last_segment; //merge TM keys: preserve only owner's keys $tm_keys = array(); foreach ($rows as $chunk_info) { $tm_keys[] = $chunk_info['tm_keys']; } try { $owner_tm_keys = TmKeyManagement_TmKeyManagement::getOwnerKeys($tm_keys); /** * @var $owner_key TmKeyManagement_TmKeyStruct */ foreach ($owner_tm_keys as $i => $owner_key) { $owner_tm_keys[$i] = $owner_key->toArray(); } $first_job['tm_keys'] = json_encode($owner_tm_keys); } catch (Exception $e) { Log::doLog(__METHOD__ . " -> Merge Jobs error - TM key problem: " . $e->getMessage()); } $oldPassword = $first_job['password']; if ($renewPassword) { $first_job['password'] = self::_generatePassword(); } $_data = array(); foreach ($first_job as $field => $value) { $_data[] = "`{$field}`='{$value}'"; } //---------------------------------------------------- $queries = array(); $queries[] = "UPDATE jobs SET " . implode(", \n", $_data) . " WHERE id = {$first_job['id']} AND password = '******'"; //ose old password //delete all old jobs $queries[] = "DELETE FROM jobs WHERE id = {$first_job['id']} AND password != '{$first_job['password']}' "; //use new password foreach ($queries as $query) { $res = $this->dbHandler->query($query); if ($res !== true) { $msg = "Failed to merge job " . $rows[0]['id'] . " from " . count($rows) . " chunks\n"; $msg .= "Tried to perform SQL: \n" . print_r($queries, true) . " \n\n"; $msg .= "Failed Statement is: \n" . print_r($query, true) . "\n"; $msg .= "Original Status for rebuild job and project was: \n" . print_r($rows, true) . "\n"; Utils::sendErrMailReport($msg); throw new Exception('Failed to merge jobs, project damaged. Contact Matecat Support to rebuild project.', -8); } } $wCountManager = new WordCount_Counter(); $wCountManager->initializeJobWordCount($first_job['id'], $first_job['password']); Shop_Cart::getInstance('outsource_to_external_cache')->emptyCart(); }
/** * if the description is empty, get cascading default descriptions * * First get the job key description, if empty, get the job owner email * * @param $key * * @return null|string * @throws Exception */ private function __getDefaultDescription($key) { $description = null; $ownerKeys = TmKeyManagement_TmKeyManagement::getOwnerKeys(array($this->jobData['tm_keys'])); //search the current key $currentKey = null; for ($i = 0; $i < count($ownerKeys); $i++) { if ($ownerKeys[$i]->key == $key) { $description = $ownerKeys[$i]->name; } } //return if something was found, avoid other computations if (!empty($description)) { return $description; } return $this->jobData['owner']; }
public function doAction() { if (empty($this->source_lang)) { $this->result['errors'][] = array("code" => -1, "message" => "missing source_lang"); } if (empty($this->target_lang)) { $this->result['errors'][] = array("code" => -2, "message" => "missing target_lang"); } if (empty($this->source)) { $this->result['errors'][] = array("code" => -3, "message" => "missing source"); } if (empty($this->target)) { $this->result['errors'][] = array("code" => -4, "message" => "missing target"); } //get Job Infos $job_data = getJobData((int) $this->id_job, $this->password); $pCheck = new AjaxPasswordCheck(); //check for Password correctness if (empty($job_data) || !$pCheck->grantJobAccessByJobData($job_data, $this->password)) { $this->result['errors'][] = array("code" => -10, "message" => "wrong password"); return; } $this->tm_keys = $job_data['tm_keys']; $this->checkLogin(); $tms = Engine::getInstance($job_data['id_tms']); $config = $tms->getConfigStruct(); // $config = TMS::getConfigStruct(); $config['segment'] = CatUtils::view2rawxliff($this->source); $config['translation'] = CatUtils::view2rawxliff($this->target); $config['source'] = $this->source_lang; $config['target'] = $this->target_lang; $config['email'] = "*****@*****.**"; $config['id_user'] = array(); //get job's TM keys try { $tm_keys = $this->tm_keys; if (self::isRevision()) { $this->userRole = TmKeyManagement_Filter::ROLE_REVISOR; } elseif ($this->userMail == $job_data['owner']) { $tm_keys = TmKeyManagement_TmKeyManagement::getOwnerKeys(array($tm_keys), 'r', 'tm'); $tm_keys = json_encode($tm_keys); } //get TM keys with read grants $tm_keys = TmKeyManagement_TmKeyManagement::getJobTmKeys($tm_keys, 'r', 'tm', $this->uid, $this->userRole); if (is_array($tm_keys) && !empty($tm_keys)) { foreach ($tm_keys as $tm_key) { $config['id_user'][] = $tm_key->key; } } } catch (Exception $e) { $this->result['errors'][] = array("code" => -11, "message" => "Cannot retrieve TM keys info."); return; } //prepare the errors report $set_code = array(); /** * @var $tm_key TmKeyManagement_TmKeyStruct */ //if there's no key if (empty($tm_keys)) { //try deleting anyway, it may be a public segment and it may work $TMS_RESULT = $tms->delete($config); $set_code[] = $TMS_RESULT; } else { //loop over the list of keys foreach ($tm_keys as $tm_key) { //issue a separate call for each key $config['id_user'] = $tm_key->key; $TMS_RESULT = $tms->delete($config); $set_code[] = $TMS_RESULT; } } $set_successful = true; if (array_search(false, $set_code, true)) { //There's an errors $set_successful = false; } $this->result['data'] = $set_successful ? "OK" : null; $this->result['code'] = $set_successful; }
/** * @param $start int * @param $step int * @param $search_in_pname string|null * @param $search_source string|null * @param $search_target string|null * @param $search_status string|null * @param $search_onlycompleted bool * @param $filter_enabled bool * @param $project_id int * * @return array * @throws Exception */ public static function queryProjects($start, $step, $search_in_pname, $search_source, $search_target, $search_status, $search_onlycompleted, $filter_enabled, $project_id) { $data = getProjects($start, $step, $search_in_pname, $search_source, $search_target, $search_status, $search_onlycompleted, $filter_enabled, $project_id); $projects = array(); $projectIDs = array(); $project2info = array(); //get project IDs from projects array foreach ($data as $item) { $projectIDs[] = $item['pid']; } if (empty($projectIDs)) { return array(); } //get job data using job IDs $jobData = getJobsFromProjects($projectIDs, $search_source, $search_target, $search_status, $search_onlycompleted); $lang_handler = Langs_Languages::getInstance(); //Prepare job data $project2jobChunk = array(); foreach ($jobData as $job_array) { $job = array(); /** * Assign job extracted variables */ $job['id'] = $job_array['id']; $job['pid'] = $job_array['id_project']; $job['password'] = $job_array['password']; $job['source'] = $job_array['source']; $job['target'] = $job_array['target']; $job['subject'] = $job_array['subject']; $job['sourceTxt'] = $lang_handler->getLocalizedName($job['source']); $job['targetTxt'] = $lang_handler->getLocalizedName($job['target']); $job['create_date'] = $job_array['create_date']; $job['formatted_create_date'] = self::formatJobDate($job_array['create_date']); $job['job_first_segment'] = $job_array['job_first_segment']; $job['job_last_segment'] = $job_array['job_last_segment']; $job['mt_engine_name'] = $job_array['name']; $job['id_tms'] = $job_array['id_tms']; //generate and set job stats $jobStats = new WordCount_Struct(); $jobStats->setDraftWords($job_array['DRAFT']); $jobStats->setRejectedWords($job_array['REJECT']); $jobStats->setTranslatedWords($job_array['TRANSLATED']); $jobStats->setApprovedWords($job_array['APPROVED']); //These would be redundant in response. Unset them. unset($job_array['DRAFT']); unset($job_array['REJECT']); unset($job_array['TRANSLATED']); unset($job_array['APPROVED']); $job['stats'] = CatUtils::getFastStatsForJob($jobStats); //generate and set job tm_keys $tm_keys_json = $job_array['tm_keys']; $tm_keys_json = TmKeyManagement_TmKeyManagement::getOwnerKeys(array($tm_keys_json)); $tm_keys = array(); foreach ($tm_keys_json as $tm_key_struct) { /** * @var $tm_key_struct TmKeyManagement_TmKeyStruct */ $tm_keys[] = array("key" => $tm_key_struct->key, "r" => $tm_key_struct->r ? 'Lookup' : ' ', "w" => $tm_key_struct->w ? 'Update' : ''); } $job['private_tm_key'] = json_encode($tm_keys); $job['disabled'] = $job_array['status_owner'] == Constants_JobStatus::STATUS_CANCELLED ? "disabled" : ""; $job['status'] = $job_array['status_owner']; //These vars will be used in projects loop for some flag evaluation. $project2info[$job['pid']]['status'][] = $job['status']; $project2info[$job['pid']]['mt_engine_name'] = $job['mt_engine_name']; $project2info[$job['pid']]['id_tms'] = $job['id_tms']; $project2jobChunk[$job['pid']][$job['id']][$job['job_first_segment']] = $job; } //Prepare project data foreach ($data as $item) { $project = array(); $project['id'] = $item['pid']; $project['name'] = $item['name']; $project['jobs'] = array(); $project['no_active_jobs'] = true; $project['has_cancelled'] = 0; $project['has_archived'] = 0; $project['password'] = $item['password']; $project['tm_analysis'] = number_format($item['tm_analysis_wc'], 0, ".", ","); $project['jobs'] = $project2jobChunk[$project['id']]; $project['no_active_jobs'] = $project['no_active_jobs'] ? ' allCancelled' : ''; $project2info[$project['id']]['status'] = array_unique($project2info[$project['id']]['status']); $project['no_active_jobs'] = !in_array(Constants_JobStatus::STATUS_ACTIVE, $project2info[$project['id']]['status']) ? ' allCancelled' : ''; $project['has_cancelled'] = in_array(Constants_JobStatus::STATUS_CANCELLED, $project2info[$project['id']]['status']); $project['has_archived'] = in_array(Constants_JobStatus::STATUS_ARCHIVED, $project2info[$project['id']]['status']); $project['mt_engine_name'] = $project2info[$project['id']]['mt_engine_name']; $project['id_tms'] = $project2info[$project['id']]['id_tms']; $projects[] = $project; } return $projects; }