17 require_once SQ_SYSTEM_ROOT.
'/core/hipo/hipo_job.inc';
44 $this->uses_trans = FALSE;
60 public static function paintConfig(&$o, $class, $write_access)
63 $o->openField(translate(
'purge_trash_threshold'));
66 text_box($class.
'[SQ_HIPO_PURGE_TRASH_THRESHOLD]', SQ_HIPO_PURGE_TRASH_THRESHOLD, 5);
68 echo SQ_HIPO_PURGE_TRASH_THRESHOLD;
70 echo
' '.translate(
'assets');
86 'SQ_HIPO_PURGE_TRASH_THRESHOLD' => Array(
'editable' => 1,
'default' => 1),
100 return translate(
'hipo_name_purge_trash');
116 'name' => translate(
'hipo_purging_trash_determining'),
117 'function_call' => Array(
118 'process_function' =>
'buildListToDelete',
120 'running_mode' =>
'server',
122 'allow_cancel' => TRUE,
128 'name' => translate(
'hipo_purging_trash_deleting'),
129 'function_call' => Array(
130 'process_function' =>
'purgeAsset',
132 'running_mode' =>
'server',
134 'allow_cancel' => TRUE,
140 'name' => translate(
'hipo_purging_trash_cleaning'),
141 'function_call' => Array(
142 'process_function' =>
'deleteRemainingTrashLinks',
144 'running_mode' =>
'server',
146 'allow_cancel' => TRUE,
167 if (SQ_HIPO_PURGE_TRASH_THRESHOLD == 0)
return 0;
168 if (!isset($this->_running_vars[
'to_check'])) {
171 return ((count($this->_running_vars[
'to_check']) / SQ_HIPO_PURGE_TRASH_THRESHOLD) * 100);
184 $trash = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'trash_folder');
185 $this->_running_vars[
'to_delete'] = Array();
186 $this->_running_vars[
'trash_id'] = $trash->id;
187 $asset_tree_ids = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTreeids($trash->id);
188 $this->_running_vars[
'trash_treeid'] = reset($asset_tree_ids);
190 if (!empty($this->_running_vars[
'purge_root_linkid'])) {
192 $purge_root_link = $GLOBALS[
'SQ_SYSTEM']->am->getLinkById($this->_running_vars[
'purge_root_linkid']);
193 $this->_running_vars[
'purge_root_id'] = $purge_root_link[
'minorid'];
194 $purge_root = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->_running_vars[
'purge_root_id']);
197 if (!$GLOBALS[
'SQ_SYSTEM']->am->assetInTrash($purge_root->id)) {
198 trigger_localised_error(
'HIPO0083', E_USER_WARNING);
201 $purge_root_children = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($purge_root->id);
203 $purge_root_children[$purge_root->id][0][
'type_code'] = $purge_root->type();
204 $link_tree_ids = $GLOBALS[
'SQ_SYSTEM']->am->getLinkTreeId($this->_running_vars[
'purge_root_linkid']);
205 $root_tree_id = reset($link_tree_ids);
206 $this->_running_vars[
'purge_root_treeid'] = $root_tree_id[0][0];
208 $purge_root = $trash;
209 $purge_root_children = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($purge_root->id);
210 $this->_running_vars[
'purge_root_treeid'] = $this->_running_vars[
'trash_treeid'];
215 $bind_vars[
'treeid'] = $this->_running_vars[
'purge_root_treeid'].
'%';
216 $this->_running_vars[
'delete_order'] =
MatrixDAL::executeAssoc(
'core',
'getPurgeTrashHipoDeleteOrder', 0, $bind_vars);
217 }
catch (Exception $e) {
218 throw new Exception(
'Failed to get delete order for purge trash HIPO: '.$e->getMessage());
222 $ordered_to_check_list = Array();
223 foreach ($this->_running_vars[
'delete_order'] as $assetid) {
224 if (isset($purge_root_children[$assetid])) {
225 $ordered_to_check_list[$assetid] = $purge_root_children[$assetid];
228 $this->_running_vars[
'to_check'] = $ordered_to_check_list;
229 $this->_running_vars[
'total_assets'] = count($this->_running_vars[
'to_check']);
231 return parent::prepare();
244 while (!empty($this->_running_vars[
'to_check'])) {
250 while (!empty($this->_running_vars[
'to_delete'])) {
276 $GLOBALS[
'SQ_PURGING_TRASH'] = TRUE;
277 if (!empty($this->_running_vars[
'to_check'])) {
278 reset($this->_running_vars[
'to_check']);
279 list($assetid, $type_code) = each($this->_running_vars[
'to_check']);
280 unset($this->_running_vars[
'to_check'][$assetid]);
282 if (strpos($assetid,
':') === FALSE) {
286 'treeid' => $this->_running_vars[
'purge_root_treeid'].
'%',
287 'assetid' => $assetid,
290 }
catch (Exception $e) {
291 throw new Exception(
'Failed to get asset external link count for purge trash HIPO: '.$e->getMessage());
292 $num_external_links = -1;
295 if ($num_external_links == 0) {
296 $this->_running_vars[
'to_delete'][$assetid] = $type_code;
297 }
else if ($num_external_links > 0) {
300 $children = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($assetid);
301 foreach ($children as $child_assetid => $type_code) {
302 if (isset($this->_running_vars[
'to_check'][$child_assetid])) {
303 unset($this->_running_vars[
'to_check'][$child_assetid]);
310 if (empty($this->_running_vars[
'to_check'])) {
311 $GLOBALS[
'SQ_PURGING_TRASH'] = FALSE;
312 $step_data[
'percent_done'] = 100;
313 $step_data[
'complete'] = TRUE;
314 $this->_running_vars[
'total_to_delete'] = count($this->_running_vars[
'to_delete']);
316 $step_data[
'percent_done'] = (int)((($this->_running_vars[
'total_assets'] - count($this->_running_vars[
'to_check'])) / $this->_running_vars[
'total_assets']) * 100);
317 $step_data[
'complete'] = FALSE;
336 $GLOBALS[
'SQ_PURGING_TRASH'] = TRUE;
337 if (!empty($this->_running_vars[
'to_delete'])) {
340 while (!isset($this->_running_vars[
'to_delete'][$assetid])) {
341 $assetid = array_pop($this->_running_vars[
'delete_order']);
342 if (is_null($assetid))
break;
344 if (!is_null($assetid)) {
345 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid, $this->_running_vars[
'to_delete'][$assetid][0][
'type_code']);
346 unset($this->_running_vars[
'to_delete'][$assetid]);
348 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
349 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
353 if (!$GLOBALS[
'SQ_SYSTEM']->am->canPurgeAsset($asset)) {
354 trigger_localised_error(
'HIPO0004', E_USER_WARNING, $asset->name, $asset->id);
355 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
356 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
360 if (!$GLOBALS[
'SQ_SYSTEM']->am->acquireLock($this->_running_vars[
'trash_id'],
'all') || !$GLOBALS[
'SQ_SYSTEM']->am->acquireLock($asset->id,
'all', $this->_running_vars[
'trash_id'])) {
361 trigger_localised_error(
'HIPO0005', E_USER_WARNING, $asset->name, $asset->id);
362 $GLOBALS[
'SQ_SYSTEM']->am->releaseLock($this->_running_vars[
'trash_id'],
'all');
363 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
364 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
369 $shadow_links = $GLOBALS[
'SQ_SYSTEM']->am->getShadowLinkByAsset($asset->id, NULL, NULL, NULL,
'major', TRUE);
370 foreach($shadow_links as $shadow_link) {
371 $GLOBALS[
'SQ_SYSTEM']->am->deleteShadowAssetLink($shadow_link[
'linkid']);
378 $links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_ALL,
'', TRUE);
379 foreach ($links as $link) {
380 $GLOBALS[
'SQ_SYSTEM']->am->deleteAssetLink($link[
'linkid'], FALSE);
384 if (!$asset->delete(FALSE, FALSE)) {
385 trigger_localised_error(
'HIPO0004', E_USER_WARNING, $asset->name, $asset->id);
386 $GLOBALS[
'SQ_SYSTEM']->am->releaseLock($asset->id,
'all');
387 $GLOBALS[
'SQ_SYSTEM']->am->releaseLock($this->_running_vars[
'trash_id'],
'all');
388 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
389 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
393 $GLOBALS[
'SQ_SYSTEM']->am->releaseLock($this->_running_vars[
'trash_id'],
'all');
395 $step_data[
'message'] = translate(
'hipo_deleting', htmlentities($asset->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET), $assetid);
396 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);
398 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
399 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
403 if (empty($this->_running_vars[
'to_delete'])) {
404 $step_data[
'percent_done'] = 100;
405 $step_data[
'complete'] = TRUE;
407 $step_data[
'percent_done'] = (int)((($this->_running_vars[
'total_to_delete'] - count($this->_running_vars[
'to_delete'])) / $this->_running_vars[
'total_to_delete']) * 100);
408 $step_data[
'complete'] = FALSE;
411 $GLOBALS[
'SQ_PURGING_TRASH'] = FALSE;
430 $GLOBALS[
'SQ_PURGING_TRASH'] = TRUE;
432 if (empty($this->_running_vars[
'purge_root_id'])) {
433 $links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($this->_running_vars[
'trash_id'], SQ_SC_LINK_ALL);
436 $links[] = $GLOBALS[
'SQ_SYSTEM']->am->getLinkById($this->_running_vars[
'purge_root_linkid'], $this->_running_vars[
'purge_root_id'],
'minor');
442 foreach (array_reverse($links) as $link) {
444 $link_info = $GLOBALS[
'SQ_SYSTEM']->am->getLinkById($link[
'linkid']);
445 if (!is_null($link_info[
'minorid'])) {
446 $under_trash = $GLOBALS[
'SQ_SYSTEM']->am->getLinkByAsset($this->_running_vars[
'trash_id'], $link_info[
'minorid']);
447 if ($GLOBALS[
'SQ_SYSTEM']->am->assetInTrash($link_info[
'minorid'], TRUE) || !empty($under_trash)) {
449 $GLOBALS[
'SQ_SYSTEM']->am->deleteAssetLink($link[
'linkid'], FALSE);
452 $GLOBALS[
'SQ_SYSTEM']->am->deleteAssetLink($link[
'linkid'], FALSE);
455 $step_data[
'percent_done'] = 100;
456 $step_data[
'complete'] = TRUE;
457 $GLOBALS[
'SQ_PURGING_TRASH'] = FALSE;