Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_acquire_locks.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
36 {
37 
38 
44  function HIPO_Job_Acquire_Locks($code_name='')
45  {
46  $this->HIPO_Job($code_name);
47 
48  }//end constructor
49 
50 
60  public static function paintConfig(&$o, $class, $write_access)
61  {
62  // metadata regeneration threshhold HIPO config entry
63  $o->openField(translate('acquire_lock_threshold'));
64 
65  if ($write_access) {
66  text_box($class.'[SQ_HIPO_ACQUIRE_LOCK_THRESHOLD]', SQ_HIPO_ACQUIRE_LOCK_THRESHOLD, 5);
67  } else {
68  echo SQ_HIPO_ACQUIRE_LOCK_THRESHOLD;
69  }
70  echo ' '.translate('assets');
71 
72  $o->closeField();
73 
74  }//end paintConfig()
75 
76 
83  public static function getConfigVars()
84  {
85  return Array(
86  'SQ_HIPO_ACQUIRE_LOCK_THRESHOLD' => Array('editable' => 1, 'default' => 1),
87  );
88 
89  }//end getConfigVars()
90 
91 
103  function getCodeName()
104  {
105  $code_name = parent::getCodeName();
106  return $code_name.'-'.md5(implode(',', $this->_running_vars['assetids']));
107 
108  }//end getCodeName()
109 
110 
117  function getHipoName()
118  {
119  return translate('hipo_name_acquire_locks');
120 
121  }//end getHipoName()
122 
123 
132  {
133  return Array(
134  Array(
135  'name' => translate('hipo_acquiring_locks'),
136  'function_call' => Array(
137  'process_function' => 'processLocking',
138  ),
139  'running_mode' => 'server',
140  'auto_step' => true,
141  'allow_cancel' => true,
142  'percent_done' => 0,
143  'complete' => false,
144  'message' => '',
145  ),
146  );
147 
148  }//end getInitialStepData()
149 
150 
159  function getAssetLockTypes($assetid)
160  {
161  $types = Array();
162  if (isset($this->_running_vars['lock_type']) && isset($assetid)) {
163  if (is_array($this->_running_vars['lock_type'])) {
164  foreach ($this->_running_vars['lock_type'] as $lock_type) {
165  foreach ($GLOBALS['SQ_SYSTEM']->am->getLockTypes($assetid, $lock_type) as $type) {
166  if (!in_array($type, $types)) $types[] = $type;
167  }
168  }
169  } else {
170  $types = $GLOBALS['SQ_SYSTEM']->am->getLockTypes($assetid, $this->_running_vars['lock_type']);
171  }
172  }
173  return $types;
174 
175  }//end getAssetLockTypes()
176 
177 
188  {
189  if (SQ_HIPO_ACQUIRE_LOCK_THRESHOLD == 0) return 0;
190  if (!isset($this->_running_vars['child_assetids'])) {
191  return 0;
192  }
193  // note that we add 1 to the count to take the root assetid into account
194  return ((($this->_running_vars['total_count'] + 1) / SQ_HIPO_ACQUIRE_LOCK_THRESHOLD) * 100);
195 
196  }//end getThresholdPercentageRequired()
197 
198 
205  function freestyle()
206  {
207  while (!empty($this->_running_vars['assetids'])/* || !$this->_running_vars['done_root']*/) {
208  if (!$this->processLocking($this->_steps[0], get_class($this))) {
209  return false;
210  }
211  }
212  return true;
213 
214  }//end freestyle()
215 
216 
223  function prepare()
224  {
225  // to start acquiring locks, we need to have an asset id to acquire a lock on
226  if (empty($this->_running_vars['assetids']) || empty($this->_running_vars['lock_type'])) {
227  trigger_localised_error('HIPO0035', E_USER_WARNING);
228  return false;
229  }
230 
231  if (!isset($this->_running_vars['todo_assetids']) || empty($this->_running_vars['todo_assetids'])) {
232  // we have a valid asset, so get all the assets below this one in the tree(s) so we know what assets
233  // we will be acquiring locks on
234 
235  $child_assetids = Array();
236  $this->_running_vars['total_count'] = 0;
237  // if there is no dependants setting, or if it is true, get only dependants
238  if (!isset($this->_running_vars['dependants_only']) || !empty($this->_running_vars['dependants_only'])) {
239  $this->_running_vars['dependants_only'] = true;
240  foreach ($this->_running_vars['assetids'] as $assetid) {
241  $this->_running_vars['total_count']++;
242  $child_assets[$assetid] = $GLOBALS['SQ_SYSTEM']->am->getDependantChildren($assetid);
243  $this->_running_vars['total_count'] += count($child_assets[$assetid]);
244 
245  }
246  } else {
247  // get locks for all children
248  $this->_running_vars['dependants_only'] = false;
249  foreach ($this->_running_vars['assetids'] as $assetid) {
250  $this->_running_vars['total_count']++;
251  $child_assets[$assetid] = $GLOBALS['SQ_SYSTEM']->am->getChildren($assetid);
252  $this->_running_vars['total_count'] += count($child_assets[$assetid]);
253  }
254  }
255  $this->_running_vars['child_assetids'] = $child_assets;
256  $this->_running_vars['current_assetid'] = reset($this->_running_vars['assetids']);
257  } else {
258  $this->_running_vars['current_assetid'] = key($this->_running_vars['todo_assetids']);
259  $this->_running_vars['total_count'] = count($this->_running_vars['todo_assetids']);
260  }
261 
262 
263  $this->_running_vars['done_assetids'] = Array();
264 
265  if (!isset($this->_running_vars['forceably_acquire'])) {
266  $this->_running_vars['forceably_acquire'] = false;
267  } else {
268  $this->_running_vars['forceably_acquire'] = (bool) $this->_running_vars['forceably_acquire'];
269  }
270 
271  return parent::prepare();
272 
273  }//end prepare()
274 
275 
285  function processLocking(&$step_data, $prefix)
286  {
287  if (!empty($this->_running_vars['assetids'])) {
288  $parent_assetid = $this->_running_vars['current_assetid'];
289 
290  if (!in_array($parent_assetid, $this->_running_vars['done_assetids'])) {
291  $assetid = $parent_assetid;
292  $asset =$GLOBALS['SQ_SYSTEM']->am->getAsset($parent_assetid);
293  } else {
294  $parent_assetid_keys = array_keys($this->_running_vars['child_assetids'][$parent_assetid]);
295  $assetid = array_shift($parent_assetid_keys);
296  if (is_array($this->_running_vars['child_assetids'][$parent_assetid][$assetid])) {
297  $asset_type = $this->_running_vars['child_assetids'][$parent_assetid][$assetid][0]['type_code'];
298  } else {
299  $asset_type = $this->_running_vars['child_assetids'][$parent_assetid][$assetid];
300  }
301  unset($this->_running_vars['child_assetids'][$parent_assetid][$assetid]);
302  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $asset_type);
303  }
304 
305  if (!is_null($asset)) {
306  $lock_types = $this->getAssetLockTypes($parent_assetid);
307  foreach ($lock_types as $lock_type) {
308  $GLOBALS['SQ_SYSTEM']->am->acquireLock($asset->id, $lock_type, $parent_assetid, $this->_running_vars['forceably_acquire']);
309  }
310  $step_data['message'] = translate('hipo_acquire_lock', htmlentities($asset->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
311  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
312  unset($asset);
313  } else {
314  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
315  $this->_addError(translate('hipo_asset_not_exist', $assetid));
316  }
317 
318  // add this assetid to the done array so we dont do it again
319  $this->_running_vars['done_assetids'][] = $assetid;
320 
321  // if we are done with the current parent asset's children, then move onto the next one
322  if (empty($this->_running_vars['child_assetids'][$parent_assetid])) {
323  array_shift($this->_running_vars['assetids']);
324  if (!empty($this->_running_vars['assetids'])) {
325  $this->_running_vars['current_assetid'] = $this->_running_vars['assetids'][0];
326  }
327  }
328  }
329 
330  if (empty($this->_running_vars['assetids'])) {
331  $step_data['percent_done'] = 100;
332  $step_data['complete'] = true;
333  } else {
334  $step_data['percent_done'] = (count($this->_running_vars['done_assetids']) / $this->_running_vars['total_count']) * 100;
335  $step_data['complete'] = false;
336  }
337 
338  return true;
339 
340  }//end processLocking()
341 
342 
343 }//end class
344 
345 ?>