[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/diffusion/query/pathid/ -> DiffusionPathIDQuery.php (source)

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


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1