Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/diffusion/query/pathid/DiffusionPathIDQuery.php
12242 views
1
<?php
2
3
/**
4
* @task pathutil Path Utilities
5
*/
6
final class DiffusionPathIDQuery extends Phobject {
7
8
private $paths = array();
9
10
public function __construct(array $paths) {
11
$this->paths = $paths;
12
}
13
14
public function loadPathIDs() {
15
$repository = new PhabricatorRepository();
16
17
$path_normal_map = array();
18
foreach ($this->paths as $path) {
19
$normal = self::normalizePath($path);
20
$path_normal_map[$normal][] = $path;
21
}
22
23
$paths = queryfx_all(
24
$repository->establishConnection('r'),
25
'SELECT * FROM %T WHERE pathHash IN (%Ls)',
26
PhabricatorRepository::TABLE_PATH,
27
array_map('md5', array_keys($path_normal_map)));
28
$paths = ipull($paths, 'id', 'path');
29
30
$result = array();
31
32
foreach ($path_normal_map as $normal => $originals) {
33
foreach ($originals as $original) {
34
$result[$original] = idx($paths, $normal);
35
}
36
}
37
38
return $result;
39
}
40
41
42
/**
43
* Convert a path to the canonical, absolute representation used by Diffusion.
44
*
45
* @param string Some repository path.
46
* @return string Canonicalized Diffusion path.
47
* @task pathutil
48
*/
49
public static function normalizePath($path) {
50
51
if ($path === null) {
52
return '/';
53
}
54
55
// Normalize to single slashes, e.g. "///" => "/".
56
$path = preg_replace('@[/]{2,}@', '/', $path);
57
58
return '/'.trim($path, '/');
59
}
60
61
62
/**
63
* Return the canonical parent directory for a path. Note, returns "/" when
64
* passed "/".
65
*
66
* @param string Some repository path.
67
* @return string That path's canonical parent directory.
68
* @task pathutil
69
*/
70
public static function getParentPath($path) {
71
$path = self::normalizePath($path);
72
$path = dirname($path);
73
if (phutil_is_windows() && $path == '\\') {
74
$path = '/';
75
}
76
return $path;
77
}
78
79
80
/**
81
* Generate a list of parents for a repository path. The path itself is
82
* included.
83
*
84
* @param string Some repository path.
85
* @return list List of canonical paths between the path and the root.
86
* @task pathutil
87
*/
88
public static function expandPathToRoot($path) {
89
$path = self::normalizePath($path);
90
$parents = array($path);
91
$parts = explode('/', trim($path, '/'));
92
while (count($parts) >= 1) {
93
if (array_pop($parts)) {
94
$parents[] = '/'.implode('/', $parts);
95
}
96
}
97
return $parents;
98
}
99
100
}
101
102