Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_export_thesaurus_xml.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
19 
20 
34 {
35 
36 
42  function HIPO_Job_Export_Thesaurus_XML($code_name='')
43  {
44  $this->uses_trans = TRUE;
45  $this->HIPO_Job($code_name);
46 
47  }//end constructor
48 
49 
56  function getCodeName()
57  {
58  return parent::getCodeName().'-'.$this->_running_vars['thesaurus_id'];
59 
60  }//end getCodeName()
61 
62 
69  function getHipoName()
70  {
71  return translate('thesaurus_hipo_name_export_thesaurus_xml');
72 
73  }//end getHipoName()
74 
75 
83  function getInitialStepData()
84  {
85  return Array(
86  Array(
87  'name' => translate('thesaurus_hipo_starting_process'),
88  'function_call' => Array(
89  'process_function' => 'processStart',
90  ),
91  'running_mode' => 'server',
92  'auto_step' => TRUE,
93  'allow_cancel' => FALSE,
94  'percent_done' => 0,
95  'complete' => FALSE,
96  'message' => translate('thesaurus_hipo_setting_up_env'),
97  ),
98  Array(
99  'name' => translate('thesaurus_hipo_acquiring_locks'),
100  'hipo_job' => Array(
101  'init_details_function' => 'getLockAssetDetails',
102  ),
103  'running_mode' => 'server',
104  'auto_step' => TRUE,
105  'allow_cancel' => TRUE,
106  'percent_done' => 0,
107  'complete' => FALSE,
108  'message' => '',
109  ),
110  Array(
111  'name' => translate('thesaurus_hipo_creating_xml'),
112  'function_call' => Array(
113  'process_function' => 'processCreateXML',
114  ),
115  'running_mode' => 'server',
116  'skip_step' => FALSE,
117  'auto_step' => TRUE,
118  'allow_cancel' => FALSE,
119  'percent_done' => 0,
120  'complete' => FALSE,
121  'message' => '',
122  ),
123  Array(
124  'name' => translate('thesaurus_hipo_finalising'),
125  'function_call' => Array(
126  'process_function' => 'processFinish',
127  ),
128  'running_mode' => 'server',
129  'skip_step' => FALSE,
130  'auto_step' => TRUE,
131  'allow_cancel' => FALSE,
132  'percent_done' => 0,
133  'complete' => FALSE,
134  'message' => '',
135  ),
136  );
137 
138  }//end getInitialStepData()
139 
140 
147  function prepare()
148  {
149  // to start regenerating metadata, we need to have a root assetid - default to root folder if none is supplied
150  if (is_null($this->_running_vars['thesaurus_id'])) {
151  trigger_localised_error('HIPO0072', E_USER_WARNING);
152  return FALSE;
153  }
154 
155  // so we have an assetid to start at, but make sure it is a valid assetid
156  $asset = $this->getThesaurusRef();
157  if (is_null($asset)) {
158  trigger_localised_error('HIPO0073', E_USER_WARNING, $this->_running_vars['thesaurus_id']);
159  return FALSE;
160  }
161 
162  return parent::prepare();
163 
164  }//end prepare()
165 
166 
177  function processStart(&$step_data, $prefix)
178  {
179  $step_data['percent_done'] = 100;
180  $step_data['complete'] = TRUE;
181  return TRUE;
182 
183  }//end processStart()
184 
185 
195  function processFinish(&$step_data, $prefix)
196  {
197 
198  $thesaurus = $this->getThesaurusRef();
199  $thesaurus->setAttrValue('contents_changed', FALSE);
200  $thesaurus->saveAttributes();
201  $step_data['percent_done'] = 100;
202  $step_data['complete'] = TRUE;
203  return TRUE;
204 
205  }//end processFinish()
206 
207 
214  function freestyle()
215  {
216  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
217  $step_data = Array();
218 
219  // skip the confirmation screen, go straight into locking
220  $job_type = '';
221  $running_vars = Array();
222  $options = '';
223  $this->getLockAssetDetails($job_type, $running_vars, $options);
224  $errors = $hh->freestyleHipo($job_type, $running_vars);
225  if (!empty($errors)) {
226  $this->_addError('acquiring locks failed');
227  return FALSE;
228  }
229 
230  // generate the xml
231  if (!$this->processCreateXML($step_data, '')) {
232  $this->_addError('generating the xml failed');
233  $success = FALSE;
234  return FALSE;
235  }
236 
237  return TRUE;
238 
239  }//end freestyle()
240 
241 
251  function processCreateXML(&$step_data, $prefix)
252  {
253  $thesaurus = $this->getThesaurusRef();
254  $filepath = $thesaurus->getXmlFilePath();
255  if (file_exists($filepath)) {
256  $filepath = realpath($filepath);
257  }
258  $finished = FALSE;
259  $iterations_per_step = 100;
260  $iterations_this_step = 0;
261 
262  $all_relations = $thesaurus->getActiveRelations();
263 
264  if (!isset($step_data['unprocessed'])) {
265  if (file_exists($filepath)) unlink($filepath);
266  create_directory(dirname($filepath));
267  touch($filepath);
268 
269  $step_data['total_terms'] = $thesaurus->countTerms();
270  $step_data['links_closed'] = 0;
271  $filehandle = fopen($filepath, 'wb');
272 
273  $step_data['unprocessed'] = $thesaurus->getAllTerms();
274  $step_data['seen_terms'] = Array();
275 
276  fwrite($filehandle, '<!-- MySource Matrix Thesaurus -->'."\n");
277  fwrite($filehandle, '<!-- Generated on '.date('r').' -->'."\n");
278  fwrite($filehandle, '<!-- Do not import into system version less than 3.12 -->'."\n");
279  fwrite($filehandle, '<thesaurus>'."\n");
280 
281  } else {
282  $filehandle = fopen($filepath, 'ab');
283  }
284 
285  if (!is_writable($filepath)) {
286  trigger_localised_error('HIPO0074', E_USER_WARNING, $filepath);
287  }
288 
289  $unprocessed =& $step_data['unprocessed'];
290  $seen_terms =& $step_data['seen_terms'];
291 
292  while (!$finished && $iterations_this_step <= $iterations_per_step) {
293  $iterations_this_step++;
294 
295  if (empty($unprocessed)) {
296  $finished = TRUE;
297  continue;
298  }
299 
300  array_unshift($seen_terms, array_shift($unprocessed));
301 
302  $current_term =& $seen_terms[0];
303 
304  $termid = $current_term['termid'];
305  $term = $current_term['term'];
306  $term_notes = $thesaurus->getTermNotes($termid);
307  $child_relations = $thesaurus->getChildRelationsForTerm($termid);
308 
309  if (empty($term_notes) && empty($child_relations)) {
310  fwrite($filehandle, "\t".'<term name="'.htmlentities($term, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'" />'."\n");
311  continue;
312  }
313 
314  fwrite($filehandle, "\t".'<term name="'.htmlentities($term, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'">'."\n");
315 
316  foreach ($term_notes as $note_array) {
317  fwrite($filehandle, "\t\t".'<note name="'.htmlentities($note_array['name'], ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'">'.$note_array['value'].'</note>'."\n");
318  }
319 
320  foreach ($child_relations as $relid => $relname) {
321  $extra_tab = '';
322  if (!is_null($relname)) {
323  fwrite($filehandle, "\t\t".'<relation name="'.htmlentities($relname, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'">'."\n");
324  $extra_tab = "\t";
325  }
326  $rel_terms = $thesaurus->getChildTerms($termid, $relid);
327  foreach ($rel_terms as $term) {
328  fwrite($filehandle, $extra_tab."\t\t".'<term name="'.htmlentities($term['term'], ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'" />'."\n");
329  }
330  if (!is_null($relname)) {
331  fwrite($filehandle, "\t\t".'</relation>'."\n");
332  }
333  }
334 
335  fwrite($filehandle, "\t".'</term>'."\n");
336 
337  }//end while
338 
339  $step_data['percent_done'] = (count($seen_terms)/$step_data['total_terms']) * 100;
340  $step_data['message'] = 'Processed '.count($seen_terms).' of '.$step_data['total_terms'].' terms';
341 
342  if ($finished) {
343  fwrite($filehandle, '</thesaurus>'."\n");
344  $step_data['complete'] = TRUE;
345  } else {
346  $step_data['complete'] = FALSE;
347  }
348  fclose($filehandle);
349  return TRUE;
350 
351  }//end processCreateXML()
352 
353 
364  function getLockAssetDetails(&$job_type, &$running_vars, &$options)
365  {
366  $job_type = 'hipo_job_acquire_locks';
367 
368  $options['auto_complete'] = TRUE;
369 
370  $running_vars = Array(
371  'assetids' => Array($this->_running_vars['thesaurus_id'],),
372  'lock_type' => 'attributes',
373  'dependants_only' => FALSE,
374  'forceably_acquire' => FALSE,
375  );
376 
377  }//end getLockAssetDetails()
378 
379 
386  function getThesaurusRef()
387  {
388  return $GLOBALS['SQ_SYSTEM']->am->getAsset($this->_running_vars['thesaurus_id']);
389 
390  }//end getThesaurusRef()
391 
392 
393 }//end class
394 ?>