[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/infrastructure/util/ -> PhabricatorSlug.php (source)

   1  <?php
   2  
   3  final class PhabricatorSlug {
   4  
   5    public static function normalize($slug) {
   6      $slug = preg_replace('@/+@', '/', $slug);
   7      $slug = trim($slug, '/');
   8      $slug = phutil_utf8_strtolower($slug);
   9      $slug = preg_replace("@[\\x00-\\x19#%&+=\\\\?<> ]+@", '_', $slug);
  10      $slug = preg_replace('@_+@', '_', $slug);
  11  
  12      // Remove leading and trailing underscores from each component, if the
  13      // component has not been reduced to a single underscore. For example, "a?"
  14      // converts to "a", but "??" converts to "_".
  15      $parts = explode('/', $slug);
  16      foreach ($parts as $key => $part) {
  17        if ($part != '_') {
  18          $parts[$key] = trim($part, '_');
  19        }
  20      }
  21      $slug = implode('/', $parts);
  22  
  23      // Specifically rewrite these slugs. It's OK to have a slug like "a..b",
  24      // but not a slug which is only "..".
  25  
  26      // NOTE: These are explicitly not pht()'d, because they should be stable
  27      // across languages.
  28  
  29      $replace = array(
  30        '.'   => 'dot',
  31        '..'  => 'dotdot',
  32      );
  33  
  34      foreach ($replace as $pattern => $replacement) {
  35        $pattern = preg_quote($pattern, '@');
  36        $slug = preg_replace(
  37          '@(^|/)'.$pattern.'(\z|/)@',
  38          '\1'.$replacement.'\2', $slug);
  39      }
  40  
  41      return $slug.'/';
  42    }
  43  
  44    public static function getDefaultTitle($slug) {
  45      $parts = explode('/', trim($slug, '/'));
  46      $default_title = end($parts);
  47      $default_title = str_replace('_', ' ', $default_title);
  48      $default_title = phutil_utf8_ucwords($default_title);
  49      $default_title = nonempty($default_title, pht('Untitled Document'));
  50      return $default_title;
  51    }
  52  
  53    public static function getAncestry($slug) {
  54      $slug = self::normalize($slug);
  55  
  56      if ($slug == '/') {
  57        return array();
  58      }
  59  
  60      $ancestors = array(
  61        '/',
  62      );
  63  
  64      $slug = explode('/', $slug);
  65      array_pop($slug);
  66      array_pop($slug);
  67  
  68      $accumulate = '';
  69      foreach ($slug as $part) {
  70        $accumulate .= $part.'/';
  71        $ancestors[] = $accumulate;
  72      }
  73  
  74      return $ancestors;
  75    }
  76  
  77    public static function getDepth($slug) {
  78      $slug = self::normalize($slug);
  79      if ($slug == '/') {
  80        return 0;
  81      } else {
  82        return substr_count($slug, '/');
  83      }
  84    }
  85  
  86  }


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