[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class DifferentialHunkModern extends DifferentialHunk {
   4  
   5    const DATATYPE_TEXT       = 'text';
   6    const DATATYPE_FILE       = 'file';
   7  
   8    const DATAFORMAT_RAW      = 'byte';
   9    const DATAFORMAT_DEFLATED = 'gzde';
  10  
  11    protected $dataType;
  12    protected $dataEncoding;
  13    protected $dataFormat;
  14    protected $data;
  15  
  16    private $rawData;
  17    private $forcedEncoding;
  18  
  19    public function getTableName() {
  20      return 'differential_hunk_modern';
  21    }
  22  
  23    public function getConfiguration() {
  24      return array(
  25        self::CONFIG_BINARY => array(
  26          'data' => true,
  27        ),
  28        self::CONFIG_COLUMN_SCHEMA => array(
  29          'dataType' => 'bytes4',
  30          'dataEncoding' => 'text16?',
  31          'dataFormat' => 'bytes4',
  32          'oldOffset' => 'uint32',
  33          'oldLen' => 'uint32',
  34          'newOffset' => 'uint32',
  35          'newLen' => 'uint32',
  36        ),
  37        self::CONFIG_KEY_SCHEMA => array(
  38          'key_changeset' => array(
  39            'columns' => array('changesetID'),
  40          ),
  41          'key_created' => array(
  42            'columns' => array('dateCreated'),
  43          ),
  44        ),
  45      ) + parent::getConfiguration();
  46    }
  47  
  48    public function setChanges($text) {
  49      $this->rawData = $text;
  50  
  51      $this->dataEncoding = $this->detectEncodingForStorage($text);
  52      $this->dataType = self::DATATYPE_TEXT;
  53      $this->dataFormat = self::DATAFORMAT_RAW;
  54      $this->data = $text;
  55  
  56      return $this;
  57    }
  58  
  59    public function getChanges() {
  60      return $this->getUTF8StringFromStorage(
  61        $this->getRawData(),
  62        nonempty($this->forcedEncoding, $this->getDataEncoding()));
  63    }
  64  
  65    public function forceEncoding($encoding) {
  66      $this->forcedEncoding = $encoding;
  67      return $this;
  68    }
  69  
  70    public function save() {
  71  
  72      $type = $this->getDataType();
  73      $format = $this->getDataFormat();
  74  
  75      // Before saving the data, attempt to compress it.
  76      if ($type == self::DATATYPE_TEXT) {
  77        if ($format == self::DATAFORMAT_RAW) {
  78          $data = $this->getData();
  79          $deflated = PhabricatorCaches::maybeDeflateData($data);
  80          if ($deflated !== null) {
  81            $this->data = $deflated;
  82            $this->dataFormat = self::DATAFORMAT_DEFLATED;
  83          }
  84        }
  85      }
  86  
  87      return parent::save();
  88    }
  89  
  90    private function getRawData() {
  91      if ($this->rawData === null) {
  92        $type = $this->getDataType();
  93        $data = $this->getData();
  94  
  95        switch ($type) {
  96          case self::DATATYPE_TEXT:
  97            // In this storage type, the changes are stored on the object.
  98            $data = $data;
  99            break;
 100          case self::DATATYPE_FILE:
 101          default:
 102            throw new Exception(
 103              pht('Hunk has unsupported data type "%s"!', $type));
 104        }
 105  
 106        $format = $this->getDataFormat();
 107        switch ($format) {
 108          case self::DATAFORMAT_RAW:
 109            // In this format, the changes are stored as-is.
 110            $data = $data;
 111            break;
 112          case self::DATAFORMAT_DEFLATED:
 113            $data = PhabricatorCaches::inflateData($data);
 114            break;
 115          default:
 116            throw new Exception(
 117              pht('Hunk has unsupported data encoding "%s"!', $type));
 118        }
 119  
 120        $this->rawData = $data;
 121      }
 122  
 123      return $this->rawData;
 124    }
 125  
 126  }


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