Path: blob/master/src/applications/diffusion/query/pathid/DiffusionPathIDQuery.php
12242 views
<?php12/**3* @task pathutil Path Utilities4*/5final class DiffusionPathIDQuery extends Phobject {67private $paths = array();89public function __construct(array $paths) {10$this->paths = $paths;11}1213public function loadPathIDs() {14$repository = new PhabricatorRepository();1516$path_normal_map = array();17foreach ($this->paths as $path) {18$normal = self::normalizePath($path);19$path_normal_map[$normal][] = $path;20}2122$paths = queryfx_all(23$repository->establishConnection('r'),24'SELECT * FROM %T WHERE pathHash IN (%Ls)',25PhabricatorRepository::TABLE_PATH,26array_map('md5', array_keys($path_normal_map)));27$paths = ipull($paths, 'id', 'path');2829$result = array();3031foreach ($path_normal_map as $normal => $originals) {32foreach ($originals as $original) {33$result[$original] = idx($paths, $normal);34}35}3637return $result;38}394041/**42* Convert a path to the canonical, absolute representation used by Diffusion.43*44* @param string Some repository path.45* @return string Canonicalized Diffusion path.46* @task pathutil47*/48public static function normalizePath($path) {4950if ($path === null) {51return '/';52}5354// Normalize to single slashes, e.g. "///" => "/".55$path = preg_replace('@[/]{2,}@', '/', $path);5657return '/'.trim($path, '/');58}596061/**62* Return the canonical parent directory for a path. Note, returns "/" when63* passed "/".64*65* @param string Some repository path.66* @return string That path's canonical parent directory.67* @task pathutil68*/69public static function getParentPath($path) {70$path = self::normalizePath($path);71$path = dirname($path);72if (phutil_is_windows() && $path == '\\') {73$path = '/';74}75return $path;76}777879/**80* Generate a list of parents for a repository path. The path itself is81* included.82*83* @param string Some repository path.84* @return list List of canonical paths between the path and the root.85* @task pathutil86*/87public static function expandPathToRoot($path) {88$path = self::normalizePath($path);89$parents = array($path);90$parts = explode('/', trim($path, '/'));91while (count($parts) >= 1) {92if (array_pop($parts)) {93$parents[] = '/'.implode('/', $parts);94}95}96return $parents;97}9899}100101102