Differential D14970 Diff 36173 src/applications/diffusion/query/filecontent/DiffusionFileContentQuery.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/diffusion/query/filecontent/DiffusionFileContentQuery.php
| <?php | <?php | ||||
| /** | |||||
| * NOTE: this class should only be used where local access to the repository | |||||
| * is guaranteed and NOT from within the Diffusion application. Diffusion | |||||
| * should use Conduit method 'diffusion.filecontentquery' to get this sort | |||||
| * of data. | |||||
| */ | |||||
| abstract class DiffusionFileContentQuery extends DiffusionQuery { | abstract class DiffusionFileContentQuery extends DiffusionQuery { | ||||
| private $fileContent; | |||||
| private $viewer; | |||||
| private $timeout; | private $timeout; | ||||
| private $byteLimit; | private $byteLimit; | ||||
| private $didHitByteLimit = false; | |||||
| private $didHitTimeLimit = false; | |||||
| public function setTimeout($timeout) { | public function setTimeout($timeout) { | ||||
| $this->timeout = $timeout; | $this->timeout = $timeout; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getTimeout() { | public function getTimeout() { | ||||
| return $this->timeout; | return $this->timeout; | ||||
| } | } | ||||
| public function setByteLimit($byte_limit) { | public function setByteLimit($byte_limit) { | ||||
| $this->byteLimit = $byte_limit; | $this->byteLimit = $byte_limit; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getByteLimit() { | public function getByteLimit() { | ||||
| return $this->byteLimit; | return $this->byteLimit; | ||||
| } | } | ||||
| public function setViewer(PhabricatorUser $user) { | final public static function newFromDiffusionRequest( | ||||
| $this->viewer = $user; | DiffusionRequest $request) { | ||||
| return $this; | return parent::newQueryObject(__CLASS__, $request); | ||||
| } | } | ||||
| public function getViewer() { | final public function getExceededByteLimit() { | ||||
| return $this->viewer; | return $this->didHitByteLimit; | ||||
| } | } | ||||
| final public static function newFromDiffusionRequest( | final public function getExceededTimeLimit() { | ||||
| DiffusionRequest $request) { | return $this->didHitTimeLimit; | ||||
| return parent::newQueryObject(__CLASS__, $request); | |||||
| } | } | ||||
| abstract public function getFileContentFuture(); | abstract protected function getFileContentFuture(); | ||||
| abstract protected function executeQueryFromFuture(Future $future); | abstract protected function resolveFileContentFuture(Future $future); | ||||
| final public function loadFileContentFromFuture(Future $future) { | final protected function executeQuery() { | ||||
| $future = $this->getFileContentFuture(); | |||||
| if ($this->timeout) { | if ($this->getTimeout()) { | ||||
| $future->setTimeout($this->timeout); | $future->setTimeout($this->getTimeout()); | ||||
| } | } | ||||
| if ($this->getByteLimit()) { | $byte_limit = $this->getByteLimit(); | ||||
| $future->setStdoutSizeLimit($this->getByteLimit()); | if ($byte_limit) { | ||||
| $future->setStdoutSizeLimit($byte_limit + 1); | |||||
| } | } | ||||
| try { | try { | ||||
| $file_content = $this->executeQueryFromFuture($future); | $file_content = $this->resolveFileContentFuture($future); | ||||
| } catch (CommandException $ex) { | } catch (CommandException $ex) { | ||||
| if (!$future->getWasKilledByTimeout()) { | if (!$future->getWasKilledByTimeout()) { | ||||
| throw $ex; | throw $ex; | ||||
| } | } | ||||
| $message = pht( | $this->didHitTimeLimit = true; | ||||
| '<Attempt to load this file was terminated after %s second(s).>', | $file_content = null; | ||||
| $this->timeout); | |||||
| $file_content = new DiffusionFileContent(); | |||||
| $file_content->setCorpus($message); | |||||
| } | |||||
| $this->fileContent = $file_content; | |||||
| $repository = $this->getRequest()->getRepository(); | |||||
| $try_encoding = $repository->getDetail('encoding'); | |||||
| if ($try_encoding) { | |||||
| $this->fileContent->setCorpus( | |||||
| phutil_utf8_convert( | |||||
| $this->fileContent->getCorpus(), 'UTF-8', $try_encoding)); | |||||
| } | |||||
| return $this->fileContent; | |||||
| } | |||||
| final protected function executeQuery() { | |||||
| return $this->loadFileContentFromFuture($this->getFileContentFuture()); | |||||
| } | } | ||||
| final public function loadFileContent() { | if ($byte_limit && (strlen($file_content) > $byte_limit)) { | ||||
| return $this->executeQuery(); | $this->didHitByteLimit = true; | ||||
| $file_content = null; | |||||
| } | } | ||||
| final public function getRawData() { | return $file_content; | ||||
| return $this->fileContent->getCorpus(); | |||||
| } | } | ||||
| } | } | ||||