Path: blob/master/src/infrastructure/diff/interface/PhabricatorInlineComment.php
12242 views
<?php12abstract class PhabricatorInlineComment3extends Phobject4implements5PhabricatorMarkupInterface {67const MARKUP_FIELD_BODY = 'markup:body';89const STATE_UNDONE = 'undone';10const STATE_DRAFT = 'draft';11const STATE_UNDRAFT = 'undraft';12const STATE_DONE = 'done';1314private $storageObject;15private $syntheticAuthor;16private $isGhost;17private $versionedDrafts = array();1819public function __clone() {20$this->storageObject = clone $this->storageObject;21}2223final public static function loadAndAttachVersionedDrafts(24PhabricatorUser $viewer,25array $inlines) {2627$viewer_phid = $viewer->getPHID();28if (!$viewer_phid) {29return;30}3132$inlines = mpull($inlines, null, 'getPHID');3334$load = array();35foreach ($inlines as $key => $inline) {36if (!$inline->getIsEditing()) {37continue;38}3940if ($inline->getAuthorPHID() !== $viewer_phid) {41continue;42}4344$load[$key] = $inline;45}4647if (!$load) {48return;49}5051$drafts = PhabricatorVersionedDraft::loadDrafts(52array_keys($load),53$viewer_phid);5455$drafts = mpull($drafts, null, 'getObjectPHID');56foreach ($inlines as $inline) {57$draft = idx($drafts, $inline->getPHID());58$inline->attachVersionedDraftForViewer($viewer, $draft);59}60}6162public function setSyntheticAuthor($synthetic_author) {63$this->syntheticAuthor = $synthetic_author;64return $this;65}6667public function getSyntheticAuthor() {68return $this->syntheticAuthor;69}7071public function setStorageObject($storage_object) {72$this->storageObject = $storage_object;73return $this;74}7576public function getStorageObject() {77if (!$this->storageObject) {78$this->storageObject = $this->newStorageObject();79}8081return $this->storageObject;82}8384public function getInlineCommentCacheFragment() {85$phid = $this->getPHID();8687if ($phid === null) {88return null;89}9091return sprintf('inline(%s)', $phid);92}9394abstract protected function newStorageObject();95abstract public function getControllerURI();9697abstract public function setChangesetID($id);98abstract public function getChangesetID();99100abstract public function supportsHiding();101abstract public function isHidden();102103public function isDraft() {104return !$this->getTransactionPHID();105}106107public function getTransactionPHID() {108return $this->getStorageObject()->getTransactionPHID();109}110111public function isCompatible(PhabricatorInlineComment $comment) {112return113($this->getAuthorPHID() === $comment->getAuthorPHID()) &&114($this->getSyntheticAuthor() === $comment->getSyntheticAuthor()) &&115($this->getContent() === $comment->getContent());116}117118public function setIsGhost($is_ghost) {119$this->isGhost = $is_ghost;120return $this;121}122123public function getIsGhost() {124return $this->isGhost;125}126127public function setContent($content) {128$this->getStorageObject()->setContent($content);129return $this;130}131132public function getContent() {133return $this->getStorageObject()->getContent();134}135136public function getID() {137return $this->getStorageObject()->getID();138}139140public function getPHID() {141return $this->getStorageObject()->getPHID();142}143144public function setIsNewFile($is_new) {145$this->getStorageObject()->setIsNewFile($is_new);146return $this;147}148149public function getIsNewFile() {150return $this->getStorageObject()->getIsNewFile();151}152153public function setFixedState($state) {154$this->getStorageObject()->setFixedState($state);155return $this;156}157158public function setHasReplies($has_replies) {159$this->getStorageObject()->setHasReplies($has_replies);160return $this;161}162163public function getHasReplies() {164return $this->getStorageObject()->getHasReplies();165}166167public function getFixedState() {168return $this->getStorageObject()->getFixedState();169}170171public function setLineNumber($number) {172$this->getStorageObject()->setLineNumber($number);173return $this;174}175176public function getLineNumber() {177return $this->getStorageObject()->getLineNumber();178}179180public function setLineLength($length) {181$this->getStorageObject()->setLineLength($length);182return $this;183}184185public function getLineLength() {186return $this->getStorageObject()->getLineLength();187}188189public function setAuthorPHID($phid) {190$this->getStorageObject()->setAuthorPHID($phid);191return $this;192}193194public function getAuthorPHID() {195return $this->getStorageObject()->getAuthorPHID();196}197198public function setReplyToCommentPHID($phid) {199$this->getStorageObject()->setReplyToCommentPHID($phid);200return $this;201}202203public function getReplyToCommentPHID() {204return $this->getStorageObject()->getReplyToCommentPHID();205}206207public function setIsDeleted($is_deleted) {208$this->getStorageObject()->setIsDeleted($is_deleted);209return $this;210}211212public function getIsDeleted() {213return $this->getStorageObject()->getIsDeleted();214}215216public function setIsEditing($is_editing) {217$this->getStorageObject()->setAttribute('editing', (bool)$is_editing);218return $this;219}220221public function getIsEditing() {222return (bool)$this->getStorageObject()->getAttribute('editing', false);223}224225public function setDocumentEngineKey($engine_key) {226$this->getStorageObject()->setAttribute('documentEngineKey', $engine_key);227return $this;228}229230public function getDocumentEngineKey() {231return $this->getStorageObject()->getAttribute('documentEngineKey');232}233234public function setStartOffset($offset) {235$this->getStorageObject()->setAttribute('startOffset', $offset);236return $this;237}238239public function getStartOffset() {240return $this->getStorageObject()->getAttribute('startOffset');241}242243public function setEndOffset($offset) {244$this->getStorageObject()->setAttribute('endOffset', $offset);245return $this;246}247248public function getEndOffset() {249return $this->getStorageObject()->getAttribute('endOffset');250}251252public function getDateModified() {253return $this->getStorageObject()->getDateModified();254}255256public function getDateCreated() {257return $this->getStorageObject()->getDateCreated();258}259260public function openTransaction() {261$this->getStorageObject()->openTransaction();262}263264public function saveTransaction() {265$this->getStorageObject()->saveTransaction();266}267268public function save() {269$this->getTransactionCommentForSave()->save();270return $this;271}272273public function delete() {274$this->getStorageObject()->delete();275return $this;276}277278public function makeEphemeral() {279$this->getStorageObject()->makeEphemeral();280return $this;281}282283public function attachVersionedDraftForViewer(284PhabricatorUser $viewer,285PhabricatorVersionedDraft $draft = null) {286287$key = $viewer->getCacheFragment();288$this->versionedDrafts[$key] = $draft;289290return $this;291}292293public function hasVersionedDraftForViewer(PhabricatorUser $viewer) {294$key = $viewer->getCacheFragment();295return array_key_exists($key, $this->versionedDrafts);296}297298public function getVersionedDraftForViewer(PhabricatorUser $viewer) {299$key = $viewer->getCacheFragment();300if (!array_key_exists($key, $this->versionedDrafts)) {301throw new Exception(302pht(303'Versioned draft is not attached for user with fragment "%s".',304$key));305}306307return $this->versionedDrafts[$key];308}309310public function isVoidComment(PhabricatorUser $viewer) {311return $this->getContentStateForEdit($viewer)->isEmptyContentState();312}313314public function getContentStateForEdit(PhabricatorUser $viewer) {315$state = $this->getContentState();316317if ($this->hasVersionedDraftForViewer($viewer)) {318$versioned_draft = $this->getVersionedDraftForViewer($viewer);319if ($versioned_draft) {320$storage_map = $versioned_draft->getProperty('inline.state');321if (is_array($storage_map)) {322$state->readStorageMap($storage_map);323}324}325}326327return $state;328}329330protected function newContentState() {331return new PhabricatorDiffInlineCommentContentState();332}333334public function newContentStateFromRequest(AphrontRequest $request) {335return $this->newContentState()->readFromRequest($request);336}337338public function getInitialContentState() {339return $this->getNamedContentState('inline.state.initial');340}341342public function setInitialContentState(343PhabricatorInlineCommentContentState $state) {344return $this->setNamedContentState('inline.state.initial', $state);345}346347public function getCommittedContentState() {348return $this->getNamedContentState('inline.state.committed');349}350351public function setCommittedContentState(352PhabricatorInlineCommentContentState $state) {353return $this->setNamedContentState('inline.state.committed', $state);354}355356public function getContentState() {357$state = $this->getNamedContentState('inline.state');358359if (!$state) {360$state = $this->newContentState();361}362363$state->setContentText($this->getContent());364365return $state;366}367368public function setContentState(PhabricatorInlineCommentContentState $state) {369$this->setContent($state->getContentText());370371return $this->setNamedContentState('inline.state', $state);372}373374private function getNamedContentState($key) {375$storage = $this->getStorageObject();376377$storage_map = $storage->getAttribute($key);378if (!is_array($storage_map)) {379return null;380}381382$state = $this->newContentState();383$state->readStorageMap($storage_map);384return $state;385}386387private function setNamedContentState(388$key,389PhabricatorInlineCommentContentState $state) {390391$storage = $this->getStorageObject();392$storage_map = $state->newStorageMap();393$storage->setAttribute($key, $storage_map);394395return $this;396}397398public function getInlineContext() {399return $this->getStorageObject()->getInlineContext();400}401402public function getContentStateMapForEdit(PhabricatorUser $viewer) {403return $this->getWireContentStateMap(true, $viewer);404}405406public function getContentStateMap() {407return $this->getWireContentStateMap(false, null);408}409410private function getWireContentStateMap(411$is_edit,412PhabricatorUser $viewer = null) {413414$initial_state = $this->getInitialContentState();415$committed_state = $this->getCommittedContentState();416417if ($is_edit) {418$active_state = $this->getContentStateForEdit($viewer);419} else {420$active_state = $this->getContentState();421}422423return array(424'initial' => $this->getWireContentState($initial_state),425'committed' => $this->getWireContentState($committed_state),426'active' => $this->getWireContentState($active_state),427);428}429430private function getWireContentState($content_state) {431if ($content_state === null) {432return null;433}434435return $content_state->newStorageMap();436}437438public function getDefaultSuggestionText() {439$context = $this->getInlineContext();440441if (!$context) {442return null;443}444445$default = $context->getBodyLines();446$default = implode('', $default);447448return $default;449}450451452/* -( PhabricatorMarkupInterface Implementation )-------------------------- */453454455public function getMarkupFieldKey($field) {456$content = $this->getMarkupText($field);457return PhabricatorMarkupEngine::digestRemarkupContent($this, $content);458}459460public function newMarkupEngine($field) {461return PhabricatorMarkupEngine::newDifferentialMarkupEngine();462}463464public function getMarkupText($field) {465return $this->getContent();466}467468public function didMarkupText($field, $output, PhutilMarkupEngine $engine) {469return $output;470}471472public function shouldUseMarkupCache($field) {473return !$this->isDraft();474}475476}477478479