17 require_once SQ_SYSTEM_ROOT.
'/core/hipo/hipo_job.inc';
61 public static function paintConfig(&$o, $class, $write_access)
64 $o->openField(translate(
'hipo_rename_thesaurus_term'));
67 text_box($class.
'[SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD]', SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD, 5);
69 echo SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD;
71 echo
' '.translate(
'terms');
87 'SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD' => Array(
'editable' => 1,
'default' => 1),
106 $code_name = parent::getCodeName();
107 return $code_name.
'-'.md5(implode(
',', $this->_running_vars[
'assetids']));
120 return translate(
'hipo_rename_thesaurus_term');
136 'name' => translate(
'hipo_rename_thesaurus_term'),
137 'function_call' => Array(
138 'process_function' =>
'processRenameTerm',
140 'running_mode' =>
'server',
142 'allow_cancel' => TRUE,
148 'name' => translate(
'hipo_generating_metadata_files'),
150 'init_details_function' =>
'regenerateMetadata',
152 'running_mode' =>
'server',
154 'allow_cancel' => TRUE,
175 if (SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD == 0)
return 0;
176 if (!isset($this->_running_vars[
'total_term_count']) || empty($this->_running_vars[
'total_term_count'])) {
179 return (($this->_running_vars[
'total_term_count'] / SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD) * 100);
192 $hh = $GLOBALS[
'SQ_SYSTEM']->getHipoHerder();
193 $step_data = Array();
197 $this->
_addError(
'updating thesaurus terms failed');
202 $schemas = array_keys($this->_running_vars[
'schemas']);
203 if (empty($schemas)) {
208 $vars = Array(
'schemaids' => $schemas);
211 $errors = $hh->freestyleHipo(
'hipo_job_regenerate_metadata', $vars);
212 if (!empty($errors)) {
213 $this->
_addError(
'regenerating metadata failed');
232 if (empty($this->_running_vars[
'thesaurusid'])) {
233 trigger_localised_error(
'HIPO0085', E_USER_WARNING);
238 if (empty($this->_running_vars[
'terms'])) {
243 $thesaurus = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->_running_vars[
'thesaurusid'],
'', TRUE);
244 if (is_null($thesaurus) || $thesaurus->type() !=
'thesaurus') {
245 trigger_localised_error(
'HIPO0086', E_USER_WARNING);
249 if (!isset($this->_running_vars[
'assetids'])) {
250 $this->_running_vars[
'assetids'] = Array();
253 $cascade = array_get_index($this->_running_vars,
'cascade', FALSE);
254 if (!isset($this->_running_vars[
'assetids'])) {
255 $this->_running_vars[
'assetids'] = Array();
256 }
else if (!empty($this->_running_vars[
'assetids']) && $cascade) {
258 foreach ($this->_running_vars[
'assetids'] as $assetid) {
259 array_splice($assetids, count($assetids), 0, array_keys($GLOBALS[
'SQ_SYSTEM']->am->getChildren($assetid)));
261 array_splice($this->_running_vars[
'assetids'], count($this->_running_vars[
'assetids']), 0, $assetids);
267 if (!isset($this->_running_vars[
'fieldids']) || empty($this->_running_vars[
'fieldids'])) {
271 return parent::prepare();
288 $job_type =
'hipo_job_regenerate_metadata';
290 $options[
'auto_complete'] = TRUE;
292 $running_vars = Array(
'schemaids' => array_keys($this->_running_vars[
'schemas']));
309 if (!isset($this->_running_vars[
'todo_terms'])) {
310 $this->_running_vars[
'todo_terms'] = $this->_running_vars[
'terms'];
311 $this->_running_vars[
'done_term_count'] = 0;
312 $this->_running_vars[
'todo_term_count'] = count($this->_running_vars[
'todo_terms']);
313 $this->_running_vars[
'total_term_count'] = count($this->_running_vars[
'todo_terms']);
317 $term_info = array_slice($this->_running_vars[
'todo_terms'], -1, 1, TRUE);
318 $old_term = key($term_info);
319 $new_term = $term_info[$old_term];
322 if (!empty($old_term) || !empty($new_term)) {
325 $field_chunks = array_chunk($this->_running_vars[
'fieldids'], $chunk_size);
326 foreach ($field_chunks as $field_chunk) {
328 foreach ($field_chunk as $index => $field_assetid) {
334 $sql =
"SELECT value, assetid, fieldid, contextid FROM sq_ast_mdata_val WHERE value like '$old_term,%' OR value like '%,$old_term' OR value like '%,$old_term,%'";
337 if (!empty($results)) {
339 foreach($results as $index => $result) {
340 $asset_id = $result[
'assetid'];
341 $value = $result[
'value'];
342 $field_id = $result[
'fieldid'];
343 $contextid= $result[
'contextid'];
345 $pattern_1 =
'/(.*)'.$old_term.
'$/';
346 $pattern_2 =
'/^'.$old_term.
'(.*)/';
347 $pattern_3 =
'/(.*)'.$old_term.
'(.*)/';
349 $replacement_1 =
'$1'.$new_term;
350 $replacement_2 = $new_term.
'$1';
351 $replacement_3 =
'$1'.$new_term.
'$2';
353 if (preg_match($pattern_2, $value)) {
354 $new_value = preg_replace($pattern_2, $replacement_2, $value);
355 }
else if (preg_match($pattern_1, $value)) {
356 $new_value = preg_replace($pattern_1, $replacement_1, $value);
358 $new_value = preg_replace($pattern_3, $replacement_3, $value);
363 $sql =
'UPDATE sq_ast_mdata_val SET value=:new_value WHERE TO_CHAR(value)=:old_value AND contextid=:contextid AND fieldid=:fieldid AND assetid=:assetid';
365 $sql =
'UPDATE sq_ast_mdata_val SET value=:new_value WHERE value=:old_value AND contextid=:contextid AND fieldid=:fieldid AND assetid=:assetid';
368 if(!empty($this->_running_vars[
'assetids'])) {
369 foreach ($this->_running_vars[
'assetids'] as $assetid) {
378 }
catch (Exception $e) {
379 throw new Exception(
'DB Error: '.$e->getMessage());
391 }
catch (Exception $e) {
392 throw new Exception(
'DB Error: '.$e->getMessage());
400 $sql =
'UPDATE sq_ast_mdata_val SET value=:new_term WHERE TO_CHAR(value)=:old_term AND fieldid IN ('.implode(
',', $field_chunk).
')';
402 $sql =
'UPDATE sq_ast_mdata_val SET value=:new_term WHERE value=:old_term AND fieldid IN ('.implode(
',', $field_chunk).
')';
404 if (!empty($this->_running_vars[
'assetids'])) {
406 $sql .=
' AND assetid=:assetid';
407 foreach ($this->_running_vars[
'assetids'] as $assetid) {
414 }
catch (Exception $e) {
415 throw new Exception(
'DB Error: '.$e->getMessage());
425 }
catch (Exception $e) {
426 throw new Exception(
'DB Error: '.$e->getMessage());
433 $this->_running_vars[
'done_term_count']++;
434 $dump = array_pop($this->_running_vars[
'todo_terms']);
437 if (empty($this->_running_vars[
'todo_terms'])) {
438 unset($this->_running_vars[
'done_term_count']);
439 unset($this->_running_vars[
'todo_term_count']);
440 unset($this->_running_vars[
'total_term_count']);
441 $step_data[
'percent_done'] = 100;
442 $step_data[
'complete'] = TRUE;
444 $step_data[
'percent_done'] = (int)(($this->_running_vars[
'done_term_count'] / $this->_running_vars[
'total_term_count']) * 100);
445 $step_data[
'complete'] = FALSE;
462 $current_schemas = $GLOBALS[
'SQ_SYSTEM']->am->getTypeAssetids(
'metadata_schema', TRUE);
463 foreach ($current_schemas as $schemaid) {
464 $schema = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($schemaid,
'metadata_schema', TRUE);
465 if (!is_null($schema)) {
466 $this->_running_vars[
'schemas'][$schemaid] = Array();
467 $fields = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($schemaid,
'metadata_field_thesaurus');
468 foreach ($fields as $fieldid => $field_type) {
469 $field = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($fieldid,
'metadata_field_thesaurus', TRUE);
470 if (!is_null($field)) {
471 $field_thesaurus = $field->attr(
'thesaurus_assetid');
472 if ($field_thesaurus == $this->_running_vars[
'thesaurusid']) {
474 $this->_running_vars[
'schemas'][$schemaid][] = $fieldid;
475 $this->_running_vars[
'fieldids'][] = $fieldid;
481 if (count($this->_running_vars[
'schemas'][$schemaid]) == 0) {
482 unset($this->_running_vars[
'schemas'][$schemaid]);