Path: blob/master/src/applications/diviner/storage/DivinerLiveSymbol.php
12256 views
<?php12final class DivinerLiveSymbol extends DivinerDAO3implements4PhabricatorPolicyInterface,5PhabricatorMarkupInterface,6PhabricatorDestructibleInterface,7PhabricatorFulltextInterface {89protected $bookPHID;10protected $repositoryPHID;11protected $context;12protected $type;13protected $name;14protected $atomIndex;15protected $graphHash;16protected $identityHash;17protected $nodeHash;1819protected $title;20protected $titleSlugHash;21protected $groupName;22protected $summary;23protected $isDocumentable = 0;2425private $book = self::ATTACHABLE;26private $repository = self::ATTACHABLE;27private $atom = self::ATTACHABLE;28private $extends = self::ATTACHABLE;29private $children = self::ATTACHABLE;3031protected function getConfiguration() {32return array(33self::CONFIG_AUX_PHID => true,34self::CONFIG_TIMESTAMPS => false,35self::CONFIG_COLUMN_SCHEMA => array(36'context' => 'text255?',37'type' => 'text32',38'name' => 'text255',39'atomIndex' => 'uint32',40'identityHash' => 'bytes12',41'graphHash' => 'text64?',42'title' => 'text?',43'titleSlugHash' => 'bytes12?',44'groupName' => 'text255?',45'summary' => 'text?',46'isDocumentable' => 'bool',47'nodeHash' => 'text64?',48'repositoryPHID' => 'phid?',49),50self::CONFIG_KEY_SCHEMA => array(51'key_phid' => null,52'identityHash' => array(53'columns' => array('identityHash'),54'unique' => true,55),56'phid' => array(57'columns' => array('phid'),58'unique' => true,59),60'graphHash' => array(61'columns' => array('graphHash'),62'unique' => true,63),64'nodeHash' => array(65'columns' => array('nodeHash'),66'unique' => true,67),68'bookPHID' => array(69'columns' => array(70'bookPHID',71'type',72'name(64)',73'context(64)',74'atomIndex',75),76),77'name' => array(78'columns' => array('name(64)'),79),80'key_slug' => array(81'columns' => array('titleSlugHash'),82),83),84) + parent::getConfiguration();85}8687public function generatePHID() {88return PhabricatorPHID::generateNewPHID(DivinerAtomPHIDType::TYPECONST);89}9091public function getBook() {92return $this->assertAttached($this->book);93}9495public function attachBook(DivinerLiveBook $book) {96$this->book = $book;97return $this;98}99100public function getRepository() {101return $this->assertAttached($this->repository);102}103104public function attachRepository(PhabricatorRepository $repository = null) {105$this->repository = $repository;106return $this;107}108109public function getAtom() {110return $this->assertAttached($this->atom);111}112113public function attachAtom(DivinerLiveAtom $atom = null) {114if ($atom === null) {115$this->atom = null;116} else {117$this->atom = DivinerAtom::newFromDictionary($atom->getAtomData());118}119return $this;120}121122public function getURI() {123$parts = array(124'book',125$this->getBook()->getName(),126$this->getType(),127);128129if ($this->getContext()) {130$parts[] = $this->getContext();131}132133$parts[] = $this->getName();134135if ($this->getAtomIndex()) {136$parts[] = $this->getAtomIndex();137}138139return '/'.implode('/', $parts).'/';140}141142public function getSortKey() {143// Sort articles before other types of content. Then, sort atoms in a144// case-insensitive way.145return sprintf(146'%c:%s',147($this->getType() == DivinerAtom::TYPE_ARTICLE ? '0' : '1'),148phutil_utf8_strtolower($this->getTitle()));149}150151public function save() {152// NOTE: The identity hash is just a sanity check because the unique tuple153// on this table is way too long to fit into a normal `UNIQUE KEY`.154// We don't use it directly, but its existence prevents duplicate records.155156if (!$this->identityHash) {157$this->identityHash = PhabricatorHash::digestForIndex(158serialize(159array(160'bookPHID' => $this->getBookPHID(),161'context' => $this->getContext(),162'type' => $this->getType(),163'name' => $this->getName(),164'index' => $this->getAtomIndex(),165)));166}167168return parent::save();169}170171public function getTitle() {172$title = parent::getTitle();173174if (!strlen($title)) {175$title = $this->getName();176}177178return $title;179}180181public function setTitle($value) {182$this->writeField('title', $value);183184if ($value !== null && strlen($value)) {185$slug = DivinerAtomRef::normalizeTitleString($value);186$hash = PhabricatorHash::digestForIndex($slug);187$this->titleSlugHash = $hash;188} else {189$this->titleSlugHash = null;190}191192return $this;193}194195public function attachExtends(array $extends) {196assert_instances_of($extends, __CLASS__);197$this->extends = $extends;198return $this;199}200201public function getExtends() {202return $this->assertAttached($this->extends);203}204205public function attachChildren(array $children) {206assert_instances_of($children, __CLASS__);207$this->children = $children;208return $this;209}210211public function getChildren() {212return $this->assertAttached($this->children);213}214215216/* -( PhabricatorPolicyInterface )----------------------------------------- */217218219public function getCapabilities() {220return $this->getBook()->getCapabilities();221}222223public function getPolicy($capability) {224return $this->getBook()->getPolicy($capability);225}226227public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {228return $this->getBook()->hasAutomaticCapability($capability, $viewer);229}230231public function describeAutomaticCapability($capability) {232return pht('Atoms inherit the policies of the books they are part of.');233}234235236/* -( PhabricatorMarkupInterface )------------------------------------------ */237238239public function getMarkupFieldKey($field) {240return $this->getPHID().':'.$field.':'.$this->getGraphHash();241}242243public function newMarkupEngine($field) {244return PhabricatorMarkupEngine::getEngine('diviner');245}246247public function getMarkupText($field) {248if (!$this->getAtom()) {249return;250}251252return $this->getAtom()->getDocblockText();253}254255public function didMarkupText($field, $output, PhutilMarkupEngine $engine) {256return $output;257}258259public function shouldUseMarkupCache($field) {260return true;261}262263264/* -( PhabricatorDestructibleInterface )----------------------------------- */265266267public function destroyObjectPermanently(268PhabricatorDestructionEngine $engine) {269270$this->openTransaction();271$conn_w = $this->establishConnection('w');272273queryfx(274$conn_w,275'DELETE FROM %T WHERE symbolPHID = %s',276id(new DivinerLiveAtom())->getTableName(),277$this->getPHID());278279$this->delete();280$this->saveTransaction();281}282283284/* -( PhabricatorFulltextInterface )--------------------------------------- */285286287public function newFulltextEngine() {288if (!$this->getIsDocumentable()) {289return null;290}291292return new DivinerLiveSymbolFulltextEngine();293}294295}296297298