Path: blob/master/src/applications/diviner/atom/DivinerAtomRef.php
12256 views
<?php12final class DivinerAtomRef extends Phobject {34private $book;5private $context;6private $type;7private $name;8private $group;9private $summary;10private $index;11private $title;1213public function getSortKey() {14return implode(15"\0",16array(17$this->getName(),18$this->getType(),19$this->getContext(),20$this->getBook(),21$this->getIndex(),22));23}2425public function setIndex($index) {26$this->index = $index;27return $this;28}2930public function getIndex() {31return $this->index;32}3334public function setSummary($summary) {35$this->summary = $summary;36return $this;37}3839public function getSummary() {40return $this->summary;41}4243public function setName($name) {44$normal_name = self::normalizeString($name);45if (preg_match('/^@\d+\z/', $normal_name)) {46throw new Exception(47pht(48"Atom names must not be in the form '%s'. This pattern is ".49"reserved for disambiguating atoms with similar names.",50'/@\d+/'));51}52$this->name = $normal_name;53return $this;54}5556public function getName() {57return $this->name;58}5960public function setType($type) {61$this->type = self::normalizeString($type);62return $this;63}6465public function getType() {66return $this->type;67}6869public function setContext($context) {70if ($context === null) {71$this->context = $context;72} else {73$this->context = self::normalizeString($context);74}75return $this;76}7778public function getContext() {79return $this->context;80}8182public function setBook($book) {83if ($book === null) {84$this->book = $book;85} else {86$this->book = self::normalizeString($book);87}88return $this;89}9091public function getBook() {92return $this->book;93}9495public function setGroup($group) {96$this->group = $group;97return $this;98}99100public function getGroup() {101return $this->group;102}103104public function setTitle($title) {105$this->title = $title;106return $this;107}108109public function getTitle() {110return $this->title;111}112113public function getTitleSlug() {114return self::normalizeTitleString($this->getTitle());115}116117public function toDictionary() {118return array(119'book' => $this->getBook(),120'context' => $this->getContext(),121'type' => $this->getType(),122'name' => $this->getName(),123'group' => $this->getGroup(),124'summary' => $this->getSummary(),125'index' => $this->getIndex(),126'title' => $this->getTitle(),127);128}129130public function toHash() {131$dict = $this->toDictionary();132133unset($dict['group']);134unset($dict['index']);135unset($dict['summary']);136unset($dict['title']);137138ksort($dict);139return md5(serialize($dict)).'S';140}141142public static function newFromDictionary(array $dict) {143return id(new DivinerAtomRef())144->setBook(idx($dict, 'book'))145->setContext(idx($dict, 'context'))146->setType(idx($dict, 'type'))147->setName(idx($dict, 'name'))148->setGroup(idx($dict, 'group'))149->setSummary(idx($dict, 'summary'))150->setIndex(idx($dict, 'index'))151->setTitle(idx($dict, 'title'));152}153154public static function normalizeString($str) {155// These characters create problems on the filesystem or in URIs. Replace156// them with non-problematic approximations (instead of simply removing157// them) to keep the URIs fairly useful and avoid unnecessary collisions.158// These approximations are selected based on some domain knowledge of159// common languages: where a character is used as a delimiter, it is more160// helpful to replace it with a "." or a ":" or similar, while it's better161// if operator overloads read as, e.g., "operator_div".162163$map = array(164// Hopefully not used anywhere by anything.165'#' => '.',166167// Used in Ruby methods.168'?' => 'Q',169170// Used in PHP namespaces.171'\\' => '.',172173// Used in "operator +" in C++.174'+' => 'plus',175176// Used in "operator %" in C++.177'%' => 'mod',178179// Used in "operator /" in C++.180'/' => 'div',181);182$str = str_replace(array_keys($map), array_values($map), $str);183184// Replace all spaces with underscores.185$str = preg_replace('/ +/', '_', $str);186187// Replace control characters with "X".188$str = preg_replace('/[\x00-\x19]/', 'X', $str);189190// Replace specific problematic names with alternative names.191$alternates = array(192'.' => 'dot',193'..' => 'dotdot',194'' => 'null',195);196197return idx($alternates, $str, $str);198}199200public static function normalizeTitleString($str) {201// Remove colons from titles. This is mostly to accommodate legacy rules202// from the old Diviner, which generated a significant number of article203// URIs without colons present in the titles.204$str = str_replace(':', '', $str);205$str = self::normalizeString($str);206return phutil_utf8_strtolower($str);207}208209}210211212