[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/differential/storage/ -> DifferentialChangeset.php (source)

   1  <?php
   2  
   3  final class DifferentialChangeset extends DifferentialDAO
   4    implements PhabricatorPolicyInterface {
   5  
   6    protected $diffID;
   7    protected $oldFile;
   8    protected $filename;
   9    protected $awayPaths;
  10    protected $changeType;
  11    protected $fileType;
  12    protected $metadata;
  13    protected $oldProperties;
  14    protected $newProperties;
  15    protected $addLines;
  16    protected $delLines;
  17  
  18    private $unsavedHunks = array();
  19    private $hunks = self::ATTACHABLE;
  20    private $diff = self::ATTACHABLE;
  21  
  22    const TABLE_CACHE = 'differential_changeset_parse_cache';
  23  
  24    protected function getConfiguration() {
  25      return array(
  26        self::CONFIG_SERIALIZATION => array(
  27          'metadata'      => self::SERIALIZATION_JSON,
  28          'oldProperties' => self::SERIALIZATION_JSON,
  29          'newProperties' => self::SERIALIZATION_JSON,
  30          'awayPaths'     => self::SERIALIZATION_JSON,
  31        ),
  32        self::CONFIG_COLUMN_SCHEMA => array(
  33          'oldFile' => 'text255?',
  34          'filename' => 'text255',
  35          'changeType' => 'uint32',
  36          'fileType' => 'uint32',
  37          'addLines' => 'uint32',
  38          'delLines' => 'uint32',
  39  
  40          // T6203/NULLABILITY
  41          // These should all be non-nullable, and store reasonable default
  42          // JSON values if empty.
  43          'awayPaths' => 'text?',
  44          'metadata' => 'text?',
  45          'oldProperties' => 'text?',
  46          'newProperties' => 'text?',
  47        ),
  48        self::CONFIG_KEY_SCHEMA => array(
  49          'diffID' => array(
  50            'columns' => array('diffID'),
  51          ),
  52        ),
  53      ) + parent::getConfiguration();
  54    }
  55  
  56    public function getAffectedLineCount() {
  57      return $this->getAddLines() + $this->getDelLines();
  58    }
  59  
  60    public function attachHunks(array $hunks) {
  61      assert_instances_of($hunks, 'DifferentialHunk');
  62      $this->hunks = $hunks;
  63      return $this;
  64    }
  65  
  66    public function getHunks() {
  67      return $this->assertAttached($this->hunks);
  68    }
  69  
  70    public function getDisplayFilename() {
  71      $name = $this->getFilename();
  72      if ($this->getFileType() == DifferentialChangeType::FILE_DIRECTORY) {
  73        $name .= '/';
  74      }
  75      return $name;
  76    }
  77  
  78    public function addUnsavedHunk(DifferentialHunk $hunk) {
  79      if ($this->hunks === self::ATTACHABLE) {
  80        $this->hunks = array();
  81      }
  82      $this->hunks[] = $hunk;
  83      $this->unsavedHunks[] = $hunk;
  84      return $this;
  85    }
  86  
  87    public function save() {
  88      $this->openTransaction();
  89        $ret = parent::save();
  90        foreach ($this->unsavedHunks as $hunk) {
  91          $hunk->setChangesetID($this->getID());
  92          $hunk->save();
  93        }
  94      $this->saveTransaction();
  95      return $ret;
  96    }
  97  
  98    public function delete() {
  99      $this->openTransaction();
 100  
 101        $legacy_hunks = id(new DifferentialHunkLegacy())->loadAllWhere(
 102          'changesetID = %d',
 103          $this->getID());
 104        foreach ($legacy_hunks as $legacy_hunk) {
 105          $legacy_hunk->delete();
 106        }
 107  
 108        $modern_hunks = id(new DifferentialHunkModern())->loadAllWhere(
 109          'changesetID = %d',
 110          $this->getID());
 111        foreach ($modern_hunks as $modern_hunk) {
 112          $modern_hunk->delete();
 113        }
 114  
 115        $this->unsavedHunks = array();
 116  
 117        queryfx(
 118          $this->establishConnection('w'),
 119          'DELETE FROM %T WHERE id = %d',
 120          self::TABLE_CACHE,
 121          $this->getID());
 122  
 123        $ret = parent::delete();
 124      $this->saveTransaction();
 125      return $ret;
 126    }
 127  
 128    public function getSortKey() {
 129      $sort_key = $this->getFilename();
 130      // Sort files with ".h" in them first, so headers (.h, .hpp) come before
 131      // implementations (.c, .cpp, .cs).
 132      $sort_key = str_replace('.h', '.!h', $sort_key);
 133      return $sort_key;
 134    }
 135  
 136    public function makeNewFile() {
 137      $file = mpull($this->getHunks(), 'makeNewFile');
 138      return implode('', $file);
 139    }
 140  
 141    public function makeOldFile() {
 142      $file = mpull($this->getHunks(), 'makeOldFile');
 143      return implode('', $file);
 144    }
 145  
 146    public function makeChangesWithContext($num_lines = 3) {
 147      $with_context = array();
 148      foreach ($this->getHunks() as $hunk) {
 149        $context = array();
 150        $changes = explode("\n", $hunk->getChanges());
 151        foreach ($changes as $l => $line) {
 152          $type = substr($line, 0, 1);
 153          if ($type == '+' || $type == '-') {
 154            $context += array_fill($l - $num_lines, 2 * $num_lines + 1, true);
 155          }
 156        }
 157        $with_context[] = array_intersect_key($changes, $context);
 158      }
 159      return array_mergev($with_context);
 160    }
 161  
 162    public function getAnchorName() {
 163      return substr(md5($this->getFilename()), 0, 8);
 164    }
 165  
 166    public function getAbsoluteRepositoryPath(
 167      PhabricatorRepository $repository = null,
 168      DifferentialDiff $diff = null) {
 169  
 170      $base = '/';
 171      if ($diff && $diff->getSourceControlPath()) {
 172        $base = id(new PhutilURI($diff->getSourceControlPath()))->getPath();
 173      }
 174  
 175      $path = $this->getFilename();
 176      $path = rtrim($base, '/').'/'.ltrim($path, '/');
 177  
 178      $svn = PhabricatorRepositoryType::REPOSITORY_TYPE_SVN;
 179      if ($repository && $repository->getVersionControlSystem() == $svn) {
 180        $prefix = $repository->getDetail('remote-uri');
 181        $prefix = id(new PhutilURI($prefix))->getPath();
 182        if (!strncmp($path, $prefix, strlen($prefix))) {
 183          $path = substr($path, strlen($prefix));
 184        }
 185        $path = '/'.ltrim($path, '/');
 186      }
 187  
 188      return $path;
 189    }
 190  
 191    public function getWhitespaceMatters() {
 192      $config = PhabricatorEnv::getEnvConfig('differential.whitespace-matters');
 193      foreach ($config as $regexp) {
 194        if (preg_match($regexp, $this->getFilename())) {
 195          return true;
 196        }
 197      }
 198  
 199      return false;
 200    }
 201  
 202    public function attachDiff(DifferentialDiff $diff) {
 203      $this->diff = $diff;
 204      return $this;
 205    }
 206  
 207    public function getDiff() {
 208      return $this->assertAttached($this->diff);
 209    }
 210  
 211  
 212  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 213  
 214  
 215    public function getCapabilities() {
 216      return array(
 217        PhabricatorPolicyCapability::CAN_VIEW,
 218      );
 219    }
 220  
 221    public function getPolicy($capability) {
 222      return $this->getDiff()->getPolicy($capability);
 223    }
 224  
 225    public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 226      return $this->getDiff()->hasAutomaticCapability($capability, $viewer);
 227    }
 228  
 229    public function describeAutomaticCapability($capability) {
 230      return null;
 231    }
 232  
 233  }


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