2014-05-25 18:28:11 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class DifferentialHunkModern extends DifferentialHunk {
|
|
|
|
|
|
|
|
const DATATYPE_TEXT = 'text';
|
|
|
|
const DATATYPE_FILE = 'file';
|
|
|
|
|
|
|
|
const DATAFORMAT_RAW = 'byte';
|
2014-05-25 20:41:16 +02:00
|
|
|
const DATAFORMAT_DEFLATED = 'gzde';
|
2014-05-25 18:28:11 +02:00
|
|
|
|
|
|
|
protected $dataType;
|
|
|
|
protected $dataEncoding;
|
|
|
|
protected $dataFormat;
|
|
|
|
protected $data;
|
|
|
|
|
2014-05-25 20:41:16 +02:00
|
|
|
private $rawData;
|
2014-06-20 20:49:41 +02:00
|
|
|
private $forcedEncoding;
|
2014-05-25 20:41:16 +02:00
|
|
|
|
2014-05-25 18:28:11 +02:00
|
|
|
public function getTableName() {
|
|
|
|
return 'differential_hunk_modern';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfiguration() {
|
|
|
|
return array(
|
|
|
|
self::CONFIG_BINARY => array(
|
|
|
|
'data' => true,
|
|
|
|
),
|
2014-09-29 00:12:58 +02:00
|
|
|
self::CONFIG_COLUMN_SCHEMA => array(
|
|
|
|
'dataType' => 'bytes4',
|
|
|
|
'dataEncoding' => 'text16?',
|
|
|
|
'dataFormat' => 'bytes4',
|
|
|
|
'oldOffset' => 'uint32',
|
|
|
|
'oldLen' => 'uint32',
|
|
|
|
'newOffset' => 'uint32',
|
|
|
|
'newLen' => 'uint32',
|
|
|
|
),
|
|
|
|
self::CONFIG_KEY_SCHEMA => array(
|
|
|
|
'key_changeset' => array(
|
|
|
|
'columns' => array('changesetID'),
|
|
|
|
),
|
|
|
|
'key_created' => array(
|
|
|
|
'columns' => array('dateCreated'),
|
|
|
|
),
|
|
|
|
),
|
2014-05-25 18:28:11 +02:00
|
|
|
) + parent::getConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setChanges($text) {
|
2014-05-25 20:41:16 +02:00
|
|
|
$this->rawData = $text;
|
|
|
|
|
2014-05-25 18:28:11 +02:00
|
|
|
$this->dataEncoding = $this->detectEncodingForStorage($text);
|
|
|
|
$this->dataType = self::DATATYPE_TEXT;
|
|
|
|
$this->dataFormat = self::DATAFORMAT_RAW;
|
|
|
|
$this->data = $text;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getChanges() {
|
|
|
|
return $this->getUTF8StringFromStorage(
|
|
|
|
$this->getRawData(),
|
2014-06-20 20:49:41 +02:00
|
|
|
nonempty($this->forcedEncoding, $this->getDataEncoding()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function forceEncoding($encoding) {
|
|
|
|
$this->forcedEncoding = $encoding;
|
|
|
|
return $this;
|
2014-05-25 18:28:11 +02:00
|
|
|
}
|
|
|
|
|
2014-05-25 20:41:16 +02:00
|
|
|
public function save() {
|
|
|
|
|
2014-05-25 18:28:11 +02:00
|
|
|
$type = $this->getDataType();
|
2014-05-25 20:41:16 +02:00
|
|
|
$format = $this->getDataFormat();
|
|
|
|
|
|
|
|
// Before saving the data, attempt to compress it.
|
|
|
|
if ($type == self::DATATYPE_TEXT) {
|
|
|
|
if ($format == self::DATAFORMAT_RAW) {
|
|
|
|
$data = $this->getData();
|
|
|
|
$deflated = PhabricatorCaches::maybeDeflateData($data);
|
|
|
|
if ($deflated !== null) {
|
|
|
|
$this->data = $deflated;
|
|
|
|
$this->dataFormat = self::DATAFORMAT_DEFLATED;
|
|
|
|
}
|
|
|
|
}
|
2014-05-25 18:28:11 +02:00
|
|
|
}
|
|
|
|
|
2014-05-25 20:41:16 +02:00
|
|
|
return parent::save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getRawData() {
|
|
|
|
if ($this->rawData === null) {
|
|
|
|
$type = $this->getDataType();
|
|
|
|
$data = $this->getData();
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case self::DATATYPE_TEXT:
|
|
|
|
// In this storage type, the changes are stored on the object.
|
|
|
|
$data = $data;
|
|
|
|
break;
|
|
|
|
case self::DATATYPE_FILE:
|
|
|
|
default:
|
|
|
|
throw new Exception(
|
|
|
|
pht('Hunk has unsupported data type "%s"!', $type));
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = $this->getDataFormat();
|
|
|
|
switch ($format) {
|
|
|
|
case self::DATAFORMAT_RAW:
|
|
|
|
// In this format, the changes are stored as-is.
|
|
|
|
$data = $data;
|
|
|
|
break;
|
|
|
|
case self::DATAFORMAT_DEFLATED:
|
|
|
|
$data = PhabricatorCaches::inflateData($data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception(
|
|
|
|
pht('Hunk has unsupported data encoding "%s"!', $type));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->rawData = $data;
|
2014-05-25 18:28:11 +02:00
|
|
|
}
|
|
|
|
|
2014-05-25 20:41:16 +02:00
|
|
|
return $this->rawData;
|
2014-05-25 18:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|