[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/diviner/atom/ -> DivinerAtomRef.php (source)

   1  <?php
   2  
   3  final class DivinerAtomRef {
   4  
   5    private $book;
   6    private $context;
   7    private $type;
   8    private $name;
   9    private $group;
  10    private $summary;
  11    private $index;
  12    private $title;
  13  
  14    public function getSortKey() {
  15      return implode(
  16        "\0",
  17        array(
  18          $this->getName(),
  19          $this->getType(),
  20          $this->getContext(),
  21          $this->getBook(),
  22          $this->getIndex(),
  23        ));
  24    }
  25  
  26    public function setIndex($index) {
  27      $this->index = $index;
  28      return $this;
  29    }
  30  
  31    public function getIndex() {
  32      return $this->index;
  33    }
  34  
  35    public function setSummary($summary) {
  36      $this->summary = $summary;
  37      return $this;
  38    }
  39  
  40    public function getSummary() {
  41      return $this->summary;
  42    }
  43  
  44    public function setName($name) {
  45      $normal_name = self::normalizeString($name);
  46      if (preg_match('/^@[0-9]+\z/', $normal_name)) {
  47        throw new Exception(
  48          "Atom names must not be in the form '/@\d+/'. This pattern is ".
  49          "reserved for disambiguating atoms with similar names.");
  50      }
  51      $this->name = $normal_name;
  52      return $this;
  53    }
  54  
  55    public function getName() {
  56      return $this->name;
  57    }
  58  
  59    public function setType($type) {
  60      $this->type = self::normalizeString($type);
  61      return $this;
  62    }
  63  
  64    public function getType() {
  65      return $this->type;
  66    }
  67  
  68    public function setContext($context) {
  69      if ($context === null) {
  70        $this->context = $context;
  71      } else {
  72        $this->context = self::normalizeString($context);
  73      }
  74      return $this;
  75    }
  76  
  77    public function getContext() {
  78      return $this->context;
  79    }
  80  
  81    public function setBook($book) {
  82      if ($book === null) {
  83        $this->book = $book;
  84      } else {
  85        $this->book = self::normalizeString($book);
  86      }
  87      return $this;
  88    }
  89  
  90    public function getBook() {
  91      return $this->book;
  92    }
  93  
  94    public function setGroup($group) {
  95      $this->group = $group;
  96      return $this;
  97    }
  98  
  99    public function getGroup() {
 100      return $this->group;
 101    }
 102  
 103    public function setTitle($title) {
 104      $this->title = $title;
 105      return $this;
 106    }
 107  
 108    public function getTitle() {
 109      return $this->title;
 110    }
 111  
 112    public function getTitleSlug() {
 113      return self::normalizeTitleString($this->getTitle());
 114    }
 115  
 116    public function toDictionary() {
 117      return array(
 118        'book'    => $this->getBook(),
 119        'context' => $this->getContext(),
 120        'type'    => $this->getType(),
 121        'name'    => $this->getName(),
 122        'group'   => $this->getGroup(),
 123        'index'   => $this->getIndex(),
 124        'summary' => $this->getSummary(),
 125        'title'   => $this->getTitle(),
 126      );
 127    }
 128  
 129    public function toHash() {
 130      $dict = $this->toDictionary();
 131  
 132      unset($dict['group']);
 133      unset($dict['index']);
 134      unset($dict['summary']);
 135      unset($dict['title']);
 136  
 137      ksort($dict);
 138      return md5(serialize($dict)).'S';
 139    }
 140  
 141    public static function newFromDictionary(array $dict) {
 142      $obj = new DivinerAtomRef();
 143      $obj->setBook(idx($dict, 'book'));
 144      $obj->setContext(idx($dict, 'context'));
 145      $obj->setType(idx($dict, 'type'));
 146      $obj->setName(idx($dict, 'name'));
 147      $obj->group = idx($dict, 'group');
 148      $obj->index = idx($dict, 'index');
 149      $obj->summary = idx($dict, 'summary');
 150      $obj->title = idx($dict, 'title');
 151  
 152      return $obj;
 153    }
 154  
 155    public static function normalizeString($str) {
 156      // These characters create problems on the filesystem or in URIs. Replace
 157      // them with non-problematic appoximations (instead of simply removing them)
 158      // to keep the URIs fairly useful and avoid unnecessary collisions. These
 159      // approximations are selected based on some domain knowledge of common
 160      // languages: where a character is used as a delimiter, it is more helpful
 161      // to replace it with a "." or a ":" or similar, while it's better if
 162      // operator overloads read as, e.g., "operator_div".
 163  
 164      $map = array(
 165        // Hopefully not used anywhere by anything.
 166        '#'   => '.',
 167  
 168        // Used in Ruby methods.
 169        '?'   => 'Q',
 170  
 171        // Used in PHP namespaces.
 172        '\\'  => '.',
 173  
 174        // Used in "operator +" in C++.
 175        '+'   => 'plus',
 176  
 177        // Used in "operator %" in C++.
 178        '%'   => 'mod',
 179  
 180        // Used in "operator /" in C++.
 181        '/'   => 'div',
 182      );
 183      $str = str_replace(array_keys($map), array_values($map), $str);
 184  
 185      // Replace all spaces with underscores.
 186      $str = preg_replace('/ +/', '_', $str);
 187  
 188      // Replace control characters with "X".
 189      $str = preg_replace('/[\x00-\x19]/', 'X', $str);
 190  
 191      // Replace specific problematic names with alternative names.
 192      $alternates = array(
 193        '.'   => 'dot',
 194        '..'  => 'dotdot',
 195        ''    => 'null',
 196      );
 197  
 198      return idx($alternates, $str, $str);
 199    }
 200  
 201    public static function normalizeTitleString($str) {
 202      // Remove colons from titles. This is mostly to accommodate legacy rules
 203      // from the old Diviner, which generated a significant number of article
 204      // URIs without colons present in the titles.
 205      $str = str_replace(':', '', $str);
 206      $str = self::normalizeString($str);
 207      return phutil_utf8_strtolower($str);
 208    }
 209  
 210  }


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