@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at recaptime-dev/main 99 lines 2.4 kB view raw
1<?php 2 3/** 4 * @task pathutil Path Utilities 5 */ 6final 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 $path Some repository path. 46 * @return string Canonicalized Diffusion path. 47 * @task pathutil 48 */ 49 public static function normalizePath($path) { 50 51 // Ensure we have a string, not a null. 52 $path = coalesce($path, ''); 53 54 // Normalize to single slashes, e.g. "///" => "/". 55 $path = preg_replace('@[/]{2,}@', '/', $path); 56 57 return '/'.trim($path, '/'); 58 } 59 60 61 /** 62 * Return the canonical parent directory for a path. Note, returns "/" when 63 * passed "/". 64 * 65 * @param string $path Some repository path. 66 * @return string That path's canonical parent directory. 67 * @task pathutil 68 */ 69 public static function getParentPath($path) { 70 $path = self::normalizePath($path); 71 $path = dirname($path); 72 if (phutil_is_windows() && $path == '\\') { 73 $path = '/'; 74 } 75 return $path; 76 } 77 78 79 /** 80 * Generate a list of parents for a repository path. The path itself is 81 * included. 82 * 83 * @param string $path Some repository path. 84 * @return list List of canonical paths between the path and the root. 85 * @task pathutil 86 */ 87 public static function expandPathToRoot($path) { 88 $path = self::normalizePath($path); 89 $parents = array($path); 90 $parts = explode('/', trim($path, '/')); 91 while (count($parts) >= 1) { 92 if (array_pop($parts)) { 93 $parents[] = '/'.implode('/', $parts); 94 } 95 } 96 return $parents; 97 } 98 99}