Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_rename_thesaurus_term.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
37 {
38 
39 
45  function HIPO_Job_Rename_Thesaurus_Term($code_name='')
46  {
47  $this->HIPO_Job($code_name);
48 
49  }//end constructor
50 
51 
61  public static function paintConfig(&$o, $class, $write_access)
62  {
63  // metadata regeneration threshhold HIPO config entry
64  $o->openField(translate('hipo_rename_thesaurus_term'));
65 
66  if ($write_access) {
67  text_box($class.'[SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD]', SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD, 5);
68  } else {
69  echo SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD;
70  }
71  echo ' '.translate('terms');
72 
73  $o->closeField();
74 
75  }//end paintConfig()
76 
77 
84  public static function getConfigVars()
85  {
86  return Array(
87  'SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD' => Array('editable' => 1, 'default' => 1),
88  );
89 
90  }//end getConfigVars()
91 
92 
104  function getCodeName()
105  {
106  $code_name = parent::getCodeName();
107  return $code_name.'-'.md5(implode(',', $this->_running_vars['assetids']));
108 
109  }//end getCodeName()
110 
111 
118  function getHipoName()
119  {
120  return translate('hipo_rename_thesaurus_term');
121 
122  }//end getHipoName()
123 
124 
133  {
134  return Array(
135  Array(
136  'name' => translate('hipo_rename_thesaurus_term'),
137  'function_call' => Array(
138  'process_function' => 'processRenameTerm',
139  ),
140  'running_mode' => 'server',
141  'auto_step' => TRUE,
142  'allow_cancel' => TRUE,
143  'percent_done' => 0,
144  'complete' => FALSE,
145  'message' => '',
146  ),
147  Array(
148  'name' => translate('hipo_generating_metadata_files'),
149  'hipo_job' => Array(
150  'init_details_function' => 'regenerateMetadata',
151  ),
152  'running_mode' => 'server',
153  'auto_step' => TRUE,
154  'allow_cancel' => TRUE,
155  'percent_done' => 0,
156  'complete' => FALSE,
157  'message' => '',
158  ),
159  );
160 
161  }//end getInitialStepData()
162 
163 
174  {
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'])) {
177  return 0;
178  }
179  return (($this->_running_vars['total_term_count'] / SQ_HIPO_RENAME_THESAURUS_TERM_THRESHOLD) * 100);
180 
181  }//end getThresholdPercentageRequired()
182 
183 
190  function freestyle()
191  {
192  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
193  $step_data = Array();
194 
195  // do the rename
196  if (!$this->processRenameTerm($step_data, '')) {
197  $this->_addError('updating thesaurus terms failed');
198  return FALSE;
199  }//end if
200 
201  // Nothing done?
202  $schemas = array_keys($this->_running_vars['schemas']);
203  if (empty($schemas)) {
204  return TRUE;
205  }//end if
206 
207  // Vars for the HIPO
208  $vars = Array('schemaids' => $schemas);
209 
210  // Regenerate metadata
211  $errors = $hh->freestyleHipo('hipo_job_regenerate_metadata', $vars);
212  if (!empty($errors)) {
213  $this->_addError('regenerating metadata failed');
214  return FALSE;
215  }//end if
216 
217  // All good
218  return TRUE;
219 
220  }//end freestyle()
221 
222 
229  function prepare()
230  {
231  // No thesaurus specified
232  if (empty($this->_running_vars['thesaurusid'])) {
233  trigger_localised_error('HIPO0085', E_USER_WARNING);
234  return FALSE;
235  }//end if
236 
237  // No terms to change, bailout
238  if (empty($this->_running_vars['terms'])) {
239  return FALSE;
240  }//end if
241 
242  // No thesaurus found
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);
246  return FALSE;
247  }//end if
248 
249  if (!isset($this->_running_vars['assetids'])) {
250  $this->_running_vars['assetids'] = Array();
251  }//end if
252 
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) {
257  $assetids = Array();
258  foreach ($this->_running_vars['assetids'] as $assetid) {
259  array_splice($assetids, count($assetids), 0, array_keys($GLOBALS['SQ_SYSTEM']->am->getChildren($assetid)));
260  }//end foreach
261  array_splice($this->_running_vars['assetids'], count($this->_running_vars['assetids']), 0, $assetids);
262  }//end if
263 
264  // Find all the schemas/fields needed for this operation
265  $this->_findSchemas();
266 
267  if (!isset($this->_running_vars['fieldids']) || empty($this->_running_vars['fieldids'])) {
268  return FALSE;
269  }//end if
270 
271  return parent::prepare();
272 
273  }//end prepare()
274 
275 
286  function regenerateMetadata(&$job_type, &$running_vars, &$options)
287  {
288  $job_type = 'hipo_job_regenerate_metadata';
289 
290  $options['auto_complete'] = TRUE;
291 
292  $running_vars = Array('schemaids' => array_keys($this->_running_vars['schemas']));
293 
294  }//end regenerateMetadata()
295 
296 
306  function processRenameTerm(&$step_data, $prefix)
307  {
308  // Just starting let's gear up
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']);
314  }//end if
315 
316  // Grab the current term
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];
320 
321  // If either is empty, continue to the next one
322  if (!empty($old_term) || !empty($new_term)) {
323  // Do the rename per asset and play nice with ORACLE
324  $chunk_size = 1000;
325  $field_chunks = array_chunk($this->_running_vars['fieldids'], $chunk_size);
326  foreach ($field_chunks as $field_chunk) {
327  // Quoting Shakespeare
328  foreach ($field_chunk as $index => $field_assetid) {
329  $field_chunk[$index] = MatrixDAL::quote($field_assetid);
330  }//end foreach
331 
332  // bug fix #4430 Thesaurus Terms Update this term system wide Issue
333  // if we have more than one term applied, then we do it differently
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,%'";
335 
336  $results = MatrixDAL::executeSqlAssoc($sql);
337  if (!empty($results)) {
338 
339  foreach($results as $index => $result) {
340  $asset_id = $result['assetid'];
341  $value = $result['value'];
342  $field_id = $result['fieldid'];
343  $contextid= $result['contextid'];
344 
345  $pattern_1 = '/(.*)'.$old_term.'$/';
346  $pattern_2 = '/^'.$old_term.'(.*)/';
347  $pattern_3 = '/(.*)'.$old_term.'(.*)/';
348 
349  $replacement_1 = '$1'.$new_term;
350  $replacement_2 = $new_term.'$1';
351  $replacement_3 = '$1'.$new_term.'$2';
352 
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);
357  } else {
358  $new_value = preg_replace($pattern_3, $replacement_3, $value);
359  }
360 
361  // Run the Query against the current assetid
362  if (MatrixDAL::getDbType() === 'oci') {
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';
364  } else {
365  $sql = 'UPDATE sq_ast_mdata_val SET value=:new_value WHERE value=:old_value AND contextid=:contextid AND fieldid=:fieldid AND assetid=:assetid';
366  }//end if
367 
368  if(!empty($this->_running_vars['assetids'])) {
369  foreach ($this->_running_vars['assetids'] as $assetid) {
370  try{
371  $query = MatrixDAL::preparePdoQuery($sql);
372  MatrixDAL::bindValueToPdo($query, 'new_value', $new_value);
373  MatrixDAL::bindValueToPdo($query, 'old_value', $value);
374  MatrixDAL::bindValueToPdo($query, 'contextid', $contextid);
375  MatrixDAL::bindValueToPdo($query, 'fieldid', $field_id);
376  MatrixDAL::bindValueToPdo($query, 'assetid', $assetid);
377  MatrixDAL::execPdoQuery($query);
378  } catch (Exception $e) {
379  throw new Exception('DB Error: '.$e->getMessage());
380  }
381  }
382  } else {
383  try{
384  $query = MatrixDAL::preparePdoQuery($sql);
385  MatrixDAL::bindValueToPdo($query, 'new_value', $new_value);
386  MatrixDAL::bindValueToPdo($query, 'old_value', $value);
387  MatrixDAL::bindValueToPdo($query, 'contextid', $contextid);
388  MatrixDAL::bindValueToPdo($query, 'fieldid', $field_id);
389  MatrixDAL::bindValueToPdo($query, 'assetid', $asset_id);
390  MatrixDAL::execPdoQuery($query);
391  } catch (Exception $e) {
392  throw new Exception('DB Error: '.$e->getMessage());
393  }
394  }
395  }
396  }
397 
398  // Run the Query against the current assetid
399  if (MatrixDAL::getDbType() === 'oci') {
400  $sql = 'UPDATE sq_ast_mdata_val SET value=:new_term WHERE TO_CHAR(value)=:old_term AND fieldid IN ('.implode(',', $field_chunk).')';
401  } else {
402  $sql = 'UPDATE sq_ast_mdata_val SET value=:new_term WHERE value=:old_term AND fieldid IN ('.implode(',', $field_chunk).')';
403  }//end if
404  if (!empty($this->_running_vars['assetids'])) {
405  // Update by asset
406  $sql .= ' AND assetid=:assetid';
407  foreach ($this->_running_vars['assetids'] as $assetid) {
408  try {
409  $query = MatrixDAL::preparePdoQuery($sql);
410  MatrixDAL::bindValueToPdo($query, 'assetid', $assetid);
411  MatrixDAL::bindValueToPdo($query, 'new_term', $new_term);
412  MatrixDAL::bindValueToPdo($query, 'old_term', $old_term);
413  MatrixDAL::execPdoQuery($query);
414  } catch (Exception $e) {
415  throw new Exception('DB Error: '.$e->getMessage());
416  }
417  }//end foreach
418  } else {
419  // Update EVERYTHING
420  try {
421  $query = MatrixDAL::preparePdoQuery($sql);
422  MatrixDAL::bindValueToPdo($query, 'new_term', $new_term);
423  MatrixDAL::bindValueToPdo($query, 'old_term', $old_term);
424  MatrixDAL::execPdoQuery($query);
425  } catch (Exception $e) {
426  throw new Exception('DB Error: '.$e->getMessage());
427  }
428  }//end if
429  }//end foreach
430  }//end if
431 
432  // Move the asset out and increate step data
433  $this->_running_vars['done_term_count']++;
434  $dump = array_pop($this->_running_vars['todo_terms']);
435 
436  // Update Step data
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;
443  } else {
444  $step_data['percent_done'] = (int)(($this->_running_vars['done_term_count'] / $this->_running_vars['total_term_count']) * 100);
445  $step_data['complete'] = FALSE;
446  }//end if
447 
448  return TRUE;
449 
450  }//end processRenameTerm()
451 
452 
459  function _findSchemas()
460  {
461  // Match the schemas and fields to the terms to change
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']) {
473  // We have a winner!
474  $this->_running_vars['schemas'][$schemaid][] = $fieldid;
475  $this->_running_vars['fieldids'][] = $fieldid;
476  }//end if
477  }//end if
478  }//end foreach
479 
480  // No fields, so don't worry about this schema
481  if (count($this->_running_vars['schemas'][$schemaid]) == 0) {
482  unset($this->_running_vars['schemas'][$schemaid]);
483  }//end if
484  }//end if
485  }//end foreach
486 
487  return TRUE;
488 
489  }//end _findSchemas()
490 
491 
492 }//end class
493 
494 ?>