[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phriction/storage/ -> PhrictionDocument.php (source)

   1  <?php
   2  
   3  final class PhrictionDocument extends PhrictionDAO
   4    implements
   5      PhabricatorPolicyInterface,
   6      PhabricatorSubscribableInterface,
   7      PhabricatorFlaggableInterface,
   8      PhabricatorTokenReceiverInterface,
   9      PhabricatorDestructibleInterface,
  10      PhabricatorApplicationTransactionInterface {
  11  
  12    protected $slug;
  13    protected $depth;
  14    protected $contentID;
  15    protected $status;
  16    protected $mailKey;
  17    protected $viewPolicy;
  18    protected $editPolicy;
  19  
  20    private $contentObject = self::ATTACHABLE;
  21    private $ancestors = array();
  22  
  23    public function getConfiguration() {
  24      return array(
  25        self::CONFIG_AUX_PHID   => true,
  26        self::CONFIG_TIMESTAMPS => false,
  27        self::CONFIG_COLUMN_SCHEMA => array(
  28          'slug' => 'sort128',
  29          'depth' => 'uint32',
  30          'contentID' => 'id?',
  31          'status' => 'uint32',
  32          'mailKey' => 'bytes20',
  33        ),
  34        self::CONFIG_KEY_SCHEMA => array(
  35          'key_phid' => null,
  36          'phid' => array(
  37            'columns' => array('phid'),
  38            'unique' => true,
  39          ),
  40          'slug' => array(
  41            'columns' => array('slug'),
  42            'unique' => true,
  43          ),
  44          'depth' => array(
  45            'columns' => array('depth', 'slug'),
  46            'unique' => true,
  47          ),
  48        ),
  49      ) + parent::getConfiguration();
  50    }
  51  
  52    public function generatePHID() {
  53      return PhabricatorPHID::generateNewPHID(
  54        PhrictionDocumentPHIDType::TYPECONST);
  55    }
  56  
  57    public static function initializeNewDocument(PhabricatorUser $actor, $slug) {
  58      $document = new PhrictionDocument();
  59      $document->setSlug($slug);
  60  
  61      $content  = new PhrictionContent();
  62      $content->setSlug($slug);
  63  
  64      $default_title = PhabricatorSlug::getDefaultTitle($slug);
  65      $content->setTitle($default_title);
  66      $document->attachContent($content);
  67  
  68      $parent_doc = null;
  69      $ancestral_slugs = PhabricatorSlug::getAncestry($slug);
  70      if ($ancestral_slugs) {
  71        $parent = end($ancestral_slugs);
  72        $parent_doc = id(new PhrictionDocumentQuery())
  73          ->setViewer($actor)
  74          ->withSlugs(array($parent))
  75          ->executeOne();
  76      }
  77  
  78      if ($parent_doc) {
  79        $document->setViewPolicy($parent_doc->getViewPolicy());
  80        $document->setEditPolicy($parent_doc->getEditPolicy());
  81      } else {
  82        $default_view_policy = PhabricatorPolicies::getMostOpenPolicy();
  83        $document->setViewPolicy($default_view_policy);
  84        $document->setEditPolicy(PhabricatorPolicies::POLICY_USER);
  85      }
  86  
  87      return $document;
  88    }
  89  
  90    public function save() {
  91      if (!$this->getMailKey()) {
  92        $this->setMailKey(Filesystem::readRandomCharacters(20));
  93      }
  94      return parent::save();
  95    }
  96  
  97    public static function getSlugURI($slug, $type = 'document') {
  98      static $types = array(
  99        'document'  => '/w/',
 100        'history'   => '/phriction/history/',
 101      );
 102  
 103      if (empty($types[$type])) {
 104        throw new Exception("Unknown URI type '{$type}'!");
 105      }
 106  
 107      $prefix = $types[$type];
 108  
 109      if ($slug == '/') {
 110        return $prefix;
 111      } else {
 112        // NOTE: The effect here is to escape non-latin characters, since modern
 113        // browsers deal with escaped UTF8 characters in a reasonable way (showing
 114        // the user a readable URI) but older programs may not.
 115        $slug = phutil_escape_uri($slug);
 116        return $prefix.$slug;
 117      }
 118    }
 119  
 120    public function setSlug($slug) {
 121      $this->slug   = PhabricatorSlug::normalize($slug);
 122      $this->depth  = PhabricatorSlug::getDepth($slug);
 123      return $this;
 124    }
 125  
 126    public function attachContent(PhrictionContent $content) {
 127      $this->contentObject = $content;
 128      return $this;
 129    }
 130  
 131    public function getContent() {
 132      return $this->assertAttached($this->contentObject);
 133    }
 134  
 135    public function getAncestors() {
 136      return $this->ancestors;
 137    }
 138  
 139    public function getAncestor($slug) {
 140      return $this->assertAttachedKey($this->ancestors, $slug);
 141    }
 142  
 143    public function attachAncestor($slug, $ancestor) {
 144      $this->ancestors[$slug] = $ancestor;
 145      return $this;
 146    }
 147  
 148  
 149  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 150  
 151  
 152    public function getCapabilities() {
 153      return array(
 154        PhabricatorPolicyCapability::CAN_VIEW,
 155        PhabricatorPolicyCapability::CAN_EDIT,
 156      );
 157    }
 158  
 159    public function getPolicy($capability) {
 160      switch ($capability) {
 161        case PhabricatorPolicyCapability::CAN_VIEW:
 162          return $this->getViewPolicy();
 163        case PhabricatorPolicyCapability::CAN_EDIT:
 164          return $this->getEditPolicy();
 165      }
 166    }
 167  
 168    public function hasAutomaticCapability($capability, PhabricatorUser $user) {
 169      return false;
 170    }
 171  
 172    public function describeAutomaticCapability($capability) {
 173  
 174      switch ($capability) {
 175        case PhabricatorPolicyCapability::CAN_VIEW:
 176          return pht(
 177            'To view a wiki document, you must also be able to view all '.
 178            'of its parents.');
 179        case PhabricatorPolicyCapability::CAN_EDIT:
 180          return pht(
 181            'To edit a wiki document, you must also be able to view all '.
 182            'of its parents.');
 183      }
 184  
 185      return null;
 186    }
 187  
 188  
 189  /* -(  PhabricatorSubscribableInterface  )----------------------------------- */
 190  
 191  
 192    public function isAutomaticallySubscribed($phid) {
 193      return false;
 194    }
 195  
 196    public function shouldShowSubscribersProperty() {
 197      return true;
 198    }
 199  
 200    public function shouldAllowSubscription($phid) {
 201      return true;
 202    }
 203  
 204  /* -(  PhabricatorApplicationTransactionInterface  )------------------------- */
 205  
 206  
 207    public function getApplicationTransactionEditor() {
 208      return new PhrictionTransactionEditor();
 209    }
 210  
 211    public function getApplicationTransactionObject() {
 212      return $this;
 213    }
 214  
 215    public function getApplicationTransactionTemplate() {
 216      return new PhrictionTransaction();
 217    }
 218  
 219  
 220  
 221  /* -(  PhabricatorTokenReceiverInterface  )---------------------------------- */
 222  
 223  
 224    public function getUsersToNotifyOfTokenGiven() {
 225      return PhabricatorSubscribersQuery::loadSubscribersForPHID($this->phid);
 226    }
 227  
 228  
 229  /* -(  PhabricatorDestructibleInterface  )----------------------------------- */
 230  
 231  
 232    public function destroyObjectPermanently(
 233      PhabricatorDestructionEngine $engine) {
 234  
 235      $this->openTransaction();
 236  
 237        $this->delete();
 238  
 239        $contents = id(new PhrictionContent())->loadAllWhere(
 240          'documentID = %d',
 241          $this->getID());
 242        foreach ($contents as $content) {
 243          $content->delete();
 244        }
 245  
 246      $this->saveTransaction();
 247    }
 248  
 249  }


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