Path: blob/master/src/applications/paste/storage/PhabricatorPaste.php
12242 views
<?php12final class PhabricatorPaste extends PhabricatorPasteDAO3implements4PhabricatorSubscribableInterface,5PhabricatorTokenReceiverInterface,6PhabricatorFlaggableInterface,7PhabricatorMentionableInterface,8PhabricatorPolicyInterface,9PhabricatorProjectInterface,10PhabricatorDestructibleInterface,11PhabricatorApplicationTransactionInterface,12PhabricatorSpacesInterface,13PhabricatorConduitResultInterface,14PhabricatorFerretInterface,15PhabricatorFulltextInterface {1617protected $title;18protected $authorPHID;19protected $filePHID;20protected $language;21protected $parentPHID;22protected $viewPolicy;23protected $editPolicy;24protected $mailKey;25protected $status;26protected $spacePHID;2728const STATUS_ACTIVE = 'active';29const STATUS_ARCHIVED = 'archived';3031private $content = self::ATTACHABLE;32private $rawContent = self::ATTACHABLE;33private $snippet = self::ATTACHABLE;3435public static function initializeNewPaste(PhabricatorUser $actor) {36$app = id(new PhabricatorApplicationQuery())37->setViewer($actor)38->withClasses(array('PhabricatorPasteApplication'))39->executeOne();4041$view_policy = $app->getPolicy(PasteDefaultViewCapability::CAPABILITY);42$edit_policy = $app->getPolicy(PasteDefaultEditCapability::CAPABILITY);4344return id(new PhabricatorPaste())45->setTitle('')46->setStatus(self::STATUS_ACTIVE)47->setAuthorPHID($actor->getPHID())48->setViewPolicy($view_policy)49->setEditPolicy($edit_policy)50->setSpacePHID($actor->getDefaultSpacePHID())51->attachRawContent(null);52}5354public static function getStatusNameMap() {55return array(56self::STATUS_ACTIVE => pht('Active'),57self::STATUS_ARCHIVED => pht('Archived'),58);59}6061public function getURI() {62return '/'.$this->getMonogram();63}6465public function getMonogram() {66return 'P'.$this->getID();67}6869protected function getConfiguration() {70return array(71self::CONFIG_AUX_PHID => true,72self::CONFIG_COLUMN_SCHEMA => array(73'status' => 'text32',74'title' => 'text255',75'language' => 'text64?',76'mailKey' => 'bytes20',77'parentPHID' => 'phid?',7879// T6203/NULLABILITY80// Pastes should always have a view policy.81'viewPolicy' => 'policy?',82),83self::CONFIG_KEY_SCHEMA => array(84'parentPHID' => array(85'columns' => array('parentPHID'),86),87'authorPHID' => array(88'columns' => array('authorPHID'),89),90'key_dateCreated' => array(91'columns' => array('dateCreated'),92),93'key_language' => array(94'columns' => array('language'),95),96),97) + parent::getConfiguration();98}99100public function generatePHID() {101return PhabricatorPHID::generateNewPHID(102PhabricatorPastePastePHIDType::TYPECONST);103}104105public function isArchived() {106return ($this->getStatus() == self::STATUS_ARCHIVED);107}108109public function save() {110if (!$this->getMailKey()) {111$this->setMailKey(Filesystem::readRandomCharacters(20));112}113return parent::save();114}115116public function getFullName() {117$title = $this->getTitle();118if (!$title) {119$title = pht('(An Untitled Masterwork)');120}121return 'P'.$this->getID().' '.$title;122}123124public function getContent() {125return $this->assertAttached($this->content);126}127128public function attachContent($content) {129$this->content = $content;130return $this;131}132133public function getRawContent() {134return $this->assertAttached($this->rawContent);135}136137public function attachRawContent($raw_content) {138$this->rawContent = $raw_content;139return $this;140}141142public function getSnippet() {143return $this->assertAttached($this->snippet);144}145146public function attachSnippet(PhabricatorPasteSnippet $snippet) {147$this->snippet = $snippet;148return $this;149}150151/* -( PhabricatorSubscribableInterface )----------------------------------- */152153154public function isAutomaticallySubscribed($phid) {155return ($this->authorPHID == $phid);156}157158159/* -( PhabricatorTokenReceiverInterface )---------------------------------- */160161public function getUsersToNotifyOfTokenGiven() {162return array(163$this->getAuthorPHID(),164);165}166167168/* -( PhabricatorPolicyInterface )----------------------------------------- */169170171public function getCapabilities() {172return array(173PhabricatorPolicyCapability::CAN_VIEW,174PhabricatorPolicyCapability::CAN_EDIT,175);176}177178public function getPolicy($capability) {179if ($capability == PhabricatorPolicyCapability::CAN_VIEW) {180return $this->viewPolicy;181} else if ($capability == PhabricatorPolicyCapability::CAN_EDIT) {182return $this->editPolicy;183}184return PhabricatorPolicies::POLICY_NOONE;185}186187public function hasAutomaticCapability($capability, PhabricatorUser $user) {188return ($user->getPHID() == $this->getAuthorPHID());189}190191public function describeAutomaticCapability($capability) {192return pht('The author of a paste can always view and edit it.');193}194195196/* -( PhabricatorDestructibleInterface )----------------------------------- */197198199public function destroyObjectPermanently(200PhabricatorDestructionEngine $engine) {201202if ($this->filePHID) {203$file = id(new PhabricatorFileQuery())204->setViewer($engine->getViewer())205->withPHIDs(array($this->filePHID))206->executeOne();207if ($file) {208$engine->destroyObject($file);209}210}211212$this->delete();213}214215216/* -( PhabricatorApplicationTransactionInterface )------------------------- */217218219public function getApplicationTransactionEditor() {220return new PhabricatorPasteEditor();221}222223public function getApplicationTransactionTemplate() {224return new PhabricatorPasteTransaction();225}226227228/* -( PhabricatorSpacesInterface )----------------------------------------- */229230231public function getSpacePHID() {232return $this->spacePHID;233}234235236/* -( PhabricatorConduitResultInterface )---------------------------------- */237238239public function getFieldSpecificationsForConduit() {240return array(241id(new PhabricatorConduitSearchFieldSpecification())242->setKey('title')243->setType('string')244->setDescription(pht('The title of the paste.')),245id(new PhabricatorConduitSearchFieldSpecification())246->setKey('uri')247->setType('uri')248->setDescription(pht('View URI for the paste.')),249id(new PhabricatorConduitSearchFieldSpecification())250->setKey('authorPHID')251->setType('phid')252->setDescription(pht('User PHID of the author.')),253id(new PhabricatorConduitSearchFieldSpecification())254->setKey('language')255->setType('string?')256->setDescription(pht('Language to use for syntax highlighting.')),257id(new PhabricatorConduitSearchFieldSpecification())258->setKey('status')259->setType('string')260->setDescription(pht('Active or archived status of the paste.')),261);262}263264public function getFieldValuesForConduit() {265return array(266'title' => $this->getTitle(),267'uri' => PhabricatorEnv::getURI($this->getURI()),268'authorPHID' => $this->getAuthorPHID(),269'language' => nonempty($this->getLanguage(), null),270'status' => $this->getStatus(),271);272}273274public function getConduitSearchAttachments() {275return array(276id(new PhabricatorPasteContentSearchEngineAttachment())277->setAttachmentKey('content'),278);279}280281282/* -( PhabricatorFerretInterface )----------------------------------------- */283284285public function newFerretEngine() {286return new PhabricatorPasteFerretEngine();287}288289290/* -( PhabricatorFulltextInterface )--------------------------------------- */291292public function newFulltextEngine() {293return new PhabricatorPasteFulltextEngine();294}295296}297298299