PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/docman/bin/DocmanImport/Trees.class.php

https://gitlab.com/prenaud76/tuleap
PHP | 143 lines | 87 code | 16 blank | 40 comment | 24 complexity | df2050c59f89f908a06288dbf4717293 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * Originally written by Clément Plantier, 2008
  4. *
  5. * This file is a part of Codendi.
  6. *
  7. * Codendi is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Codendi is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Codendi; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /**
  22. * Utility class for creating and merging trees
  23. */
  24. class Trees {
  25. /**
  26. * Returns a tree of nodes build using a list of nodes: (node_id => array of children_id) (recursive)
  27. */
  28. private static function nodeListToTreeRec($listOfNodes, $nodeId) {
  29. $children = null;
  30. if (array_key_exists($nodeId, $listOfNodes)) {
  31. foreach ($listOfNodes[$nodeId] as $child) {
  32. $children[$child] = self::nodeListToTreeRec($listOfNodes, $child);
  33. }
  34. }
  35. return $children;
  36. }
  37. /**
  38. * Find the root of a tree in a list of nodes
  39. */
  40. private static function findRoot($listOfNodes) {
  41. foreach ($listOfNodes as $rootCandidate => $children) {
  42. $isRoot = true;
  43. foreach ($listOfNodes as $currentNode) {
  44. if (in_array($rootCandidate, $currentNode)) {
  45. $isRoot = false;
  46. break;
  47. }
  48. }
  49. if ($isRoot) {
  50. return $rootCandidate;
  51. }
  52. }
  53. return null;
  54. }
  55. /**
  56. * Returns a tree of nodes build using a list of nodes: (node_id => array of children_id)
  57. */
  58. public static function nodeListToTree($listOfNodes) {
  59. $root = self::findRoot($listOfNodes);
  60. if ($root === null) {
  61. return null;
  62. } else {
  63. return array($root => self::nodeListToTreeRec($listOfNodes, $root));
  64. }
  65. }
  66. /**
  67. * Megre two trees and tag the nodes with the information: IN_FIRST, IN_SECOND, IN_BOTH (recursive)
  68. */
  69. private static function mergeTagRec($array1, $array2) {
  70. $res = null;
  71. if ($array1 != null) {
  72. foreach ($array1 as $k => $v) {
  73. if ($k != 'children') {
  74. $res[$k] = $v;
  75. }
  76. }
  77. }
  78. if ($array2 != null) {
  79. foreach ($array2 as $k => $v) {
  80. if ($k != 'children') {
  81. $res[$k] = $v;
  82. }
  83. }
  84. }
  85. if ($array1 != null && isset($array1['children'])) {
  86. foreach ($array1['children'] as $name1 => $node1) {
  87. if (isset($array2['children']) && array_key_exists($name1, $array2['children'])) {
  88. $res['children'][$name1] = self::mergeTagRec($array1['children'][$name1], $array2['children'][$name1]);
  89. $res['children'][$name1]['tag'] = 'IN_BOTH';
  90. } else {
  91. $res['children'][$name1] = $node1;
  92. self::tagTree($res['children'][$name1], 'IN_FIRST');
  93. }
  94. }
  95. }
  96. if ($array2 != null && isset($array2['children'])) {
  97. foreach ($array2['children'] as $name2 => $node2) {
  98. if (!isset($res['children'][$name2])) {
  99. $res['children'][$name2] = $node2;
  100. self::tagTree($res['children'][$name2], 'IN_SECOND');
  101. }
  102. }
  103. }
  104. return $res;
  105. }
  106. /**
  107. * Set recursively the given tag to all nodes of the tree
  108. */
  109. private static function tagTree(&$tree, $tag) {
  110. $tree['tag'] = $tag;
  111. if (isset($tree['children'])) {
  112. foreach ($tree['children'] as $name => $node) {
  113. self::tagTree($tree['children'][$name], $tag);
  114. }
  115. }
  116. }
  117. /**
  118. * Merge two trees and tag the nodes with the information: IN_FIRST, IN_SECOND, IN_BOTH
  119. */
  120. public static function mergeTag(array $array1, array $array2) {
  121. $root1 = array_pop(array_keys($array1));
  122. $root2 = array_pop(array_keys($array2));
  123. $res['(root)'] = self::mergeTagRec($array1[$root1], $array2[$root2]);
  124. return $res;
  125. }
  126. }
  127. ?>