[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/herald/storage/transcript/ -> HeraldTranscript.php (source)

   1  <?php
   2  
   3  final class HeraldTranscript extends HeraldDAO
   4    implements
   5      PhabricatorPolicyInterface,
   6      PhabricatorDestructibleInterface {
   7  
   8    protected $objectTranscript;
   9    protected $ruleTranscripts = array();
  10    protected $conditionTranscripts = array();
  11    protected $applyTranscripts = array();
  12  
  13    protected $time;
  14    protected $host;
  15    protected $duration;
  16  
  17    protected $objectPHID;
  18    protected $dryRun;
  19    protected $garbageCollected = 0;
  20  
  21    const TABLE_SAVED_HEADER = 'herald_savedheader';
  22  
  23    public function getXHeraldRulesHeader() {
  24      $ids = array();
  25      foreach ($this->applyTranscripts as $xscript) {
  26        if ($xscript->getApplied()) {
  27          if ($xscript->getRuleID()) {
  28            $ids[] = $xscript->getRuleID();
  29          }
  30        }
  31      }
  32      if (!$ids) {
  33        return 'none';
  34      }
  35  
  36      // A rule may have multiple effects, which will cause it to be listed
  37      // multiple times.
  38      $ids = array_unique($ids);
  39  
  40      foreach ($ids as $k => $id) {
  41        $ids[$k] = '<'.$id.'>';
  42      }
  43  
  44      return implode(', ', $ids);
  45    }
  46  
  47    public static function saveXHeraldRulesHeader($phid, $header) {
  48  
  49      // Combine any existing header with the new header, listing all rules
  50      // which have ever triggered for this object.
  51      $header = self::combineXHeraldRulesHeaders(
  52        self::loadXHeraldRulesHeader($phid),
  53        $header);
  54  
  55      queryfx(
  56        id(new HeraldTranscript())->establishConnection('w'),
  57        'INSERT INTO %T (phid, header) VALUES (%s, %s)
  58          ON DUPLICATE KEY UPDATE header = VALUES(header)',
  59        self::TABLE_SAVED_HEADER,
  60        $phid,
  61        $header);
  62  
  63      return $header;
  64    }
  65  
  66    private static function combineXHeraldRulesHeaders($u, $v) {
  67      $u = preg_split('/[, ]+/', $u);
  68      $v = preg_split('/[, ]+/', $v);
  69  
  70      $combined = array_unique(array_filter(array_merge($u, $v)));
  71      return implode(', ', $combined);
  72    }
  73  
  74    public static function loadXHeraldRulesHeader($phid) {
  75      $header = queryfx_one(
  76        id(new HeraldTranscript())->establishConnection('r'),
  77        'SELECT * FROM %T WHERE phid = %s',
  78        self::TABLE_SAVED_HEADER,
  79        $phid);
  80      if ($header) {
  81        return idx($header, 'header');
  82      }
  83      return null;
  84    }
  85  
  86  
  87    protected function getConfiguration() {
  88      // Ugh. Too much of a mess to deal with.
  89      return array(
  90        self::CONFIG_AUX_PHID     => true,
  91        self::CONFIG_TIMESTAMPS   => false,
  92        self::CONFIG_SERIALIZATION => array(
  93          'objectTranscript'      => self::SERIALIZATION_PHP,
  94          'ruleTranscripts'       => self::SERIALIZATION_PHP,
  95          'conditionTranscripts'  => self::SERIALIZATION_PHP,
  96          'applyTranscripts'      => self::SERIALIZATION_PHP,
  97        ),
  98        self::CONFIG_BINARY => array(
  99          'objectTranscript'      => true,
 100          'ruleTranscripts'       => true,
 101          'conditionTranscripts'  => true,
 102          'applyTranscripts'      => true,
 103        ),
 104        self::CONFIG_COLUMN_SCHEMA => array(
 105          'time' => 'epoch',
 106          'host' => 'text255',
 107          'duration' => 'double',
 108          'dryRun' => 'bool',
 109          'garbageCollected' => 'bool',
 110        ),
 111        self::CONFIG_KEY_SCHEMA => array(
 112          'key_phid' => null,
 113          'phid' => array(
 114            'columns' => array('phid'),
 115            'unique' => true,
 116          ),
 117          'objectPHID' => array(
 118            'columns' => array('objectPHID'),
 119          ),
 120          'garbageCollected' => array(
 121            'columns' => array('garbageCollected', 'time'),
 122          ),
 123        ),
 124      ) + parent::getConfiguration();
 125    }
 126  
 127    public function __construct() {
 128      $this->time = time();
 129      $this->host = php_uname('n');
 130    }
 131  
 132    public function addApplyTranscript(HeraldApplyTranscript $transcript) {
 133      $this->applyTranscripts[] = $transcript;
 134      return $this;
 135    }
 136  
 137    public function getApplyTranscripts() {
 138      return nonempty($this->applyTranscripts, array());
 139    }
 140  
 141    public function setDuration($duration) {
 142      $this->duration = $duration;
 143      return $this;
 144    }
 145  
 146    public function setObjectTranscript(HeraldObjectTranscript $transcript) {
 147      $this->objectTranscript = $transcript;
 148      return $this;
 149    }
 150  
 151    public function getObjectTranscript() {
 152      return $this->objectTranscript;
 153    }
 154  
 155    public function addRuleTranscript(HeraldRuleTranscript $transcript) {
 156      $this->ruleTranscripts[$transcript->getRuleID()] = $transcript;
 157      return $this;
 158    }
 159  
 160    public function discardDetails() {
 161      $this->applyTranscripts = null;
 162      $this->ruleTranscripts = null;
 163      $this->objectTranscript = null;
 164      $this->conditionTranscripts = null;
 165    }
 166  
 167    public function getRuleTranscripts() {
 168      return nonempty($this->ruleTranscripts, array());
 169    }
 170  
 171    public function addConditionTranscript(
 172      HeraldConditionTranscript $transcript) {
 173      $rule_id = $transcript->getRuleID();
 174      $cond_id = $transcript->getConditionID();
 175  
 176      $this->conditionTranscripts[$rule_id][$cond_id] = $transcript;
 177      return $this;
 178    }
 179  
 180    public function getConditionTranscriptsForRule($rule_id) {
 181      return idx($this->conditionTranscripts, $rule_id, array());
 182    }
 183  
 184    public function getMetadataMap() {
 185      return array(
 186        'Run At Epoch' => date('F jS, g:i:s A', $this->time),
 187        'Run On Host'  => $this->host,
 188        'Run Duration' => (int)(1000 * $this->duration).' ms',
 189      );
 190    }
 191  
 192    public function generatePHID() {
 193      return PhabricatorPHID::generateNewPHID('HLXS');
 194    }
 195  
 196  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 197  
 198    public function getCapabilities() {
 199      return array(
 200        PhabricatorPolicyCapability::CAN_VIEW,
 201      );
 202    }
 203  
 204    public function getPolicy($capability) {
 205      switch ($capability) {
 206        case PhabricatorPolicyCapability::CAN_VIEW:
 207          return PhabricatorPolicies::POLICY_USER;
 208      }
 209    }
 210  
 211    public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 212      return false;
 213    }
 214  
 215    public function describeAutomaticCapability($capability) {
 216      return pht(
 217        'To view a transcript, you must be able to view the object the '.
 218        'transcript is about.');
 219    }
 220  
 221  
 222  /* -(  PhabricatorDestructibleInterface  )----------------------------------- */
 223  
 224  
 225    public function destroyObjectPermanently(
 226      PhabricatorDestructionEngine $engine) {
 227  
 228      $this->openTransaction();
 229        $this->delete();
 230      $this->saveTransaction();
 231    }
 232  
 233  
 234  }


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