Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_regenerate_metadata.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
32 {
33 
34 
40  function HIPO_Job_Regenerate_Metadata($code_name='')
41  {
42  $this->uses_trans = FALSE;
43  $this->HIPO_Job($code_name);
44 
45  }//end constructor
46 
47 
57  public static function paintConfig(&$o, $class, $write_access)
58  {
59  // metadata regeneration threshhold HIPO config entry
60  $o->openField(translate('metadata_regeneration_threshold'));
61 
62  if ($write_access) {
63  text_box($class.'[SQ_HIPO_REGENERATE_METADATA_THRESHOLD]', SQ_HIPO_REGENERATE_METADATA_THRESHOLD, 5);
64  } else {
65  echo SQ_HIPO_REGENERATE_METADATA_THRESHOLD;
66  }
67  echo ' '.translate('assets');
68 
69  $o->closeField();
70 
71  }//end paintConfig()
72 
73 
80  public static function getConfigVars()
81  {
82  return Array('SQ_HIPO_REGENERATE_METADATA_THRESHOLD' => Array('editable' => 1, 'default' => 1));
83 
84  }//end getConfigVars()
85 
86 
96  function getCodeName()
97  {
98  return parent::getCodeName().'-'.md5(implode('-',$this->_running_vars['schemaids']));
99 
100  }//end getCodeName()
101 
102 
109  function getHipoName()
110  {
111  return translate('hipo_name_regenerate_metadata');
112 
113  }//end getHipoName()
114 
115 
124  {
125  return Array(
126  Array(
127  'name' => translate('hipo_generating_metadata_files'),
128  'function_call' => Array(
129  'process_function' => 'processMetadata',
130  ),
131  'running_mode' => 'server',
132  'auto_step' => TRUE,
133  'allow_cancel' => TRUE,
134  'percent_done' => 0,
135  'complete' => FALSE,
136  'message' => '',
137  ),
138  );
139 
140  }//end getInitialStepData()
141 
142 
152  {
153  if (SQ_HIPO_REGENERATE_METADATA_THRESHOLD == 0) {
154  return 0;
155  }
156  if (!isset($this->_running_vars['todo_assetids'])) {
157  return 0;
158  }
159  return ((count($this->_running_vars['todo_assetids']) / SQ_HIPO_REGENERATE_METADATA_THRESHOLD) * 100);
160 
161  }//end getThresholdPercentageRequired()
162 
163 
170  function freestyle()
171  {
172  while (!empty($this->_running_vars['todo_assetids'])) {
173  if (!$this->processMetadata($this->_steps[0], get_class($this))) {
174  return FALSE;
175  }
176  }
177  return TRUE;
178 
179  }//end freestyle()
180 
181 
188  function prepare()
189  {
190  // to start regenerating metadata, we need to have a schema that has been updated
191  if (empty($this->_running_vars['schemaids'])) {
192  trigger_localised_error('HIPO0003', E_USER_WARNING);
193  return FALSE;
194  }
195 
196  // If no context IDs are provided, it will do all of them
197  if (empty($this->_running_vars['contextids']) === TRUE) {
198  $this->_running_vars['contextids'] = array_keys($GLOBALS['SQ_SYSTEM']->getAllContexts());
199  }
200 
201  // If a scalar is given instead of an array, assume a single context
202  // and convert it into an array
203  if (is_array($this->_running_vars['contextids']) === FALSE) {
204  $this->_running_vars['contextids'] = Array($this->_running_vars['contextids']);
205  }
206 
207  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
208 
209  $this->_running_vars['todo_assetids'] = Array();
210  $this->_running_vars['done_assetids'] = Array();
211 
212  foreach ($this->_running_vars['schemaids'] as $schemaid) {
213  $this->_running_vars['todo_assetids'] = array_merge($this->_running_vars['todo_assetids'], $mm->getSchemaAssets($schemaid, TRUE));
214  }
215 
216  // remove duplicates
217  $this->_running_vars['todo_assetids'] = array_values(array_unique($this->_running_vars['todo_assetids']));
218 
219  // if nothing to do, bail out
220  if (empty($this->_running_vars['todo_assetids'])) {
221  return FALSE;
222  }
223 
224  return parent::prepare();
225 
226  }//end prepare()
227 
228 
238  function processMetadata(&$step_data, $prefix)
239  {
240  if (!isset($this->_running_vars['process_metadata'])) {
241  $this->_running_vars['process_metadata'] = $this->_running_vars['todo_assetids'];
242  }
243 
244  if (!empty($this->_running_vars['process_metadata'])) {
245  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
246  $assetid = array_shift($this->_running_vars['process_metadata']);
247  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
248 
249  if (!is_null($asset)) {
250  // try to acquire lock
251  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($asset->id, 'metadata')) {
252  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
253  trigger_localised_error('HIPO0077', E_USER_WARNING, $asset->name, $assetid);
254  }
255 
256  if (!$asset->writeAccess('metadata')) {
257  // we don't have write access to this asset for another
258  // reason (eg. status = Archived), skip the asset
259  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
260  trigger_localised_error('HIPO0078', E_USER_WARNING, $asset->name, $assetid);
261  } else {
262  foreach ($this->_running_vars['contextids'] as $contextid) {
263  if (!$mm->regenerateMetadata($asset->id, $contextid)) return FALSE;
264  }
265  $step_data['message'] = translate('hipo_generate_metadata', htmlentities($asset->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
266  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
267  unset($asset);
268  }
269 
270  // release lock
271  $GLOBALS['SQ_SYSTEM']->am->releaseLock($assetid, 'metadata');
272 
273  } else {
274  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
275  $this->_addError(translate('hipo_cannot_regenerate_metadata', $assetid));
276  }
277 
278  // add this assetid to the done array so we dont do it again
279  $this->_running_vars['done_assetids'][] = $assetid;
280  }//end if
281 
282  if (empty($this->_running_vars['process_metadata'])) {
283  $step_data['percent_done'] = 100;
284  $step_data['complete'] = TRUE;
285  unset($this->_running_vars['done_assetids']);
286  $this->_running_vars['todo_assetids'] = Array();
287  } else {
288  $total = count($this->_running_vars['process_metadata']) + count($this->_running_vars['done_assetids']);
289  $step_data['percent_done'] =(count($this->_running_vars['done_assetids']) / $total) * 100;
290  $step_data['complete'] = FALSE;
291  }
292 
293  return TRUE;
294 
295  }//end processMetadata()
296 
297 
298 }//end class
299 
300 ?>