Last active
August 29, 2015 14:06
-
-
Save lufeihaidao/3cafc88a690f1175a2a6 to your computer and use it in GitHub Desktop.
resolve_name_conflicts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~~~php | |
protected static function resolve_name_conflicts($new_parent, $items, $params) | |
{ | |
$resolve_conflicts_strategy = isset($params['resolve_conflicts_strategy']) ? $params['resolve_conflicts_strategy'] : null; | |
// there are speical logic for folder creation | |
$new_items_to_check = isset($params['new_items_to_check']) ? $params['new_items_to_check']: false; | |
// what does it mean? | |
$ignore_self_conflicts = isset($params['ignore_self_conflicts']) ? $params['ignore_self_conflicts']: true; | |
if (!in_array($resolve_conflicts_strategy, array('rename_if_conflicts', 'overwrite_if_conflicts', 'throw_exception'))) | |
{ | |
throw new Internal_Exception("invalid resolve name conflicts strategy: $resolve_conflicts_strategy", WRONG_RESOLVE_NAME_CONFLICTS_STRATEGY); | |
} | |
$items_to_performance_conflicts_check = array(); | |
$items_result_with_name_resolution = array(); | |
foreach ($items as $key => $item) | |
{ | |
$name = $item->name; | |
$name = Item::validate_name($name, $item->item_type()); | |
// bind validated names with items | |
$items_result_with_name_resolution[$key] = array( | |
'item' => $item, | |
'name' => $name, | |
); | |
} | |
$conflicts_items = $new_parent->detect_name_conflicts_in_target_folder($items_result_with_name_resolution, $check_next_name = ($resolve_conflicts_strategy == 'rename_if_conflicts')); | |
if (!$new_items_to_check && $ignore_self_conflicts) | |
{ | |
$conflicts_items = array_diff_key($conflicts_items, $items); | |
} | |
$conflicted_items_with_new_names = array(); | |
$item_to_overwritten_item_map = array(); | |
switch ($resolve_conflicts_strategy) | |
{ | |
case 'rename_if_conflicts': | |
self::get_new_names_for_conflicts( | |
$conflicts_items, | |
$items_result_with_name_resolution, | |
$new_parent | |
); | |
break; | |
case 'overwrite_if_conflicts': | |
$item_to_overwritten_item_map = self::get_overwritten_items($items_result_with_name_resolution, $conflicts_items); | |
// return conflicts so that we can run versioning to overwrite these items | |
break; | |
case 'throw_exception': | |
throw new Action_Validation_Exception("Name conflict found", NAME_CONFLICTS, array('conflicts' => $conflicts_items)); | |
default: | |
throw new Internal_Exception("Wrong name conflict resolve strategy : " . $resolve_conflicts_strategy); | |
} | |
$final_items = array(); | |
foreach ($items_result_with_name_resolution as $key => $item_result) | |
{ | |
if ($item_result['name'] != $item_result['item']->name) | |
{ | |
$items[$key]->name = $item_result['name']; | |
} | |
$final_items[$key] = $items[$key]; | |
} | |
return array('items' => $final_items, 'overwritten_items' => $item_to_overwritten_item_map); | |
} | |
~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected static function resolve_name_conflicts($new_parent, $items, $params) | |
{ | |
$resolve_conflicts_strategy = isset($params['resolve_conflicts_strategy']) ? $params['resolve_conflicts_strategy'] : null; | |
// there are speical logic for folder creation | |
$new_items_to_check = isset($params['new_items_to_check']) ? $params['new_items_to_check']: false; | |
// what does it mean? | |
$ignore_self_conflicts = isset($params['ignore_self_conflicts']) ? $params['ignore_self_conflicts']: true; | |
if (!in_array($resolve_conflicts_strategy, array('rename_if_conflicts', 'overwrite_if_conflicts', 'throw_exception'))) | |
{ | |
throw new Internal_Exception("invalid resolve name conflicts strategy: $resolve_conflicts_strategy", WRONG_RESOLVE_NAME_CONFLICTS_STRATEGY); | |
} | |
$items_to_performance_conflicts_check = array(); | |
$items_result_with_name_resolution = array(); | |
foreach ($items as $key => $item) | |
{ | |
$name = $item->name; | |
$name = Item::validate_name($name, $item->item_type()); | |
// bind validated names with items | |
$items_result_with_name_resolution[$key] = array( | |
'item' => $item, | |
'name' => $name, | |
); | |
} | |
$conflicts_items = $new_parent->detect_name_conflicts_in_target_folder($items_result_with_name_resolution, $check_next_name = ($resolve_conflicts_strategy == 'rename_if_conflicts')); | |
if (!$new_items_to_check && $ignore_self_conflicts) | |
{ | |
$conflicts_items = array_diff_key($conflicts_items, $items); | |
} | |
$conflicted_items_with_new_names = array(); | |
$item_to_overwritten_item_map = array(); | |
// if resolve_conflicts_strategy is rename_if_conflicts, we need to resolve conflicts always no matter whether conflicts_items is null or not, because items to take an action may have name conflicts themselves | |
if ('rename_if_conflicts' === $resolve_conflicts_strategy) | |
{ | |
self::get_new_names_for_conflicts( | |
$conflicts_items, | |
$items_result_with_name_resolution, | |
$new_parent | |
); | |
} | |
elseif (conflicts_items) | |
{ | |
switch ($resolve_conflicts_strategy) | |
{ | |
case 'overwrite_if_conflicts': | |
$item_to_overwritten_item_map = self::get_overwritten_items($items_result_with_name_resolution, $conflicts_items); | |
// return conflicts so that we can run versioning to overwrite these items | |
break; | |
case 'throw_exception': | |
throw new Action_Validation_Exception("Name conflict found", NAME_CONFLICTS, array('conflicts' => $conflicts_items)); | |
default: | |
throw new Internal_Exception("Wrong name conflict resolve strategy : " . $resolve_conflicts_strategy); | |
} | |
} | |
$final_items = array(); | |
foreach ($items_result_with_name_resolution as $key => $item_result) | |
{ | |
if ($item_result['name'] != $item_result['item']->name) | |
{ | |
$items[$key]->name = $item_result['name']; | |
} | |
$final_items[$key] = $items[$key]; | |
} | |
return array('items' => $final_items, 'overwritten_items' => $item_to_overwritten_item_map); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected static function resolve_name_conflicts($new_parent, $items, $params) | |
{ | |
$resolve_conflicts_strategy = isset($params['resolve_conflicts_strategy']) ? $params['resolve_conflicts_strategy'] : null; | |
// there are speical logic for folder creation | |
$new_items_to_check = isset($params['new_items_to_check']) ? $params['new_items_to_check']: false; | |
// what does it mean? | |
$ignore_self_conflicts = isset($params['ignore_self_conflicts']) ? $params['ignore_self_conflicts']: true; | |
if (!in_array($resolve_conflicts_strategy, array('rename_if_conflicts', 'overwrite_if_conflicts', 'throw_exception'))) | |
{ | |
throw new Internal_Exception("invalid resolve name conflicts strategy: $resolve_conflicts_strategy", WRONG_RESOLVE_NAME_CONFLICTS_STRATEGY); | |
} | |
$items_to_performance_conflicts_check = array(); | |
$items_result_with_name_resolution = array(); | |
foreach ($items as $key => $item) | |
{ | |
$name = $item->name; | |
$name = Item::validate_name($name, $item->item_type()); | |
// bind validated names with items | |
$items_result_with_name_resolution[$key] = array( | |
'item' => $item, | |
'name' => $name, | |
); | |
} | |
$conflicts_items = $new_parent->detect_name_conflicts_in_target_folder($items_result_with_name_resolution, $check_next_name = ($resolve_conflicts_strategy == 'rename_if_conflicts')); | |
if (!$new_items_to_check && $ignore_self_conflicts) | |
{ | |
$conflicts_items = array_diff_key($conflicts_items, $items); | |
} | |
$conflicted_items_with_new_names = static::get_conflicts_items($items_result_with_name_resolution); | |
$item_to_overwritten_item_map = array(); | |
if ($conflicts_items || $conflicted_items_with_new_names) | |
{ | |
switch ($resolve_conflicts_strategy) | |
{ | |
case 'rename_if_conflicts': | |
self::get_new_names_for_conflicts( | |
array_merge($conflicted_items_with_new_names, $conflicts_items), | |
$items_result_with_name_resolution, | |
$new_parent | |
); | |
break; | |
case 'overwrite_if_conflicts': | |
$item_to_overwritten_item_map = self::get_overwritten_items($items_result_with_name_resolution, $conflicts_items); | |
// return conflicts so that we can run versioning to overwrite these items | |
break; | |
case 'throw_exception': | |
throw new Action_Validation_Exception("Name conflict found", NAME_CONFLICTS, array('conflicts' => $conflicts_items)); | |
default: | |
throw new Internal_Exception("Wrong name conflict resolve strategy : " . $resolve_conflicts_strategy); | |
} | |
} | |
$final_items = array(); | |
foreach ($items_result_with_name_resolution as $key => $item_result) | |
{ | |
if ($item_result['name'] != $item_result['item']->name) | |
{ | |
$items[$key]->name = $item_result['name']; | |
} | |
$final_items[$key] = $items[$key]; | |
} | |
return array('items' => $final_items, 'overwritten_items' => $item_to_overwritten_item_map); | |
} | |
private static function get_conflicts_items($items_result_with_name_resolution) | |
{ | |
$items_index_with_name = array(); | |
$result = array(); | |
foreach ($items_result_with_name_resolution as $key => $item_result) | |
{ | |
if (array_key_exists($item_result['name'], $items_index_with_name)) | |
{ | |
$result[$key] = $item_result; | |
} | |
else | |
{ | |
$items_index_with_name[$item_result['name']] = true; | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment